# coding=gbk
from xml.etree import ElementTree as ET
import os
def contains(text, targets):
result = False
for target in targets:
# 删除改造形式的空格,也可以根据需要进行改造删除
target = target.replace(" ", "")
if text.__contains__(target):
result = True
return result
def find(filepath):
total = 0
res = []
# 添加改造形式,改造包含一个就可以视为改造完成
targets = ["'【'+CallID+'】", "'【'+callID+'】", "'【'+CALLID+'】", "'【'+callId+'】"]
tree = ET.parse(filepath)
root = tree.getroot()
childs = root.getchildren()
for child in childs:
chas = child.getchildren()
for cha in chas:
if cha.tag == "Node":
if cha.attrib["FormName"].__contains__("Log-"):
FormName = cha.attrib["FormName"]
chs = cha.getchildren()
for ch in chs:
if ch.tag == "Input":
cs = ch.getchildren()
for c in ch.getchildren():
ttt = c.tag
if c.tag == "Row":
total += 1
for cc in c.getchildren():
if cc.attrib["Name"] == "ParamValue":
txt = cc.attrib["Text"]
if not contains(txt, targets):
tem = FormName+" ;"+ txt
res.append(tem)
print(FormName)
return res, total
def show_files(base_path, all_files=[]):
"""
遍历当前目录所有hxml文件及文件夹
:param path:
:param all_files:
:return:
"""
file_list = os.listdir(base_path)
# 准备循环判断每个元素是否是文件夹还是文件,是文件的话,把名称传入list,是文件夹的话,递归
for file in file_list:
# 利用os.path.join()方法取得路径全名,并存入cur_path变量,否则每次只能遍历一层目录
cur_path = os.path.join(base_path, file)
# 判断是否是文件夹
if os.path.isdir(cur_path):
show_files(cur_path, all_files)
else:
if not file.endswith('.hxml'):
print("非hxml文件", file)
continue
else:
all_files.append(cur_path)
return all_files
if __name__ == '__main__':
# 流程文件目录
path = r'D:\software\WeChat\file-wx\WeChat Files\wxid_rlhirxykrwre22\FileStorage\File\2023-01\适老化1.0.8'
# 核查结果保存的文件的目录
resPath = r'./res'
if not os.path.exists(resPath): # 创建保存目录
os.makedirs(resPath)
files = []
show_files(path, files)
# 扫描到的文件数量
num = 0
# 存在未修改的文件数量
num1 = 0
# 未修改行数统计
nums = 0
# 总行数
totals = 0
finishFile = open(os.path.join(resPath, "finish.txt"), 'w')
for file in files:
if os.path.isfile(file) and file.endswith("hxml"):
print("正在检测文件名:", file)
fn = os.path.basename(file) # 文件名字
filePath1 = os.path.dirname(file)
curPath = str(filePath1.replace(path, resPath))
if not os.path.exists(curPath):
os.makedirs(curPath)
res, total = find(file)
totals += total
num += 1
nums += len(res)
if len(res) > 0:
num1 += 1
bsn = os.path.join(curPath, "{}.txt".format(fn.split(".hxml")[0])) # 将要保存的路径
ff = open(bsn, "w")
ff.write(file+"\n")
for re in res:
ff.write(str(re)+"\n")
ff.close()
else:
print(file, "已改造完成")
finishFile.write(file+"\n")
else:
print("当前目录为文件夹或文件后缀非hxml----", file)
print("共检测到文件数量为:", len(files))
finishFile.write("共检测到文件数量为:{}\n".format(len(files)))
print("共扫描文件数量为:", num)
finishFile.write("共扫描文件数量为:{}\n".format(num))
print(path, "===>路径下共有log行数:", totals)
finishFile.write("{}===>路径下共有log行数:{}\n".format(path, totals))
print("未修改行数为:", nums)
finishFile.write("未修改行数为:{}\n".format(nums))
print("未修改率:", nums/totals)
finishFile.write("未修改率:{}\n".format(nums/totals))
print("修改率:", (totals-nums) / totals)
finishFile.write("修改率:{}\n".format((totals-nums) / totals))
print("存在未修改的文件数量为:", num1)
finishFile.write("存在未修改的文件数量为:{}\n".format(num1))
print("文件已保存至:", os.path.abspath(resPath))
finishFile.close()
python代码
于 2023-01-06 23:22:23 首次发布