@@ -75,20 +75,61 @@ The module defines the following user-callable items:
7575 Added *errors * parameter.
7676
7777
78- .. function :: NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True, *, errors=None)
79-
80- This function operates exactly as :func: `TemporaryFile ` does, except that
81- the file is guaranteed to have a visible name in the file system (on
82- Unix, the directory entry is not unlinked). That name can be retrieved
83- from the :attr: `name ` attribute of the returned
84- file-like object. Whether the name can be
85- used to open the file a second time, while the named temporary file is
86- still open, varies across platforms (it can be so used on Unix; it cannot
87- on Windows). If *delete * is true (the default), the file is
88- deleted as soon as it is closed.
89- The returned object is always a file-like object whose :attr: `!file `
90- attribute is the underlying true file object. This file-like object can
91- be used in a :keyword: `with ` statement, just like a normal file.
78+ .. function :: NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True, *, errors=None, delete_on_close=True)
79+
80+ This function operates exactly as :func: `TemporaryFile ` does, except the
81+ following differences:
82+
83+ * This function returns a file that is guaranteed to have a visible name in
84+ the file system.
85+ * To manage the named file, it extends the parameters of
86+ :func: `TemporaryFile ` with *delete * and *delete_on_close * parameters that
87+ determine whether and how the named file should be automatically deleted.
88+
89+ The returned object is always a :term: `file-like object ` whose :attr: `!file `
90+ attribute is the underlying true file object. This :term: `file-like object `
91+ can be used in a :keyword: `with ` statement, just like a normal file. The
92+ name of the temporary file can be retrieved from the :attr: `name ` attribute
93+ of the returned file-like object. On Unix, unlike with the
94+ :func: `TemporaryFile `, the directory entry does not get unlinked immediately
95+ after the file creation.
96+
97+ If *delete * is true (the default) and *delete_on_close * is true (the
98+ default), the file is deleted as soon as it is closed. If *delete * is true
99+ and *delete_on_close * is false, the file is deleted on context manager exit
100+ only, or else when the :term: `file-like object ` is finalized. Deletion is not
101+ always guaranteed in this case (see :meth: `object.__del__ `). If *delete * is
102+ false, the value of *delete_on_close * is ignored.
103+
104+ Therefore to use the name of the temporary file to reopen the file after
105+ closing it, either make sure not to delete the file upon closure (set the
106+ *delete * parameter to be false) or, in case the temporary file is created in
107+ a :keyword: `with ` statement, set the *delete_on_close * parameter to be false.
108+ The latter approach is recommended as it provides assistance in automatic
109+ cleaning of the temporary file upon the context manager exit.
110+
111+ Opening the temporary file again by its name while it is still open works as
112+ follows:
113+
114+ * On POSIX the file can always be opened again.
115+ * On Windows, make sure that at least one of the following conditions are
116+ fulfilled:
117+
118+ * *delete * is false
119+ * additional open shares delete access (e.g. by calling :func: `os.open `
120+ with the flag ``O_TEMPORARY ``)
121+ * *delete * is true but *delete_on_close * is false. Note, that in this
122+ case the additional opens that do not share delete access (e.g.
123+ created via builtin :func: `open `) must be closed before exiting the
124+ context manager, else the :func: `os.unlink ` call on context manager
125+ exit will fail with a :exc: `PermissionError `.
126+
127+ On Windows, if *delete_on_close * is false, and the file is created in a
128+ directory for which the user lacks delete access, then the :func: `os.unlink `
129+ call on exit of the context manager will fail with a :exc: `PermissionError `.
130+ This cannot happen when *delete_on_close * is true because delete access is
131+ requested by the open, which fails immediately if the requested access is not
132+ granted.
92133
93134 On POSIX (only), a process that is terminated abruptly with SIGKILL
94135 cannot automatically delete any NamedTemporaryFiles it created.
@@ -98,6 +139,9 @@ The module defines the following user-callable items:
98139 .. versionchanged :: 3.8
99140 Added *errors * parameter.
100141
142+ .. versionchanged :: 3.12
143+ Added *delete_on_close * parameter.
144+
101145
102146.. class :: SpooledTemporaryFile(max_size=0, mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None)
103147
@@ -346,6 +390,19 @@ Here are some examples of typical usage of the :mod:`tempfile` module::
346390 >>>
347391 # file is now closed and removed
348392
393+ # create a temporary file using a context manager
394+ # close the file, use the name to open the file again
395+ >>> with tempfile.TemporaryFile(delete_on_close=False) as fp:
396+ ... fp.write(b'Hello world!')
397+ ... fp.close()
398+ # the file is closed, but not removed
399+ # open the file again by using its name
400+ ... with open(fp.name) as f
401+ ... f.read()
402+ b'Hello world!'
403+ >>>
404+ # file is now removed
405+
349406 # create a temporary directory using the context manager
350407 >>> with tempfile.TemporaryDirectory() as tmpdirname:
351408 ... print('created temporary directory', tmpdirname)
0 commit comments