Came out of the 'one hundred test' besttest
Deletes the specified path under a fixed format, ending with .log, three days ago the file, or delete an empty log file.
Log File Format: XXXX_2019-01-01.log.
import os,datetime # Determine whether the file is empty, if you delete an empty DEF del_file (File): with open(file,encoding='utf-8') as f: if f.read(): pass else: f.close() os.remove(file) # Find xx end of the specified file path DEF search_file (path, name): for cur_dir, dirs, Files in os.walk (path): for File in Files: IF name in File: abs_path with = the os.path.join (cur_dir, File) Print ( ' !% s file path% s ' % (File, abs_path with)) file_date = file[-14:-4] file_datetime = datetime.datetime.strptime(file_date,'%Y-%m-%d') now = datetime.datetime.now() sub_day = (now - file_datetime).days if sub_day > 3: os.remove(abs_path) else: del_file(abs_path) search_file('\练习\logs',name = '.log')