On this page
  1. tempfile.NamedTemporaryFile
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

tempfile.NamedTemporaryFile

Prev: tempfile.mktemp | Up: tempfile | Next: tempfile.SpooledTemporaryFile
Rel: tempfile.gettempdir, tempfile.gettempdirb, tempfile.gettempprefix, tempfile.gettempprefixb
Ref: docs.python.org — tempfile.NamedTemporaryFile
Tags: #public #python3 #stdlib #tempfile


Import

import tempfile
from tempfile import NamedTemporaryFile

Signature & Overview

tempfile.NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True, *, errors=None, delete_on_close=True)

Create and return a temporary file.

Usage

from pathlib import Path
import tempfile

with tempfile.NamedTemporaryFile("w+", suffix=".txt", encoding="utf-8") as f:
    f.write("hello")
    f.flush()
    assert Path(f.name).read_text(encoding="utf-8") == "hello"

Gotchas

REPL Inspect

from tempfile import NamedTemporaryFile
help(NamedTemporaryFile)

Linked from

Related notes