Obtain File size with os.path.getsize() in Python 2.7.5

ε祈祈猫儿з 提交于 2019-12-02 10:14:44

Use Unicode filenames and let Python encode the codepoints to the correct encoding for your system.

Alternatively, detect the filesystem encoding yourself, and ensure that your filenames are using that specific encoding when passing these to the os.path.getsize() function.

If you do not yet know what Unicode is, or how that relates to encodings, I urge you to read:

before you continue.

If you are specifying a literal string in your source code, then you need to make sure that you have specified the codec used to save your source, and to use a unicode literal:

# -*- coding: utf-8 -*-

path = u"C:\xxxx\xxx\xxxx\Показатели естественного и миграционного прироста до 2030г.doc"

specifies that you saved your source code in UTF-8 and that the path variable should hold a Unicode string (note the u'' string literal).

Luis Alcántara

You can solve your problem with this code:

import codecs

path="C:\xxxx\xxx\xxxx\Показатели естественного и миграционного прироста до 2030г.doc"

path=codecs.decode(path,'utf8') 
os.path.getsize(path)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!