Compare 2 files and discard common lines

alex23 wuwei23 at gmail.com
Thu May 29 07:45:06 EDT 2008


On May 29, 6:36 pm, loial <jldunn2... at googlemail.com> wrote:
> I have a requirement to compare 2 text files and write to a 3rd file
> only those lines that appear in the 2nd file but not in the 1st file.
>
> Rather than re-invent the wheel I am wondering if anyone has written
> anything already?

>>> file1 = set((x for x in open('file1')))
>>> file2 = set((x for x in open('file2')))
>>> file3 = file2.difference(file1)
>>> open('file3','w').writelines(file3)



More information about the Python-list mailing list