In CPython, at least, files are closed when the file object is deallocated. See the file_dealloc
function in Objects/fileobject.c
in the CPython source. Dealloc methods are sort-of like __del__
for C types, except without some of the problems inherent to __del__
.