Jon
Jon

Reputation: 786

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

path = '\'
listing = os.listdir(path)
for infile in listing:
    if infile.endswith(".csv"):
        print infile + ";"+ str(os.path.getsize(path + infile))

this is the error I get

Traceback (most recent call last):
  File "file_size.py", line 8, in <module>
    print infile + ";"+ str(os.path.getsize(path + infile))
  File "C:\Python27\lib\genericpath.py", line 49, in getsize
    return os.stat(filename).st_size
WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: '\DB?1333366331.436754.048342.csv'

C:\>python file_size.py
  File "file_size.py", line 7
    if infile.endswith(".csv"):
                              ^
IndentationError: unindent does not match any outer indentation level

The name of the file that caused the error is DB表1333366331.436754.048342.csv

How can i avoid this problem?

thanks in advance

Upvotes: 0

Views: 3482

Answers (1)

Edgar Allan Pwn
Edgar Allan Pwn

Reputation: 311

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."

Upvotes: 3

Related Questions