Advertisements
Advertisements
Question
How many file modes can be used with the open() function to open a file? State the function of each mode.
Very Long Answer
Advertisements
Solution
The main access modes are:
| Mode | Function |
|---|---|
r |
Opens a text file for reading; the file must exist. |
rb |
Opens a binary file for reading. |
w |
Opens a text file for writing; creates a new file or overwrites an existing one. |
wb |
Opens a binary file for writing; creates or overwrites the file. |
a |
Opens a text file for appending at the end; creates it if necessary. |
ab |
Opens a binary file for appending. |
r+ |
Opens a text file for both reading and writing; the file must exist. |
rb+ |
Opens a binary file for both reading and writing. |
w+ |
Opens a text file for reading and writing; overwrites or creates the file. |
wb+ |
Opens a binary file for reading and writing; overwrites or creates it. |
a+ |
Opens a text file for reading and appending. |
ab+ |
Opens a binary file for reading and appending. |
The b indicates binary mode and + indicates both reading and writing.
shaalaa.com
Is there an error in this question or solution?
