How to get file size in Python? [SOLVED]

How to get file size in Python?

In this short article, we will discuss how we can get file size in Python. We will use various methods along with Python code and explain each step to understand the process of getting file size in Python.

Usually, the file size is measured in Bytes (B), Kilobytes (KB), Megabytes (MB), Gigabytes (GB), Terabytes (TB), and so on. The file sizes can be measured using a binary system (where kilo means 1024) or metric system (kilo means 1000). We can follow different approaches to get the file size in Python. It’s important to get the file size in Python to monitor file size or in case of ordering files in the directory according to file size.

Advertisement

Let us explore various methods to get file size in Python.

 

Method-1: Using os.path.getsize()

In the first method, we will use the os module to get the size of the file in Python. The OS module in Python provides functions for creating and removing a directory (folder), fetching its contents, changing and identifying the current directory, etc. You first need to import the os module to interact with the underlying operating system. We can use the os.path() method to get the size of the file.

Let us assume that we have the following file in the same directory where our Python file is located.

get-file-size-in-Python

Now let us use the os.path() method to get the size of the file.

# importing os module
import os
 
# get size of file in Python
file_size = os.path.getsize('bitcoin_csv.csv')

# printing size of file 
print("File Size is :", file_size, "bytes")

Output:

File Size is : 444891 bytes

As you can see, we get the size of the file which is in bytes. To convert the bytes into megabytes, we need to divide the size by (1024 *1024) as shown below.

Advertisement
# get size of file in Python
file_size = os.path.getsize('bitcoin_csv.csv')/(1024*1024)

# printing size of file 
print("File Size is :", file_size, "mega-bytes")

Output:

File Size is : 0.42428112030029297 mega-bytes

As you can see, this time we were able to get file size in Python in megabytes.

 

Method-2: Using os.stat()

Another important function in the os module is the os.stat() method that gives us different information about the file including the size as well. Let us use the os.stat method to get file size in Python.

# importing os module
import os
 
file_info = os.stat('bitcoin_csv.csv')

# printing the information 
print("stats about the file is :", file_size)

Output:

stats about the file is : os.stat_result(st_mode=33204, st_ino=6160388, st_dev=2053, st_nlink=1, st_uid=1000, st_gid=1000, st_size=444891, st_atime=1668422753, st_mtime=1668166135, st_ctime=1668166143)

As shown above, the stat method returns a lot of information about the file including the size. We can also extract the information about size only as well.

# importing os module
import os

# info about file
file_info = os.stat('bitcoin_csv.csv')
 
# printing get file size in python 
print("File Size is :", file_info.st_size, "bytes")
print("File Size is :", file_info.st_size/(1024*1024), "megabytes")

Output:

Advertisement
File Size is : 444891 bytes
File Size is : 0.42428112030029297 megabytes

This time we were able to get only the size of the file.

 

Method-3: Using file.seek()

Another method to get the  size of file is to open the file and store the data in a variable. Then we can use the seek method can be used to get the size of the file as shown below:

# open file using open method
file = open('bitcoin_csv.csv')
 
# get the cursor positioned at end
file.seek(0, os.SEEK_END)
 
# get file size in python
print("Size of file is :", file.tell(), "bytes")
print("Size of file is :", file.tell()/(1024*1024), "megabytes")

Output:

Size of file is : 444891 bytes
Size of file is : 0.42428112030029297 megabytes

As shown above, we get the size of the file. The seek method used to set the cursor to the desired location. It accepts 2 arguments – start location and end location. To set the cursor at the end location of the file use method os.SEEK_END. While the tell() method that can be used to get the current cursor location which will be equivalent to the number of bytes cursor has moved. So this method actually returns the size of the file in bytes.

 

Method-4: Using Path().stat()

Another method to get file size in Python is to use Pathlib module. The pathlib is a Python module which provides an object API for working with files and directories. The pathlib is a standard module. Path is the core object to work with files.

The stat() method of the Path object returns the information about the file and we can extract the size of file from there.

Advertisement

Let us first use the stat() method to get the basic information about the file.

# using pathlib module
from pathlib import Path


# information about file
file_info=Path('bitcoin_csv.csv').stat()

# display the info of the file
print(file_info)

Output:

os.stat_result(st_mode=33204, st_ino=6160388, st_dev=2053, st_nlink=1, st_uid=1000, st_gid=1000, st_size=444891, st_atime=1668422753, st_mtime=1668166135, st_ctime=1668166143)

Now, we will access the size of the file from the above information

# using pathlib module
from pathlib import Path

# get file size in python
file_size=Path('bitcoin_csv.csv').stat().st_size

# print the size of the file
print("Size of file is :", file_size, "bytes")
print("Size of file is :", file_size/(1024*1024), "megabytes")

Output:

Size of file is : 444891 bytes
Size of file is : 0.42428112030029297 megabytes

As you can see, we were able to get the size of the file in Python.

 

Summary

A file size is the measure of space a file takes up on a storage medium, such as a computer hard drive. In Python we can use various methods to get the size of the file. In this short article, we discussed how we can use the OS and Pathlib modules to get the size of the file. Moreover, we learned 4 various methods through which we can get the size of the file.

Advertisement

 

Further Readings

Python Pathlib
How do I check file size in Python? - Stack Overflow

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X