import os
import glob# 查找最新的文件夹path = r’D:\ca’list_of_files = glob.glob(path + ‘/’)latest_folder = max(list_of_files, key=os.path.getctime)# 查找xml文件并读取内容xml_file = glob.glob(latest_folder + '/.xml’)[0] # 只取第一个XML文件with open(xml_file, ‘r’) as f: xml_text = f.read() # 统计success和fall出现的次数success_count = xml_text.count(‘success’)fall_count = xml_text.count(‘fall’)# 写入结果到文件中output_path = r’D:\sd\output.txt’with open(output_path, ‘w’) as f: f.write(f’success了{success_count}个,fall了{fall_count}个’)
可以查找D:\ca目录下最新生成的文件夹中唯一的xml文件中包含"success"和"fall"关键字,并分别记录他们出现的次数,并将结果输出到D:\sd\output.txt文档中:
python
最新推荐文章于 2024-08-24 11:00:49 发布