SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes——记一次失误

读取文件时报错:
SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 105-106: truncated \uXXXX escape
文件路径为:F:…(省略了中间路径)\urls.txt
在file_path = 'F:…(省略了中间路径)urls.txt’中没有出现报错,以为这种写法可以。
当出现报错时,去pycharm中修改文件编码结果没有用。
找到博主Your-Nikee《SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UX》才知道还是路径问题,路径必须加上双斜杠
‘F:\…\urls.txt’

读取成功
在这里插入图片描述

[1]https://blog.csdn.net/YourNikee/article/details/102738764

发布了22 篇原创文章 · 获赞 25 · 访问量 3293

猜你喜欢

转载自blog.csdn.net/python__reported/article/details/105327448