[Python-Dev] Rationale for NamedTemporaryFile?

dustin at v.igoro.us dustin at v.igoro.us
Sun Mar 18 04:31:44 CET 2007

On Sun, Mar 18, 2007 at 11:49:55AM +1200, Greg Ewing wrote:
> I've just discovered the hard way that NamedTemporaryFile
> automatically deletes the file when you close it. That
> doesn't seem very useful to me, since surely the reason
> you're using NamedTemporaryFile instead of TemporaryFile
> is that you want to do something else with it afterwards?
> What's the rationale for this behaviour?

For both TemporaryFile and NamedTemporaryFile, the rationale is that
"temporary" extends until the object is garbage collected.
TemporaryFile is deleted immediately (to prevent other applications from
modifying your temporary file).  NamedTemporaryFile is inteded for use
when you need access to the file by filename during the lifetime of the
file.  In either case, when the object goes, the file should too.


More information about the Python-Dev mailing list