removedirs()方法递归删除多个目录。如果子目录删除成功,removedirs 试图删除每个父目录路径。如果子目录不能成功删除则会引发 OSError 异常。
语法
以下是 removedirs() 方法的语法:
os.removedirs(path)
参数
-
path -- 这是一个目录,是需要删除的路径。
返回值
此方法不返回任何值。
示例
下面的例子显示 removedirs()方法的使用。
# !/usr/bin/python3 import os, sys os.chdir("d:\\tmp") # listing directories print ("The dir is: %s" %os.listdir(os.getcwd())) # removing os.removedirs("home\\monthly\\daily") # listing directories after removing directory print ("The dir after removal is:" %os.listdir(os.getcwd()))
当我们运行上面的程序,它会产生以下结果:
The dir is: ['Applicationdocs.docx', 'book.zip', 'foo.txt', 'home', 'Java Multiple Inheritance.html', 'Java Multiple Inheritance_files', 'java.ppt', 'ParallelPortViewer'] The dir after removal is: ['Applicationdocs.docx', 'book.zip', 'foo.txt', 'Java Multiple Inheritance.html', 'Java Multiple Inheritance_files', 'java.ppt', 'ParallelPortViewer']
上一篇:
Python3文件方法
下一篇:
Python3 os文件目录的方法