Is explicitly closing files important?

前端 未结 6 669
野的像风
野的像风 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:15

    During the I/O process, data is buffered: this means that it is held in a temporary location before being written to the file.

    Python doesn't flush the buffer—that is, write data to the file—until it's sure you're done writing. One way to do this is to close the file.

    If you write to a file without closing, the data won't make it to the target file.

提交回复
热议问题