for line in file weirdness

Cordula's Web cpghost at cordula.ws
Wed Apr 13 06:33:31 EDT 2005


Hello,

here's a strange bug (?) I've came across (using Python 2.2):

# loop_1
for line in file:
    if some_condition(line): break
    do_something()

# loop_2
for line in file:
    do_something_else()

The problem is, that loop_2 doesn't resume where loop_1 left off, but
skips many lines (a block's worth or so) before continuing.

Why is this? Is reading from a file non-reentrant?

It is always possible to slurp the whole file content into a list, and
then iterate through the list, but I want to handle HUGE files too.

Thanks,
-cpghost.

-- 
Cordula's Web. http://www.cordula.ws/




More information about the Python-list mailing list