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

