Compare 2 files and discard common lines

afrobeard afrobeard at gmail.com
Thu May 29 07:24:55 EDT 2008


Another way of doing this might be to use the module difflib to
calculate the differences. It has a sequence matcher under it which
has the function get_matching_blocks

difflib is included with python.


On May 29, 2:02 pm, Chris <cwi... at gmail.com> wrote:
> 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