Why is "while" ticking me off???

Tyler Eaves tyler at tylereaves.com
Thu Oct 5 21:01:10 EDT 2000


On Thu, 05 Oct 2000 19:58:25 -0400, Thomas Gagne
<tgagne at ix.netcom.com> wrote:

This works:

a=fp.readlines()
for x in a[]:
   do something



>I have a file that I want to read line-by-line, and stop reading when I run
>out of lines.  My first desire was to attempt:
>
>while line = fp.readline():
>    do something with 'line'
>
>but that doesn't work because Python doesn't allow assignments inside flow
>control statements.  The example I see in books is pretty useless:
>
>while fp.readline():
>    count = count + 1
>
>because if I don't capture the return value of fp.readline() I'm kinda hosed,
>aren't I?
>
>I would try a do:, but there isn't one, is there?  At least not one documented
>in, "The Quick Python Book."
>
>So I thought I'd try something like:
>
>def myReadline(fp, line):
>    line = fp.readline()
>    return line
>...
>while myReadline(theFpImReading, data):
>
>but 'data' doesn't seem to get mutated from the myReadline function!
>
>I'm obviously not approaching this in the Python idiomatic way.
>
>--
>.tom
>
>
>

---
Tyler Eaves

Visit Ultra Coaster Central!
The internet's largest repository
of Ultra Coaster Tracks!

http://www.ultracoastercentral.com


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----



More information about the Python-list mailing list