python代码

该Python脚本用于查找指定目录下所有.hxml文件中的特定字符串(如`[【CallID】,`【callID】`,...]`),统计含有这些字符串的行数,并将结果保存到新文件中。它还遍历子目录,检查文件是否已进行过改造,生成完成情况报告。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# 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()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徒手写bug326

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值