Python3 os.removedirs() Method

Python3 OS 文件/目录方法Python3 OS File/Directory Methods


Overview

The os.removedirs() method is used to recursively remove directories. Like rmdir(), if subfolders are successfully deleted, removedirs() only then attempts their parent folders until an error is thrown (it 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-- Directory path to be removed

Return Value

This method has no return value.

Example

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

#!/usr/bin/python3

import os, sys

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

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

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

The output of the above program is:

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

Python3 OS 文件/目录方法Python3 OS File/Directory Methods

Other Extensions