I have a requirement that i have two files, one is input file and another is log file(both will contain the same data mostly). I need to compare these two files and if any difference found then those lines should be write to another file(3rd file).
I done googling, but I am able to find only two files comparison which returns true or false.
Simple read the use file.readlines() on both files to give you 2 arrays
so something like this
f1 = open("file1.txt", "r")
f2 = open("file2.txt", "r")
fileOne = f1.readlines()
fileTwo = f2.readlines()
outFile = open("results.txt", "w")
x = 0
for i in fileOne:
if i != fileTwo[x]:
outFile.write(i+" <> "+fileTwo[x])
x += 1
We had a topic like this a short while ago. The easiest/most efficient method is to use two sets and compare with set1.difference(set_2) and vice-versa. Also, python has difflib which may or may not be more than you want. http://docs.python.org/lib/differ-examples.html