what is wrong with this line? while (line = workfile.readline()) != "":

Magnus Lie Hetland mlh at idi.ntnu.no
Mon May 28 12:06:54 EDT 2001


"erudite" <ask at me.com> wrote in message
news:nxvO6.5946$LT4.524910 at e420r-sjo2.usenetserver.com...
> Hey,
>     I'm coming from the Java world to the python world which is probably
why
> I'm a little confused as to why this statement doesn't work:
>
> while (line = workfile.readline()) != "":
>
> when I try to run it I get a :
>
>   File "srns.py", line 38
>     while (line = workfile.readline()) != "":
>                 ^
> SyntaxError: invalid syntax
>
> error....

See the Python FAQ, section 6.30
(http://python.org/doc/FAQ.html#6.30)

Note that you may rather want to use the file methods
readlines(), xreadlines() or the fileinput module
rather than the while 1:/break solution in the FAQ.
Your mileage may vary.

In the upcoming Python version, you can actually
just do this:

   for line in workfile:
       ...

--

  Magnus Lie Hetland         http://www.hetland.org

 "Reality is that which, when you stop believing in
  it, doesn't go away."           -- Philip K. Dick






More information about the Python-list mailing list