• 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 / How to Check If File is Empty with Python

How to Check If File is Empty with Python

June 13, 2022 Leave a Comment

To check if a file is empty in Python, the easiest way is check if a file has size 0 with the os.path module getsize() function.

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

You can also use the os module stat() function to get the size of a file in Python and check if the file size is equal to 0.

import os

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

#Output:
file is empty

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

from pathlib import Path

if Path("C:/Users/TheProgrammingExpert/example.txt").stat().st_size == 0: 
    print("file is empty")
else:
    print("file is not empty")


#Output:
file is empty

When working with files in Python, the ability to find the statistics of a file is important.

One such piece of information which is valuable is if a file is empty or not.

We can check if a file is empty by first getting the size of a file, and then checking if the file size is equal to 0.

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 Check if File is Empty 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.

After using getsize(), you can then check if the size is equal to 0.

Below is a simple example showing how you can check if a file is empty using 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

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.txt"))

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

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

Using pathlib Module to Check if File is Empty 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. Then, in the same way as above, you can check if the file size is 0.

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 and check if the file is empty.

from pathlib import Path

if Path("C:/Users/TheProgrammingExpert/example.txt").stat().st_size == 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 check if a file is empty using Python.

Other Articles You'll Also Like:

  • 1.  How to Group By Columns and Find Sum in pandas DataFrame
  • 2.  How to Use Python to Remove Zeros from List
  • 3.  Check if Number is Between Two Numbers Using Python
  • 4.  Python turtle dot() – Draw Dot on Turtle Screen
  • 5.  Python Get First Word in String
  • 6.  Check if Class is Subclass in Python with issubclass()
  • 7.  Convert True to 1 in Python
  • 8.  How to Iterate over Everything in Word Document using python-docx
  • 9.  How to Check if List is Empty in Python
  • 10.  How to Group By Columns and Find Maximum in pandas DataFrame

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