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...
๐ŸŒ
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....
๐ŸŒ
Python Module of the Week
pymotw.com โ€บ 2 โ€บ tempfile
tempfile โ€“ Create temporary filesystem resources. - Python Module of the Week
import tempfile tempfile.tempdir = '/I/changed/this/path' print 'gettempdir():', tempfile.gettempdir() $ python tempfile_tempdir.py gettempdir(): /I/changed/this/path
๐ŸŒ
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).
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ the-python-tempfile-module
The Python tempfile Module
July 20, 2023 - import tempfile print("Current temp directory:", tempfile.gettempdir()) tempfile.tempdir = "/temp" print("Temp directory after change:", tempfile.gettempdir()) If you will run the above program, it will print an output similar to the following: $ python3 dir-loc-temp-file.py Current temp directory: /var/folders/tp/pn3dvz_n7cj7nfs0y2szsk9h0000gn/T Temp directory after change: /temp
๐ŸŒ
Safjan
safjan.com โ€บ home โ€บ note โ€บ mastering temporary files and directories with...
Mastering Temporary Files and Directories with Python's tempfile Module
July 12, 2023 - Python's tempfile module is an incredibly powerful tool that allows you to create and manage temporary files and directories with ease.
๐ŸŒ
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 ...
๐ŸŒ
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...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ create-temporary-files-and-directories-using-python-tempfile
Create temporary files and directories using tempfile - GeeksforGeeks
January 18, 2024 - We can set the location where the files are stored by setting the tempdir attribute. The location can be fetched using gettempdir() method. When we create a temporary file or directory, Python searches in a standard list of directories to find ...
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Lbl
davis.lbl.gov โ€บ Manuals โ€บ PYTHON โ€บ library โ€บ tempfile.html
10.6. tempfile โ€” Generate temporary files and directories โ€” Python v2.6.6 documentation
August 24, 2010 - Return the directory currently selected to create temporary files in. If tempdir is not None, this simply returns its contents; otherwise, the search described above is performed, and the result returned.
๐ŸŒ
Jython
jython.org โ€บ jython-old-sites โ€บ docs โ€บ library โ€บ tempfile.html
10.5. tempfile โ€” Generate temporary files and directories โ€” Jython v2.5.2 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.
๐ŸŒ
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:
๐ŸŒ
Medium
medium.com โ€บ @ashikranjan โ€บ creating-temporary-file-and-directory-using-python-tempfile-module-68db55a39cdd
Creating temporary file and directory using python tempfile module | by Ashik Ranjan | Medium
July 12, 2022 - To handle such scenarios python has an inbuilt module named tempfile to be used to create a unique temporary file and directory that gets deleted automatically as soon as they are closed.