欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

02-对比两个文件的差异

程序员文章站 2022-06-22 15:32:31
#!/usr/bin/python #coding=utf8 """ # Author: xiaoyafei # Created Time : 2018-04-04 17:14:20 # File Name: check_Nginx_conf.py # Description: """ import... ......
#!/usr/bin/python
#coding=utf8
"""
# Author: xiaoyafei
# Created Time : 2018-04-04 17:14:20

# File Name: check_Nginx_conf.py
# Description:

"""
import difflib
import sys

try:
    textfile1 = sys.argv[1]
    textfile2 = sys.argv[2]
except Exception,e:
    print "Error:"+str(e)
    print "Usage: check_Nginx_conf.py filename1 filename2"
    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 "Usage: check_Nginx_conf.py filename1 filename2"

text1_lines = readfile(textfile1)
text2_lines = readfile(textfile2)

d = difflib.HtmlDiff()
print d.make_file(text1_lines,text2_lines)