python写的一个文本比较工具 difflib

import os,difflib,datetime
import re
def diff(file1,file2):
    if not os.path.exists(file1) or not os.path.exists(file2):
        print("{} 或者 {} 文件不存在".format(file1,file2))
        return -1,"文件不存在"
    try:
        content1=open(file1,'r',encoding='utf-8').readlines()
        content2=open(file2,'r',encoding='utf-8').readlines()
        d=difflib.HtmlDiff()
        result = d.make_file(content1,content2,context=True)
        regex = re.compile("(<table[\s\S]*</table>)[\s]*<table class=\"diff\" summary=\"Legends\">")
        table = regex.findall(result)[0]
        regex2 = re.compile("<style[\s\S]*</style>")
        style = regex2.findall(result)[0]
        regex3 = re.compile("<table class=\"diff\" summary=\"Legends\">[\S\s]*</table>")
        legend = regex3.findall(result)[0]
        if 'No Differences Found' in result:
            return 0,"相等"
        return 1,[result,table,style,legend]
    except Exception as e:
        print("{} 与 {} 比较异常 {}".format(file1, file2,e))
        return -1,"比较异常"
def dispose(datapath):
    files=os.listdir(datapath)
    style = ""
    legend = ""
    tables=[]
    is_first=True
    for lf in files:
        if not os.path.exists("right/"+lf):
            print("未发现right中存在同名文件:{}".format(lf))
            continue
        print("比较left/{} 与 right/{}".format(lf,lf))
        ret,result=diff('left/'+lf,'right/'+lf)
        if ret==0:
            print("{} 无变化".format(lf))
            continue
        elif ret==-1:
            print("{} 比较异常".format(lf))
            continue
        if is_first:
            style=result[2]
            legend=result[3]
            is_first=False
        print("保存比较 {}".format(lf+"_compare.html"))
        tables.append({"name":lf,"body":result[1]})
        save=open("report/"+lf+"_compare.html","w")
        save.writelines(result[0])
        save.close()
    if not tables:
        print("无新变化,无报告")
        return
    print("生成报告")
    html=style
    html+="<body><p><h1 style='text-align:center;background-color:#528BFC;padding:6px;border-bottom:1px solid #333;margin-bottom:8px;'><strong>对比报告</strong></h1></p>"
    for table in tables:
        html+="<p><h2 style='text-align:center;background-color:#D0E1FF;padding:6px;border-bottom:1px solid #333;'><strong>{}</strong></h2></p>".format(table["name"])
        html+=table["body"]
    html += legend
    html+="</body>"
    today = datetime.datetime.now().strftime("%Y-%m-%d")
    report=open("report/"+today+".html","w")
    report.writelines(html)
    report.close()
#进入主程序
if __name__=="__main__":
    print("开始分析目录 left - right")
    if not os.path.exists("left") or not os.path.exists("right"):
        print("不存在left文件夹与right文件夹,请自行创建")
        exit(0)
    dispose('left')

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值