Python : How to Get List of all empty Directories ?

In this article we will discuss how to get list of all empty directories.

Python’s os module provides a function to iterate over a directory tree i.e.

os.walk(path)

It iterates of the directory tree at give path and for each directory or sub directory it returns a tuple containing,
(<Dir Name> , <List of Sub Dirs> , <List of Files>.

Now let’s use this to create a list of empty directories in a directory tree.

Creating a list of all empty Directories in a directory tree

  • Create a list to Store empty directories
  • Now Traverse all the files in given path using os.walk() :
    • For each Directory, check if it has no files and sub directories. If yes then add its path in the list.

dirName = '/home/varun/temp';

'''
    Get a list of empty directories in a directory tree
'''

# Create a List    
listOfEmptyDirs = list()

# Iterate over the directory tree and check if directory is empty.
for (dirpath, dirnames, filenames) in os.walk(dirName):
    if len(dirnames) == 0 and len(filenames) == 0 :
        listOfEmptyDirs.append(dirpath)

Advertisements

Creating a list of all empty Directories in a directory tree using List Comprehension

More pythonic approach is a single line solution using List Comprehension,

listOfEmptyDirs = [dirpath for (dirpath, dirnames, filenames) in os.walk(dirName) if len(dirnames) == 0 and len(filenames) == 0]

Complete example is as follows,
import os

def main():
    
    dirName = '/home/varun/temp';
    
    '''
        Get a list of empty directories in a directory tree
    '''
    
    # Create a List    
    listOfEmptyDirs = list()
    
    # Iterate over the directory tree and check if directory is empty.
    for (dirpath, dirnames, filenames) in os.walk(dirName):
        if len(dirnames) == 0 and len(filenames) == 0 :
            listOfEmptyDirs.append(dirpath)

    
    # Iterate over the empty directories and print it
    for elem in listOfEmptyDirs:
        print(elem)    
        
    print ("****************")


    listOfEmptyDirs = [dirpath for (dirpath, dirnames, filenames) in os.walk(dirName) if len(dirnames) == 0 and len(filenames) == 0]
    
    
    for elem in listOfEmptyDirs:
        print(elem)  
        
        
        
if __name__ == '__main__':
    main()


Output:
/home/varun/temp/temp1
/home/varun/temp/temp2
/home/varun/temp/temp3
****************
/home/varun/temp/temp1
/home/varun/temp/temp2
/home/varun/temp/temp3

 

 

 

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top