使用python在目录中列出中文文件名 [英] Listing chinese filenames in directory with python
问题描述
我试图列出目录中所有文件的名称和大小,但是当中文文件出现错误时,我在Windows 7上使用Python 2.7
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
这是我的代码
import os
path = '\'
listing = os.listdir(path)
for infile in listing:
if infile.endswith(".csv"):
print infile + ";"+ str(os.path.getsize(path + infile))
这是我得到的错误
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
引起错误的文件名是DB表1333366331.436754.048342.csv
The name of the file that caused the error is DB表1333366331.436754.048342.csv
我该如何避免这个问题?
How can i avoid this problem?
预先感谢> p>
thanks in advance
推荐答案
我会尝试使您的根路径成为unicode。我的猜测是listdir使用与初始字符串相同的编码,并且在读取非ASCII字符时出错。
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.
即
path = u'\'
来源:
http://docs.python.org/library/os.html#os.listdir
在版本2.3中进行了更改:在Windows NT / 2k / XP和Unix上,如果path是Unicode对象,则结果将是Unicode对象的列表。无法解码的文件名仍将作为字符串对象返回。
"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."
这篇关于使用python在目录中列出中文文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!