How to Get File Size in Python?

I recently faced an issue where I needed to retrieve the size of a file. After researching and testing various methods, I discovered several effective ways to accomplish this task. In this tutorial, I will explain how to get file size in Python and I will share my findings and provide you with detailed examples and screenshots of executed example code.

Get File Size in Python

Let us learn some important methods to get file size in Python.

Read How to Overwrite a File in Python?

Method 1. Use the os.path.getsize() Function

One of the most simple ways to get the size of a file in Python is by using the os.path.getsize() function from the built-in os module. This function takes the file path as an argument and returns the file size in bytes. Here’s an example:

import os

file_path = "C:\Users\Public\code\sales_report.csv"
file_size = os.path.getsize(file_path)
print(f"The size of {file_path} is {file_size} bytes.")

Output:

The size of C:\Users\Public\code\sales_report.csv is 1111182 bytes.

I executed the above example code and added the screenshot below.

Get File Size in Python

In this example, we provide the path to a sales report CSV file located in the Documents folder of a user named John Doe. The os.path.getsize() function retrieves the size of the file in bytes, which we then print to the console.

Check out How to Rename Files in Python?

Method 2. Use the os.stat() Function

Another way to get the file size in Python is by using the os.stat() function. This function provides more detailed information about a file, including its size. Here’s an example:

import os

file_path =  r"C:\Users\Public\code\images\family_vacation.jpg"
file_stats = os.stat(file_path)
file_size = file_stats.st_size
print(f"The size of {file_path} is {file_size} bytes.")

Output:

The size of C:\Users\Public\code\images\family_vacation.jpg is 12452 bytes.

I executed the above example code and added the screenshot below.

How to Get File Size in Python

In this example, we have a file path pointing to a family vacation photo in the Pictures folder of a user named Sarah Johnson. We use the os.stat() function to retrieve the file statistics and then access the st_size attribute to get the file size in bytes.

Read How to Check if a File is Empty in Python?

Handle Exceptions

When working with file paths, it’s important to handle potential exceptions that may occur. For example, if the specified file path doesn’t exist or is inaccessible, an OSError will be raised. Here’s an example of how to handle such exceptions:

import os

file_path = "C:\\Users\\MichaelSmith\\Documents\\financial_report.xlsx"
try:
    file_size = os.path.getsize(file_path)
    print(f"The size of {file_path} is {file_size} bytes.")
except OSError as e:
    print(f"Error: {e}")

In this example, we attempt to get the size of a financial report Excel file in the Documents folder of a user named Michael Smith. We wrap the os.path.getsize() function call inside a try-except block to catch any OSError exceptions that may occur. If an exception is raised, we print the error message.

Check out How to Get File Name Without Extension in Python?

Conclusion

In this tutorial, I explored how to get file size in Python. I helped you learn how to use the os.path.getsize() function and the os.stat() function to retrieve the file size in bytes. Additionally, I discussed the importance of handling exceptions when working with file paths.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.