Python运维自动化之nginx配置文件对比操作示例

yipeiwu_com6年前Python基础

本文实例讲述了Python运维自动化之nginx配置文件对比操作。分享给大家供大家参考,具体如下:

文件差异对比diff.py

#!/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)

#python diff.py nginx1.conf nginx2.conf > diff.html

利用的是difflib模块,Python2.3以上版本自带的库

PS:这里再为大家推荐一款相似的在线工具供大家参考:

在线文本比较工具:
http://tools.jb51.net/aideddesign/txt_diff

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python文件与目录操作技巧汇总》、《Python文本文件操作技巧汇总》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

python 梯度法求解函数极值的实例

如下所示: #coding utf-8 a=0.001 #定义收敛步长 xd=1 #定义寻找步长 x=0 #定义一个种子x0 i=0 #循环迭代次数 y=...

Python Pickle 实现在同一个文件中序列化多个对象

也是看别人代码才知道可以打开一个文件就可以把多个对象序列化到这个文件中。 with open('../raw_data/remap.pkl', 'wb') as f: pickle...

Python 按字典dict的键排序,并取出相应的键值放于list中的实例

方法一: def dict_to_numpy_method1(dict): dict_sorted=sorted(dict.iteritems(), key=lambda d:d[...

python3+PyQt5实现支持多线程的页面索引器应用程序

python3+PyQt5实现支持多线程的页面索引器应用程序

本文通过Python3+pyqt5实现了python Qt GUI 快速编程的19章的页面索引器应用程序例子。 /home/yrd/eric_workspace/chap19/walke...

在Python中操作文件之read()方法的使用教程

 read()方法读取文件size个字节大小。如果读取命中获得EOF大小字节之前,那么它只能读取可用的字节。 语法 以下是read()方法的语法: fileObject.r...