Mode Analysis of open File Operations and openmode Analysis
Python can use open functions to open, close, and read/write files;
The open function in Python3 is defined:
open(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True)
The mode list is:
'R' # open for reading (default) 'W' # open for writing, truncating the file first 'X' # create a new file and open it for writing, python3 adds 'A' # open for writing, appending to the end of the file if it exists 'B' # binary mode 'T' # text mode (default ), python3 added '+' # open a disk file for updating (reading and writing) 'U' # universal newline mode (deprecated)
Here we mainly care about 'R', 'w', 'A', 'r + ', 'W +', 'a + ', 'x ', many users may confuse read/write operations in different modes.
1) 'R'
Read-only mode: the default mode of the mode parameter in the open function. If the file does not exist, FileNotFoundError is reported (python2 is IOError );
After the file is opened, the initial cursor position is 0;
Each read operation starts from the cursor position;
If a write operation is performed, the following exception is reported:
io.UnsupportedOperation: not writable
2) 'W'
Write-only mode. If the file does not exist, create the file. If the file exists,First, clear the file and start writing;
After the file is opened, the initial cursor position is 0;
Each write operation starts from the cursor position;
If a read operation is performed,First, the file will be cleared,The following exception is reported:
io.UnsupportedOperation: not readable
3) 'A'
Append mode. If the file does not exist, create the file. If the file exists,Files are not cleared;
After the file is opened, the initial cursor position is the end of the file;
Each write starts from the end;
If a read operation is performed,The following exception is reported:
io.UnsupportedOperation: not readable
The above is easy to understand, and the following is a bit difficult.
4) 'r +'
Read/write mode. If the file does not exist, FileNotFoundError (python2 is an IOError) is reported );
After the file is opened, the initial cursor position is 0;
Each read/write operation starts from the cursor position. However, write operations are similar to replacement operations;
See the following code:
File Content:
abcdefg
The Code content is:
f = open('open_mode.txt', 'r+')f.write('xyz')f.close()
After the code is run, the file content changes:
xyzdefg
5) 'W +'
Write-only mode. If the file does not exist, create the file. If the file exists,First, clear the file;
After the file is opened, the initial cursor position is 0;
Each read/write operation starts from the cursor position. Write operations are similar to replacement operations;
6) 'A +'
Append mode. If the file does not exist, create the file. If the file exists,Files are not cleared;
After the file is opened, the initial cursor position is the end of the file;
Each write starts from the end;
The read operation starts from the cursor position;
7) 'X'
Python3 new addition
Create and write a file. The operation must be a non-existent file. If the operated file already exists, the error "FileExistsError" is returned.
Unreadable. If a read operation is performed,The following exception is reported:
io.UnsupportedOperation: not readable
Finally, draw a table to summarize:
There is still a lot of open content. For reading purposes, there is also a better module linecache for reading large files; it will continue to expand in the future