How can I check file size in Python?

| 0 Comments| | 15:16
Categories:
import os
os.path.getsize('C:\\Python27\\Lib\\genericpath.py')

or

import os
os.stat('C:\\Python27\\Lib\\genericpath.py').st_size

or

from pathlib import Path
Path('C:\\Python27\\Lib\\genericpath.py').stat().st_size

 
By the way, to convert from bytes to any other unit by doing a right shift by 10 you basically shift it by an order (multiple).
 
Example: 5GB are 5368709120 bytes

print (5368709120 >> 10)  # 5242880 kilobytes (kB)
print (5368709120 >> 20 ) # 5120 megabytes (MB)
print (5368709120 >> 30 ) # 5 gigabytes (GB)

 
 
ref:
Getting file size in Python?
How can I check file size in Python?

Leave a Reply

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *