使用Python自带difflib模块进行文件内容差异对比

difflib_text.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/python
import difflib
import sys
 
try:
    textfile1=sys.argv[1]
    textfile2=sys.argv[2]
except Exception,e:
    print "Error:"+str(e)
    print "Usage: difflib_text.py filename1 filename2"
    sys.exit()
 
def readfile(filename):
    try:
    fileHandle = open (filename,'rb')
        text = fileHandle.read().splitlines()
    fileHandle.close()
        return text
    except IOError as error:
        print('Read file Error:'+str(error))
    sys.exit()
 
if textfile1=="" or textfile2=="":
    print "Usage: simple3.py filename1 filename2"
    sys.exit()
 
text1_lines = readfile(textfile1)
text2_lines = readfile(textfile2)
 
d = difflib.HtmlDiff()
print d.make_file(text1_lines,text2_lines)

测试:python difflib_text.py nginxdiff.conf nginxdiff2.conf

结果:

 

搜索

posted @   人艰不拆_zmc  阅读(554)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2017-03-24 centos7提示ifconfig command not found解决
点击右上角即可分享
微信分享提示