In Python, if you either open a file without calling close()
, or close the file but not using try
-finally
or the \"with
\"
The file does get garbage collected, and hence closed. The GC determines when it gets closed, not you. Obviously, this is not a recommended practice because you might hit open file handle limit if you do not close files as soon as you finish using them. What if within that for
loop of yours, you open more files and leave them lingering?