testing true condition

Remco Gerlich scarblac-spamtrap at pino.selwerd.nl
Wed Apr 19 12:52:15 CEST 2000


Gregoire Welraeds wrote in comp.lang.python:
> I know that I can't do something like the following:
> 
> >>> while (line= f.readline()):
> 	...
> 
> as i could in C.  Is there any replacement tricks ?

The standard Python idiom is:

while 1:
   line = f.readline()
   if not line:
      break

There are also wrapper classes around file input, so you can use it in a for
loop, but it doesn't read in the whole file at once (same difference as with
range and xrange). Don't remember its name, but it's not hard to roll your
own.

-- 
Remco Gerlich,  scarblac at pino.selwerd.nl
  Murphy's Rules, "Try a firecracker":
   In TSR's Top Secret, it usually takes three grenade explosions to kill
   the average player-character-agent.



More information about the Python-list mailing list