本文实例讲述了Python运维自动化之nginx配置文件对比操作。分享给大家供大家参考,具体如下:
文件差异对比diff.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
|
#!/usr/bin/env python # import difflib import sys try : textfile1 = sys.argv[ 1 ] textfile2 = sys.argv[ 2 ] except exception,e: print "Error:" + str ( 2 ) print "Usge: difflib.py file1 file2" 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 "usege :difflib.py file1 file2" sys.exit() text1_lines = readfile(textfile1) text2_lines = readfile(textfile2) d = difflib.HtmlDiff() print d.make_file(text1_lines, text2_lines) |
1
|
#python diff.py nginx1.conf nginx2.conf > diff.html |
利用的是difflib
模块,Python2.3以上版本自带的库
希望本文所述对大家Python程序设计有所帮助。
原文链接:https://blog.csdn.net/yufei6808/article/details/51291658