tempfile.mkstemp()
tempfile.mkstemp() is a Python function in the Tempfile Module category. Creates a temporary file and returns a (fd, path) tuple. Caller must close and delete. The syntax is tempfile.mkstemp(suffix=None, prefix=None, dir=None, text=False). Returns: (fd, pathname) tuple. A typical example: import os, tempfile
fd, path = tempfile.mkstemp()
os.write(fd, b"data")
os.close(fd)
os.remove(path). A close sibling is tempfile.TemporaryFile(), which creates a temporary file that is automatically deleted when closed. A close sibling is tempfile.NamedTemporaryFile(), which creates a temporary file with a visible name in the filesystem. A close sibling is tempfile.mkdtemp(), which creates a temporary directory that persists after creation. A close sibling is tempfile.TemporaryDirectory(), which creates a temporary directory that is automatically cleaned up when closed. More about this category: Temporary file and directory creation — TemporaryFile, mkstemp, mkdtemp. Related Tempfile Module entries: tempfile.TemporaryFile(), tempfile.NamedTemporaryFile(), tempfile.mkdtemp(), tempfile.TemporaryDirectory(), tempfile.gettempdir(), tempfile.tempdir. This page is part of the free Python Reference documentation covering 978+ functions, methods, and modules with examples and parameter details.