Looping issues

B

brochu121

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?
 
A

anglozaxxon

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?

Instead of "for line in correct_settings", try "for line in
correct_settings.readlines()".
 
L

Larry Bates

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?
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"

This does what you asked for but somehow I don't think it is
what you want. I would suggest that you take a look at difflib.

Somththing along the lines of:

import difflib

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()

delta=difflib.unified_diff(correct_lines, current_lines)
diffs=''.join(delta)

print diffs

Will show you the lines that are different and some lines
around it for context.

-Larry
 
A

anglozaxxon

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"

This does what you asked for but somehow I don't think it is
what you want. I would suggest that you take a look at difflib.

Somththing along the lines of:

import difflib

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()

delta=difflib.unified_diff(correct_lines, current_lines)
diffs=''.join(delta)

print diffs

Will show you the lines that are different and some lines
around it for context.

-Larry

Sorry my solution didn't work. The only other thing I can think of is
that something is screwy with the newlines, although this seems
silly. I've heard Python has "universal newline" support, meaning \n
= \r = \r\n, etc.

Try printing the contents of the file in its entirety:
print current_settings.read()
And see if it prints the entire file, or just the first line. I can't
imagine it won't print the whole thing. Next, do print
current_settings.read().replace('\\','\\\\'). This will make the
escape characters visible, so you can see exactly what type of
newlines it's printing. If the file is in Unix format and you're on
Windows, Python may be assuming the latter and not breaking lines
correctly. Post what it prints.

Nick
 
B

Bruno Desthuilliers

anglozaxxon a écrit :
On Apr 5, 2:01 pm, (e-mail address removed) wrote:
(snip)


Instead of "for line in correct_settings", try "for line in
correct_settings.readlines()".

What is this supposed to change ? (apart from loading the whole file in
memory, which may not be a problem here - config files are usually not
that big - but might become one for huge files...)
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
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.

Not quite. It really go thru the whole file. But it only enter the inner
loop for the first line. Then the file iterator is exhausted, and the
other iterations are noop. You can verify this by adding a couple print
statements - one in the outer loop and one in the inner one.
Any ideas as to how i can loop through each
line of the correct_settings file instead of just looking at the first?
The good question is "how to loop thru the lines of *current_*settings
for each line of correct_settings". The answer is : don't use the file
iterator here, read the whole file in memory. Cf Larry's post for a howto.
 
H

hlubenow

That Still didnt fix it. Same output

Try this:

correct_settings = file("C:\Python25\Scripts\Output\correct_settings.txt",
"r")
current_settings = file("C:\Python25\Scripts\Output\output.txt", "r")

a = correct_settings.readlines()
b = current_settings.readlines()

correct_settings.close()
current_settings.close()

for line in a:
for val in b:
if val == line:
print line + " found."
 
P

Peter Otten

Larry said:
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
 
T

thebjorn

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top