-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
gh-58451: Add optional delete_on_close parameter to NamedTemporaryFile #97015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
3df957c
c4cf39f
ac0b241
384614f
0e0b6ba
cc81ab5
5af6150
239354e
5de68c0
eef3439
c9801ee
6944ab2
44f0664
1dfaf72
e54feb5
d2d1119
bd4b2dd
29eeaff
5306b6d
1c99fb3
29b89d3
e586028
3eceb45
892e87d
67996b2
07b963e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -75,20 +75,53 @@ The module defines the following user-callable items: | |
| Added *errors* parameter. | ||
|
|
||
|
|
||
| .. function:: NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True, *, errors=None) | ||
|
|
||
| This function operates exactly as :func:`TemporaryFile` does, except that | ||
| the file is guaranteed to have a visible name in the file system (on | ||
| Unix, the directory entry is not unlinked). That name can be retrieved | ||
| from the :attr:`name` attribute of the returned | ||
| file-like object. Whether the name can be | ||
| used to open the file a second time, while the named temporary file is | ||
| still open, varies across platforms (it can be so used on Unix; it cannot | ||
| on Windows). If *delete* is true (the default), the file is | ||
| deleted as soon as it is closed. | ||
| The returned object is always a file-like object whose :attr:`!file` | ||
| attribute is the underlying true file object. This file-like object can | ||
| be used in a :keyword:`with` statement, just like a normal file. | ||
| .. 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) | ||
|
|
||
| This function operates exactly as :func:`TemporaryFile` does, except the | ||
| following differences: | ||
|
|
||
| * The file is guaranteed to have a visible name in the file system (on Unix, | ||
| the directory entry is not unlinked). | ||
|
|
||
| * There is more granularity in the deletion behaviour of the file (see | ||
| ``delete_on_close`` below) | ||
|
|
||
| The returned object is always a :term:`file-like object` whose :attr:`!file` | ||
| attribute is the underlying true file object. This :term:`file-like object` can be | ||
| used in a :keyword:`with` statement, just like a normal file. The name of the | ||
| temporary file can be retrieved from the :attr:`name` attribute of the | ||
| returned file-like object. | ||
|
|
||
| If *delete* is true (the default) and *delete_on_close* is true (the | ||
| default), the file is deleted as soon as it is closed. If *delete* is true | ||
| and *delete_on_close* is false, the file is deleted on context manager exit | ||
| only, if no context manager was used, then the file is deleted only when the | ||
| :term:`file-like object` is finalized and hence deletion is not always | ||
| guaranteed in this case (see :meth:`object.__del__`). If *delete* is false, | ||
| the value of *delete_on_close* is ignored. | ||
Ev2geny marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| While the named temporary file is open, the file can always be opened again | ||
| on POSIX. On Windows, it can be opened again if *delete* is false, or if | ||
| *delete_on_close* is false, or if the additional open shares delete access | ||
| (e.g. by calling :func:`os.open` with the flag ``O_TEMPORARY``). On | ||
| Windows, if *delete* is true and *delete_on_close* is false, additional | ||
| opens that do not share delete access (e.g. via builtin :func:`open`) must | ||
| be closed before exiting the context manager, else the :func:`os.unlink` | ||
| call on context manager exit will fail with a :exc:`PermissionError`. | ||
|
||
|
|
||
| To use the name of the temporary file to open the closed file second time, | ||
| either make sure not to delete the file upon closure (set the *delete* | ||
| parameter to be false) or, in case the temporary file is created in a | ||
| :keyword:`with` statement, set the *delete_on_close* parameter to be false. | ||
| The latter approach is recommended as it provides assistance in automatic | ||
| cleaning of the temporary file upon the context manager exit. | ||
zooba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| On Windows, if *delete_on_close* is false, and the file is created in a | ||
| directory for which the user lacks delete access, then the :func:`os.unlink` | ||
| call on exit of the context manager will fail with a :exc:`PermissionError`. | ||
| This cannot happen when *delete_on_close* is true because delete access is | ||
| requested by the open, which fails immediately if the requested access is not | ||
| granted. | ||
|
|
||
| On POSIX (only), a process that is terminated abruptly with SIGKILL | ||
| cannot automatically delete any NamedTemporaryFiles it created. | ||
|
|
@@ -98,6 +131,9 @@ The module defines the following user-callable items: | |
| .. versionchanged:: 3.8 | ||
| Added *errors* parameter. | ||
|
|
||
| .. versionchanged:: 3.12 | ||
| Added *delete_on_close* parameter. | ||
|
|
||
|
|
||
| .. class:: SpooledTemporaryFile(max_size=0, mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None) | ||
|
|
||
|
|
@@ -346,6 +382,22 @@ Here are some examples of typical usage of the :mod:`tempfile` module:: | |
| >>> | ||
| # file is now closed and removed | ||
|
|
||
| # create a temporary file using a context manager, note the name, | ||
| # close the file, use the name to open the file again | ||
| >>> with tempfile.TemporaryFile(delete_on_close=False) as fp: | ||
| ... fp.write(b'Hello world!') | ||
| ... fp_name = fp.name | ||
| ... fp.close() | ||
| # the file is closed, but not removed | ||
| # open the file again by using its name | ||
| ... f = open(fp_name) | ||
Ev2geny marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ... f.seek(0) | ||
Ev2geny marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ... f.read() | ||
| b'Hello world!' | ||
| ... f.close() | ||
| >>> | ||
| # file is now removed | ||
|
|
||
| # create a temporary directory using the context manager | ||
| >>> with tempfile.TemporaryDirectory() as tmpdirname: | ||
| ... print('created temporary directory', tmpdirname) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| The :class:`tempfile.NamedTemporaryFile` has a new optional parameter *delete_on_close* | ||
Ev2geny marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Uh oh!
There was an error while loading. Please reload this page.