Sunday, November 17, 2024
Google search engine
HomeLanguagesPython: Get List of all empty Directories

Python: Get List of all empty Directories

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 –

Python-list-of-empty-directories

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.




# 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.




# 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']
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments