This is the error I made reading the file from the absolute path, using the following command
file = open('C:\Users\Wudl\Desktop\pi_digits1.txt','r')
The result is wrong
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
By looking up the problem, you find that \ is an escape character in Python
Encyclopaedia: Escape characters – all ASCII codes can be represented as “\” plus a number (usually in base 8). However, in C, some letters are defined with “\” before them to indicate common ASCII characters that cannot be displayed, such as \0,\t,\n, etc., which are called escape characters, because the following characters are not the original ASCII characters.
So it’s the error of \ being escaped as a character in the file, the two methods modify
1) add r before the path, the purpose of r is to prevent \ being escaped as a character, but to treat \ after r as a normal character \
file = open(r'C:\Users\Wudl\Desktop\pi_digits1.txt','r')
2) Use \ instead of \
file = open('C:\\Users\\Wudl\\Desktop\\pi_digits1.txt','r')
3) Use/replace \
file = open('C:/Users/Wudl/Desktop/pi_digits1.txt','r')
div>
Read More:
- Python SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3:
- SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 2-3: truncated \UX
- SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 2-3: truncated \UX
- Syntax Error: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position resolution
- How to solve the problem of syntax error: (Unicode error)’unicodescape ‘codec can’t decode bytes in position 2-3: truncat
- When reading the CSV file with Python 3, the Unicode decodeerror: ‘UTF-8’ codec can’t decode byte 0xd0 in position 0: invalid con appears
- Unicode decodeerror: ‘UTF-8’ codec can’t decode byte 0xd3 in position 238: invalid continuation B
- Solution of Unicode decodeerror -‘utf-8 ‘codec can’t decode byte 0xc4 in position 0 – invalid continuation byte
- Successfully resolved Unicode decodeerror: ‘UTF-8’ codec can’t decode byte 0xd3 in position 238: invalid continuation B
- Unicode decodeerror: ‘UTF-8’ codec can’t decode byte 0x80 in position 3131: invalid start byte solution
- Run Python file for the first time with eclipse / pydev: “UTF-8 ‘codec can’t decode byte 0xc4 in position
- ‘ascii‘ codec can‘t decode byte 0x90 in position 614: ordinal not in range(128)
- UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xc3 in position 54: invalid continuation byte
- UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xd6 in position 3089: invalid continuation byte
- Solve the ‘UTF-8’ codec can’t decode byte 0xe9 in position 3114: invalid continuation byte error
- UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-2: ordinal not in range(128)
- [system]SyntaxError: Unexpected token u in JSON at position 0
- Unicode encodeerror encountered in Python 3
- Unicode encodeerror: ‘GBK’ codec can’t encode character solution
- 12304;388382;3906442nd 123057 SyntaxError and unicode error and 8216215;