newbie question: if var1 == var2:

Reggie Dugard reggiedugard at
Sat Nov 29 21:54:39 CET 2008

On Fri, 2008-11-28 at 19:47 -0800, joemacbusiness at wrote:
> Hi All,
> I dont understand why the following code never finds "tree".

The problem is that the lines you are reading from the file have a
newline at the end so 'tree' != 'tree\n'.  See below for suggested

> I could not find the answer in the Python tutorials.
> Here is the code,, and runtime:
> #!/usr/bin/python
> fp = open("")
> var = 'tree'
> for item in fp:
      item = item.strip()  # Strip off white space
      # No commas necessary at the end of the prints
      # now that we've removed the newlines.
>     print "item:", item
>     if item == var:
>         print "found tree:", item

Hope this helps you understand what was happening.  Have fun with


More information about the Python-list mailing list