tempfile.NamedTemporaryFile()
tempfile.NamedTemporaryFile() is a Python function in the Tempfile Module category. Creates a temporary file with a visible name in the filesystem. The syntax is tempfile.NamedTemporaryFile(mode="w+b", buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True). Parameters: suffix, prefix, dir. Returns: File-like object with .name attribute. A typical example: with tempfile.NamedTemporaryFile(suffix=".txt") as f:
print(f.name) # "/tmp/tmpxyz.txt"
f.write(b"hello"). A close sibling is tempfile.TemporaryFile(), which creates a temporary file that is automatically deleted when closed. A close sibling is tempfile.mkstemp(), which creates a temporary file and returns a (fd, path) tuple. Caller must close and delete. 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.mkstemp(), tempfile.mkdtemp(), tempfile.TemporaryDirectory(), tempfile.gettempdir(), tempfile.tempdir.