Python Get File Size in KB, MB or GB

Python Get File Size : In this article we will see how to get the size of a file on python and convert it into a unit of measure adapted to our understanding.
Get file size using os.path.getsize()
In the OS module, the getsize() function allows to get the size of a file in bytes, To use it nothing more simple :
import os
fileSize = os.path.getsize(file_path)
We are going to create a function that allows us to retrieve the size of the file passed as a parameter of the function :
import os
def get_file_size(file_path):
size = os.path.getsize(file_path)
return size
file_path = 'amiradata_example.csv'
size = get_file_size(file_path)
print('File size: '+ str(size) +' bytes')
Output :
File size: 26187953 bytes
The getsize() function returned the size in bytes of the amiradata_example.csv file.
Get file size using os.stat().st_size
the OS module offers a second way to do this by directly retrieving the statistics from the file . the syntax is as follows :
os.stat(file).st_size
Note: the stat() function allows you to retrieve a lot of information about the file, in particular the date of creation, modification and of course the size of the file.
Here is the code to recover the size of the amiradata_example.csv file :
import os
def get_file_size_2(file):
stat = os.stat(file)
print(stat)
size = stat.st_size
return size
file_path = 'amiradata_example.csv'
size = get_file_size_2(file_path)
print('File size: ' + str(size) + ' bytes')
Output:
os.stat_result(st_mode=33206, st_ino=16044073672847566, st_dev=606155843, st_nlink=1, st_uid=0, st_gid=0, st_size=26187953, st_atime=1589472190, st_mtime=1589472190, st_ctime=1589472134)
File size: 26187953 bytes
We get the same result as the previous function ( which is rather reassuring :))
Get size using pathlib.Path.stat().st_size
the pathlib module also allows to get the size of the file passed in :
size = Path(file_path).stat().st_size
Let’s take again our csv file and create the following function :
from pathlib import Path
def get_file_size_3(file):
size = Path(file).stat().st_size
return size
file = 'amiradata_example.csv'
size = get_file_size_3(file)
print('File size: ' + str(size) + ' bytes')
Output :
File size: 26187953 bytes
Get file size in KiloBytes, MegaBytes or GigaBytes
Here is a conversion table to switch from bytes to another unit of measurement :
Units | Number of bytes |
KiloBytes | 1024 |
MegaBytes | 1024*1024 = 1 048 576 |
GigaBytes | 1024*1024*1024 = 1 073 741 824 |
To convert 26187953 bytes into kilobytes or megabytes, we have created this function which allows to convert the size into Mb, Kb or Gb (We have rounded off to 3 decimal places for ease of reading, but you can remove it if you need to):
import os
from pathlib import Path
def get_file_size(file_path):
size = os.path.getsize(file_path)
return size
def get_file_size_2(file):
stat = os.stat(file)
size = stat.st_size
return size
def get_file_size_3(file):
size = Path(file).stat().st_size
return size
def convert_bytes(size, unit=None):
if unit == "KB":
return print('File size: ' + str(round(size / 1024, 3)) + ' Kilobytes')
elif unit == "MB":
return print('File size: ' + str(round(size / (1024 * 1024), 3)) + ' Megabytes')
elif unit == "GB":
return print('File size: ' + str(round(size / (1024 * 1024 * 1024), 3)) + ' Gigabytes')
else:
return print('File size: ' + str(size) + ' bytes')
file = 'amiradata_example.csv'
print("Using 1st method : ")
size = get_file_size(file)
convert_bytes(size)
convert_bytes(size, "KB")
convert_bytes(size, "MB")
convert_bytes(size, "GB")
print("Using Second method : ")
size = get_file_size_2(file)
convert_bytes(size)
convert_bytes(size, "KB")
convert_bytes(size, "MB")
convert_bytes(size, "GB")
print("Using third method : ")
size = get_file_size_3(file)
convert_bytes(size)
convert_bytes(size, "KB")
convert_bytes(size, "MB")
convert_bytes(size, "GB")
Conclusion
There are several ways to retrieve the size of a python file, I won’t advise you one more than the other as I find them all simple to use and produce the same result so it’s up to you.
I hope this tutorial has helped you learn more about the subject.
See you soon!
Comments
Leave a comment