Why should I close files in Python? [duplicate]

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?


Solution 1:

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!)

Hope this helps!

Solution 2:

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

Solution 3:

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.

Solution 4:

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.