1 # '''
2 # 1、写一个清理日志程序
3 # 1、使用造日志的脚本造出来数据
4 # 2、删除logs目录下,三天前的日志文件或者为空的日志文件,如果当天的日志文件为空,不删除
5 # '''
6
7 from day5.time模块 import str_to_timestamp,timestamp_to_str
8 import os
9
10 def clean_log(path,day=3):
11 step = 60 * 60 * 24 * day
12 if os.path.isdir(path): #目录是否存在,或者传入的路径是不是一个文件夹
13 for cur_path,dirs,files in os.walk(path):
14 os.chdir(cur_path)
15 for file in files:
16 # file = os.path.join(cur_path,file)
17 if file.endswith(".log"):
18 file_date = file.split('_')[-1].split('.')[0]
19 file_date_timestamp = str_to_timestamp(file_date,"%Y-%m-%d")
20 if str_to_timestamp() - file_date_timestamp > step:
21 os.remove(file)
22 elif os.path.getsize(file) == 0 and file_date!=timestamp_to_str(format="%Y-%m-%d"):
23 os.remove(file)
24 print("清理完成!")
25 else:
26 print("请传入一个正确路径!")
27
28 #apache 2021-05-06 log
29 if __name__ == '__main__':
30
31 clean_log("logs")