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)
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
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
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.