码迷,mamicode.com
首页 > 其他好文 > 详细

清理日志

时间:2019-10-24 15:40:29      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:return   led   exists   date   mes   oca   format   time()   result   

写一个清理日志的程序,把三天前的日志和为空的日志都删掉:

 

 

import os,time,datetime

def timestamps_to_str(timestamp=None,format=%Y-%m-%d): #时间戳转为格式化时间
    if timestamp:
        time_tuple = time.localtime(timestamp)
        result = time.strftime(format,time_tuple)
    else:
        result = time.strftime(format)
    return result

def str_to_timestamps(string=None,format=%Y-%m-%d): #格式化时间转为时间戳
    if string:
        time_tuple = time.strptime(string,format)
        result = time.mktime(time_tuple)
    else:
        result = time.time()
    return result

path = rC:\Users\ght\PycharmProjects\untitled\homeword(day4)\logs
name = .log
def del_log(path):  #把三天前的日志和为空的日志都删掉
    if os.path.exists(path) and os.path.isdir(path):
        today = time.strftime(%Y-%m-%d)
        cur_timestamp = str_to_timestamps(today)
        date_timestamp = cur_timestamp-86400*3    #3天前时间戳
        date = timestamps_to_str(date_timestamp)  #3天前的日期
        for cur_dir,dirs,files in os.walk(path,name):
            for file in files:
                if name in file:
                    file_time = file.split(_)[1].split(.)[0]
                    os.chdir(cur_dir)
                    if file_time<date or os.path.getsize(file) == 0:
                        abs_path = os.path.join(cur_dir, file)
                        os.remove(abs_path)
del_log(path)

 

清理日志

标签:return   led   exists   date   mes   oca   format   time()   result   

原文地址:https://www.cnblogs.com/cathyg/p/11732078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!