当用pycharm读取具体位置如桌面时,运行报错:SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape
如:find_read = open(‘C:\Users\Administrator\Desktop\hello.txt’),其实是转义的问题,在python中会有’\t’、’\n’等转义字符,所以可以用以下三种方法来解决:
1、在路径前面加r,即保持字符原始值的意思。
find_read = open(r'C:\Users\Administrator\Desktop\hello.txt')
2、使用双反斜杠
find_read = open('C:\\Users\\Administrator\\Desktop\\hello.txt')
3、替换为正斜杠
find_read = open('C:/Users/Administrator/Desktop/hello.txt')
**特别注意的是:**在使用文件完毕的最后,要有关闭文件的代码如:find_read.close()