looping through a file

Bengt Richter bokr at oz.net
Sun Jul 27 19:10:55 CEST 2003


On 27 Jul 2003 01:55:11 +0100, jjl at pobox.com (John J. Lee) wrote:

>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...
>
What mess was that? (unless you meant to re-open the file to read from the beginning)

>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 ...

Regards,
Bengt Richter




More information about the Python-list mailing list