Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guaranteeing a file close

Tags:

python

I have a class where I create a file object in the constructor. This class also implements a finish() method as part of its interface and in this method I close the file object. The problem is that if I get an exception before this point, the file will not be closed. The class in question has a number of other methods that use the file object. Do I need to wrap all of these in a try finally clause or is there a better approach?

Thanks,

Barry

like image 498
Baz Avatar asked Oct 25 '11 12:10

Baz


2 Answers

You could make your class a context-manager, and then wrap object creation and use of that class in a with-statement. See PEP 343 for details.

To make your class a context-manager, it has to implement the methods __enter__() and __exit__(). __enter__() is called when you enter the with-statement, and __exit__() is guaranteed to be called when you leave it, no matter how.

You could then use your class like this:

with MyClass() as foo:
    # use foo here

If you acquire your resources in the constructor, you can make __enter__() simply return self without doing anything. __exit__() should just call your finish()-method.

like image 142
Björn Pollex Avatar answered Oct 21 '22 09:10

Björn Pollex


For short lived file objects, a try/finally pair or the more succinct with-statement is recommended as a clean way to make sure the files are flushed and the related resources are released.

For long lived file objects, you can register with atexit() for an explicit close or just rely on the interpreter cleaning up before it exits.

At the interactive prompt, most people don't bother for simple experiments where there isn't much of a downside to leaving files unclosed or relying on refcounting or GC to close for you.

Closing your files is considered good technique. In reality though, not explicitly closing files rarely has any noticeable effects.

like image 28
Raymond Hettinger Avatar answered Oct 21 '22 08:10

Raymond Hettinger