[Python] RE: How to safely maintain a status file

Hans Mulder hansmu at xs4all.nl
Fri Jul 13 20:28:13 CEST 2012


On 13/07/12 19:59:59, Prasad, Ramit wrote:

> I lean slightly towards the POSIX handling with the addition that 
> any additional write should throw an error. You are now saving to 
> a file that will not exist the moment you close it and that is
> probably not expected.

I'd say: it depends.

If the amount of data your script needs to process does not fit
in RAM, then you may want to write some of it to a temporary file.
On a Posix system, it's entirely normal to unlink() a temp file
first thing after you've created it.  The expectation is that the
file will continue to exists, and be writeable, until you close it.

In fact, there's a function in the standard library named
tempfile.TemporaryFile that does exactly that: create a file
and unlink it immediately.  This function would be useless
if you couldn't write to your temporary file.

Hope this helps,

-- HansM





More information about the Python-list mailing list