This article mainly introduces the Python Learning Note open () function opens file path error problem, now share to everyone, but also for everyone to make a reference. Come and see it together.
To open a file object in read-file mode, use the Python built-in open () function to pass in the file name and identifier, and the identifier ' r ' for reading.
>>> f = open (' D:/test.txt ', ' R ')
Note that, for beginners of Python, the open () function really exists a pit of a moderate, and very difficult to find.
Error Demo:
>>> f = open (' D:\test.txt ', ' R ') Traceback (most recent call last): File "<ipython-input-56-6a0acaf613c9 > ", Line 1, in <module> F =open (' D:\test.txt ', ' R ') OSError: [Errno] Invalid argument: ' D:\test.txt '
As if there is nothing wrong in the place, the exact same! Look carefully, the direction of the slash is not the same, why this situation occurs, because we copy the file from the system directly from the path caused by the Windows System file path of the slash symbol is ' \ '.
This leads to the concept of escape characters, where Python uses the backslash ' \ ' escape character when using special characters in characters, so ' \ ' in the error case is used as an escape, causing the interpreter to interpret the file path error.
There are two ways to correct this: first, the direction of ' \ ' is reversed to '/', that is, the correct wording of the text; second, the string containing the escape character preceded by ' R ' means that the string is interpreted in the original meaning and not escaped. Recommended )
>>> f = open (R ' D:\test.txt ', ' R ')
Also attached:
Escape Character Descriptor
Escape character |
Describe |
| \ (at end of line) |
NewLine |
| \ |
Backslash symbol | /tr>
| \ ' |
Single quotes |
| \ " |
Double quotes |
| \a |
Ring |
| \b |
Backspace (Backspace) |
| \e |
Escape |
| \000 |
Empty |
| \ n |
Line break |
| \v |
Portrait tab |
| \ t |
Landscape tab |
| \ r |
Carriage return |
| \f |
Page break |
| \oyy |
Octal number, yy represents the character, for example: \o12 for line break |
| \xyy |
Hexadecimal number, yy represents the character, for example: \x0a for line break |
| \other |
Other characters output in normal format |
Open () function identifier table
R opens the file in read-only mode. The pointer to the file will be placed at the beginning of the file. This is the default mode.
RB opens a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode.
r+ open a file for read-write. The file pointer will be placed at the beginning of the file.
rb+ opens a file in binary format for read-write. The file pointer will be placed at the beginning of the file.
W opens a file for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file.
WB opens a file in binary format for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file.
w+ open a file for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file.
wb+ opens a file in binary format for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file.
A opens a file for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to.
AB opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to.
A + opens a file for read and write. If the file already exists, the file pointer will be placed at the end of the file. The file opens with an append mode. If the file does not exist, create a new file to read and write.
ab+ opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file to read and write.