Python: Get List of all empty Directories
Last Updated :
29 Dec, 2020
The
OS module in Python is used for interacting with the operating system. This module comes with Python's standard utility module so there is no need to install it externally. All functions in the OS module raise
OSError
in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type but are not accepted by the operating system.
os.walk()
method of this module can be used for listing out all the empty directories. This method basically generates the file names in the directory tree either top-down or bottom-up. For each directory in the tree rooted at directory top (including top itself), it
yields a 3-tuple (
dirpath
,
dirnames
,
filenames
).
- dirpath: A string that is the path to the directory
- dirnames: All the sub-directories from root.
- filenames: All the files from root and directories.
Syntax: os.walk(top, topdown=True, onerror=None, followlinks=False)
Parameters:
top: Starting directory for os.walk().
topdown: If this optional argument is True then the directories are scanned from top-down otherwise from bottom-up. This is True by default.
onerror: It is a function that handles errors that may occur.
followlinks: This visits directories pointed to by symlinks, if set to True.
Return Type: For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).
Example : Suppose the directories looked like this -

We want to print out all the empty directories. As this method returns the tuple of sub-directories and files, we will check the size of each tuple and if the size is zero then the directory will be empty. Below is the implementation.
Python3 1==
# Python program to list out
# all the empty directories
import os
# List to store all empty
# directories
empty = []
# Traversing through Test
for root, dirs, files in os.walk('Test'):
# Checking the size of tuple
if not len(dirs) and not len(files):
# Adding the empty directory to
# list
empty.append(root)
Print("Empty Directories:")
print(empty)
Output:
Empty Directories:
['Test\\A\\A2', 'Test\\B', 'Test\\D\\E']
The above code can be shortened using
List Comprehension which is a more Pythonic way. Below is the implementation.
Python3 1==
# Python program to list out
# all the empty directories
import os
# List comprehension to enter
# all empty directories to list
empty = [root for root, dirs, files, in os.walk('Test')
if not len(dirs) and not len(files)]
print("Empty Directories:")
print(empty)
Output:
Empty Directories:
['Test\\A\\A2', 'Test\\B', 'Test\\D\\E']
Similar Reads
Declare an Empty List in Python Declaring an empty list in Python creates a list with no elements, ready to store data dynamically. We can initialize it using [] or list() and later add elements as needed.Using Square Brackets []We can create an empty list in Python by just placing the sequence inside the square brackets[]. To dec
3 min read
Python - List Files in a Directory Sometimes, while working with files in Python, a problem arises with how to get all files in a directory. In this article, we will cover different methods of how to list all file names in a directory in Python.Table of ContentWhat is a Directory in Python?How to List Files in a Directory in PythonLi
8 min read
Listing out directories and files in Python The following is a list of some of the important methods/functions in Python with descriptions that you should know to understand this article. len() - It is used to count number of elements(items/characters) of iterables like list, tuple, string, dictionary etc. str() - It is used to transform data
6 min read
Python - Get list of files in directory with size In this article, we are going to see how to extract the list of files of the directory along with its size. For this, we will use the OS module. OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a
9 min read
Python - Get list of files in directory sorted by size In this article, we will be looking at the different approaches to get the list of the files in the given directory in the sorted order of size in the Python programming language. The two different approaches to get the list of files in a directory are sorted by size is as follows: Using os.listdir(
3 min read