In Python 3, TemporaryDirectory from the tempfile module can be used.

From the examples:

import tempfile

with tempfile.TemporaryDirectory() as tmpdirname:
     print('created temporary directory', tmpdirname)

# directory and contents have been removed

To manually control when the directory is removed, don't use a context manager, as in the following example:

import tempfile

temp_dir = tempfile.TemporaryDirectory()
print(temp_dir.name)
# use temp_dir, and when done:
temp_dir.cleanup()

The documentation also says:

On completion of the context or destruction of the temporary directory object the newly created temporary directory and all its contents are removed from the filesystem.

At the end of the program, for example, Python will clean up the directory if it wasn't removed, e.g. by the context manager or the cleanup() method. Python's unittest may complain of ResourceWarning: Implicitly cleaning up <TemporaryDirectory... if you rely on this, though.

Answer from Nagev on Stack Overflow
🌐
Python
docs.python.org › 3 › library › tempfile.html
tempfile — Generate temporary files and directories
Source code: Lib/tempfile.py This module creates temporary files and directories. It works on all supported platforms. TemporaryFile, NamedTemporaryFile, TemporaryDirectory, and SpooledTemporaryFil...
🌐
Read the Docs
python.readthedocs.io › fr › stable › library › tempfile.html
11.6. tempfile — Generate temporary files and directories — documentation Python 3.6.1
If tempdir is unset or None at any call to any of the above functions except gettempprefix() it is initialized following the algorithm described in gettempdir().
🌐
DEV Community
dev.to › nadun96 › understanding-pythons-temporarydirectory-mec
Understanding Python's TemporaryDirectory() - DEV Community
July 26, 2024 - When working with temporary files ... features is TemporaryDirectory(), which allows you to create temporary directories that are automatically cleaned up when no longer needed....
🌐
Heitor's Log
heitorpb.github.io › bla › pytmp
Temporary files and directories in Python | Heitor's log
Both pytest’s tmp_path and tempfile.TemporaryDirectory directories are created with 700 permission, that is, only the user that owns the Python process can read, write, and execute it, group and others have zero permissions (rwx------). Directories created via pathlib.Path.mkdir have 755, so group and others can read and execute (rwxr-xr-x).
🌐
Python.org
discuss.python.org › ideas
Create temporary files in a temporary directory - Ideas - Discussions on Python.org
January 16, 2024 - Often, I find myself needing to create multiple temporary files that are related. It would be nice if tempfile.TemporaryDirectory could make that easier for me using something like this: with tempfile.TemporaryDirectory() as temp_dir: fd1, temp_file1_path = temp_dir.mkstemp() fd2, temp_file2_path = temp_dir.mkstemp() # Do something with the files...
🌐
Real Python
realpython.com › ref › stdlib › tempfile
tempfile | Python Standard Library – Real Python
The Python tempfile module provides a simple way to create temporary files and directories.
Find elsewhere
🌐
Python.org
discuss.python.org › ideas
tempfile.TemporaryDirectory().name should return pathlib.Path instead of str
December 11, 2023 - I’m curious how you’re using it to actually end up with a TemporaryDirectory object. I always use: · The TemporaryDirectory object never gets named, I just get the string tmpdir. If I need a Path, I use Path(tmpdir) · I suppose if I wrote x = TemporaryDirectory(), then Path(x.name) is ...
🌐
Geek Python
geekpython.in › tempfile-in-python
How To Use tempfile To Create Temporary Files and Directories in Python
August 15, 2023 - When we create a temporary directory using the with statement the name of the directory will be assigned to the target of the as clause. To get the name of the temporary directory, we passed tempdir rather than tempdir.name in the print statement.
🌐
Reddit
reddit.com › r/developersindia › how to use tempfile to create temporary files and directories in python
r/developersIndia on Reddit: How To Use tempfile To Create Temporary Files and Directories in Python
March 12, 2023 -

Python has a rich collection of standard libraries to carry out various tasks. In Python, there is a module called tempfile that allows us to create and manipulate temporary files and directories.

We can use tempfile to create temporary files and directories for storing temporary data during the program execution. The module has functions that allow us to create, read, and manipulate temporary files and directories.

The tempfile module includes a function called TemporaryFile() that allows us to create a temporary file for use as temporary storage.

Here the guide to generate and manipulate the temporary files using the tempfile module👇👇

Generate And Manipulate Temporary Files and Directories in Python

🌐
Medium
siddharth1.medium.com › temp-files-and-tempfile-module-in-python-with-examples-570b4ee96a38
temp files and tempfile module in python(with examples) | by Siddharth Kshirsagar | Medium
August 6, 2020 - temp files and tempfile module in python(with examples) First lets understand what a temp or temporary file is. A temporary file is created by a program that serves a temporary purpose and is created …
🌐
Ohidur
ohidur.com › blog › how-to-create-temporary-directories-and-files-in-python
How to Create Temporary Directories and Files in Python - Ohidur
The tempfile module is a powerful tool for managing temporary files and directories, ensuring efficient and clean handling of temporary data. By utilizing functions like TemporaryDirectory(), you can safely create and dispose of temporary storage in your Python applications.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-tempfile-module
Python tempfile module - GeeksforGeeks
December 11, 2020 - Tempfile is a Python module used in a situation, where we need to read multiple files, change or access the data in the file, and gives output files based on the result of processed data.
🌐
7-Zip Documentation
documentation.help › Python-2.6 › tempfile.html
tempfile — Generate temporary files and directories - Python 2.6 Documentation
If tempdir is unset or None at any call to any of the above functions, Python searches a standard list of directories and sets tempdir to the first one which the calling user can create files in.
🌐
Medium
byteshiva.medium.com › hands-on-guide-to-managing-temporary-directories-in-python-3-12-6ea5d7d65670
Hands-On Guide to Managing Temporary Directories in Python 3.12 | by Siva | Medium
May 27, 2024 - In this hands-on guide, we’ll explore how to effectively manage temporary directories in Python 3.12 using the tempfile module. We’ll walk through a practical example where we create temporary directories, perform operations within them, and ensure automatic cleanup.
🌐
Java2Blog
java2blog.com › home › python › python file › get temp directory in python
Get Temp Directory in Python - Java2Blog
November 16, 2022 - Then, we demonstrated the use of the gettempdir() and gettempdirb() functions to get temp directory in Python in different formats. We discussed the precedence of rules to determine the same and how they both use the global variable tempdir to return the value.
🌐
pytest
docs.pytest.org › en › stable › how-to › tmp_path.html
How to use temporary directories and files in tests - pytest documentation
You can use the tmp_path fixture which will provide a temporary directory unique to each test function · tmp_path is a pathlib.Path object. Here is an example test usage:
🌐
GitHub
github.com › openai › codex › discussions › 1174
Codex CLI is Going Native · openai/codex · Discussion #1174
We're planning to continue merging bugfixes in the TypeScript implementation, while we get the Rust implementation to experience and feature parity in the coming weeks. We'll share more updates soon on how TypeScript and other languages like Python will fit in long-term into this project.
Author   openai
🌐
Astral
docs.astral.sh › uv › reference › cli
Commands | uv
December 8, 2025 - uv is an extremely fast Python package and project manager, written in Rust.
🌐
Developpez
python.developpez.com › cours › docs.python.org › 3.0 › library › tempfile.php
Documentation officielle de Python
If tempdir is unset or None at any call to any of the above functions, Python searches a standard list of directories and sets tempdir to the first one which the calling user can create files in.