file_handling_notes
file_handling_notes
File Handling
Introduction to Files in Python
In Python, files are used to store data persistently. Files can contain any kind of data, and you
can use Python to read from and write to files. There are various types of files that you might
encounter, including text files, binary files, and CSV files.
Types of Files
1. Text Files: Text files contain plain text and are typically human-readable. They use character
encoding, such as ASCII or UTF-8, to store text data.
Examples: .txt, .csv, .html
Usage: You can open text files in different modes like r (read), w (write), a (append), etc.
2. Binary Files: Binary files store data in binary format, which is not human-readable. They can
store any type of data, including images, audio, video, and more.
Examples: .jpg, .png, .exe, .dat
Usage: You use the rb (read binary) or wb (write binary) modes to handle binary files.
3. CSV Files: CSV (Comma-Separated Values) files are a special type of text file where data is
organized in rows and columns. Each line in the file corresponds to a row, and values within a
row are separated by commas.
Examples: .csv
Usage: Python provides the csv module to work with CSV files easily.
import csv
with open('example.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
Example:
file_path = "/home/user/documents/example.txt"
with open(file_path, 'r') as file:
content = file.read()
Relative Path:A relative path specifies the location of a file or directory relative to the current
working directory. It does not start from the root directory but from the current directory.
Example:
file_path = "documents/example.txt"
with open(file_path, 'r') as file:
content = file.read()
- `r+` (Read and Write): Opens the file for both reading and writing. The file pointer is placed at
the beginning of the file. If the file does not exist, an error is raised.
- `w` (Write): Opens the file for writing only. If the file exists, it truncates the file to zero length
(i.e., deletes the content). If the file does not exist, it creates a new file.
- `a` (Append): Opens the file for writing. The file pointer is placed at the end of the file if it
exists. It creates a new file if it doesn't exist.
- `a+` (Append and Read): Opens the file for both appending and reading. The file pointer is
placed at the end of the file. It creates a new file if it doesn't exist.
- Appending Data: You can append data to a file using the `a` or `a+` mode.
with open('example.txt', 'a') as file:
file.write("This is an appended line.\n")
- Using `readlines()`: Reads all the lines of a file and returns them as a list of strings.
Binary files store data in a binary format, which is not human-readable. These files are handled
differently than text files in Python.
Example
binary_file.close()
import pickle
1. Read
- Reading from a binary file can be done using the `load()` method from the `pickle` module.
2. Write/Create
- Writing to a binary file can be done using the `dump()` method.
3. Search
- To search for specific data in a binary file, you need to load the data and then search within
the loaded object.
4. Append
- To append data to a binary file, open it in `ab` or `ab+` mode and then write the additional
data.
5. Update
- Updating involves reading the data, modifying it, and then writing it back to the file.
import csv
csv.writer(): Creates a writer object that converts data into a delimited string format.
writerow(): Writes a single row of data to the CSV file.
writerows(): Writes multiple rows of data to the CSV file at once.
csv.reader(): Creates a reader object that iterates over lines in the CSV file.