Get size of a file using python

Get File size using Python

In this tutorial, we will look at how to the get the file size using Python.

There are a number of ways to get the size of a file in Python. You can use the os module’s os.stat() function to get a number of file statistics including its size. You can also use the pathlib module to get the file size.

Let’s look at the usage of the above-mentioned methods with the help of some examples. First, let’s manually check the size of the file we’ll be measuring in python which is a CSV file containing 5k reviews of movies.

File properties in Windows showing the file size of a CSV file.

You can see that the file size is approximately 6.40 MB. Let’s now go ahead and use python to get the size of the very same file.

The os module in python comes with a number of useful functions to interact with the file system. To get the file size, you can use the os.stat() function which different file statistics. To specifically get the file size, use the st_size attribute. For example, let’s get the size of the above file using this method.

import os

# the absoulte path of the file
file_path = r"C:\Users\piyush\Documents\Projects\movie_reviews_data\IMDB Dataset 5k.csv"
# get the file size
print(os.stat(file_path).st_size)

Output:

6716779

Note that we used an absolute path to the file here. You can also use a relative path. You can see that we get the file size in bytes which can easily be converted to Kilobytes and Megabytes. Let’s create a simple function to get the file size in Bytes, Kilobytes, and Megabytes.

# function to show file size in bytes, kilobytes, and megabytes
def show_file_size(size):
    kb = size/1024
    mb = kb/1024
    print("The file size in -")
    print("Bytes: {}".format(size))
    print("Kilobytes (KB): {0:.2f}".format(kb))
    print("Megabytes (MB): {0:.2f}".format(mb))
    
# display the file size
size = os.stat(file_path).st_size
show_file_size(size)

Output:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

The file size in -
Bytes: 6716779
Kilobytes (KB): 6559.35
Megabytes (MB): 6.41

You can see that we get the file size as ~6.41MB which is approximately equal to the size we got from the file properties in Windows.

Alternatively, you can also use the os.path.getsize() function to get the file size.

# the absoulte path of the file
file_path = r"C:\Users\piyush\Documents\Projects\movie_reviews_data\IMDB Dataset 5k.csv"
# get the file size
print(os.path.getsize(file_path))

Output;

6716779

We get the same result as we did with os.stat(file_path).st_size.

For more on the os.stat() function, refer to its documentation.

You can also get the file size using the pathlib module. Note that this method is simply a wrapper around os.stat(file_path) as it returns the same object.

from pathlib import Path

# the absoulte path of the file
file_path = r"C:\Users\piyush\Documents\Projects\movie_reviews_data\IMDB Dataset 5k.csv"
# get the file size
print(Path(file_path).stat().st_size)

Output:

6716779

We get the file size in bytes.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with python (version 3.8.3) kernel.


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Tutorials on interacting with the file system in Python –

Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top