file iterator - what happens here?

Helmut Jarausch jarausch at skynet.be
Fri May 16 04:45:19 EDT 2003


Hi,

Python's behaviour on the following code snippet
puzzles me

cmd_input= open('Tree.inp','r')
for line in cmd_input:
     while  line.startswith('%') : # skip comment lines
         print "!!!",line
         line= cmd_input.readline()
     print "===",line,"<<<"
     if not line: break

print "finished"


for the input file 'Tree.inp'
------------ snip -------------
# *.bak *.bck
% ++++++++++++++++++++++++++++++++++++++++
------------------------------------------
- TD
= *.bck
------------ snip -------------

the output is

------------ snip -------------
=== # *.bak *.bck
<<<
!!! % ++++++++++++++++++++++++++++++++++++++++

===  <<<
finished
------------ snip -------------

So, obviously the line 'line= cmd_input.readline()'
disturbs the for iterator completely.
Is that a feature?
(This is Python cvs version from 2003/05/13)

Thanks for shedding some light on this mystery (at least for me)

-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany





More information about the Python-list mailing list