From 385a6f8ac571a7bb1b959a9f0d00b6ed718e8bfa Mon Sep 17 00:00:00 2001 From: Nick Crews Date: Thu, 3 Dec 2026 02:18:17 -0900 Subject: [PATCH] gh-95484: Improve `tempfile` docs for conditions when files are removed (GH-95485) (cherry picked from commit b40f5b9fc3993952ffe32ac14f622ba3373c55a3) Co-authored-by: Nick Crews Co-authored-by: Stephen Rosen Co-authored-by: Petr Viktorin --- Doc/library/tempfile.rst | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Doc/library/tempfile.rst b/Doc/library/tempfile.rst index 874612994aad3a..785dca35a147f1 100644 --- a/Doc/library/tempfile.rst +++ b/Doc/library/tempfile.rst @@ -32,17 +32,19 @@ The module defines the following user-callable items: .. function:: TemporaryFile(mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None) Return a :term:`file-like object` that can be used as a temporary storage area. - The file is created securely, using the same rules as :func:`mkstemp`. It will be destroyed as soon - as it is closed (including an implicit close when the object is garbage - collected). Under Unix, the directory entry for the file is either not created at all or is removed - immediately after the file is created. Other platforms do not support + The file is created securely, using the same rules as :func:`mkstemp`. + + The object can be used as a context manager + (see :ref:`tempfile-examples`). + Exiting the context manager closes the file. + + Under Unix, the directory entry for the file is either not created at all or is + removed immediately after the file is created. Other platforms do not support this; your code should not rely on a temporary file created using this function having or not having a visible name in the file system. - The resulting object can be used as a :term:`context manager` (see - :ref:`tempfile-examples`). On completion of the context or - destruction of the file object the temporary file will be removed - from the filesystem. + The file will be removed from the file system when the object is closed + (including an implicit close when the object is garbage collected). The *mode* parameter defaults to ``'w+b'`` so that the file created can be read and written without being closed. Binary mode is used so that it