标签: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 = r‘C:\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