Python os.removedirs() Method

Python File(文件) 方法Python OS File/Directory Methods


Overview

The os.removedirs() method is used to recursively delete directories. Like rmdir(), if subfolders are successfully deleted, removedirs() only attempts their parent folders until an error is raised (which is basically ignored, because it generally means your folder is not empty).

Syntax

removedirs()The method syntax is as follows:

os.removedirs(path)

Parameters

  • path-- The directory path to be removed

Return Value

This method does not return a value.

Example

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

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

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

# 移除
os.removedirs("/test")

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

The output of the above program is:

目录为:
[  'a1.txt','resume.doc','a3.py','test' ]
移除后目录为:
[  'a1.txt','resume.doc','a3.py' ]

Python File(文件) 方法Python OS File/Directory Methods

Other Extensions