我试图列出目录中所有文件的名称和大小,但是当有中文文件时出现错误,我在 Windows 7 上使用 Python 2.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
我怎样才能避免这个问题?
提前致谢