On this page
tempfile.mkdtemp
Prev: tempfile.gettempprefixb | Up: tempfile | Next: tempfile.mkstemp
Rel: tempfile.gettempdir, tempfile.gettempdirb, tempfile.gettempprefix, tempfile.gettempprefixb
Ref: docs.python.org — tempfile.mkdtemp
Tags: #public #python3 #stdlib #tempfile
Import
import tempfile
from tempfile import mkdtemp
Signature & Overview
tempfile.mkdtemp(suffix=None, prefix=None, dir=None)
User-callable function to create and return a unique temporary directory. The return value is the pathname of the directory.
Usage
from pathlib import Path
import shutil
import tempfile
d = Path(tempfile.mkdtemp(prefix="work_"))
try:
assert d.is_dir()
finally:
shutil.rmtree(d)
Gotchas
- Caller is responsible for cleanup: Unlike
TemporaryDirectory(),mkdtemp()does not auto-delete—preferwith tempfile.TemporaryDirectory():whenever possible. - Owner-only permissions (
0700): Safe against symlink/race attacks.
REPL Inspect
from tempfile import mkdtemp
help(mkdtemp)
Linked from
Related notes
- tuts/python3/stdlib_module/tempfile/gettempdir Linked here; links to this page; shared tags: #stdlib, #tempfile
- tuts/python3/stdlib_module/tempfile/gettempdirb Linked here; links to this page; shared tags: #stdlib, #tempfile
- tuts/python3/stdlib_module/tempfile/gettempprefix Linked here; links to this page; shared tags: #stdlib, #tempfile
- tuts/python3/stdlib_module/tempfile/gettempprefixb Linked here; links to this page; shared tags: #stdlib, #tempfile
- tuts/python3/stdlib_module/tempfile/mkstemp Linked here; links to this page; shared tags: #stdlib, #tempfile
- tuts/python3/stdlib_module/tempfile Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tempfile/mktemp shared tags: #stdlib, #tempfile
- tuts/python3/stdlib_module/tempfile/NamedTemporaryFile shared tags: #stdlib, #tempfile
- tuts/python3/stdlib_module/tempfile/SpooledTemporaryFile shared tags: #stdlib, #tempfile
- tuts/python3/stdlib_module/tempfile/TemporaryDirectory shared tags: #stdlib, #tempfile