Last updated: Apr 10, 2024
Reading timeยท4 min
To check if a file is empty:
os.stat()
method to get the status of the file.st_size
attribute to get the size of the file in bytes.0
bytes, then the file is empty.import os # โ with relative path if os.stat('example.txt').st_size == 0: print('The file is empty') else: print('The file is not empty') # --------------------------------------------- # โ with absolute path if os.stat( '/home/borislav/Desktop/bobbyhadz_python/example.txt' ).st_size == 0: print('The file is empty') else: print('The file is not empty')
We used the os.stat()
method to get the status of a file.
The method can be passed a relative path or an absolute path to the file.
The os.stat() method
returns a stat_result
object representing the status of the given file.
The st_size attribute of the object returns the size of the file in bytes.
import os if os.stat('example.txt').st_size == 0: print('The file is empty') else: print('The file is not empty')
0
bytes, then the file is empty.You can use the not equals !=
operator if you need to check if the file is not
empty.
import os if os.stat('example.txt').st_size != 0: print('The file is NOT empty')
The method will raise a FileNotFoundError
exception if the specified file
doesn't exist.
You can use a try/except statement if you need to handle the error.
import os try: if os.stat('not-found.txt').st_size == 0: print('The file is empty') except FileNotFoundError: # ๐๏ธ this runs print('The specified file does NOT exist')
The specified file doesn't exist, so a FileNotFoundError
exception is raised
and is then handled by the except
block.
Alternatively, you can use the os.path.getsize()
method.
If the os.path.getsize()
method returns 0
, then the file has a size of 0
bytes and is empty.
import os # ๐๏ธ with relative path if os.path.getsize('example.txt') == 0: print('The file is empty') else: print('The file is NOT empty') # --------------------------------------------- # ๐๏ธ with absolute path if os.path.getsize( '/home/borislav/Desktop/bobbyhadz_python/example.txt' ) == 0: print('The file is empty') else: print('The file is NOT empty')
The os.path.getsize() method takes a path and returns the size of the path in bytes.
If the file has a size of 0
bytes, then it's empty.
The method raises an OSError
exception if the specified file doesn't exist or
is inaccessible.
Use a try/except statement if you need to handle the error.
import os try: if os.path.getsize('not-found.txt') == 0: print('The file is empty') except OSError: # ๐๏ธ this runs print('The specified file does NOT exist')
The given path doesn't exist, so an OSError
exception is raised and is then
handled by the except
block.
Alternatively, you can check if the file exists using the os.path.exists()
method.
import os my_path = '/home/borislav/Desktop/bobbyhadz_python/example.txt' if os.path.exists(my_path) and os.path.getsize(my_path) == 0: print('The file is empty') else: print('The file does NOT exist or is not empty')
os.path.exists()
method returns True
if the provided path exists and False
otherwise.Alternatively, you can use the pathlib.Path
class.
This is a four-step process:
pathlib.Path()
class to create a Path object.stat()
method to get a stat_result
object.st_size
attribute to get the file's size.0
bytes, then it's empty.import pathlib my_path = pathlib.Path( '/home/borislav/Desktop/bobbyhadz_python/example.txt' ) if my_path.stat().st_size == 0: print('The file is empty') else: print('The file is NOT empty')
The pathlib.Path
class is used to create a PosixPath
or a WindowsPath
object depending on
your operating system.
The
Path.stat()
method returns a stat_result
object just like the one from the os.stat()
method.
The object has a st_size
attribute that returns the size of the file in bytes.
If the specified path doesn't exist, a FileNotFoundError
exception is raised.
You can use a try/except
block if you need to handle the error.
import pathlib my_path = pathlib.Path( '/home/borislav/Desktop/bobbyhadz_python/example.txt' ) try: if my_path.stat().st_size == 0: print('The file is empty') else: print('The file is NOT empty') except FileNotFoundError: print('The file does NOT exist')
Which approach you pick is a matter of personal preference. They all get the file's size.
If the file has a size of 0
bytes, then it's empty, otherwise, it isn't empty.
You can learn more about the related topics by checking out the following tutorials: