Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

python - Why do I get a SyntaxError for a Unicode escape in my file path?

The folder I want to get to is called python and is on my desktop.

I get the following error when I try to get to it

>>> os.chdir('C:UsersexpoperialedDesktopPython')
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated UXXXXXXXX escape
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to use a raw string, double your slashes or use forward slashes instead:

r'C:UsersexpoperialedDesktopPython'
'C:\Users\expoperialed\Desktop\Python'
'C:/Users/expoperialed/Desktop/Python'

In regular python strings, the U character combination signals a extended Unicode codepoint escape.

You can hit any number of other issues, for any of the recognised escape sequences, such as a or or x, etc.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...