Python读取unicode文件夹和文件名 [英] Python reading unicode folder and file names

查看:691
本文介绍了Python读取unicode文件夹和文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手。我试图输入一个路径并使用 os.walk()搜索所有文件,并返回文件的名称。我还想使用 os.path.getsize()获取文件大小,最后,将它们写入csv文件。

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.

但是,如果文件名不是英文,而是中文,德文,法文等,Python无法识别它,并且不返回文件大小。我想使用 os.path.getsize(path)(参考下面的示例),但它不能识别文件的名称。如何让Python识别文件的名称并返回这些文件的大小?

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?

例如:文件名为:Показатели естественногоимиграционногоприростадо2030г.doc path =C:\xxxx\xxx\xxxx\\\\\\××××××××××××××××××××××××××××××××××

For example: the file's name is: "Показатели естественного и миграционного прироста до 2030г.doc". path="C:\xxxx\xxx\xxxx\Показатели естественного и миграционного прироста до 2030г.doc"

推荐答案

如果将Unicode输入传递给 os.walk(),则会将文件名

If you pass a Unicode input to os.walk() you will get back file-names as Unicode as well.

以下内容适用于您

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

这篇关于Python读取unicode文件夹和文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆