Python file comparison

Source: Internet
Author: User

Environment:

python2.6.6

Linux Systems


Compare file differences with the Difflib module in Python:

Python comes with a difflib module without installation.


Compare two files is the general idea is: Read the contents of the file and then compare


Symbol Meaning
+ Contained in the second sequence, but does not contain the first sequence in a
- Contained in the first sequence, but not in the second sequence
Flag two sequence rows with delta differences
^ Flag two sequence existence difference character
‘‘ Two sequence lines consistent

Example one:

#!/usr/bin/python#coding:utf-8#2017,8,27import difflibtext1= ' 1234567890this is a text One.heihiehie ' text1_line= Text1.splitlines () #以行进行分割 so that later contrast text2= ' 235678956545This is a Text Two.heiheihei ' Text2_line=text2.splitlines () d = Difflib. Differ () #创建Differ () object diff = d. Compare (text1_line,text2_line) print ' \ n '. Join (List (diff))

Look at the results of the operation:


650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/A4/A1/wKioL1muneKQiZ-VAAAbFrbCiAk671.png "title=" 2017-09-05_205129.png "alt=" Wkiol1munekqiz-vaaabfrbciak671.png "/>


are symbols, the symbols are not good to distinguish, the next generation of HTML files.


Example two:

#!/usr/bin/python#coding:utf-8#2017,8,27import difflibtext1= ' 1234567890this is a text One.heihiehie ' text1_line= Text1.splitlines () #以行进行分割 so that later contrast text2= ' 235678956545This is a Text Two.heiheihei ' Text2_line=text2.splitlines () d = Difflib. Htmldiff () #创建Differ () object print D.make_file (text1_line,text2_line)

Execute command to generate diff.html file

[email protected] difflib]# python diff_simple2.py >> diff.html

To open diff.html file in a browser:

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/A4/A1/wKioL1muoECAgp_5AAAZWMKBH6g864.png "title=" 1.png "alt=" Wkiol1muoecagp_5aaazwmkbh6g864.png "/>


That would be a lot more beautiful.


Comprehensive applications, comparing the differences between two files:

#!/usr/bin/python#-*-coding:utf-8-*-#2017.9.5import difflibimport sys# receive Parameters try:textfile1=sys.argv[1] textfile2=sys.argv[2]except exception,e:print  "Error:" +str (e) print  "Usage:python contrast_ File.py filename1 filename2 >> diff.html "Sys.exit () #读取文件函数def   readfile ( FileName): Try:filehandle = open (filename,  ' RB ') Text = filehandle.read (). Splitlines () Filehandle.close () return textexcept ioerror as error:print  "Read file error : "+str (Error) Sys.exit () #判断接收的是否为空if   (textfile1 == " " or textfile2==" "):p rint " Usage:python contrast_file.py filename1 filename2 >> diff.html " sys.exit () #调用函数获取分割后的字符串text1_lines  = readfile (textfile1)   text2_lines = readfile ( Textfile2) #创建html类对象, and compare. Diff = difflib. Htmldiff () print diff.make_file (Text1_lines,text2_lines)

Execute the command to generate the diff2.html file, with the browser open as in example two.

[[email protected] difflib]# python contrast_file.py httpd.conf httpd.conf.bak >> diff2.html

Reference: Network and Python automation operations technology and best practices

Summary: The idea is very important, this document comparison of the idea is to first read out the document, in contrast. Make a little progress every day.


650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/A4/A1/wKioL1muoZrC6lsKAAQF1aT1ixQ620.png "title=" Enjoy. png "alt=" Wkiol1muozrc6lskaaqf1at1ixq620.png "/>

This article from "Tiandaochouqin" blog, declined reprint!

Python file comparison

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.