Looping issues

Collin Stocks collinstocks at gmail.com
Thu Apr 5 14:20:00 EDT 2007


I don't know what is wrong with your code yet, but first you should clean it
up. Either replace those backslashes with forward slashes, or put r before
the first quote in the path string. This prevents special characters from
being evaluated as such.

Second, you should debug a little. Feel free to put print statements in!

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

for line in correct_settings:
        print "line=", line
        for val in current_settings:
            print "val=", val
            if val == line:
                print line, "found." ## please don't concatenate strings in
the print statement! separate them with commas!
                break ## You have already found that the same line is in
both files: you don't need to keep searching


correct_settings.close()
current_settings.close()


On 5 Apr 2007 11:01:09 -0700, brochu121 at gmail.com <brochu121 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?
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070405/c4b1d57a/attachment.html>


More information about the Python-list mailing list