赞
踩
I am new to Python. I am trying to input one path and use os.walk() to search all files, and return the files' names. I also want to use os.path.getsize() to get file size, and finally, write them into a csv file.
However, if the file name is not in English, but in Chinese, German, French, etc, Python cannot recognize it and does not return the size of the file. I'd like to use os.path.getsize(path) (referring to the example below), but it does not recognize the file's name. How can I let Python recognize the file's name and return the size of these kind of files?
For example: the file's name is: "Показатели естественного и миграционного прироста до 2030г.doc". path="C:\xxxx\xxx\xxxx\Показатели естественного и миграционного прироста до 2030г.doc"
解决方案
If you pass a Unicode input to os.walk() you will get back file-names as Unicode as well.
The following should work for you
your_base_path = u"C:\\Directory" # note this is Unicode
for root, dirs, files in os.walk(your_base_path):
for f in files:
print os.stat(os.path.join(root, f)).st_size
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。