Skip to main content
added 368 characters in body
Source Link
Ethan Furman
  • 70.1k
  • 21
  • 174
  • 251

Files get closedSome Pythons will close files automatically when they are no longer referenced, butwhile others will not and it's up to the O/S to close files when the Python interpreter exits.

Even for the Pythons that will close files for you, the timing is not guaranteed: it could be immediately, or it could be seconds/minutes/hours/days later.

So, while you may not experience problems with the Python you are using, it is definitely not good practice to do soleave your files open. In fact, in Pythoncpython 3 you will now get warnings that the system had to close files for you if you didn't do it.

Moral: Clean up after yourself. :)

Files get closed automatically when they are no longer referenced, but it is not good practice to do so. In fact, in Python 3 you will now get warnings that the system had to close files for you if you didn't do it.

Moral: Clean up after yourself. :)

Some Pythons will close files automatically when they are no longer referenced, while others will not and it's up to the O/S to close files when the Python interpreter exits.

Even for the Pythons that will close files for you, the timing is not guaranteed: it could be immediately, or it could be seconds/minutes/hours/days later.

So, while you may not experience problems with the Python you are using, it is definitely not good practice to leave your files open. In fact, in cpython 3 you will now get warnings that the system had to close files for you if you didn't do it.

Moral: Clean up after yourself. :)

Source Link
Ethan Furman
  • 70.1k
  • 21
  • 174
  • 251

Files get closed automatically when they are no longer referenced, but it is not good practice to do so. In fact, in Python 3 you will now get warnings that the system had to close files for you if you didn't do it.

Moral: Clean up after yourself. :)