Python os.removedirs() 方法

Python 教程 · 2019-02-04 10:13:41

概述

os.removedirs() 方法用于递归删除目录。像rmdir(), 如果子文件夹成功删除, removedirs()才尝试它们的父文件夹,直到抛出一个error(它基本上被忽略,因为它一般意味着你文件夹不为空)。

语法

removedirs()方法语法格式如下:

os.removedirs(path)

参数

  • path -- 要移除的目录路径

返回值

该方法没有返回值

实例

以下实例演示了 removedirs() 方法的使用:

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

import os, sys

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

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

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

执行以上程序输出结果为:

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

点击查看所有 Python 教程 文章: https://www.codercto.com/courses/l/8.html

查看所有标签

Zero to One

Zero to One

Peter Thiel、Blake Masters / Crown Business / 2014-9-16 / USD 27.00

“This book delivers completely new and refreshing ideas on how to create value in the world.” - Mark Zuckerberg, CEO of Facebook “Peter Thiel has built multiple breakthrough companies, and ......一起来看看 《Zero to One》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

html转js在线工具
html转js在线工具

html转js在线工具