python 文本比对

 

# -*- coding:utf-8 -*-
import difflib
import sys

def readfile(filename):
    try:
        fileHandle = open(filename, 'r+')
        text = fileHandle.read().splitlines()
        fileHandle.close()
        return text
    except IOError as error:
        print('Read file Error:' + str(error))
        sys.exit()

def savediffile():
    text1_lines = readfile(textfile1)
    text2_lines = readfile(textfile2)
    d = difflib.HtmlDiff()
    with open('difffile.html','w') as f:
        f.write(d.make_file(text1_lines, text2_lines))

if __name__ == '__main__':
    try:
        textfile1 = sys.argv[1]
        textfile2 = sys.argv[2]
        savediffile()

    except Exception as e:
        print("Error:" + str(e))
        print("Usage: 3.py filename1 filename2")
        sys.exit()

 

posted @ 2019-04-15 18:23  Anec  阅读(1331)  评论(0编辑  收藏  举报