磁盘垃圾文件清理器python代码实现
作者:董付国 时间:2023-08-24 23:20:41
本文假设某些特定类型的文件和大小为0的文件为垃圾文件,可以自由扩展代码的列表,也就是垃圾文件的类型。
from os.path import isdir, join, splitext
from os import remove, listdir, chmod, stat
import sys
#指定要删除的文件类型
filetypes = ['.tmp', '.log', '.obj', '.txt']
def delCertainFiles(directory):
for filename in listdir(directory):
temp = join(directory, filename)
if isdir(temp):
#递归调用
delCertainFiles(temp)
elif splitext(temp)[1] in filetypes or stat(temp).st_size==0:
#修改文件属性,获取访问权限
chmod(temp, 0o777)
#删除文件
remove(temp)
print(temp, ' deleted....')
if __name__ == '__main__':
paths = sys.argv[1:]
for path in paths:
if isdir(path):
delCertainFiles(path)
把上面的代码保存为t.py,然后打开命令提示符窗口,执行命令“Python t.py c:\test”,其中“c:\test”表示要清理的文件夹,如果有多个文件夹要清理的话,可以使用空格隔开。
来源:http://blog.csdn.net/dongfuguo/article/category/6798962
标签:python,文件清理器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Python中的自定义密码验证
2021-06-05 00:38:59
javscript 数组扁平化的实现
2024-04-28 10:18:49
spark编程python实例解读
2023-06-02 06:12:52
![](https://img.aspxhome.com/file/2023/4/115704_0s.png)
解决SQL SERVER数据库备份时出现“操作系统错误5(拒绝访问)。BACKUP DATABASE 正在异常终止。”错误的解决办法
2024-01-22 08:24:14
![](https://img.aspxhome.com/file/2023/5/89655_0s.jpg)
程序员的八种境界,你在哪一境?
2022-07-19 11:22:19
![](https://img.aspxhome.com/file/2023/8/112068_0s.jpg)
ASP名次排列函数
2008-07-20 13:42:00
selenium在scrapy中的使用代码
2021-11-24 09:34:16
![](https://img.aspxhome.com/file/2023/1/99141_0s.jpg)
精心整理总结的Python自动化测试面试题
2022-07-27 08:13:00
![](https://img.aspxhome.com/file/2023/4/91934_0s.png)
日文片假名导致 Access 搜索“内存溢出”
2009-07-07 22:23:00
![](https://img.aspxhome.com/file/UploadPic/20097/7/japancode-54s.gif)
JS实现div居中示例
2024-04-28 09:47:39
MySQL关联查询优化实现方法详解
2024-01-12 17:06:11
![](https://img.aspxhome.com/file/2023/0/109790_0s.png)
Python爬虫爬取杭州24时温度并展示操作示例
2022-01-04 14:43:33
![](https://img.aspxhome.com/file/2023/1/81741_0s.png)
Python获取当前时间日期的实现示例
2021-12-26 04:43:21
Python 中的Sympy详细使用
2021-10-03 03:22:45
![](https://img.aspxhome.com/file/2023/2/80782_0s.png)
ASP访问数量统计代码
2011-04-08 10:59:00
浅谈PHP的$_SERVER[SERVER_NAME]
2024-05-03 15:48:46
Java使用正则表达式验证用户名和密码的方法
2023-06-13 18:05:56
MySQL中truncate误操作后的数据恢复案例
2024-01-12 20:45:56
潜谈产品设计中的可用性和可访问性
2009-01-18 12:47:00
![](https://img.aspxhome.com/file/UploadPic/20091/18/prdocut_reg-60s.jpg)
golang 自旋锁的实现
2024-05-02 16:24:03