This question already has an answer here:
Closed 2 years ago.
I'm completely new to Python and even programming and also this forum. So I just was testing some new stuff that I have learned with Python like printing statements but I get this error:
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 1-2: truncated \uXXXX escape
Basically I just wrote this code:
print('C\users\desktop')
So what's going on there??
As the error message suggests, \u
is being interpreted as an escape. In Python 3, you can avoid this by using a raw string:
>>> print(r'C\users\desktop')
C\users\desktop
Unfortunately, in Python 2 \u
escapes are still interpreted inside raw (unicode) strings, so you have to do something else. One possibility is to use bytestrings (i.e., not unicode). One possibility is to do 'C\\users\\desktop'
. Another is to do something like 'C\\' r'users\desktop'
(using string concatenation by juxtaposition). I have encountered this issue myself with Python 2 and, personally, since 99% of the problem is with the Users
directory on Windows, I just created a directory junction that aliases C:\TheUsers
to C:\Users
. Then I can do r"C:\TheUsers"
with no problems.