删除历史日志文件

import os,time

#格式化时间转时间戳
def strToTimestamp(time_str,format='%Y%m%d%H%M%S'):
t = time.strptime(time_str,format)#格式化时间转化为时间元组
res = time.mktime(t)#时间元组转化为时间戳
return res

#时间戳转格式化时间
def timestampToStr(time_stamp,format='%Y%m%d%H%M%S'):
cur_time=time.localtime(time_stamp)#时间戳转化为时间元组
res = time.strftime(format,cur_time)#元组转化为格式化时间
return res


def clean_log(path):
#判断路径是否存在是文件夹
if os.path.exists(path) and os.path.isdir(path):
today = time.strftime('%Y-%m-%d')#20170102
cur_stamp = strToTimestamp(today,'%Y-%m-%d')
yesterday_stamp=cur_stamp-60*60*24
before_yesterday_stamp=yesterday_stamp-60*60*24
time_list=[timestampToStr(yesterday_stamp,'%Y-%m-%d'),today,timestampToStr(before_yesterday_stamp,'%Y-%m-%d')]
for file in os.listdir(path):
file_name_sp=file.split('.')
if len(file_name_sp)>2:
file_date = file_name_sp[1]
if file_date not in time_list:
abs_path=os.path.join(path,file)
print('删除的文件是:%s'%abs_path)
os.remove(abs_path)
else:
print('没有删除的文件是:%s'%file)

else:
print('路径不存在/不是目录')

clean_log(r'logs')

转载于:https://www.cnblogs.com/luonanhenniu/p/9398377.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值