when should I explicitely close a file?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Tue Apr 13 20:34:23 EDT 2010


On Tue, 13 Apr 2010 15:01:25 -0700, gelonida wrote:

> Hi,
> 
> 
> I've been told, that following code snippet is not good.
> 
> 
> open("myfile","w").write(astring) , because I'm neither explicitely
> closing nor using the new 'with' syntax.
> 
> What exactly is the impact of not closing the file explicitely
> (implicitley with a 'with' block)?


Your data may not be actually written to disk until the file closes. If 
you have a reference loop, and Python crashes badly enough, the garbage 
collector may never run and the file will never be closed, hence you will 
get data loss.

If you are running something other than CPython (e.g. IronPython or 
Jython) then the file might not be closed until your program exits. If 
you have a long-running program that opens many, many files, it is 
possible for you to run out of system file handles and be unable to open 
any more.

Best practice is to explicitly close the file when you are done with it, 
but for short scripts, I generally don't bother. Laziness is a virtue :)

But for library code and larger applications, I always explicitly close 
the file, because I want to control exactly when the file is closed 
rather than leave it up to the interpreter. I don't know if my code might 
one day end up in a long-running Jython application so I try to code 
defensively and avoid even the possibility of a problem.


> Even with my example
> I'd expected to get an exception raised if not all data could have been
> written.

Generally if you get an exception while trying to *close* a file, you're 
pretty much stuffed. What are you going to do? How do you recover?

My feeling is that you're probably safe with something as simple as 

file("myfile", "w").write("my data\n")

but if you do something like

some_data_structure.filehandle = file("myfile", "w")
some_data_structure.filehandle.write("my data\n")
# ... lots more code here

and some_data_structure keeps the file open until the interpreter shuts 
down, there *might* be rare circumstances where you won't be notified of 
an exception, depending on the exact circumstances of timing of when the 
file gets closed. In the worst case, the file might not be closed until 
the interpreter is shutting down *and* has already dismantled the 
exception infrastructure, and so you can't get an exception. I don't know 
enough about the Python runtime (particularly about how it works during 
shutdown) to know how real this danger is, but if it is a danger, I bet 
it involves __del__ methods.



> I'd also expected, that all write data is flushed as soon as the
> filehandle is out of scope (meaning in the next line of my source code).

This is only guaranteed with CPython, not other implementations.

My feeling is that explicit closing is pedantic and careful, implicit 
closing is lazy and easy. You make your choice and take your chance :)




-- 
Steven



More information about the Python-list mailing list