Python is a widely used general-purpose, high-level programming language. It provides many functionalities and one among them is checking whether a directory is empty or not. This can be achieved by using os module. The OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. The os and os.path modules include many functions to interact with the file system.
Check if a directory is empty
To check whether a directory is empty or not os.listdir() method is used. os.listdir() method of os module is used to get the list of all the files and directories in the specified directory.
Syntax: os.listdir(path)
Parameters:
path (optional): path of the directoryReturn Type: This method returns the list of all files and directories in the specified path. The return type of this method is list.
Example #1: If the list returned by os.listdir() is empty then the directory is empty otherwise not. Below is the implementation.
# Python program to check whether # the directory empty or not import os # path of the directory path = "D:/Pycharm projects/GeeksforGeeks/Nikhil" # Getting the list of directories dir = os.listdir(path) # Checking if the list is empty or not if len(dir) == 0: print("Empty directory") else: print("Not empty directory") |
Output:
Empty directory
Example #2: Suppose the path specified in the above code is a path to a text file or is an invalid path, then, in that case, the above code will raise an OSError. To overcome this error we can use os.path.isfile() method and os.path.exists() method. Below is the implementation.
# Python program to check whether # the directory is empty or not import os # Function to Check if the path specified # specified is a valid directory def isEmpty(path): if os.path.exists(path) and not os.path.isfile(path): # Checking if the directory is empty or not if not os.listdir(path): print("Empty directory") else: print("Not empty directory") else: print("The path is either for a file or not valid") # path to a file path = "D:/Pycharm projects/GeeksforGeeks/Nikhil/gfg.txt"isEmpty(path) print() # valid path path = "D:/Pycharm projects/GeeksforGeeks/Nikhil/"isEmpty(path) |
Output:
The path is either for a file or not valid Not empty directory
Recommended Posts:
- Python - Check if a file or directory exists
- Python: Check if a File or Directory Exists
- Check if directory contains files using python
- Rename all file names in your directory using Python
- Create a directory in Python
- Delete a directory or file using Python
- Copy a directory recursively using Python (with examples)
- Get directory of current Python script
- Get parent of current directory using Python
- Delete an entire directory tree using Python | shutil.rmtree() method
- Change current working directory with Python
- Directory traversal tools in Python
- Finding the largest file in a directory using Python
- Implementing a Contacts directory in Python
- Python Directory Management
- PYGLET – Getting Directory for Saving User Paths
- PYGLET – Getting Program Entry Directory
- String slicing in Python to check if a string can become empty by recursive deletion
- Python - Check if a list is empty or not
- Python program to check if string is empty or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

