Looping issues
thebjorn
BjornSteinarFjeldPettersen at gmail.com
Fri Apr 6 05:56:55 EDT 2007
On Apr 5, 8:01 pm, brochu... at gmail.com wrote:
> What I am trying to do is compare two files to each other.
>
> If the 2nd file contains the same line the first file contains, I want
> to print it. I wrote up the following code:
>
> correct_settings = open("C:\Python25\Scripts\Output
> \correct_settings.txt","r")
> current_settings = open("C:\Python25\Scripts\Output\output.txt","r")
>
> for line in correct_settings:
> for val in current_settings:
> if val == line:
> print line + " found."
>
> correct_settings.close()
> current_settings.close()
>
> For some reason this only looks at the first line of the
> correct_settings.txt file. Any ideas as to how i can loop through each
> line of the correct_settings file instead of just looking at the first?
I'm not entirely sure I understand what you're trying to do, but in
case you're trying to walk through the two files in lockstep printing
the lines that correspond, here's a way to do that:
# note the r'..' syntax
correct = open(r'c:\python25\scripts\output\correct_settings.txt')
current = open(r'c:\python25\scripts\output\output.txt')
for correct_line, current_line in zip(correct, current):
if correct_line == current_line:
print correct_line, 'found.'
correct.close()
current.close()
hth,
-- bjorn
More information about the Python-list
mailing list