Easy Tutorial
❮ Python Os Statvfs Python String Lstrip ❯

Python3 os.removedirs() Method

Python3 OS File/Directory Methods


Overview

The os.removedirs() method is used to recursively remove directories. Similar to rmdir(), removedirs() attempts to remove the parent directories of the subfolders only if the subfolders are successfully deleted, until an error is thrown (which is essentially ignored, as it typically means the directory is not empty).

Syntax

The syntax for the removedirs() method is as follows:

os.removedirs(path)

Parameters

-

path -- The directory path to be removed.

Return Value

This method does not return any value.

Example

The following example demonstrates the use of the removedirs() method:

#!/usr/bin/python3

import os, sys

# List directories
print ("Directories are: %s" %os.listdir(os.getcwd()))

# Remove
os.removedirs("/test")

# List directories after removal
print ("Directories after removal are: %s" %os.listdir(os.getcwd()))

Executing the above program will output:

Directories are:
[  'a1.txt','resume.doc','a3.py','test' ]
Directories after removal are:
[  'a1.txt','resume.doc','a3.py' ]

Python3 OS File/Directory Methods

❮ Python Os Statvfs Python String Lstrip ❯