Python刪除windows垃圾文件的方法
更新時(shí)間:2015年07月14日 09:37:32 作者:G0561
這篇文章主要介紹了Python刪除windows垃圾文件的方法,涉及Python針對(duì)系統(tǒng)垃圾文件的查找與清理技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了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')
#遞歸刪除文件
#里面和下面的函數(shù)用try是拋出刪除正在使用的零時(shí)文件出錯(cuò)
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'))
#刪除文件家的時(shí)候必須為空文件夾,而且只能從最里層刪起
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 """
系統(tǒng)產(chǎn)生的零時(shí)垃圾文件清理完畢!
"""
raw_input("請(qǐng)按回車鍵退出!")
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
相關(guān)文章
Python中urllib+urllib2+cookielib模塊編寫爬蟲實(shí)戰(zhàn)
這篇文章主要介紹了Python的urllib+urllib2+cookielib模塊編寫爬蟲實(shí)戰(zhàn),文中給出了抓取豆瓣同城和登陸圖書館查詢圖書歸還的爬取例子,需要的朋友可以參考下2016-01-01
Python requests.post()方法中data和json參數(shù)的使用方法
這篇文章主要介紹了Python requests.post()方法中data和json參數(shù)的使用方法,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-08-08
舉例講解Python設(shè)計(jì)模式編程中的訪問者與觀察者模式
這篇文章主要介紹了Python設(shè)計(jì)模式編程中的訪問者與觀察者模式,設(shè)計(jì)模式的制定有利于團(tuán)隊(duì)協(xié)作編程代碼的協(xié)調(diào),需要的朋友可以參考下2016-01-01
對(duì)python遍歷文件夾中的所有jpg文件的實(shí)例詳解
今天小編就為大家分享一篇對(duì)python遍歷文件夾中的所有jpg文件的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Python3使用Selenium獲取session和token方法詳解
這篇文章主要介紹了Python3使用Selenium獲取session和token方法詳解,需要的朋友可以參考下2021-02-02

