How Getting file Size in Python?

Here we will learn How Getting file Size in Python where we can use the stat() function of the os module which could get us all the details of the file that we passed as an argument to it, And it will return all the possible information related to that file including the size in bytes, in Mega Bytes, And some other information too.

Size of File

Way to Get The Size Of File

To get the size of the python file stored we need to run a piece of code which is given below, where first we need to import the os module, and then as the name of the file we need to provide the complete path of the file along with the name of the file so that it could retrieve the file and read it, analyst it and produce the result as the size of the file.

import os

file_name = "/Users /pankaj /abcdef .txt"

file_stats = os.stat( file_name)

print(f'File Size in Bytes is {file_stats.st_size}')
print(f'File Size in MegaBytes is {file _size / (1024 * 1024)}')

Here we simply get the data size in bytes and then simply convert it into mega bytes and even we can convert it into any of the units as per our choice.

Although it might be producing some other information related to that file which could also be used for other purposes which might not be related to getting the size that we need right now.


To learn more about how to get the size of any of the python files stored in a local computer and need to read more you may visit: Get the size of a python file stored in local drive.

To learn more about python and solve the problems related to python you may also visit: Python’s list: What is the difference Between methods Append and Extend?. where you may find different python tutorials.

To learn more about different other programming languages visit beta python programming languages Solutions.

Leave a Comment

%d bloggers like this: