How to Get File size in bytes, KB, MB, GB, TB format, python with example
This short post explains multiple ways to get the size of a file in Python with examples
Python File Size Example
To get file size in bytes, Python provides multiple ways. The first way is using os.path.getsize() function
Syntax:
os.path.getsize(filepath)
Arguments: filepath: Path of a file.
Returns:
- Returns file size in bytes.
- throws
OSErrorif the file is not found or no file read access permission
Here is an example
import os
print(os.path.getsize("./emp.json"))
Output:
129
This function internally calls to os.stat("./emp.json").st_size method.
- use os stat function st_size property.
os module has [stat](https://docs.python.org/3/library/os.html#os.stat) function that returns os.stat_result data.
Syntax:
os.stat(filepath)
Arguments: filepath: Path of a file.
Returns:
Returns data of the
os.stat_resultclass.stat_resultcontainsst_mode,st_ino,st_dev,st_nlink,st_uid,st_gidst_size,st_atime,st_mtime,st_ctimeproperties.st_sizecontains the size of a file in bytes.throws
FileNotFoundErrorif the file is not found
import os
print(os.stat("./emp.json"))
Output:
os.stat_result(st_mode=33206, st_ino=1407374886069737, st_dev=3330445139, st_nlink=1, st_uid=0, st_gid=0, st_size=129, st_atime=1668608646, st_mtime=1668608646, st_ctime=1668608600)
To get file size in bytes, call os.start(filepath).st_size Here is an example
import os
startResult=os.stat("./emp.json")
print(startResult.st_size)
129
- use Path(path).stat().st_size
Path class has stat()🔗 method that returns the os.stat_result, which contains st_size property to get file size.
Here is an example
import os
from pathlib import Path
startResult=Path("./emp.json").stat()
print(startResult)
print(startResult.st_size)
Output:
os.stat_result(st_mode=33206, st_ino=1407374886069737, st_dev=3330445139, st_nlink=1, st_uid=0, st_gid=0, st_size=129, st_atime=1668608646, st_mtime=1668608646, st_ctime=1668608600)
129
Convert file size in bytes to KB, MB, GB, and TB in python examples
To get the file size in KB, MB, GB, and TB bytes in python, use
Python os stat method contains the size of the file in Bytes. This example converts file size in bytes to kilobytes, megabytes, gigabytes, terabytes
- First, find the size of a file using os.stat().st_size, returns size in bytes
- Convert the bytes to
Kilobytes(KB)using floating division with float(1024) - kilobytes converted to
megabytes(MB)using floating division with float(1024) - megabytes converted to
gigabytes(GB)using floating division with float(1024) - gigabytes converted to
terabytes(TB)using floating division with float(1024)
Here is an example
import os
startResult=os.stat("b:\meeting.mp4")
sizeinbytes=startResult.st_size;
print (sizeinbytes,"Bytes") # Bytes (B)
sizeinkb = sizeinbytes / float(1024);
print (sizeinkb,"KB") # kilobytes (kB)
sizeinmb = sizeinkb / float(1024);
print (sizeinmb ,"MB") # megabytes (MB)
sizeingb = sizeinmb / float(1024);
print (sizeingb,"GB") # gigabytes (GB)
sizeintb = sizeingb / float(1024);
print (sizeintb,"TB") # terabytes (TB)
Output:
3145728000 Bytes
3072000.0 KB
3000.0 MB
2.9296875 GB
0.00286102294921875 TB
Conclusion
Learned multiple ways to get file size in python example. It returns bytes and converts these to KB, MB, GB, and TB.
