This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: option to leave tempfile.NamedTemporaryFile around on close
Type: Stage:
Components: Library (Lib) Versions: Python 2.5
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, djmdjm, georg.brandl
Priority: normal Keywords: patch

Created on 2006-08-10 06:57 by djmdjm, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
tempfile.diff djmdjm, 2006-08-10 06:57 Add optional 'delete' argument to NamedTemporaryFile constructor
python-tempfile.diff djmdjm, 2006-08-25 04:27 Diff with regress test
Messages (8)
msg50852 - (view) Author: Damien Miller (djmdjm) Date: 2006-08-10 06:57

tempfile.NamedTemporaryFile provides a good interface
to creating temporary files, but its insistence on
deleting the file upon close is limiting. The attached
patch adds an optional parameter to NamedTemporaryFile
that allows persistence of the temp file after it has
been closed.

One use-case that demonstrates where keeping the
temporary file around is handy would be when you need
to safely create and fill a temp file before it is
atomically moved into place with os.rename(). E.g.

def update_conf(conf_path):
    old = open(conf_path)
    tmp = tempfile.NamedTemporaryFile(prefix =
os.basename(conf_path), \
        dir = os.dirname(conf_path), delete = False)
    for l in old:
        tmp.write(l.replace('war', 'peace'))
    close(tmp), conf_path + ".old")
    os.rename(, conf_path)
msg50853 - (view) Author: Damien Miller (djmdjm) Date: 2006-08-10 06:58
Logged In: YES 

oops, wrong Category: this should be Lib and not Modules
msg50854 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2006-08-24 23:52
Logged In: YES 

Why can't you store into an instance of StringIO instead of
a temp file?
msg50855 - (view) Author: Damien Miller (djmdjm) Date: 2006-08-25 00:22
Logged In: YES 

As far as I can tell, StringIO doesn't actually create a
filesystem object that can be manipulated.
msg50856 - (view) Author: Damien Miller (djmdjm) Date: 2006-08-25 04:27
Logged In: YES 

Here is an diff that includes a regress test
msg50857 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2006-08-26 06:40
Logged In: YES 

Right, it doesn't create a filesystem file.  But that is the
point.  You work in memory and then write to your final
destination as needed.  Your code you have pasted in the
description does nothing special that requires the use of a
temporary file.  You can just write into a StringIO object,
skip the call, and then just write out to the final
file location.
msg50858 - (view) Author: Damien Miller (djmdjm) Date: 2006-08-26 06:47
Logged In: YES 

Well, that would a) not be an atomic replacement and b) you
miss (or would have to reimplement) the mkstemp() like
msg50859 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-03-13 18:32
Thanks for the patch, committed with doc changes as rev. 54344.
Date User Action Args
2022-04-11 14:56:19adminsetgithub: 43804
2006-08-10 06:57:31djmdjmcreate