looping through a file

John J. Lee jjl at pobox.com
Sun Jul 27 20:23:18 EDT 2003


bokr at oz.net (Bengt Richter) writes:

> On 27 Jul 2003 01:55:11 +0100, jjl at pobox.com (John J. Lee) wrote:
[...]
> >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...

[my example should have been:

for line in f:
    if line.startswith("magic"): break  # skip header
for line in f:
    ...process data...
]

> What mess was that? (unless you meant to re-open the file to read
> from the beginning)

That example creates (used to, anyway) two iterators -- one for each
'for line in f'.  The first reads chunks (not just one line at a time)
and caches the read data.  The second one knows nothing about that and
innocently starts reading from the seek position that the first
happened to stop at -- ie., at some random point further on than the
point where you thought you'd finished up at the break in the first
loop.

http://www.google.com/groups?hl=en&lr=&ie=UTF-8&selm=3C6D73DE.3090201%40wi.mit.edu&rnum=3


> >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?
> >
> Curious what was changed ...

I'm not certain it has changed -- haven't tested.


John




More information about the Python-list mailing list