[New-bugs-announce] [issue14514] Equivalent to tempfile.NamedTemporaryFile that deletes file at context exit

R. David Murray report at bugs.python.org
Fri Apr 6 04:00:29 CEST 2012

New submission from R. David Murray <rdmurray at bitdance.com>:

A common pattern (especially in writing tests) is to create a named temporary file, operate on it with tools that take the filename, and then delete the file.  This pattern would be facilitated by a version of NamedTemporaryFile that deleted the named file at the end of the context, but allowed the file to continue to exist when closed.  This latter requirement arises because the file cannot be opened a second time on Windows, and so needs to be closed before other operations that open it can be performed in order for the code to work cross platform.

It's tempting just to change delete=True to mean delete on exit (not close) if and only if NamedTemporaryFile is being used as a context manager, but there might be backward compatibility issues with that.  It might be acceptable as a feature change, though, since the likelyhood of someone closing such a tempfile inside the context and then depending on it *not* existing between then and the end of the context seems low.

If that it deemed unacceptable, this could be implemented as a new flag on NamedTemporaryFile, or by changing the delete flag to take additional values (True/False == current behavior, but add delete='onclose', delete='onexit', delete='no', and document True/False as deprecated.)

components: Library (Lib)
messages: 157634
nosy: r.david.murray
priority: normal
severity: normal
status: open
title: Equivalent to tempfile.NamedTemporaryFile that deletes file at context exit
type: enhancement
versions: Python 3.3

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list