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