Compare 2 files and discard common lines

Lie Lie.1296 at gmail.com
Tue Jun 3 08:09:24 EDT 2008


On May 29, 3: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?

It's so easy to do that it won't count as reinventing the wheel:

a = open('a.txt', 'r').read().split('\n')
b = open('b.txt', 'r').read().split('\n')
c = open('c.txt', 'w')
c.write('\n'.join([comm for comm in b if not (comm in a)]))
c.close()

it's not the fastest common searcher but it works.



More information about the Python-list mailing list