问题
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"
回答1:
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
来源:https://stackoverflow.com/questions/18105873/python-reading-unicode-folder-and-file-names