当前位置:   article > 正文

python读取文件名有中文,Python读取unicode文件夹和文件名

python 中文文件名 import unicode

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/204076
推荐阅读
相关标签
  

闽ICP备14008679号