Unit 4 Notes
Unit 4 Notes
File Input/Output: A file is a container in computer storage devices used for storing data. File
Input/Output (I/O) operations in Python allow you to read from and write to files on your computer.
Python provides built-in functions and methods to perform file I/O operations.
Mode Description
r Open a file for reading. (default)
w Open a file for writing. Creates a new file if it does not exist or truncates the file if it
exists.
x Open a file for exclusive creation. If the file already exists, the operation fails.
a Open a file for appending at the end of the file without truncating it. Creates a new file if
it does not exist.
t Open in text mode. (default)
b Open in binary mode.
+ Open a file for updating (reading and writing)
Reading Files in Python: Reading files in Python involves opening a file and then using various
methods to read its contents. Here's a basic overview:
1. Opening a File: Use the open() function to open a file in read mode (`r`). You can specify the file
path as the first argument and the mode as the second argument.
Example:
file = open("example.txt", "r")
2. Reading the Entire File: Use the read() method to read the entire contents of the file as a string.
Example:
content = file.read()
3. Reading Line by Line: Use a loop to iterate over the file object. Each iteration reads a single line
from the file using the readline() method.
Example:
line = file.readline()
while line:
print(line)
line = file.readline()
4. Reading All Lines: Use the readlines() method to read all lines from the file and return them as a
list of strings.
Example:
lines = file.readlines()
5. Closing the File: It's important to close the file using the close() method when you're done reading
from it to free up system resources.
Example:
file.close()
6. Using with Statement: It's recommended to use the with statement to automatically close the file
after reading. This ensures that the file is properly closed even if an exception occurs.
Example:
with open("example.txt", "r") as file:
content = file.read()
7. Working with Binary Files: To read binary files (such as images), open the file in binary mode
(`"rb"`) and use the read() method to read bytes.
Example:
with open("image.jpg", "rb") as file:
image_data = file.read()
Note: - Remember to handle exceptions that may occur while reading files, such as
`FileNotFoundError` if the file does not exist.
Writing Files in Python: Writing files in Python involves opening a file in write mode (“w”),
appending mode (“a”) or binary write mode (“wb”) and then using various methods to write data to
the file. Here's a basic overview:
1. Opening a File: Use the open() function to open a file in write mode (“w”), appending mode (“a”)
or binary write mode (“wb”). You can specify the file path as the first argument and the mode as the
second argument.
Example:
file = open("example.txt", "w")
2. Writing to a File: Use the write() method to write data to the file. The write() method takes a
string as an argument.
Example:
file.write("Hello, world!\n")
3. Writing Multiple Lines: Use the writelines() method to write multiple lines to the file. It takes a list
of strings as an argument, where each string represents a line.
Example:
lines = ["Line 1\n", "Line 2\n", "Line 3\n"]
file.writelines(lines)
4. Closing the File: It's important to close the file using the close() method when you're done writing
to it to ensure that all data is flushed to the file and to free up system resources.
Example:
file.close()
5. Using with Statement: It's recommended to use the with statement to automatically close the file
after writing. This ensures that the file is properly closed even if an exception occurs.
Example:
with open("example.txt", "w") as file:
file.write("Hello, world!\n")
6. Working with Binary Files: To write binary data to a file (such as images), open the file in binary
write mode (“wb”) and use the Write() method to write bytes.
Example:
with open("image.jpg", "wb") as file:
file.write(image_data)
Note: -Remember to handle exceptions that may occur while writing files, such as `IOError` if the file
cannot be written to.
Manipulating file Pointer using Seek in Python: In Python, the seek() method is used to change the
current position of the file pointer within a file. The file pointer is an indicator that points to the
current position in the file where the next read or write operation will take place. The seek()
method takes two arguments: an offset and a reference point.
1. offset: The number of bytes to move the file pointer. A positive offset moves the pointer forward,
while a negative offset moves it backward.
2. whence: The reference point from where the offset is applied. It can take one of the following
values:
0 (default): The offset is relative to the beginning of the file.
1: The offset is relative to the current position of the file pointer.
2: The offset is relative to the end of the file.
5. Move the file pointer to the 20th byte from the beginning of the file:
file.seek(20)
Note: -Remember to always close the file after you're done using it to free up system resources:
file.close()