• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Get Size of File in Python with os.path.getsize() Function

Get Size of File in Python with os.path.getsize() Function

May 9, 2022 Leave a Comment

To get the size of a file in Python, the easiest way is with the os.path module getsize() function.

import os

print(os.path.getsize("C:/Users/TheProgrammingExpert/example.png"))

#Output:
351

You can also use the os module stat() function to get the size of a file in Python.

import os

print(os.stat("C:/Users/TheProgrammingExpert/example.png").st_size)

#Output:
351

Finally, if you are using the pathlib module and Path, you can get the size of a file with the Path.stat() function.

from pathlib import Path

print(Path("C:/Users/TheProgrammingExpert/example.png").stat().st_size)

#Output:
351

When working with files in Python, the ability to easily get the size of a file is valuable.

In Python, there are a few ways you can get the size of a file. The easiest way is with the os module, but you can also use the pathlib module.

Using os Module to Get File Size in Python

The Python os module has many great functions which help us interact with the operating system of our computer.

To get the size of a file in Python, you can use the os.path module getsize() function. getsize() returns the size of the file in bytes.

Below is a simple example showing how you can get the size of a file using Python.

import os

print(os.path.getsize("C:/Users/TheProgrammingExpert/example.png"))

#Output:
351

You can also use the os module stat() function to get the size of a file in Python.

The stat() function returns various statistics about a given file. The file size is stored in the ‘st_size’ attribute.

import os

print(os.stat("C:/Users/TheProgrammingExpert/example.png"))
print(os.stat("C:/Users/TheProgrammingExpert/example.png").st_size)

#Output:
os.stat_result(st_mode=33206, st_ino=562949953632850, st_dev=2117907462, st_nlink=1, st_uid=0, st_gid=0, st_size=351, st_atime=1652100546, st_mtime=1652018258, st_ctime=1644459271)
351

Using pathlib Module to Get File Size in Python

You can also use the pathlib module to get file size in your Python code.

With the Python pathlib module, we can perform many operations to access files and directories in our environments.

Using the pathlib module and Path, you can get the size of a file with the Path.stat() function.

The Path.stat() function is similar to the os.stat() function.

Below is an example of how you can use the pathlib module to get a file’s size in bytes in Python.

from pathlib import Path

print(Path("C:/Users/TheProgrammingExpert/example.png").stat().st_size)

#Output:
351

How to Check if a File is Empty in Python

To check if a file is empty using Python, you can use any of the methods above to get the file size and then check if the file size is equal to 0.

If the file size is equal to 0, then the file is empty.

Below shows you a simple example of how to check if a file is empty in Python.

import os

if os.path.getsize("C:/Users/TheProgrammingExpert/example.txt") == 0: 
    print("file is empty")
else:
    print("file is not empty")

#Output:
file is empty

Hopefully this article has been useful for you to learn how to get the size of a file using Python.

Other Articles You'll Also Like:

  • 1.  How to Write Excel File to AWS S3 Bucket Using Python
  • 2.  Find Index of Maximum in List Using Python
  • 3.  Check if Character is Letter in Python
  • 4.  pandas Drop Rows – Delete Rows from DataFrame with drop()
  • 5.  Python getsizeof() Function – Get Size of Object
  • 6.  Creating a Random Color Turtle in Python
  • 7.  Sort Series in pandas with sort_values() Function
  • 8.  Convert timedelta to Seconds in Python
  • 9.  Comparing Datetimes in Python
  • 10.  Count Number of Keys in Dictionary in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy