If you're opening a file using the 'with' statement, do you still need to close the file object?

后端 未结 3 1153
春和景丽
春和景丽 2021-02-19 00:45

For opening files, I\'m used to the apparently older syntax:

f = open(\"sub_ranks.txt\",\"r+\")
for line in f:
    ...
f.close()

I\'ve been tol

3条回答
  •  孤独总比滥情好
    2021-02-19 01:22

    The answer to your immediate question is "No". The with block ensures that the file will be closed when control leaves the block, for whatever reason that happens, including exceptions (well, excluding someone yanking the power cord to your computer and some other rare events).

    So it's good practice to use a with block.

    Now arguably, having opened a file only for reading and then failing to close it is not that much of a problem. When garbage collection comes around (whenever that may be), that file will be closed, too, if there are no references to it anymore; at the latest that will happen when your program exits. In fact, several code samples in the official docs neglect closing a file that has been opened only for read access. When writing a file or when using the "read plus" mode like in your example, you definitely need to close the file. There are many questions her on SO dealing with incomplete/corrupted files because of a failure to close them properly.

提交回复
热议问题