The Python difflib module implements two file diff comparisons and outputs HTML format.

Source: Internet
Author: User
Tags diff file diff readfile

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.