Why should I close files in Python?

Question:

Usually when I open files I never call the close() method, and nothing bad happens. But I’ve been told this is bad practice. Why is that?

Asked By: Jessica

||

Answers:

The close() method of a file object flushes any unwritten information and closes the file object, after which no more writing can be done.

Python automatically closes a file when the reference object of a file is reassigned to another file. It is a good practice to use the close() method to close a file.Here is the link about the close() method. I hope this helps.

Answered By: JesterTemporada

You only have to call close() when you’re writing to a file.

Python automatically closes files most of the time, but sometimes it won’t, so you want to call it manually just in case.

Answered By: Thomas Hobohm

Open files use resources and may be locked, preventing other programs from using them. Anyway, it is good practice to use with when reading files, as it takes care of closing the file for you.

with open('file', 'r') as f:
   read_data = f.read()

Here’s an example of something “bad” that might happen if you leave a file open.
Open a file for writing in your python interpreter, write a string to it, then open that file in a text editor. On my system, the file will be empty until I close the file handle.

Answered By: Matt Adams

For the most part, not closing files is a bad idea, for the following reasons:

  1. It puts your program in the garbage collectors hands – though the file in theory will be auto closed, it may not be closed. Python 3 and Cpython generally do a pretty good job at garbage collecting, but not always, and other variants generally suck at it.

  2. It can slow down your program. Too many things open, and thus more used space in the RAM, will impact performance.

  3. For the most part, many changes to files in python do not go into effect until after the file is closed, so if your script edits, leaves open, and reads a file, it won’t see the edits.

  4. You could, theoretically, run in to limits of how many files you can have open.

  5. As @sai stated below, Windows treats open files as locked, so legit things like AV scanners or other python scripts can’t read the file.

  6. It is sloppy programming (then again, I’m not exactly the best at remembering to close files myself!)

Answered By: rocket101

Found some good answers:

(1) It is a matter of good programming practice. If you don’t close
them yourself, Python will eventually close them for you. In some
versions of Python, that might be the instant they are no longer
being used; in others, it might not happen for a long time. Under
some circumstances, it might not happen at all.

(2) When writing to a file, the data may not be written to disk until
the file is closed. When you say “output.write(…)”, the data is
often cached in memory and doesn’t hit the hard drive until the file
is closed. The longer you keep the file open, the greater the
chance that you will lose data.

(3) Since your operating system has strict limits on how many file
handles can be kept open at any one instant, it is best to get into
the habit of closing them when they aren’t needed and not wait for
“maid service” to clean up after you.

(4) Also, some operating systems (Windows, in particular) treat open
files as locked and private. While you have a file open, no other
program can also open it, even just to read the data. This spoils
backup programs, anti-virus scanners, etc.

http://python.6.x6.nabble.com/Tutor-Why-do-you-have-to-close-files-td4341928.html
https://docs.python.org/2/tutorial/inputoutput.html

Answered By: Sai

I had a problem with that recently:
I was writing some stuff to a file in a for-loop, but if I interrupt the script with ^C, a lot of data which should have actually been written to the file wasn’t there. It looks like Python stops to writing there for no reason. I opened the file before the for loop. Then I changed the code so that Python opens and closes the file for ever single pass of the loop.

Basically, if you write stuff for your own and you don’t have any issues – it’s fine, if you write stuff for more people than just yourself – put a close() inside the code, because someone could randomly get an error message and you should try to prevent this.

Answered By: Exceen
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.