文本对比的工具有很多常用的有Beyond Compare、Notepad++插件比对等,这些工具比对已经很成熟而且很好用,但是我需要比对几十个文件甚至几百个,如果一个一个去比较差不多要浪费我一整天的时间。
结果一番折腾,总算找到了比较方便,展示结果也比较清晰的批量文本对比工具,就是在Beyond Compare的基础上用脚本直接调用,下面展示一下我的成果。
批量比对,需要建立两个比对的文件目录,结构要一致,也可以根据自己需要去修改
生成的结果报告为html文件,可以直接在浏览器中查看
最后附上Python编写的文本比对脚本
# encoding:utf-8
import subprocess
from os import path,walk
def get_txt(input_txt_dir):
'''获取需要对比的txt文件'''
txt_list = []
for root,dirs,files in walk(input_txt_dir):
for file in files:
if file.split('.')[-1] == 'txt':
txt_list.append(path.join(root,file))
return txt_list
def file_compare(input_txt_dir_left,input_txt_dir_right):
'''文件比对输出html报告'''
BeyondCompare_path = r"BeyondCompare.exe"
hide_gui = '/silent'
base_script = r'@{}'.format(path.abspath('base_script.txt'))
file_1 = get_txt(input_txt_dir_left)
file_2 = get_txt(input_txt_dir_right)
count = 1
for txt_left,txt_right in zip(file_1,file_2):
html_report_path = path.abspath(f"output_html\{count}.html")
print('文件比对:',txt_left,txt_right)
cmd = f'{BeyondCompare_path} {hide_gui} {base_script} {txt_left} {txt_right} {html_report_path}'
run = subprocess.Popen(cmd,stdout=subprocess.PIPE,shell=True)
run.wait()
count += 1
if __name__ == '__main__':
input_txt_dir_left = r"C:\Users\linxiaoqiang\Desktop\1"
input_txt_dir_right = r"C:\Users\linxiaoqiang\Desktop\2"
file_compare(input_txt_dir_left,input_txt_dir_right)
编码不易,请多多关注,持续分享更多知识!