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

MRAB python at mrabarnett.plus.com
Fri Jul 13 21:57:34 CEST 2012

On 13/07/2012 19:28, Hans Mulder wrote:
> 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.
Strictly speaking, the file does exist, it's just that there are no
names referring to it. When any handles to it are also closed, the file
_can_ truly be deleted.

As has been said before, in the *nix world, "unlink" _doesn't_ delete
a file, it deletes a name.

> 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.
It's possible to create a temporary file even in Windows.

More information about the Python-list mailing list