PYTHON I/O FILE HANDLING:
MAIN STEP IN FILE HANDLING:
Opening a File: To perform any file operations, you first need to open the file. You use the open() function to do this. The open() function takes two arguments: the path to the file and the mode in which you want to open it. The modes include:
- 'r': Read (default mode). Opens the file for reading.
- 'w': Write. Opens the file for writing. If the file already exists, it truncates it. If the file does not exist, it creates a new one.
- 'a': Append. Opens the file for writing, but if the file already exists, it appends new content to the end of it.
- 'b': Binary mode. Used in conjunction with other modes to indicate binary file handling (e.g., 'rb' or 'wb').
EXAMPLE:
Sr.No. |
Modes & Description |
1 |
r Opens a file for reading only.
The file pointer is placed at the beginning of the file. This is the default
mode. |
2 |
rb Opens a file for reading only
in binary format. The file pointer is placed at the beginning of the file.
This is the default mode. |
3 |
r+ Opens a file for both reading
and writing. The file pointer placed at the beginning of the file. |
4 |
rb+ Opens a file for both reading
and writing in binary format. The file pointer placed at the beginning of the
file. |
5 |
w Opens a file for writing only.
Overwrites the file if the file exists. If the file does not exist, creates a
new file for writing. |
6 |
wb Opens a file for writing only
in binary format. Overwrites the file if the file exists. If the file does
not exist, creates a new file for writing. |
7 |
w+ Opens a file for both writing
and reading. Overwrites the existing file if the file exists. If the file
does not exist, create a new file for reading and writing. |
8 |
wb+ Opens a file for both writing
and reading in binary format. Overwrites the existing file if the file
exists. If the file does not exist, create a new file for reading and
writing. |
9 |
a Opens a file for appending.
The file pointer is at the end of the file if the file exists. That is, the
file is in the append mode. If the file does not exist, it creates a new file
for writing. |
10 |
ab Opens a file for appending in
binary format. The file pointer is at the end of the file if the file exists.
That is, the file is in the append mode. If the file does not exist, it
creates a new file for writing. |
11 |
a+ Opens a file for both
appending and reading. The file pointer is at the end of the file if the file
exists. The file opens in the append mode. If the file does not exist, it
creates a new file for reading and writing. |
12 |
ab+ Opens a file for both appending
and reading in binary format. The file pointer is at the end of the file if
the file exists. The file opens in the append mode. If the file does not
exist, it creates a new file for reading and writing. |
Performing Operations: Once the file is open, you can perform various operations on it. The most common ones are:
- Reading: Use methods like read(), readline(), or readlines() to retrieve content from the file.
- Writing: Use the write() method to write content to the file.
- Appending: Use the write() method with the 'a' mode to append content to the end of the file.
EXAMPLE:
Closing the File: After performing operations on the file, it's important to close it using the close() method. This releases system resources and ensures that changes are saved to the file. If you forget to close the file, it might lead to unexpected behavior.
EXAMPLE:
EXAMPLE:
THE FILE OBJECT ATTRIBUTES:
Sr.No. |
Attribute & Description |
1 |
file.closed Returns true if file is
closed, false otherwise. |
2 |
file.mode Returns access mode with which
file was opened. |
3 |
file.name Returns name of the file. |
4 |
file.softspace Returns false if space
explicitly required with print, true otherwise. |
0 Comments