difflib can give you what you need. Suppose:
a.txt
this is a bunch of lines
b.txt
this is a different bunch of other lines
The code is as follows:
import difflib fileA = open("a.txt", "rt").readlines() fileB = open("b.txt", "rt").readlines() d = difflib.Differ() diffs = d.compare(fileA, fileB) lineNum = 0 for line in diffs: # split off the code code = line[:2] # if the line is in both files or just b, increment the line number. if code in (" ", "+ "): lineNum += 1 # if this line is only in b, print the line number and the text on the line if code == "+ ": print "%d: %s" % (lineNum, line[2:].strip())
gives a conclusion, for example:
bgporter@varese ~/temp:python diffy.py 4: different 7: other
You will also want to look at the difflib "? " Code and see how you want to handle this.
(also in real code, would you like to use context managers to make sure the files are closed, etc. etc.)
source share