using NamedTemporaryFile on windows
peter at engcorp.com
Thu Dec 29 06:40:34 CET 2005
Lee Harr wrote:
> Is there any other reason to use a named tempfile other than
> to be able to open it again? I am trying to understand this
> section of the documentation regarding NamedTemporaryFile:
> Whether the name can be used to open the file a second time, while the named temporary file
> is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT
> or later)
As it says, if you *don't close* the file, you can open it again if you
are on a platform which supports that.
>>From looking through the code, the NamedTemporaryFile will be
> deleted as soon as it is closed.
> So... if I can't open it again why does it need a name?
Because you can open it again *if* you don't close it... but not on Windows.
> Is there a way on windows to make a tempfile that I can open again?
Do you mean open again without having closed it (in other words,
basically get two different file handles to the same open file)? That's
apparently exactly what you cannot do on Windows, as noted above.
Do you mean a file that you can open again *later*, after having closed
it? If so, you obviously don't want a file that is automatically
deleted when you close it, so you just want mkstemp().
> Maybe what I need is just mkstemp, since that also returns a name?
> If so, what is the point of NamedTemporaryFile?
It creates a file that can be reopened under Unix provided you haven't
closed it yet. ;-)
NamedTemporaryFile doesn't appear to have much purpose on Windows, but
that's more or less what the docs already say. I think the only thing
you were missing perhaps was this idea of "opening a file a second time"
*without having closed it first*.
What I don't understand is why you _can't_ reopen the NamedTemporaryFile
under Windows when you can reopen the file created by mkstemp (and the
files created by TemporaryFile are created by mkstemp in the first place).
More information about the Python-list