Compares the two files entered by the user, showing the position of the line number and the first different character in all different places

Source: Internet
Author: User
Tags chop

1. Write a program that compares the two files entered by the user, and, if different, displays the position of the line number and the first different character in all different places.

ImportOSImportRECWD=os.getcwd () os.chdir (CWD) filename1= Input ("Please enter the first file name to compare:") filename2= Input ("Please enter a different file name to compare:") F1=Open (filename1) F2=Open (filename2) rownum=0count=0#defined as each row has a different for(Line1,line2)inchZip (f1,f2): RowNum+=1ifLine1! =Line2:colnum=0 for(Chara,charb)inchZip (line1,line2):ifChara = =Charb:colnum+=1Else:                Print 'row%d, column%d, is not the same'%(rownum,colnum) Count+ = 1Print("two files in total of "%d" different"%count) f1.close () f2.close ()

A.txt content:

From tomorrow onwards, be a happy man.
Feed the horses, chop the wood, travel around the world
From tomorrow onwards, care for food and vegetables
I have a house, facing the sea, with spring blossoms

B.txt content:

From tomorrow onwards, be a happy man.
Feed horses, chop wood, travel the world, Da da
From tomorrow onwards, care for food and vegetable dishes
I have a house, facing the sea, with spring blossoms

Operation Result:

>>>

Please enter the first file name to compare: A.txt
Please enter another file name to compare: B.txt
2nd row, column 20th, not the same.
3rd row, column 24th, not the same.
Two files in total of "2" at different

Compares the two files entered by the user, showing the position of the line number and the first different character in all different places

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.