我仍然从python开始,陷入了这个错误,不知道该怎么办,我使用了spyder:
File "C:\Users\Win8.1\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2963, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-26-d3792eadd9d7>", line 1, in <module>
runfile('C:/Users/Win8.1/Documents/Python Scripts/ipnd-starter-code-master/stage_3/lesson_3.3_classes/c_profanity_editor/check_profanity.py', wdir='C:/Users/Win8.1/Documents/Python Scripts/ipnd-starter-code-master/stage_3/lesson_3.3_classes/c_profanity_editor')
File "C:\Users\Win8.1\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Users\Win8.1\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Win8.1/Documents/Python Scripts/ipnd-starter-code-master/stage_3/lesson_3.3_classes/c_profanity_editor/check_profanity.py", line 18
quotes = open("C:\Users\Win8.1\Documents\Python Scripts\ipnd-starter-code-master\stage_3\lesson_3.3_classes\c_profanity_editor\movies_quotes.txt")
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
代码:
import urllib
def read_text():
quotes = open("C:\Users\Win8.1\Documents\Python Scripts\ipnd-
starter-code-
master\stage_3\lesson_3.3_classes\c_profanity_editor\movies_quotes.txt")
contents_of_file = quotes.read()
print(contents_of_file)
read_text()
注意缩进和我的无知。
答案 0 :(得分:2)
除非使用原始字符串,否则您需要在文件名中将反斜杠\
换成\\
。
答案 1 :(得分:2)
如果您在Windows OS上使用spyder,则您的输入字符串应包含双斜杠'\',如下所示:
"C:\\Users\\Win8.1\\Documents\\Python Scripts\\ipnd-starter-code-
master\\stage_3\\lesson_3.3_classes\\c_profanity_editor\\movies_quotes.txt"