Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python os.removedirs() method

Python os.removedirs() method

Python File methods Python OS file/directory methods


Overview

The os.removedirs() method recursively deletes directories. Like rmdir(), only after a subdirectory is successfully deleted does removedirs() try its parent directory, until an error is raised (which is basically ignored, since it usually means the folder is not empty).

Syntax

The **removedirs()** method syntax is as follows:



os.removedirs(path)

Parameters

Return Value

This method has no return value

Example

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



#!/usr/bin/python

# -*- coding: UTF-8 -*-



import os, sys



# List directory

print "目录为: %s" %os.listdir(os.getcwd())



# 移除

os.removedirs("/test")



# List the removed directories

print "移除后目录为 %s :" %os.listdir(os.getcwd())

The output of the above program is:



Directory is:

[  'a1.txt','resume.doc','a3.py','test' ]

The directory after removal is:

[  'a1.txt','resume.doc','a3.py' ]

Python File methods Python OS file/directory methods