compare - Comparing two files using difflib in python -
i trying compare 2 files using difflib. after comparing want print "no changes" if no difference detected. if difference in lines. want print line.
i tried this:
with open("compare.txt") f, open("test.txt") g: flines = f.readlines() glines = g.readlines() d = difflib.differ() diff = d.compare(flines, glines) print("\n".join(diff))
it print contents of file if "no changes" detected. want print "no changes" if there no difference.
check see if first character in each element has +
or -
@ start (marking line having changed):
with open("compare.txt") f, open("test.txt") g: flines = f.readlines() glines = g.readlines() d = difflib.differ() diffs = [x x in d.compare(flines, glines) if x[0] in ('+', '-')] if diffs: # rows changes else: print('no changes')
Comments
Post a Comment