"for line in fd" twice

Jack Diederich jack at performancedrivers.com
Tue Jun 3 17:53:47 CEST 2003

On Tue, Jun 03, 2003 at 05:24:16PM +0200, Thomas Güttler wrote:
> the following does not work:
> fd=open(file)
> for line in fd:
>     if line.startswith("mymark"):
>         break
> for line in fd:
>     #Read lines after "mymark"
> It works with the old way:
> while 1:
>     line=fd.readline()
>     if not line:
>         break

The part
for (line) in fd:
will make a temporary list of all the lines in fd before it starts iterating.
so by the time you get to the second for() loop the file has been exhausted.


More information about the Python-list mailing list