Python删除windows垃圾文件的方法
2015-07-14 00:00
736 查看
本文实例讲述了Python删除windows垃圾文件的方法。分享给大家供大家参考。具体如下:
#coding:utf-8 import os #from glob import glob if os.name == 'nt': if 'HOMEPATH' in os.environ: home = os.environ['HOMEDRIVE'] + os.environ['HOMEPATH'] else: home = os.environ['HOMEPATH'] workpath = os.path.join(home,'Local Settings') #递归删除文件 #里面和下面的函数用try是抛出删除正在使用的零时文件出错 def delfile(path): for file in os.listdir(path): if os.path.isfile(os.path.join(path,file)): try: print "\n删除垃圾文件: %s" % (os.path.join(path,file)) os.remove(os.path.join(path,file)) except: pass elif os.path.isdir(os.path.join(path,file)): delfile(os.path.join(path,file)) else: pass delfile(os.path.join(workpath,'Temp')) delfile(os.path.join(workpath,'Temporary Internet Files')) #删除文件家的时候必须为空文件夹,而且只能从最里层删起 def deldir(pa): for i in os.listdir(pa): if os.path.isdir(os.path.join(pa,i)): if len(os.listdir(os.path.join(pa,i))) > 0: deldir(os.path.join(pa,i)) try: os.rmdir(os.path.join(pa,i)) except: pass else: try: print "\n删除文件夹 %s" % (os.path.join(pa,i)) os.rmdir(os.path.join(pa,i)) except: pass deldir(os.path.join(workpath,'Temp')) deldir(os.path.join(workpath,'Temporary Internet Files')) print """ 系统产生的零时垃圾文件清理完毕! """ raw_input("请按回车键退出!")
希望本文所述对大家的Python程序设计有所帮助。
相关文章推荐
- Python实现批量将word转html并将html内容发布至网站的方法
- Python删除空文件和空文件夹的方法
- Python实现查找系统盘中需要找的字符
- python实现爬虫下载美女图片
- python 安装MySQLdb模块
- python下module、package导入
- python3 urllib.request抓资源的方式
- 《python学习手册》(1)
- Python学习笔记(一)--注释
- Python学习笔记(一)-- print语句
- 每日一python
- Python学习笔记(一)--数据类型
- Python Security 入门
- python——动态类型
- python 正则表达式
- Python socket 基础
- Python开发入门与实战3-Django动态页面
- Python下opencv使用笔记(八)(图像金字塔)
- python基础教程总结3—字典
- Python读写文件