#!/usr/bin/python
#-*-coding:utf-8 -*-
import sys
import os
import time
import datetime
import shutil
reload(sys)
sys.setdefaultencoding('utf8')
db_dir = "/home/udns/db/"
log_dir = "/home/udns/log/log_processed/"
tmp_dir = "/home/udns/log_tmp/"
now_date = datetime.datetime.now()
db_date = (now_date+datetime.timedelta(days=-110)).strftime('%Y%m%d')
log_date = (now_date+datetime.timedelta(days=-4)).strftime('%Y%m%d')
Time = now_date.strftime('%Y-%m-%d %H:%M:%S')
db_path = db_dir+db_date
log_path = log_dir+log_date
tmp_path = tmp_dir+log_date
log=open("/home/udns/python/log/clen.log",'a')
class clean:
def __init__(self, file_url):
self.file_url = file_url
def delfile(self):
if os.path.exists(self.file_url):
print("开始清理目录%s\n.... " % self.file_url)
try:
shutil.rmtree(self.file_url)
a=u"%s 已删除目录 : %s" %(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),self.file_url)
print(u"%s 已删除目录 : %s" %(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),self.file_url))
print >> log,a
except Exception as e:
print(e)
else:
print >> log,u"目录 %s 不存在" % self.file_url
f1 = clean(db_path)
f1.delfile()
f2 = clean(log_path)
f2.delfile()
f3 = clean(tmp_path)
f3.delfile()
log.close()
使用python的shutil模块删除指定目录下,过期的文件和目录。
最新推荐文章于 2024-04-21 23:14:09 发布