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.

Categorized in:

Python,

Last Update: March 14, 2024

Tagged in: