python SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: trunca

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')

Read More: