清理日志
Posted cathyg
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了清理日志相关的知识,希望对你有一定的参考价值。
写一个清理日志的程序,把三天前的日志和为空的日志都删掉:
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:UsersghtPycharmProjectsuntitledhomeword(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)
以上是关于清理日志的主要内容,如果未能解决你的问题,请参考以下文章
我的Android进阶之旅NDK开发之在C++代码中使用Android Log打印日志,打印出C++的函数耗时以及代码片段耗时详情