本文实例讲述了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程序设计有所帮助。
SHOPEX简灰服装商城整站源码下载。 安装方法:1.解压上传程序至网站根目录.. 访问:域名/bak.(用户名:admin 密码:123456)2.进入帝国备份王后,配置数据库数据库信息.选择-www.taomoban.net目录.还原数据库.3.修改FTP目录下的config/config.phpphp 数据库连接信息.4.登陆网站后台--清空缓存..5.删除bak文件夹 后台:shopadm
0
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号