In this tutorial, we will look at how to the get the file size using Python.
Programmatically get file size
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.

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.
1. Using os
module
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:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.
2. Using pathlib
module
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 –