Python Open File SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in …

#path is the file path

f=open(‘C:\Users\jingqiu\Desktop\New Text Document.txt’)

SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape

The file path plus r becomes f=open(r‘C:\Users\jingqiu\Desktop\New Text Document.txt’)

or f=open(‘C:\\Users\\jingqiu\\Desktop\\New Text Document.txt’)

the output of f.read() is …

Traceback (most recent call last):
File “<pyshell#4>”, line 1, in <module>
f.read()
File “C:\Users\xiuwe\AppData\Local\Programs\Python\Python37\lib\encodings\cp1252.py”, line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: ‘charmap’ codec can’t decode byte 0x8d in position 17: character maps to <undefined>

encode issue

f = open(r’C:\Users\jingqiu\Desktop\New Text Document.txt’,encoding=’utf-8′)

f.read()

OK ^_^

Similar Posts: