import os
import sys
import time
#coding=utf-8
import xml.dom.minidom
##########################################################################
#读取配置文件
def readconfigfiles(filename,NeedDelFolderPath,NeedDelFolderName,NOTNeedDelFileName,datatime,logtime):
dom = xml.dom.minidom.parse(filename)
root = dom.documentElement
#获取需要删除文件夹路径
try:
itemlistFolderPath = root.getElementsByTagName('NeedDelFolderPath')
for i in range(0,len(itemlistFolderPath)):
itemfloder = itemlistFolderPath[i]
NeedDelFolderPath.append(itemfloder.getAttribute("path"))
except WindowsError:
pass
#获取所有需要删除文件的文件夹
try:
itemlistFolderName = root.getElementsByTagName('NeedDelFolderName')
for i in range(0,len(itemlistFolderName)):
itemname = itemlistFolderName[i]
NeedDelFolderName.append(itemname.getAttribute("name"))
except WindowsError:
pass
#获取不需删除数据文件的文件名包含内容
try:
itemlistFileName = root.getElementsByTagName('NotNeedDelFileName')
for i in range(0,len(itemlistFileName)):
itemname = itemlistFileName[i]
NOTNeedDelFileName.append(itemname.getAttribute("filename"))
except WindowsError:
pass
#获取所有需要数据文件的日期前
try:
itemdatalist = root.getElementsByTagName('Datatime')
for i in range(0,len(itemdatalist)):
itemdata = itemdatalist[i]
datatime.append(itemdata.getAttribute("datatime"))
except WindowsError:
pass
#获取所有需要数据文件的日期前
try:
itemloglist = root.getElementsByTagName('Logtime')
for i in range(0,len(itemdatalist)):
itemlog = itemloglist[i]
logtime.append(itemlog.getAttribute("logtime"))
except WindowsError:
pass
#########################################################################
#删除文件操作
def deletefiles(path,dataflodername,datatimes,logtimes,NOTNeedDelFileNameStr):
#获取文件目录
l=[]
for p,d,f in os.walk(path):
if dataflodername in p: #判断是否是包含特定文件夹
for f1 in f:
l.append(os.path.join(p,f1)) #获取所有文件,把目录和文件名合成一个路径
print l
name=[]
for item in l:
if os.path.isdir(path):
name=os.path.basename(item) #获取文件名称#
# print name
statinfo=os.stat(item) #获取文件信息
t = statinfo.st_mtime #获取文件的修改时间
# print t
#判断文件名称包含特定内容并且时间是X天前的文件,删除数据文件
times = time.time()
# print times
if times - datatimes >t and NOTNeedDelFileNameStr not in name and ".mdb" not in name:
try:
# print "DATA>>>>>>>>>>>>"
# print item
os.remove( item )
except WindowsError:
pass
# 30天秒数:2592000s
#判断文件名称包含特定内容并且时间是X天前的文件,删除日志文件
if times - logtimes >t and ".log" in name:
try:
# print "LOG>>>>>>>>>>>>>"
# print item
os.remove( item )
except WindowsError:
pass
#############################################################################
if __name__ == "__main__":
filename="D:/tool/DelFilesTool/delfilesconfig.xml"
NeedDelFolderName=[]
NeedDelFolderPath =[]
NOTNeedDelFileName=[]
datatime = []
logtime = []
readconfigfiles(filename,NeedDelFolderPath,NeedDelFolderName,NOTNeedDelFileName,datatime,logtime)
datatimes = float(datatime[0])
logtimes = float(logtime[0])
NOTNeedDelFileNameStr = NOTNeedDelFileName[0]
for j in range(0,len(NeedDelFolderPath)):
path = NeedDelFolderPath[j]
for i in range(0,len(NeedDelFolderName)):
dataflodername = NeedDelFolderName[i]
deletefiles(path,dataflodername,datatimes,logtimes,NOTNeedDelFileNameStr)
#####################################################config####################