Looping issues

Sick Monkey sickcodemonkey at gmail.com
Thu Apr 5 16:07:42 EDT 2007


Try this.
NOTE:  The script below will compare lines of both files (even hidden
characters such as \n)...

################################################################
def fopen(fname):
    try:
        return open(fname, 'U')
    except IOError, detail:
        return fail("couldn't open " + fname + ": " + str(detail))
################################################################
f1 = fopen('file1.txt')
a = f1.readlines(); f1.close()
d1 = {}
for c in a:
      d1[c.lower()] = None
      #print d1
f2 = fopen('file2.txt')
data = f2.readlines(); f2.close()
for line in data:
      if d1.has_key( line ):
          print line
################################################################

wdh

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/f7e2b9fa/attachment.html>


More information about the Python-list mailing list