Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare two text file in python

Tags:

python

file

I want to compare two test file in python.(actually they are windows registry files(.reg) but they are all text). im looking for all the differences between two files and not just the first line which is not the same of second file. thanks in advance

like image 646
user1229351 Avatar asked Jan 20 '26 23:01

user1229351


1 Answers

f1 = open(filepath1)
f2 = open(filepath2)

lines = f2.readlines()
for i,line in enumerate(f1):
    if line != lines[i]:
        print "line", i, "is different:"
        print '\t', line
        print '\t', lines[i]
        print "\t differences in positions:", ', '.join(map(str, [c for c in range(len(line)) if line[c]!= lines[i][c]]))

Hope this helps

like image 98
inspectorG4dget Avatar answered Jan 22 '26 15:01

inspectorG4dget