python 配置文件对比_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!
總結
以上是生活随笔為你收集整理的python 配置文件对比_difflib模块对比apache配置文件差异的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浅拷贝和深拷贝的区别_深拷贝和浅拷贝的区
- 下一篇: python设置全局变量失败_Pytho