xxxxxxxxxx
import tempfile
with tempfile.TemporaryDirectory() as tmpdirname:
print('Created temporary directory:', tmpdirname)
# Both the directory and its contents have been deleted
xxxxxxxxxx
Use the mkdtemp() function from the tempfile module:
import tempfile
import shutil
dirpath = tempfile.mkdtemp()
# ... do stuff with dirpath
shutil.rmtree(dirpath)
xxxxxxxxxx
import tempfile
with tempfile.TemporaryDirectory() as tmpdirname:
print('created temporary directory', tmpdirname)
# directory and contents have been removed