Files in Python
Python supports file handling and allows users to handle files like read and write files, along with many other file handling options. Until now the reading and writing of input and output is performed by standard input and output devices. Now the data files are utilized. Python provides us with an important feature for reading data from the file and writing data into a file.
`open()` Method
Before performing any operation on the file like reading or writing, first, we have to open that file. For this, we should use Python's inbuilt function open() but at the time of opening, we have to specify the mode, which represents the purpose of the opening file.
Syntax:
fileobject = open(filename, mode)
The list of the different modes of opening a file are:
| Mode | Description |
|---|---|
r |
Open a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode. |
rb |
Open a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode. |
r+ |
Open a file for both reading and writing. The file pointer placed at the beginning of the file. |
rb+ |
Opens a file for both reading and writing in binary format. The file pointer placed at the beginning of the file. |
w |
Open a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. |
wb |
Open a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, create a new file for writing. |
w+ |
Open 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. |
wb+ |
Open 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. |
a |
Open 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. |
ab |
Open 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. |
a+ |
Open 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. |
ab+ |
Open 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. |
Example:
# creates a new file sample.txt give read permissions on file
f = open('sample.txt', 'r')
# File object attributes
print('Name of the file: ', f.name)
print('Closed or not : ', f.closed)
print('Opening mode : ', f.mode)
f.close()
Output:
Name of the file: sample.txt
Closed or not : False
Opening mode : r
`close()` Method
The close() method of a file object flushes any unwritten information and closes the file object, after which no more writing can be done. It is a good practice to use the close() method to close a file.
Syntax:
fileobject.close()
Example:
# creates a new file sample.txt give write permissions on file
f = open('sample.txt', 'w')
# File object attributes
print('Name of the file: ', f.name)
print('Closed or not : ', f.closed)
print('Opening mode : ', f.mode)
f.close()
Output:
Name of the file: sample.txt
Closed or not : False
Opening mode : w