First, the Python open file can have a variety of modes, read mode, write mode, append mode, while reading and writing mode, and so on, here mainly introduces the mode of simultaneous reading and writing r+
Python opens the file by using the Open method
file_handler = open (filename,mode) |
Mode has the following types of modes
R
Open the file as read to read the file information.
W
Open a file in write mode to write information to the file. If the file exists, empty the file, and then write the new content
A
Open a file in Append mode (that is, an open file, the file pointer is automatically moved to the end of the file), and if the file does not exist, create
r+
The file can be read and written by opening the file in read/write mode.
w+
Remove the file contents, and then open the file as read-write.
A +
Open the file in read-write mode and move the file pointer to the end of the file.
B
Open the file in binary mode instead of in text mode. This mode is only valid for Windows or DOS, and Unix-like files are operated in binary mode.
So to open the file in both read and write mode, you can use r+
Python open file can have multiple modes