当前位置: 移动技术网 > IT编程>脚本编程>Python > Python运维自动化之nginx配置文件对比操作示例

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

2018年09月23日  | 移动技术网IT编程  | 我要评论

无纺布储物箱,卜卦铃声,花魁的玩物txt

本文实例讲述了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:这里再为大家推荐一款相似的在线工具供大家参考:

在线文本比较工具:

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

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网