Preface
In Python, when processing file paths, we may be often affected by the minor problem of file reading. I would like to introduce in detail how to correctly read file paths in Python, including the use of normal strings, original strings, forward slashes, andApplication of modules.
It is for everyone to learn, refer to and use, and thoroughly master it, so that they will not be bothered in the future.
1. Normal string
In normal strings, backslash\
is an escape character. For example,\n
Indicates a new line,\t
Represents a tab character. If you want to use the backslash itself in a string, you need to use two backslashes\\
。
path = "C:\\Users\\Username\\Documents\\"
2. Original string
user''
orr""
The prefix can create the original string. In the original string, the backslash is not considered an escape character, so you can use a single backslash directly.
path = r"C:\Users\Username\Documents\"
3. Use the forward slash
In Python, forward slash/
can also be used as a path separator, even on Windows systems. This makes the path more portable among different operating systems.
path = "C:/Users/Username/Documents/"
4. Use the module
In order to better handle file paths, it is recommended to useModule, which provides cross-platform path operation function. For example, use
()
To build the path:
import os path = ("C:", "Users", "Username", "Documents", "")
Sample code
Here is a simple example showing how to read a file:
# Use original stringfile_path = r"C:\Users\Username\Documents\" # Read file contentwith open(file_path, 'r') as file: content = () print(content)
Summarize
- use
r''
The problem of escape characters can be avoided. - Using forward slashes in paths can improve portability.
- use
The module can handle file paths more conveniently.
Linux path knowledge
In Linux, if pathdirectory_path
There is no backslash at the end (/
),For example:
/home/user/documents
with path with backslash:
/home/user/documents/
These two paths actually point to the same directory. When the Linux file system processes the path, it will automatically treat the directory path without backslashes as a directory.
Example
No matter which form you use, the following command will return the same result:
# List files in the directoryls /home/user/documents ls /home/user/documents/
Both commands will be listeddocuments
Files in the directory.
Performance in Python
In Python, using both paths will also get the same result. For example:
import os # directory path, no backslashdirectory_path_no_slash = "/home/user/documents" # directory path with backslashdirectory_path_with_slash = "/home/user/documents/" # List files in the directoryfiles_no_slash = (directory_path_no_slash) files_with_slash = (directory_path_with_slash) print(files_no_slash) print(files_with_slash)
This code will output the same file list.
Summarize
This is the article about reading example code for Python file paths. For more related contents for reading Python file paths, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!