On this page
tempfile.TemporaryFile
Prev: tempfile.TemporaryDirectory | Up: tempfile
Rel: tempfile.gettempdir, tempfile.gettempdirb, tempfile.gettempprefix, tempfile.gettempprefixb
Ref: docs.python.org — tempfile.TemporaryFile
Tags: #public #python3 #stdlib #tempfile
Import
import tempfile
from tempfile import TemporaryFile
Signature & Overview
tempfile.TemporaryFile(mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None)
Create and return a temporary file.
Usage
import tempfile
with tempfile.TemporaryFile("w+b") as f:
f.write(b"scratch bytes")
f.seek(0)
assert f.read() == b"scratch bytes"
Gotchas
- Unlinked on Unix: The file has no visible directory entry on Unix and is reclaimed automatically by the OS when closed—even if the process crashes.
- Use
NamedTemporaryFileif you need a path:TemporaryFilecannot be passed by filename to an external command.
REPL Inspect
from tempfile import TemporaryFile
help(TemporaryFile)
Linked from
Related notes
- tuts/python3/stdlib_module/tempfile/TemporaryDirectory 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/mkstemp shared tags: #stdlib, #tempfile
- tuts/python3/stdlib_module/tempfile/mktemp shared tags: #stdlib, #tempfile
- tuts/python3/stdlib_module/tempfile/NamedTemporaryFile shared tags: #stdlib, #tempfile