tempfile.TemporaryFile()
tempfile.TemporaryFile() is a Python function in the Tempfile Module category. Creates a temporary file that is automatically deleted when closed. The syntax is tempfile.TemporaryFile(mode="w+b", buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None). Returns: File-like object. A typical example: import tempfile
with tempfile.TemporaryFile() as f:
f.write(b"temporary data")
f.seek(0)
print(f.read()) # b"temporary data"
# File deleted after with block. A close sibling is tempfile.NamedTemporaryFile(), which creates a temporary file with a visible name in the filesystem. 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.NamedTemporaryFile(), tempfile.mkstemp(), tempfile.mkdtemp(), tempfile.TemporaryDirectory(), tempfile.gettempdir(), tempfile.tempdir.