How can I traverse directories named in Japanese in Python?

I am trying to create a simple helper utility that will scan my projects and find and return open ones to me through the command line. But my calls to os.listdirreturn gibberish (example:) '\x82\xa9\x82\xcc\x96I'whenever the folder or file name is in Japanese, and this gibberish cannot be passed on to the call again to get into the folder. i.e. os.listdir('C:\Documents and Settings\\x82\xa9\x82\xcc\x96I')returns an error:

'WindowsError: [Error 3] ๆŒ‡ๅฎšใ•ใ‚ŒใŸใƒ‘ใ‚นใŒ่ฆ‹ใคใ‹ใ‚Šใพใ›ใ‚“ใ€‚'

Does anyone know how I can get around this? Many thanks.

+5
source share
2 answers

Unicode, UTF-8, os.listdir. , shift-JIS:

>>> '\x82\xa9\x82\xcc\x96I'.decode('shift-jis').encode('utf-8')
'\xe3\x81\x8b\xe3\x81\xae\xe8\x9c\x82'
>>> print '\x82\xa9\x82\xcc\x96I'.decode('shift-jis')
ใ‹ใฎ่œ‚

os.listdir, Unicode:

Windows NT/2k/XP Unix, Unicode, Unicode. - .

:

os.listdir(ur'C:\Documents and Settings')
# ---------^
+6

Unicode-literal (u'your/path'). , Unicode (, , ).

:

Windows NT/2k/XP Unix, Unicode, Unicode. .

+2

All Articles