Python – How to create a temporary directory and get its path/ file name

pythontemporary-directorytemporary-files

How can I create a temporary directory and get its path/file name in Python?

Best Answer

Use the mkdtemp() function from the tempfile module:

import tempfile
import shutil

dirpath = tempfile.mkdtemp()
# ... do stuff with dirpath
shutil.rmtree(dirpath)