Looping issues

Peter Otten __peter__ at web.de
Fri Apr 6 02:29:25 EDT 2007


Larry Bates wrote:

> If the files aren't terribly large (not tested):
> 
> correct_lines=open(r"C:\Python25\Scripts\Output" \
>                     "\correct_settings.txt", "r").readlines()
> 
> current_lines=open(r"C:\Python25\Scripts\Output\output.txt",
>                     "r").readlines()
> 
> for line in current_settings:
>     if line in correct_lines:
>         print line + " found"

You only have to read the "inner" file into memory, and a set is more
efficient than a list here:

current_settings = open(...)
correct_settings = set(open(...))

for line in current_settings:
   if line in correct_settings:
      print line.rstrip(), "found"

Of course your suggestion of difflib is spot-on.

Peter




More information about the Python-list mailing list