Modes of Operations in python:
Modes of Operations in python:
Below is a list of the different modes of opening a file in Python −
| Sr.No. | Description of Modes |
|---|---|
| 1 | r this opens a file for reading only and the file pointer is placed at the start of the file. This is often the default mode. |
| 2 | rb this opens a file for reading only in binary format and the file pointer is placed at the start of the file. This is often the default mode receptively. |
| 3 | r+ this opens a file for both reading and writing. and the file pointer placed at the start of the file respectively. |
| 4 | rb+ this opens a file for both reading and writing in binary format and the file pointer placed at the start of the file respectively. |
| 5 | w this opens a file for writing only, where it overwrites the file if the file exists and If the file doesn’t exist, creates a replacement file for writing respectively. |
| 6 | wb this opens a file for writing only, where it overwrites the file if the file exists and If the file doesn’t exist, creates a replacement file for writing respectively. |
| 7 | w+ this opens a file for both writing and reading, where it overwrites the prevailing file if the file exists. and If the file doesn’t exist, creates a replacement file for reading and writing respectively. |
| 8 | wb+ this opens a file for both writing and reading in binary format, where it overwrites the prevailing file if the file exists and If the file doesn’t exist, creates a replacement file for reading and writing respectively. |
| 9 | a this opens a file for appending, in which the file pointer is at the up to the top of the file if the file exists. however, the file is within the append mode and if the file doesn’t exist, it creates a replacement file for writing respectively. |
| 10 | ab this opens a file for appending in binary format where the file pointer is at the top of the file if the file exists. However, the file is within the append mode. If the file doesn’t exist, it creates a replacement file for writing respectively. |
| 11 | a+ this opens a file for both appending and reading However the file pointer is at the top of the file if the file exists. Therefore file opens within the append mode and if the file doesn’t exist, it creates a replacement file for reading and writing respectively. |
| 12 | ab+ this opens a file for both appending and reading in binary format. However the file pointer is at the top of the file if the file exists and the file opens within the append mode. Therefore if the file doesn’t exist, it creates a replacement file for reading and writing respectively. |
Attributes of file objects:
At the movement when the file is opened and you have one file object with the help of which you can get different information connected to that file respectively.
Below is a list of all attributes connected to file object :
| Sr.No. | Description |
|---|---|
| 1 | file.closed = it returns value true if file is closed, false if the file is not close. |
| 2 | file.mode = it returns access mode with which file was opened respectively. |
| 3 | file.name = it returns name of the file respectively. |
| 4 | file.softspace = it returns value as false if space explicitly required with print, true otherwise opposite condition respectively. |
Example:
# Open a file
file = open("file.txt", "wb")
print ("Name of the file: ", file.name)
print ("Closed or not : ", file.closed)
print ("Opening mode : ", file.mode)
print ("Softspace flag : ", file.softspace)
