Starting from Python 3.2 your can use tmpfile.TemporaryDirectory
like this:
from tempfile import TemporaryDirectory with TemporaryDirectory(prefix="myapp-") as tmpdir: print(tmpdir) # e.g. "/tmp/myapp-fevhzh93" # Once you are outside the "with" block, the directory will be deleted!
In case you are stuck with using older Python versions, check out How to create temporary directory in Python.