Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do files get closed during an exception exit?

Tags:

python

file

Do open files (and other resources) get automatically closed when the script exits due to an exception?

I'm wondering if I need to be closing my resources during my exception handling.

EDIT: to be more specific, I am creating a simple log file in my script. I want to know if I need to be concerned about closing the log file explicitly in the case of exceptions. since my script has a complex, nested, try/except blocks, doing so is somewhat complicated, so if python, CLIB, or the OS is going to close my text file when the script crashes/errors out, I don't want to waste too much time on making sure the file gets closed.

If there is a part in Python manual that talks about this, please refer me to it, but I could not find it.

like image 375
Siavash Avatar asked Jul 10 '13 17:07

Siavash


People also ask

What happens if you don't close a file after writing to it?

If you write to a file without closing, the data won't make it to the target file. But after some surfing I got to know that 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.

Is it mandatory to close a text file which is opened?

Yes, it's better to close file after reading is completed. That's necessary because the other software might request exclusive access to that file. If file is still opened then such request will fail.

What are the recommended options to ensure that a file is always closed correctly in Python?

Another option offered by Python is to use a with statement which will ensure the file is closed when the code that uses it finishes running. This holds true even if an exception is thrown. with open('filename', 'w') as f: f. write('Hello world!'

Why it is important to close a file how it is closed?

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.


1 Answers

A fairly straightforward question.

Two answers.

One saying, “Yes.”

The other saying, “No!”

Both with significant upvotes.

Who to believe? Let me attempt to clarify.


Both answers have some truth to them, and it depends on what you mean by a file being closed.

First, consider what is meant by closing a file from the operating system’s perspective.

When a process exits, the operating system clears up all the resources that only that process had open. Otherwise badly-behaved programs that crash but didn’t free up their resources could consume all the system resources.

If Python was the only process that had that file open, then the file will be closed. Similarly the operating system will clear up memory allocated by the process, any networking ports that were still open, and most other things. There are a few exceptional functions like shmat that create objects that persist beyond the process, but for the most part the operating system takes care of everything.

Now, what about closing files from Python’s perspective? If any program written in any programming language exits, most resources will get cleaned up—but how does Python handle cleanup inside standard Python programs?

The standard CPython implementation of Python—as opposed to other Python implementations like Jython—uses reference counting to do most of its garbage collection. An object has a reference count field. Every time something in Python gets a reference to some other object, the reference count field in the referred-to object is incremented. When a reference is lost, e.g, because a variable is no longer in scope, the reference count is decremented. When the reference count hits zero, no Python code can reach the object anymore, so the object gets deallocated. And when it gets deallocated, Python calls the __del__() destructor.

Python’s __del__() method for files flushes the buffers and closes the file from the operating system’s point of view. Because of reference counting, in CPython, if you open a file in a function and don’t return the file object, then the reference count on the file goes down to zero when the function exits, and the file is automatically flushed and closed. When the program ends, CPython dereferences all objects, and all objects have their destructors called, even if the program ends due to an unhanded exception. (This does technically fail for the pathological case where you have a cycle of objects with destructors, at least in Python versions before 3.4.)

But that’s just the CPython implementation. Python the language is defined in the Python language reference, which is what all Python implementations are required to follow in order to call themselves Python-compatible.

The language reference explains resource management in its data model section:

Some objects contain references to “external” resources such as open files or windows. It is understood that these resources are freed when the object is garbage-collected, but since garbage collection is not guaranteed to happen, such objects also provide an explicit way to release the external resource, usually a close() method. Programs are strongly recommended to explicitly close such objects. The ‘try...finally‘ statement and the ‘with‘ statement provide convenient ways to do this.

That is, CPython will usually immediately close the object, but that may change in a future release, and other Python implementations aren’t even required to close the object at all.

So, for portability and because explicit is better than implicit, it’s highly recommended to call close() on everything that can be close()d, and to do that in a finally block if there is code between the object creation and close() that might raise an exception. Or to use the with syntactic sugar that accomplishes the same thing. If you do that, then buffers on files will be flushed, even if an exception is raised.

However, even with the with statement, the same underlying mechanisms are at work. If the program crashes in a way that doesn’t give Python’s __del__() method a chance to run, you can still end up with a corrupt file on disk:

#!/usr/bin/env python3.3  import ctypes  # Cast the memory adress 0x0001 to the C function int f() prototype = ctypes.CFUNCTYPE(int) f = prototype(1)  with open('foo.txt', 'w'):     x.write('hi')     # Segfault     print(f()) 

This program produces a zero-length file. It’s an abnormal case, but it shows that even with the with statement resources won’t always necessarily be cleaned up the way you expect. Python tells the operating system to open a file for writing, which creates it on disk; Python writes hi into the C library’s stdio buffers; and then it crashes before the with statement ends, and because of the apparent memory corruption, it’s not safe for the operating system to try to read the remains of the buffer and flush them to disk. So the program fails to clean up properly even though there’s a with statement. Whoops. Despite this, close() and with almost always work, and your program is always better off having them than not having them.

So the answer is neither yes nor no. The with statement and close() are technically not necessary for most ordinary CPython programs. But not using them results in non-portable code that will look wrong. And while they are extremely helpful, it is still possible for them to fail in pathological cases.

like image 156
andrewdotn Avatar answered Oct 04 '22 00:10

andrewdotn