Open In App

Python | os.removedirs() method

Last Updated : 29 May, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.

All functions in 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.removedirs() method in Python is used to remove directories recursively. If the leaf directory in the specified path is successfully removed, then os.removedirs() tries to successively remove every parent directory mentioned in path until an error is raised. The raised error is ignored because generally error is raised because directory to be deleted is not empty.
For example consider the following path:

'/home/User/Documents/foo/bar/baz'

In above path, os.removedirs() method will try to remove the leaf directory first i.e ‘baz’. If the leaf directory ‘baz’ is successfully removed then method will try to remove ‘/home/User/Documents/foo/bar’ then ‘/home/User/Documents/foo/’ then ‘/home/User/Documents’ until an error is raised. The directory to be deleted should be empty.

Syntax: os.removedirs(path)

Parameter:
path: A path-like object representing a file path. A path-like object is either a string or bytes object representing a path.

Return Type: This method does not return any value.

Code #1: Use of os.removedirs() method to remove an empty directory tree




# Python program to explain os.removedirs() method 
    
# importing os module 
import os
  
# Leaf Directory name
directory = "baz"
  
# Parent Directory
parent = "/home/User/Documents/foo/bar"
  
# Path
path = os.path.join(parent, directory)
  
# Remove the Directory
# "baz"
os.removedirs(path)
print("Directory '%s' has been removed successfully" %directory)
  
# All parent directory
# of 'baz' will be also
# removed if they are empty
  


Output:

Directory 'baz' has been removed successfully
Code #2: Possible errors while using os.removedirs() method




# Python program to explain os.removedirs() method 
    
# importing os module 
import os
  
  
# If the specified path 
# is not a directory
# then 'NotADirectoryError'
# exception will be raised
  
# If the specified path 
# is not an empty directory
# then an 'OSError'
# will be raised
  
# If there is any
# permission issue while
# removing the directory
# then the 'PermissionError'
# exception will be raised
  
  
# similarly if specified path
# is invalid an 'OSError'
# will be raised
  
# Path
path = '/home/User/Documents/ihritik/file.txt'
  
# Try to remove
# the specified path
os.removedirs(path) 


Output:

Traceback (most recent call last):
  File "removedirs.py", line 33, in 
    os.removedirs(path)
  File "/usr/lib/python3.6/os.py", line 238, in removedirs
    rmdir(name)
NotADirectoryError: [Errno 20] Not a directory: '/home/User/Documents/ihritik/file.txt'
Code #3: Handling errors while using os.removedirs() method




# Python program to explain os.removedirs() method 
    
# importing os module 
import os
  
# Path
path = '/home/User/Documents/ihritik/file.txt'
  
# Try to remove
# the specified path
  
try:
    os.removedirs(path)
    print("Director removed successfully")
  
# If path is not a directory
except NotADirectoryError:
    print("Specified path is not a directory.")
  
# If permission related errors
except PermissionError:
    print("Permission denied.")
  
# for other errors
except OSError as error:
    print(error)
    print("Directory can not be removed")


Output:

Specified path is not a directory.

Reference: https://docs.python.org/3/library/os.html



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads