difflib模块对比apache配置文件差异

我们可以同下面的代码对所有的配置文件进行差异的html输出.

[root@www difflib]# cat diff_conf.py

!/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)
text2_lines = readfile(textfile2)

d = difflib.HtmlDiff()
print d.make_file(text1_lines, text2_lines)
[root@www difflib]#

测试:

[root@www difflib]# python diff_conf.py /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.confv2 >diff_con.html

然后你打开网页看一下,ok!

下面是我51cto的博客 上面有图片效果.
http://9399369.blog.51cto.com/9389369/1735149

推荐阅读更多精彩内容