"for line in fd" twice

Thomas Güttler guettler at thomas-guettler.de
Tue Jun 3 11:24:16 EDT 2003


Hi!

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"
    ....
fd.close()
"""

The second loop misses some lines.

It works with the old way:

"""
while 1:
    line=fd.readline()
    if not line:
        break
"""

instead of "for line in fd"

If you mustnot call "for line in fd" twice, it would 
be nice to get an exception which reminds me about it.

Version:
 Python 2.2.2 (#1, Mar 17 2003, 15:17:58)
 [GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2

 thomas





More information about the Python-list mailing list