the Difflib module contains tools for calculating and processing differences between sequences. It is especially useful for comparing text, which contains functions that can generate reports using a variety of common difference formats.
Three classes are implemented:
-
sequencematcher comparison of any type sequence (can compare strings)
-
differ Comparison of strings
-
Htmldiff output of comparison results to HTML format
#!/usr/bin/env python##-*- coding: utf-8 -*-import difflibimport systry: textfile1=sys.argv[1] textfile2=sys.argv[2]except exception as e: print ("Error:" +str (e)) print ("Usage:diff_ Simple3.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:diff_simple3") sys.exit () Text1_lines=readfile ( Textfile1) Text2_lines=readfile (textfile2) d = difflib. Htmldiff () print (D.make_file (text1_lines,text2_lines))
The Python difflib module implements two file diff comparisons and outputs HTML format.