Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I close files in Python? [duplicate]

Tags:

python

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?

like image 348
Jessica Avatar asked Aug 01 '14 00:08

Jessica


People also ask

Why is closing files important in Python?

You've learned why it's important to close files in Python. Because files are limited resources managed by the operating system, making sure files are closed after use will protect against hard-to-debug issues like running out of file handles or experiencing corrupted data.

Do I need to close file python with?

Thus, by using “with”, you avoid the need to explicitly close files. Python does it for you, in a somewhat un-Pythonic way, magically, silently, and behind the scenes.

Why is it important to close files and streams?

When you open a file using any programming language , that is actually a request to operating system to access the file. When such a request is made , resources are allocated by the OS. When you gracefully close those files, those resources are set free.

Why is it important to close the file objects after using them in the code in Java?

Not closing a file will result in unnecessary resources being taken from the system (File Descriptors on Unix and Handles on windows). Especially when a bug happens in some sort of loop or a system is never turned off, this gets important.


2 Answers

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!

like image 52
rocket101 Avatar answered Oct 14 '22 01:10

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

like image 28
Sai Avatar answered Oct 14 '22 03:10

Sai