File open: Is this bad Python style?

前端 未结 6 1745
庸人自扰
庸人自扰 2021-01-17 17:18

To read contents of a file:

data = open(filename, \"r\").read()

The open file immediately stops being referenced anywhere, so the file obje

6条回答
  •  滥情空心
    2021-01-17 17:56

    It is true that it will close eventually, but eventually might not be soon enough. Especially if you're using this inside a loop, the system might run out of file handles before the GC gets to the file objects.

提交回复
热议问题