From: gelonida on
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)?


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

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


Thanks for explaining me exactly what kind of evil I could encounter
with not explicitely closing.

From: Chris Rebert on
On Tue, Apr 13, 2010 at 3:01 PM, gelonida <gelonida(a)gmail.com> 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)?
>
> Even with my example
> I'd expected to get an exception raised if not all data could have
> been written.
>
> 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).

That extremely-quick responsiveness of the garbage-collection
machinery is only guaranteed by CPython, not the language
specification itself, and indeed some of the other implementations
*explicitly don't* make that guarantee (and hence the data may not get
flushed in a timely manner on those implementations). And portability
of code is encouraged, hence the admonishment you encountered.

Cheers,
Chris
--
http://blog.rebertia.com
From: Steven D'Aprano on
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
From: Giampaolo Rodola' on
What about open('foo', 'w').close().
Does it have the same problems?


--- Giampaolo
http://code.google.com/p/pyftpdlib
http://code.google.com/p/psutil
From: Chris Rebert on
On Tue, Apr 13, 2010 at 5:45 PM, Giampaolo Rodola' <gnewsg(a)gmail.com> wrote:
> What about open('foo', 'w').close().
> Does it have the same problems?

Well, no, but that's only because it's a pointless no-op that doesn't
really do anything besides possibly throwing an exception (e.g. if the
script didn't have write access to the current directory).

Cheers,
Chris
--
http://blog.rebertia.com