Python os.removedirs() Method

Python os.removedirs() method is used to remove directories recursively. It removes leaf directories progressively up the path.

This method attempts to successively remove each directory in the given path from the leaf (the last directory in the path) up through the tree until an ancestor directory is either non-empty or encounters an error (such as permission issues).

If it encounters a directory that is not empty, it stops and does not attempt to remove any further directories up the path.

If the removal of any directory fails (due to reasons like insufficient permissions or being non-empty), an exception is raised, and the removal process stops.

Syntax

os.removedirs(path)

Parameters

Argument Description
path This is the path of the directory to be removed. It may include parent directories.

Example 1: Removing a path of nested directories

Suppose you have a nested directory structure like dir1/dir2/dir3.

If dir3 is empty, os.removedirs(‘dir1/dir2/dir3’) will remove dir3.

Screenshot of empty directories

import os

# Assuming dir1/dir2/dir3 exists
try:
  os.removedirs('dir1/dir2/dir3')
  print("Directories removed successfully")
except OSError as e:
  print("Error occurred:", e)

Output

Directories removed successfully

Output of os.removedirs() function

Example 2: Error occurred: [Errno 20] Not a directory

Screenshot of a file
Let’s try to remove a file and not a directory and check the output.
import os

# Assuming dir1/dir2/app.txt is a directory which is not but still
# try to remove it
try:
  os.removedirs('dir1/dir2/app.txt')
  print("Directories removed successfully")
except OSError as e:
  print("Error occurred:", e)

Output

Error occurred: [Errno 20] Not a directory: 'dir1/dir2/app.txt'

Method 3: Handling different type of errors

# Python program to demonstrate os.removedirs() method

# Importing os module
import os

# Directory path to remove
# Note: Make sure this is a path to a directory, and it should be empty
path = 'dir1/dir2/app.txt'

# Attempt to remove the specified path
try:
  os.removedirs(path)
  print("Directory removed successfully")

# Catch the error if the specified path is not a directory
except NotADirectoryError:
  print("Specified path is not a directory.")

# Catch permission errors
except PermissionError:
  print("Permission denied.")

# Catch other OS-related errors
except OSError as error:
  print(error)
  print("Directory cannot be removed")

Output

Specified path is not a directory.

NotADirectoryError: This error will be raised if the specified path is not a directory, ensuring that the script targets directories only.

PermissionError: If the script lacks the necessary permissions to remove the directory, this error will be raised.

OSError: This is a general catch-all for other types of errors, such as the directory being non-empty or other file system issues.

As always with file and directory operations, use caution to avoid unintended deletion of important data. Ensure that the directory you’re targeting is meant to be deleted.

Leave a Comment

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