Compare 2 files and discard common lines

Chris cwitts at gmail.com
Thu May 29 05:02:31 EDT 2008


On May 29, 10:36 am, 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?

How large are the files ? You could load up the smallest file into
memory then while iterating over the other one just do 'if line in
other_files_lines:' and do your processing from there.  By your
description it doesn't sound like you want to iterate over both files
simultaneously and do a line for line comparison because that would
mean if someone plonks an extra newline somewhere it wouldn't gel.



More information about the Python-list mailing list