File handling 20: Compare two files and print the line where they differ

# Files: Difference between files

import sys

try:
    fname1 = sys.argv[1]
    fname2 = sys.argv[2]

    fs1 = open(fname1,"r")
    fs2 = open(fname2,"r")
    Flag = False
  
    L1 = fs1.readlines()
    L2 = fs2.readlines()

    nlines = min(len(L1), len(L2))

    for idx in range(0, nlines, 1):
        if (L1[idx] != L2[idx]):
            print("Files differ at line number", idx+1)
            Flag = True
            break
          
    if(Flag == False):
        if(len(L1)==len(L2)):
            print("Files are the same. They do not differ")
        else:
            print("Files differ at line number", nlines+1)
except:
    print("Error")
  
finally:  
    fs1.close()
    fs2.close()



No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu