looping through a file

John J. Lee jjl at pobox.com
Sat Jul 26 20:55:11 EDT 2003


Heiko Wundram <heikowu at ceosg.de> writes:

> On Fri, 2003-07-25 at 22:24, Bruno Desthuilliers wrote:
> > for line in thefile.readlines():
> >      doWhatEverWithTheLine()
> 
> Or, if you're on a sufficiently new Python:
> 
> for line in thefile:
> 	doWhateverWithTheLine()

I recall that in an old Python you could get into an awful mess (as I
did) with this by having several 'for line in file' blocks in a row:

for line in f:
    if not line.startswith("magic"): continue  # skip header

for line in f:
    ...process data...

I gathered from a recent thread that this has changed -- the file is
its own iterator now.  Was the old behaviour ever released in 2.2, or
was it just part of a 2.3 beta?


John




More information about the Python-list mailing list