Skip to content Skip to sidebar Skip to footer

Listing Chinese Filenames In Directory With Python

I am trying to list the names and sizes of all files in a directory but get an error when there are files in chinese, i am using Python 2.7 on windows 7 this is my code import os

Solution 1:

I would try making your root path unicode. My guess is that listdir is using the same encoding as the initial string and is erroring when reading the non-ascii character.

i.e.

path = u'\'

Source: http://docs.python.org/library/os.html#os.listdir

"Changed in version 2.3: On Windows NT/2k/XP and Unix, if path is a Unicode object, the result will be a list of Unicode objects. Undecodable filenames will still be returned as string objects."

Post a Comment for "Listing Chinese Filenames In Directory With Python"