8

Regarding my previous question, I noticed that in both answers that used the open() function, there was no mention of closing the file.

I've read that it's good practice to do so, but is there actually any need for it? Is it just unnecessary code?

Does the file get closed automatically?

closed as off-topic by terdon, kos, Jacob Vlijm, karel, Eric Carvalho Nov 24 '15 at 10:54

  • This question does not appear to be about Ubuntu within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    with open (file_name, ...) as variable: automatically invokes the close() method as soon as you leave this code block. – Byte Commander Nov 23 '15 at 12:45
  • 1
    Cross-site duplicate of stackoverflow.com/q/7395542/4464570 – Byte Commander Nov 23 '15 at 12:47
  • @ByteCommander do you want to give your comment as an answer, to make this question 'solved'? – TellMeWhy Nov 23 '15 at 13:50
  • 1
    In python3, the file is garbaged automatically once it has no more references. – Jacob Vlijm Nov 23 '15 at 14:59
  • 4
    I'm voting to close this question as off-topic because it is a general question about Python with no connection to scripting on Ubuntu. it is asking about best practices in a programming language. – terdon Nov 23 '15 at 15:11
7

When do files get closed?

As we can learn from Is explicitly closing files important? (StackOverflow), the Python interpreter closes the file in the following cases:

  • you manually invoke the close() method of a file object explicitly or implicitly by leaving a with open(...): block. This works of course always and on any Python implementation.
  • the file object's last reference got removed and therefore the object gets processed by the Garbage Collector. This is not a language feature, but a special feature of the CPython implementation only, so for portability don't rely on this!
  • the Python interpreter terminates. In this case it should close all file handles that got opened. Some older versions of Python3 would have also printed a warning that you should have closed them manually yourself. However, imagine a crash or you force-killing the Python interpreter and you will see that this is also not reliable.

So only the first (manual) method is reliable!

What would happen if a file stays open?

First, depending on the implementation of your Python interpreter, if you opened a file with write access, you can not be sure that your modifications got flushed to the disk until you either manually induce it or the file handler gets closed.

Second, you may only open a limited number of files on your system per user. If you exceed this limit by e.g. opening many files in a loop in your Python program without closing them as soon as possible, the system may refuse to open further file handles for you and you'll receive an exception. It may also happen that your program takes the last allowed open file and another program will fail because it gets declined.

Third, open files on a removable device prevent it from getting unmounted or ejected. You may still delete the file on some file systems like ext4, where simply the file descriptor/hard link to the file's inode gets removed/unlinked but the program which opened the file may still access the inode through its own temporary file handler. This is e.g. also the mechanism that allows you to update packages while the respective software is running. However, e.g. NTFS has no such feature. It may however never get modified by two concurrent processes, so it will be still somehow blocked for others.

  • Not entirely true for python3 !! – Jacob Vlijm Nov 23 '15 at 15:45
  • @JacobVlijm Would you mind explaining it? – Byte Commander Nov 23 '15 at 15:46
  • See the comment below the question. The answer about "not good practice" in the link of your answer is from 2011. If it ever existed, the warning in python3 does not exist any more and the answer is outdated. Automatic garbage collection exists for a reason and works perfectly. It has been years since I used close() specifically. Never ever ran into a single error caused by not using it. – Jacob Vlijm Nov 23 '15 at 16:19
  • Soo... You know whether the other implementations like PyPy, Jython or IronPython close files in the same way CPython does? And if I killed the interpreter, it would not have a chance to close the file, right? And I also think that the open file limit still applies. What about flushing written data? You know anything more up to date there? – Byte Commander Nov 23 '15 at 16:23
  • 1
    @JacobVlijm Thanks for the tip. :P My machine already crashed while creating that file... I did not even need Python for that. – Byte Commander Nov 23 '15 at 17:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.