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

yipeiwu_com5年前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程序设计有所帮助。

相关文章

node.js获取参数的常用方法(总结)

1、req.body 2、req.query 3、req.params 一、req.body例子 body不是nodejs默认提供的,你需要载入body-parser中间件才可以使用re...

python实现多线程的两种方式

目前python 提供了几种多线程实现方式 thread,threading,multithreading ,其中thread模块比较底层,而threading模块是对thread做了一...

Python中使用item()方法遍历字典的例子

Python中使用item()方法遍历字典的例子

Python字典的遍历方法有好几种,其中一种是for...in,这个我就不说明,在Python了几乎随处都可见for...in。下面说的这种遍历方式是item()方法。 item() i...

python二维列表一维列表的互相转换实例

二维列表转一维列表 from compiler.ast import flatten a=[[1,2],[5,6]] print(flatten(a)) 结果:[1, 2, 5,...

Python multiprocess pool模块报错pickling error问题解决方法分析

本文实例讲述了Python multiprocess pool模块报错pickling error问题解决方法。分享给大家供大家参考,具体如下: 问题 之前在调用class内的函数用mu...