I. Problem description
Recently, I used Python to read the file and found that using '\' would report an error.
2. Cause analysis
'\' Is an escape character for Python. If there are special characters such as '\ t' or '\ r' in the path, '\' cannot play the role of directory jump, so an error is reported. The solution is to tell the system that '\' is not an escape character, and '\\' plays such a role. Here is an example.
Three. Problem solving
method one:
# Absolute path double backslash (Python in \ an escape effect) with Open ( ' E: \\ \\ use data.txt ' ) the file_object AS: Contents = file_object.read () Print (contents.rstrip () )
Method 2:
# Plus r tells the system to escape are not escape with Open (r ' E: \ use \ data.txt ' ) the file_object AS: Contents = file_object.read () Print (contents.rstrip ())
Method 3:
# Using an absolute path forward slash with Open ( ' E: /use/data.txt ' ) AS the file_object: Contents = file_object.read () Print (contents.rstrip ())