首页 > Python资料 博客日记

Python os.removedirs()方法

2023-08-05 17:23:14Python资料围观236

Python资料网推荐Python os.removedirs()方法这篇文章给大家,欢迎收藏Python资料网享受知识的乐趣

Python的removedirs()方法递归删除目录。 如果子目录成功删除,则removeirs会尝试依次删除路径中显示的每个父目录。如果子目录无法成功删除,则引发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', 'test.java'
]
The dir after removal of path : [
   'Applicationdocs.docx', 'book.zip', 'foo.txt', 'home', 
   'Java Multiple Inheritance.html', 'Java Multiple Inheritance_files', 
   'java.ppt', 'ParallelPortViewer'
]

版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐