1import os 2import time 3# import shutil 4local_path = 'D:\Local\ATLog' 5wait_count = 0 6total_count = 0 7fail_count = 0 8log_path = 'D:/MobileUpgrade/fail_upgrade_list.log' 9 10if os.path.exists(log_path): 11 print("删除以往日志") 12 os.remove(log_path) 13 14 15with open(log_path,'a+') as fw: 16 fw.write('开始遍历日志'.center(100,'*')) 17 fw.write('\n') 18for dir_path, dir_names, file_names in os.walk(local_path): 19 if 'upgrade.log' in file_names: 20 total_count += 1 21 # print(dir_path, dir_names, file_names) 22 try: 23 items = dir_path.split(os.sep) 24 block_name, task_name = (items[-1],items[-2]) 25 except Exception as e: 26 print(dir_path) 27 print(str(e)) 28 with open(os.path.join(dir_path, 'upgrade.log'),'r') as fr: 29 if 'upgrade failed' in fr.read(): 30 fail_count += 1 31 with open(log_path,'a+') as fw: 32 print(f'发现失败任务:{task_name}') 33 fw.write(f'失败任务路径:{dir_path}\n') 34 wait_count += 1 35 if wait_count == 5: 36 wait_count = 0 37 # time.sleep(30) 38 39with open(log_path,'a+') as fw: 40 fw.write('失败日志遍历结束'.center(100,'*')) 41 fw.write('\n') 42 fw.write(f'总共检索日志:{total_count}个\n') 43 fw.write(f'其中存在失败的日志:{fail_count}个\n')