[英]Python reading unicode folder and file names
I am new to Python. 我是Python的新手。 I am trying to input one path and use
os.walk()
to search all files, and return the files' names. 我试图输入一个路径并使用
os.walk()
搜索所有文件,并返回文件的名称。 I also want to use os.path.getsize()
to get file size, and finally, write them into a csv file. 我还想使用
os.path.getsize()
来获取文件大小,最后将它们写入csv文件。
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. 但是,如果文件名不是英文,而是中文,德文,法文等,则Python无法识别它并且不返回文件的大小。 I'd like to use
os.path.getsize(path)
(referring to the example below), but it does not recognize the file's name. 我想使用
os.path.getsize(path)
(参考下面的示例),但它无法识别文件的名称。 How can I let Python recognize the file's name and return the size of these kind of files? 如何让Python识别文件的名称并返回这些文件的大小?
For example: the file's name is: "Показатели естественного и миграционного прироста до 2030г.doc"
. 例如:文件的名称是:
"Показатели естественного и миграционного прироста до 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. 如果将Unicode输入传递给
os.walk()
您也会将文件名作为Unicode返回。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.