Is explicitly closing files important?

前端 未结 6 734
野的像风
野的像风 2020-11-22 00:26

In Python, if you either open a file without calling close(), or close the file but not using try-finally or the \"with\"

6条回答
  •  孤城傲影
    2020-11-22 01:31

    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?

提交回复
热议问题