Python os.removedirs() Method



Description

Python method removedirs() removes dirs recursively. If the leaf directory is succesfully removed, removedirs tries to successively remove every parent directory displayed in path.

Syntax

Following is the syntax for removedirs() method −

os.removedirs(path)

Parameters

  • path − This is the path of the directory, which needs to be removed.

Return Value

This method does not return any value.

Example

The following example shows the usage of removedirs() method.

# !/usr/bin/python

import os, sys

# listing directories
print "The dir is: %s" %os.listdir(os.getcwd())

# removing
os.removedirs("/tutorialsdir")

# listing directories after removing directory
print "The dir after removal is:" %os.listdir(os.getcwd())

When we run above program, it produces following result −

The dir is:
[  'a1.txt','resume.doc','a3.py','tutorialsdir','amrood.admin' ]
The dir after removal is:
[  'a1.txt','resume.doc','a3.py','amrood.admin' ]
python_files_io.htm
Advertisements