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

2

0

I am new to python. I am trying to use os.path.getsize() to obtain the file size. However, if the file name is not in Englist, but in Chinese, Gemany, or French, etc, Python cannot recognize it and do not return the size of the file. Could you please help me with it? 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"

I'd like to use" os.path.getsize(path)

But it does not recognize the file name. Could you please kindly tell me what should I do?

Thank you very much!

Shluh answered 1/7, 2013 at 17:53 Comment(0)
B
1

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).

Blown answered 1/7, 2013 at 17:54 Comment(3)
Would you please tell me how I can transform the filenames into unicode filenames?Shluh
That is too broad a question; you'll need to add examples of what you have now, how you got those names, what works and what doesn't.Blown
Could you please see my edited question if possible? Thank you very much!Shluh
G
0

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)
Giffie answered 13/4, 2018 at 1:58 Comment(1)
TypeError: decoding with 'utf8' codec failed (TypeError: a bytes-like object is required, not 'str')Polyphemus

© 2022 - 2024 — McMap. All rights reserved.