使用open()后出现Python Unicode转义错误
我试图打开一个文本文件“Person.txt”,但我发现了这个错误 这是我的代码:使用open()后出现Python Unicode转义错误,python,file,pycharm,text-files,Python,File,Pycharm,Text Files,我试图打开一个文本文件“Person.txt”,但我发现了这个错误 这是我的代码: PD = open('C:\Users\Scooter\PeopleList.txt') 这是文件: a b c d 这就是错误: C:\Users\Scooter\PycharmProjects\class.py\venv\Scripts\python.exe C:/Users/Scooter/PycharmProjects/class.py/main.py File "C:\Users\Sc
PD = open('C:\Users\Scooter\PeopleList.txt')
这是文件:
a
b
c
d
这就是错误:
C:\Users\Scooter\PycharmProjects\class.py\venv\Scripts\python.exe C:/Users/Scooter/PycharmProjects/class.py/main.py
File "C:\Users\Scooter\PycharmProjects\class.py\main.py", line 1
PD = open('C:\Users\Scooter\PeopleList.txt')
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
反斜杠
\
是转义字符,要在字符串中使用文字反斜杠,必须使用\
对其进行转义
PD = open('C:\\Users\\Scooter\\PeopleList.txt')
或者使用原始字符串通过在字符串前面加r
PD = open(r'C:\Users\Scooter\PeopleList.txt')
尝试使用“\\”而不是“\”。