Why no open(f, "w").write()?
Isaac To
kkto at csis.hku.hk
Sun Jun 2 00:30:57 EDT 2002
>>>>> "John" == John La Rooy <larooy at xtar.co.nz> writes:
John> I've often used the 'open(f, "w").write()' right at the end of a
John> program where I know the file is about to get closed anyway. From
John> what I've seen here about Jython, it seems that I can't even count
John> on that behaviour. eeek!
John> Perhaps the specification will some day change to make the objects
John> returned by open() to be *smarter* so we can count on them knowing
John> when they should flush and when they should close the toilet seat.
John> A few people seem to be getting stuck on the explicit instead of
John> implicit idea but I think that is mostly because we are used to be
John> told to close files and we've all had a few late nights because we
John> forgot to. Just as lots of us learned we should always free() our
John> mallocs() and now we think nothing of letting python do that
John> housekeeping for us.
That's possible if you just want to make sure that all files are closed at
the end of execution: just keep a list of opened files objects somewhere
(perhaps using weak references?), and, when the program exits, close all of
them. This is standard OS technique (all process has a list of opened file,
which are closed on exit). It is even easy to implement at library level.
But that won't solve all the problems. In particular, if the output of your
files are read by somebody else, you have to know at some time that the
content is valid for reading.
Regards,
Isaac.
More information about the Python-list
mailing list