Describes how to use the open () function in Python to specify the file opening method,
File Opening Method
When we use the open () function to open a file, there are several open modes.
'R'-> Read-Only
'W'-> write-only. If the file already exists, it is cleared. If the file does not exist, it is created.
'A'-> append, written to the end of the file
'B'-> binary mode, such as opening an image, audio, or word file.
'+'-> Update (readable and writable)
It is a bit difficult to understand the code with the '+' sign.
with open('foo.txt', 'w+') as f: f.write('bar\n') f.seek(0) data = f.read()
As you can see, the code above can not only be written but also read. Note that you must first locate f. seek (0). Otherwise, empty data is read.
Some may be confused. Since the plus sign is readable and writable, what is the difference between 'W + 'and 'R +.
That is,
'W + 'is cleared and will be created. (If a file already exists, it will be cleared. If it does not exist, it will be created .)
'R + 'is not cleared and is not created
Do not open text files in binary mode
Let's take a look at the "weird" Phenomenon of the following code.
In Windows, I Have A f.txt file, which contains the following content.
helloworld
Code 1,
with open('f.txt', 'r') as f: print f.readlines() with open('f.txt', 'rb') as f: print f.readlines()
Output
['hello\n', 'world\n']['hello\r\n', 'world\r\n']
Code 2,
with open('f.txt', 'rb') as f: data = f.read() with open('f.txt', 'w') as f: f.write(data)
Open the file and change it to the following,
hello^Mworld^M
First, understand the concept of linefeed '\ n' and carriage return' \ R.
'\ N', Line Feed (LF, Line-Feed), indicates a new row.
'\ R', Carriage Return (CR, Carriage-Return), indicates Return to the line header.
Because the line feed identifier is different in different systems.
windows->'\r\n'unix->'\n'mac->'\r'
This is why '^ m' appears at the end of a txt file in windows when it is opened in linux '.
This is why I run a script in linux to export game data and turn it into a line in the local windows environment.
In fact, text files are also binary files and text-encoded binary files. Text Files process invisible characters to increase readability.
In python, you can use OS. linesep to obtain the line feed identifier of the current system. For example, in windows, OS. linesep is '\ r \ n '.
When operating the line feed identifier in python, you can directly use '\ n' instead of the platform on which it is stored. python will automatically convert it to a different identifier based on different systems.
With the above theoretical basis, we can resolve the "weird" Phenomenon of the code at the beginning of this article.
In code 1, for a file opened in text mode, the line feed identifier will be processed as '\ n' by python, but it will not be blocked when opened in binary mode.
Code 2: open in binary mode and write in text mode. When the binary is opened, it is still '\ r \ n'. When writing in text mode, python converts' \ n' to '\ r \ n ', therefore, it is equivalent to writing '\ r \ n', so there is an extra' ^ M '.