NSS
NSS

Reputation: 37

Os.chdir with non-Latin characters

While learning the os module in Python and I've come across a problem.

Let's pretend my current working directory is: C:\Users\Москва\Desktop\Coding\Project 1.

I'd like to change the cwd to Desktop but since the path contains some Russian letters (Москва) it throws a Syntax error:

print(os.getcwd()) # C:\Users\Москва\Desktop\Coding\Project 1
os.chdir('C:\Users\Москва\Desktop')
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes...

How shall I usually treat non-standard characters in paths and change the directory in my case?

Upvotes: 0

Views: 197

Answers (1)

Chris
Chris

Reputation: 29742

It isn't about the russian, it's about the backslash with u: \U.

When you print os.getcwd, escaped backslashes goes away:

os.getcwd()
# 'C:\\Users\\chris\\Documents\\Москва\\test'
print(os.getcwd())
C:\Users\chris\Documents\Москва\test

And now if you try to use the printed one by copy-paste, python will understand \Users part as a unicode but of course fail. You can simply reproduce by executing

"\Uaaaa"

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \UXXXXXXXX escape

You can either use raw string, or use escaped backslashes:

os.chdir(r'C:\Users\sjysk\Documents\Москва')
#        ^ note `r` here
os.getcwd()
# 'C:\\Users\\chris\\Documents\\Москва'

os.chdir('C:\\Users\\sjysk\\Documents\\Москва')
os.getcwd()
# 'C:\\Users\\chris\\Documents\\Москва'

Upvotes: 1

Related Questions