Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python generator how does it close a file handle when the for loop calling the generator returns suddenly?

Tags:

python

If the function lookForSpecificLine returns True (aka, if the file "foo.txt" contains the targetLine), how does Python know to close the file handle? Would the file "foo.txt" remain open?

def lines(filename):
    with open(filename, encoding='utf-8') as file:
        for line in file:
            yield line

def lookForSpecificLine(targetLine):
    for line in lines('foo.txt'):
        if targetLine == line:
            return True
    return False
like image 940
platypus Avatar asked Aug 09 '14 06:08

platypus


People also ask

What does the close () method do to a generator?

Add a close() method for generator-iterators, which raises GeneratorExit at the point where the generator was paused. If the generator then raises StopIteration (by exiting normally, or due to already being closed) or GeneratorExit (by not catching the exception), close() returns to its caller.

How many times can you iterate through a generator?

This is because generators, like all iterators, can be exhausted. Unless your generator is infinite, you can iterate through it one time only. Once all values have been evaluated, iteration will stop and the for loop will exit. If you used next() , then instead you'll get an explicit StopIteration exception.

Can we call generator multiple times in Python?

A Python generator is a function that produces a sequence of results. It works by maintaining its local state, so that the function can resume again exactly where it left off when called subsequent times.

Why does a generator return in Python?

Python generators are a simple way of creating iterators. All the work we mentioned above are automatically handled by generators in Python. Simply speaking, a generator is a function that returns an object (iterator) which we can iterate over (one value at a time).


1 Answers

Your file will remain open as long as the generator object is alive. When the generator is garbage collected (at the end of the lookForSpecificLine function, usually), Python will call close on it, as part of the co-routine protocol described in PEP 342. The close method causes Python to throw a GeneratorExit exception into the generator's code at the place it was paused (just after the yield statement). Since you don't catch that exception (as you usually shouldn't), it will break out of the loop and cause the with statement to close the file.

Note that if lookForSpecificLine was more complicated and there was some risk of it causing an exception (that would be caught at a higher level), things might not get cleaned up quickly. That's because the exception traceback will keep the function's stack frame alive, and so the generator will not be garbage collected immediately and the file will not be closed.

like image 69
Blckknght Avatar answered Sep 20 '22 17:09

Blckknght