Compare the contents of two files with Python and lose the travel number and content.

Source: Internet
Author: User

Code section:
"' cmpfile.py-compared to two files, if there are differences, print content and line number ' ' ' Import osclass cmpfile:def __init__ (self, File1, file2): Self.file1 = File1 Self.file2 = file2 def fileexists (self): if Os.path.exists (self.file1) and Os.pat H.exists (self.file2): Return True else:return False # Compare file differences and return the result def compare (self ): If Cmpfile (Self.file1, Self.file2). FileExists (): FP1 = open (self.file1) FP2 = open (self.fi  le2) Flist1 = [I for i in FP1] flist2 = [x for x in FP2] flines1 = Len (flist1) Flines2 = Len (flist2) if flines1 < flines2:flist1[flines1:flines2+1] = "* (flines2-flines1) if Flines2 < Flines1:flist2[flines2:flines1+1] = "* (flines1-flines2) counter = 1 cmpreses            = [] for x in Zip (Flist1, flist2): if x[0] = = X[1]: Counter +=1 continue If x[0]! = x[1]: cmpres = '%s ' and%s row%s, content:%s---%s '% (Self.file1, self.file2, counter, X [0].strip (), X[1].strip ()) Cmpreses.append (cmpres) counter +=1 return Cmpresesif __na        me__ = = ' __main__ ': cmpfile = cmpfile (' a1.txt ', ' a2.txt ') Difflines = Cmpfile.compare () for I in Difflines: Print (i, end= ' \ n ')
Execution results
A1.txt and A2.txt Line 4th, the content is:--4444444444444444444a1.txt and A2.txt 5th line, the content is: SFSDFASDF-- 5555555555555555555a1.txt and A2.txt Line 7th, the content is: FASDFASDF---Sfsdfasdfa1.txt and A2.txt 8th line, the content is: 1111111111111111111-- > A1.txt and A2.txt Line 9th, the content is: 2222222222222222222---Fasdfasdfa1.txt and A2.txt 10th line, the content is: 3333333333333333333-- A1.txt and A2.txt line 11th, the content is: 4444444444444444444---A1.txt and A2.txt 12th line, the content is: 5555555555555555555-- A1.txt and A2.txt Line 13th, the content is:--a1.txt and A2.txt 14th line, the content is: Sfsdfasdf and A1.txt and A2.txt 15th line, content is:--a1.txt and a2.t The 16th line of the XT is different: FASDFASDF---A1.txt and A2.txt 17th line, the content is: 1111111111111111111---A1.txt and A2.txt 18th line, the content is: 2222222222222222222---A1.txt and A2.txt line 19th, the content is: 3333333333333333333---A1.txt and a2.txt 20th row, content is: 4444444444444444444---A1.txt and A2.txt line 21st, the content is: 5555555555555555555---A1.txt and A2.txt 22nd row, content is:- A1.txt and A2.txt Line 23rd, the content is: SFSDFASDF---A1.txt and A2.txt 24th line, the content is:--a1.txt and A2.txt 25th line, the content is: FASDFASDF--A1.txt and A2.txt line 26th, the content is: 1111111111111111111---A1.txt and A2.txt 27th line, the content is: 2222222222222222222-- A1.txt and A2.txt line 28th, the content is: 3333333333333333333---A1.txt and A2.txt 29th line, the content is: 4444444444444444444-- A1.txt and A2.txt Line 30th, the content is: 5555555555555555555---A1.txt and A2.txt 31st line, the content is:--a1.txt and A2.txt 32nd line, the content is:  SFSDFASDF---A1.txt and a2.txt the 33rd row, the content is:--a1.txt and A2.txt 34th line, the content is: FASDFASDF--

The

Compares the contents of two files with Python and loses the travel number and content.

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.