using NamedTemporaryFile on windows

Chris Lambacher chris at kateandchris.net
Thu Dec 29 17:44:31 EST 2005


On Thu, Dec 29, 2005 at 12:40:34AM -0500, Peter Hansen wrote:
> 
> 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).

Basically the only reason that you want a NamedTemporaryFile is so that you
can write something to a file, tell another process to use that file (that you
have just written to) and then have file cleanup taken care of for you
automatically.  This works on Unix where you can have process open a
file for reading while another process has the file open for writing.  You
can't do this on Windows without jumping through a lot of hoops(sqlite and MS
Access come to mind as programs that manage this, though they may start a
server process to manage it).

mkstemp does create a file for you, but you are responsible for removing the
file when you are done.  Unfortunately this is what you are left with on
Windows.

-Chris



More information about the Python-list mailing list