python脚本多个文件夹下多个文件中sql语句片段比对

一、需求:

日常测试授权脚本,需要检查多个行业文件夹下单独的授权脚本和汇总授权脚本,一个文件根目录下有多个子文件夹,子文件夹下有多个sql文件,人为比对较为耗时。

需要实现在文件中找到某个sql语句片段,然后遍历比对此sql语句片段在各目录各文件中是否一致。

二、思路:

该脚本将遍历根目录下的所有子文件夹中的 SQL 文件,检查在根目录下的多个文件中是否都存在匹配的 SQL 语句,并在找到匹配的文件时输出相应路径信息。

三、python代码

import os

#遍历各目录各文件,寻找有此sql语句的文件,并打印出对应文件的路径
def find_sql(root_dir, sql_fragment):
    for dirpath, dirnames, filenames in os.walk(root_dir):
        for filename in filenames:
            if filename.endswith(".sql"):
                filepath = os.path.join(dirpath, filename)
                try:
                    with open(filepath, 'r', encoding='utf-8') as file:
                        sql_content = file.read()
                        if sql_fragment in sql_content:
                            print("在文件 {} 中找到了指定的 SQL 语句片段:{}".format(os.path.abspath(filepath), sql_fragment))
                            compare_sql_in_files(root_dir, sql_fragment)
                            return
                except UnicodeDecodeError:
                    try:
                        with open(filepath, 'r', encoding='utf-8-sig') as file:
                            sql_content = file.read()
                            if sql_fragment in sql_content:
                                print("在文件 {} 中找到了指定的 SQL 语句片段:{}".format(os.path.abspath(filepath), sql_fragment))
                                compare_sql_in_files(root_dir, sql_fragment)
                                return
                    except UnicodeDecodeError:
                        with open(filepath, 'r', encoding='gbk') as file:
                            sql_content = file.read()
                            if sql_fragment in sql_content:
                                print("在文件 {} 中找到了指定的 SQL 语句片段:{}".format(os.path.abspath(filepath), sql_fragment))
                                compare_sql_in_files(root_dir, sql_fragment)
                                return
    print("未找到指定的 SQL 语句片段:{}".format(sql_fragment))
#包含此sql语句的文件进行内容比对
def compare_sql_in_files(root_dir, sql_fragment):
    for dirpath, dirnames, filenames in os.walk(root_dir):
        for filename in filenames:
            if filename.endswith(".sql"):
                filepath = os.path.join(dirpath, filename)
                try:
                    with open(filepath, 'r', encoding='utf-8') as file:
                        sql_content = file.read()
                        if sql_fragment in sql_content:
                            print("在文件 {} 中找到了与指定的 SQL 语句片段一致的内容".format(os.path.abspath(filepath)))
                except UnicodeDecodeError:
                    try:
                        with open(filepath, 'r', encoding='utf-8-sig') as file:
                            sql_content = file.read()
                            if sql_fragment in sql_content:
                                print("在文件 {} 中找到了与指定的 SQL 语句片段一致的内容".format(os.path.abspath(filepath)))
                    except UnicodeDecodeError:
                        with open(filepath, 'r', encoding='gbk') as file:
                            sql_content = file.read()
                            if sql_fragment in sql_content:
                                print("在文件 {} 中找到了与指定的 SQL 语句片段一致的内容".format(os.path.abspath(filepath)))

# 在此处指定根目录和要查找的 SQL 语句片段
root_dir = "D:/hswork/PB2.0/环境搭建_任务/svn脚本/202301.03版本/升级脚本-2023-11-21/升级脚本/01开通脚本/"    #授权文件根目录
sql_fragment = "update pub_tsysparameter set visible_flag='1'  where sysparam_no = 400264;"   #要查找的 SQL 语句片段

# 调用函数开始查找和比对
find_sql(root_dir, sql_fragment)

四、使用

将 根目录 替换为实际的根目录路径,并将 要查找的 SQL 语句片段 替换为实际要查找的 SQL 语句片段。

五、实现效果

  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值