Python difflib对比配置文件差异脚本

!/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: simple3.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) #调用readfile函数,获取分隔后的字符串
text2_lines = readfile(textfile2)

d = difflib.HtmlDiff() #创建HtmlDiff()类对象
print d.make_file(text1_lines, text2_lines) #通过make_file方法输出HTML格式的比对结果
运行如下代码:

python simple3.py nginx.conf.v1 nginx.conf.v2 > diff.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值