On this page
tempfile.mktemp
Prev: tempfile.mkstemp | Up: tempfile | Next: tempfile.NamedTemporaryFile
Rel: tempfile.gettempdir, tempfile.gettempdirb, tempfile.gettempprefix, tempfile.gettempprefixb
Ref: docs.python.org — tempfile.mktemp
Tags: #public #python3 #stdlib #tempfile
Import
import tempfile
from tempfile import mktemp
Signature & Overview
tempfile.mktemp(suffix='', prefix='tmp', dir=None)
User-callable function to return a unique temporary file name. The file is not created.
Usage
import tempfile
# Legacy/insecure: prefer NamedTemporaryFile or TemporaryDirectory instead
with tempfile.NamedTemporaryFile(suffix=".txt") as f:
assert f.name.endswith(".txt")
Gotchas
- Deprecated & insecure (TOCTOU race condition):
mktemp()only generates a filename without creating the file, allowing an attacker to create a symlink at that path before you open it. - Always replace with
NamedTemporaryFile,mkstemp, orTemporaryDirectory.
REPL Inspect
from tempfile import mktemp
help(mktemp)
Linked from
Related notes
- tuts/python3/stdlib_module/tempfile/mkstemp Linked here; links to this page; shared tags: #stdlib, #tempfile
- tuts/python3/stdlib_module/tempfile/NamedTemporaryFile 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/gettempdir Linked here; shared tags: #stdlib, #tempfile
- tuts/python3/stdlib_module/tempfile/gettempdirb Linked here; shared tags: #stdlib, #tempfile
- tuts/python3/stdlib_module/tempfile/gettempprefix Linked here; shared tags: #stdlib, #tempfile
- tuts/python3/stdlib_module/tempfile/gettempprefixb Linked here; shared tags: #stdlib, #tempfile
- tuts/python3/stdlib_module/tempfile/mkdtemp shared tags: #stdlib, #tempfile
- tuts/python3/stdlib_module/tempfile/SpooledTemporaryFile shared tags: #stdlib, #tempfile
- tuts/python3/stdlib_module/tempfile/TemporaryDirectory shared tags: #stdlib, #tempfile