python递归删除指定目录及其所有内容的方法
作者:jingxian 时间:2022-12-12 02:42:51
实例如下:
#! /usr/bin/python
# -*- coding: utf-8 -*-
import os
def del_dir_tree(path):
''' 递归删除目录及其子目录,子文件'''
if os.path.isfile(path):
try:
os.remove(path)
except Exception, e:
#pass
print e
elif os.path.isdir(path):
for item in os.listdir(path):
itempath = os.path.join(path, item)
del_dir_tree(itempath)
try:
os.rmdir(path) # 删除空目录
except Exception, e:
#pass
print e
if __name__ == '__main__':
dirname = '/home/xx/log'
del_dir_tree(dirname)
标签:python,递归,删除,目录
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
关于点击区域
2009-07-24 13:08:00
![](https://img.aspxhome.com/file/UploadPic/20097/24/3734741624_d711acb9a5-35s.jpg)
Pandas直接读取sql脚本的方法
2022-08-16 20:16:34
![](https://img.aspxhome.com/file/2023/8/103478_0s.png)
EF Core基础入门教程
2023-07-24 09:01:51
![](https://img.aspxhome.com/file/2023/1/78081_0s.jpg)
使用Python自动化Microsoft Excel和Word的操作方法
2023-04-11 16:15:21
![](https://img.aspxhome.com/file/2023/3/75953_0s.png)
Pandas之Fillna填充缺失数据的方法
2022-11-17 20:17:12
Django项目使用ckeditor详解(不使用admin)
2022-12-15 01:52:52
![](https://img.aspxhome.com/file/2023/1/63591_0s.jpg)
Python实现数字的格式化输出
2021-10-11 18:11:27
Python生成8位随机字符串的方法分析
2023-06-15 20:53:08
Python标准库sched模块使用指南
2022-09-20 12:19:06
JavaScript中变量、指针和引用功能与操作示例
2024-04-17 10:07:16
Javascript图像处理—图像形态学(膨胀与腐蚀)
2024-04-23 09:27:58
![](https://img.aspxhome.com/file/2023/0/135520_0s.png)
mysql对binlog的处理说明
2024-01-14 18:34:49
Python制作简易版小工具之计算天数的实现思路
2023-10-29 08:12:07
![](https://img.aspxhome.com/file/2023/4/91874_0s.png)
Python selenium模块实现定位过程解析
2021-01-10 10:50:27
![](https://img.aspxhome.com/file/2023/8/102808_0s.png)
详解Django中间件执行顺序
2023-01-27 20:30:00
![](https://img.aspxhome.com/file/2023/0/85150_0s.png)
vue 虚拟DOM的原理
2023-07-02 17:03:18
![](https://img.aspxhome.com/file/2023/5/139905_0s.png)
python通配符之glob模块的使用详解
2021-07-16 23:13:18
手把手教你使用Python解决简单的zip文件解压密码
2021-01-20 10:08:41
![](https://img.aspxhome.com/file/2023/4/117894_0s.jpg)
js处理括弧配对替换的方法
2008-01-16 13:48:00
解决MySQL 5数据库连接超时问题
2009-03-25 15:24:00