import os
import glob
import re
查找最新的文件夹
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()
匹配关键字并提取数字
match = re.search(r’Summary success: “(?P\d+)” fall: “(?P\d+)”’, xml_text)
if match:
success_count = int(match.group(‘success’))
fall_count = int(match.group(‘fall’))
写入结果到文件中
output_path = r’D:\sd\output.docx’
with open(output_path, ‘w’) as f:
f.write(f’success了{success_count}个,fall了{fall_count}个’)