Use os.path.getsize(path) which will
Return the size, in bytes, of path. Raise
OSErrorif the file does not exist or is inaccessible.
import os
os.path.getsize('C:\\Python27\\Lib\\genericpath.py')
Or use os.stat(path).st_size
import os
os.stat('C:\\Python27\\Lib\\genericpath.py').st_size
Or use Path(path).stat().st_size (Python 3.4+)
from pathlib import Path
Path('C:\\Python27\\Lib\\genericpath.py').stat().st_size
Answer from Artsiom Rudzenka on Stack OverflowUse os.path.getsize(path) which will
Return the size, in bytes, of path. Raise
OSErrorif the file does not exist or is inaccessible.
import os
os.path.getsize('C:\\Python27\\Lib\\genericpath.py')
Or use os.stat(path).st_size
import os
os.stat('C:\\Python27\\Lib\\genericpath.py').st_size
Or use Path(path).stat().st_size (Python 3.4+)
from pathlib import Path
Path('C:\\Python27\\Lib\\genericpath.py').stat().st_size
os.path.getsize(path)
Return the size, in bytes, of path. Raise os.error if the file does not exist or is inaccessible.
linux - How to get the size of tar.gz in (MB) file in python - Stack Overflow
How do I check file size in Python? - Stack Overflow
Is there function to know raster's size (KB) with python? - Geographic Information Systems Stack Exchange
Need to resize multiple images to a specific size in KB. How do I achieve this?
Videos
It's not clear from your question whether you want to the compressed or uncompressed size of the file, but in the former case, it's easy with the os.path.getsize function from the os module
>>> import os
>>> os.path.getsize('flickrapi-1.2.tar.gz')
35382L
To get the answer in megabytes you can shift the answer right by 20, e.g.
os.path.getsize('large.tar.gz') >> 20
Although that operation will be done in integers - if you want to preserve fractions of a megabyte, divide by (1024*1024.0) instead. (Note the .0 so that the divisor will be a float.)
Update: In the comments below, Johnsyweb points out a useful recipe for more generally producing human readable representations of file sizes.
To get file size in MB, I created this function:
import os
def get_size(path):
size = os.path.getsize(path)
if size < 1024:
return f"{size} bytes"
elif size < pow(1024,2):
return f"{round(size/1024, 2)} KB"
elif size < pow(1024,3):
return f"{round(size/(pow(1024,2)), 2)} MB"
elif size < pow(1024,4):
return f"{round(size/(pow(1024,3)), 2)} GB"
>>> get_size("k.tar.gz")
1.4MB
Use os.path.getsize:
>>> import os
>>> os.path.getsize("/path/to/file.mp3")
2071611
The output is in bytes.
You need the st_size property of the object returned by os.stat. You can get it by either using pathlib (Python 3.4+):
>>> from pathlib import Path
>>> Path('somefile.txt').stat()
os.stat_result(st_mode=33188, st_ino=6419862, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=1564, st_atime=1584299303, st_mtime=1584299400, st_ctime=1584299400)
>>> Path('somefile.txt').stat().st_size
1564
or using os.stat:
>>> import os
>>> os.stat('somefile.txt')
os.stat_result(st_mode=33188, st_ino=6419862, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=1564, st_atime=1584299303, st_mtime=1584299400, st_ctime=1584299400)
>>> os.stat('somefile.txt').st_size
1564
Output is in bytes.
Try referring to this Stack Overflow post. They are looking at zip files, but it should work fine for your raster file.
If you are using a type of raster that is made up of multiple files, then I would suggest getting the file size of each and summing it.
The code would be similar to:
import os
os.path.getsize(#your file here)
As the post says, you can shift it to MB like this:
os.path.setsize(#your file here) >> 20
Credit definitely goes to Mark Longair and his answer on the referenced form post.
Using hurry.filesize you could do something like this :
import os
from hurry.filesize import size
raster_size = os.path.getsize('YOUR_PATH_HERE')
size(raster_size, system=alternative)