I frequently see python code similar to
for line in open(filename): do_something(line)
When does filename get closed with this code?>
python is garbage-collected - cpython has reference counting and a backup cycle detecting garbage collector.
File objects close their file handle when the are deleted/finalized.
Thus the file will be eventually closed, and in cpython will closed as soon as the for loop finishes.