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

相关文章

centos7之Python3.74安装教程

centos7之Python3.74安装 安装版本:Python3.74 系统版本:centos7 系统默认安装Python2.7,保留。 安装/usr/bin/Python3 安装需要...

python flask中动态URL规则详解

URL是可以添加变量部分的, 把类似的部分抽象出来, 比如: @app.route('/example/1/') @app.route('/example/2/') @app.rou...

使用 python pyautogui实现鼠标键盘控制功能

pyautogui是一个可以控制鼠标和键盘的python库,类似的还有pywin32。 pyautogui的安装 pip3 install python3-xlib 依赖库 sudo a...

利用python、tensorflow、opencv、pyqt5实现人脸实时签到系统

利用python、tensorflow、opencv、pyqt5实现人脸实时签到系统

基于python opencv人脸识别的签到系统前言先看下效果实现的功能开始准备页面的构建功能实现代码部分总结 前言 一个基于opencv人脸识别和TensorFlow进行模型训练的人脸...

pytorch 批次遍历数据集打印数据的例子

我就废话不多说了,直接上代码吧! from os import listdir import os from time import time import torch.util...