Is explicitly closing files important?

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

    Hi It is very important to close your file descriptor in situation when you are going to use it's content in the same python script. I today itself realize after so long hecting debugging. The reason is content will be edited/removed/saved only after you close you file descriptor and changes are affected to file!

    So suppose you have situation that you write content to a new file and then without closing fd you are using that file(not fd) in another shell command which reads its content. In this situation you will not get you contents for shell command as expected and if you try to debug you can't find the bug easily. you can also read more in my blog entry http://magnificentzps.blogspot.in/2014/04/importance-of-closing-file-descriptor.html

提交回复
热议问题