Python os.removedirs () method
Python OS file / directory methods
Outline
os.removedirs () method is used to recursively delete directories. Like rmdir (), if the sub-folder successfully removed, removedirs () to try their parent folders until you throw an error (which largely ignored, because it generally means that your folder is not empty).
grammar
removedirs () method syntax is as follows:
os.removedirs(path)
parameter
path - path to the directory to be removed
return value
This method does not return value
Examples
The following example demonstrates removedirs () method of use:
#!/usr/bin/python # -*- coding: UTF-8 -*- import os, sys # 列出目录 print "目录为: %s" %os.listdir(os.getcwd()) # 移除 os.removedirs("/test") # 列出移除后的目录 print "移除后目录为:" %os.listdir(os.getcwd())
The above program output is:
目录为: [ 'a1.txt','resume.doc','a3.py','test' ] 移除后目录为: [ 'a1.txt','resume.doc','a3.py' ]