How to create a temporary directory and get the path / file name in Python

duhhunjonn picture duhhunjonn · Jul 11, 2010 · Viewed 106k times · Source

how to create a temporary directory and get the path / file name in python

Answer

Philipp picture Philipp · Jul 11, 2010

Use the mkdtemp() function from the tempfile module:

import tempfile
import shutil

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