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()

 

转载于:https://www.cnblogs.com/Anec/p/10712181.html

你可能感兴趣的:(python 文本比对)