Why is "while" ticking me off???

Thomas Gagne tgagne at ix.netcom.com
Thu Oct 5 19:58:25 EDT 2000


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






More information about the Python-list mailing list