python 删除非空文件夹的实例
作者:xiaodongxiexie 时间:2022-11-07 01:25:23
一般删除文件时使用os库,然后利用os.remove(path)即可完成删除,如果删除空文件夹则可使用os.removedirs(path)即可,
但是如果需要删除整个文件夹,且文件夹非空时使用os.removedirs(path)就会报错了,此时可以使用shutil库,该库为python内置库,是一个对文件及文件夹高级操作的库,可以与os库互补完成一些操作,如文件夹的整体复制,移动文件夹,对文件重命名等。
import os
import shutil
os.remove(path) #删除文件
os.removedirs(path) #删除空文件夹
shutil.rmtree(path) #递归删除文件夹
来源:https://blog.csdn.net/xiaodongxiexie/article/details/77155864
标签:python,删除,非空,文件夹
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python字符串拼接.join()和拆分.split()详解
2021-11-12 04:09:17
用openCV和Python 实现图片对比,并标识出不同点的方式
2023-08-31 09:30:55
![](https://img.aspxhome.com/file/2023/2/89352_0s.jpg)
Pandas 筛选和删除目标值所在的行的实现
2021-11-16 03:11:31
![](https://img.aspxhome.com/file/2023/6/92486_0s.png)
MySQL 数据库的基础知识
2024-01-17 03:05:52
![](https://img.aspxhome.com/file/2023/1/99341_0s.png)
简化ADO数据库操作的控件(带分页功能)
2008-05-20 13:15:00
基于Python制作短信发送程序
2022-06-19 16:47:25
![](https://img.aspxhome.com/file/2023/4/69644_0s.gif)
turtle的基础使用之python turtle递归绘图
2023-04-25 23:36:08
![](https://img.aspxhome.com/file/2023/1/115601_0s.png)
在SQL Server中使用索引的技巧
2009-02-24 17:50:00
python监控网卡流量并使用graphite绘图的示例
2022-06-24 22:35:54
vue 二维码长按保存和复制内容操作
2024-04-27 16:04:39
![](https://img.aspxhome.com/file/2023/5/133015_0s.jpg)
python+opencv+selenium自动化登录邮箱并解决滑动验证的问题
2021-10-01 07:32:18
![](https://img.aspxhome.com/file/2023/4/76094_0s.jpg)
Python从数据库读取大量数据批量写入文件的方法
2024-01-27 14:48:10
pytorch tensor int型除法出现的问题
2021-05-18 13:18:17
![](https://img.aspxhome.com/file/2023/5/121155_0s.jpg)
python使用redis模块来跟redis实现交互
2023-05-06 04:13:40
![](https://img.aspxhome.com/file/2023/6/78476_0s.png)
React+TypeScript+webpack4多入口配置详解
2024-02-24 03:23:31
python里使用正则表达式的组嵌套实例详解
2021-05-09 18:11:34
pandas数据分组和聚合操作方法
2023-09-07 05:15:44
手把手教你使用Python解决简单的zip文件解压密码
2021-01-20 10:08:41
![](https://img.aspxhome.com/file/2023/4/117894_0s.jpg)
python用来获得图片exif信息的库实例分析
2022-07-16 12:29:38
django实现用户登陆功能详解
2021-03-04 20:19:06
![](https://img.aspxhome.com/file/2023/1/88891_0s.jpg)