most idiomatic looping over a file?
Erik Max Francis
max at alcyone.com
Wed May 28 21:19:27 EDT 2003
Mark Harrison wrote:
> What's the most idiomatic way to loop over input
> one line at at time, like the C code
>
> while ((s = fgets(...)) != NULL) { ... }
In Python this is most frequently written as
while True:
line = fileLike.readline()
if not line:
break
... do something with line ...
If you plan to iterate over the whole file, then you can use something
like
for line in fileLike.xreadlines():
...
or
for line in fileLike:
...
--
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Only the winners decide what were war crimes.
\__/ Gary Wills
More information about the Python-list
mailing list