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

open

Prev: oct | Up: __builtins__ | Next: ord
Rel: breakpoint, compile, eval, exec, globals
Ref: docs.python.org — open
Tags: #public #python3 #builtins


Import & Availability

# Built-in (available globally without import)
from builtins import open

Overview & Signature

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

Open file and return a stream. Raise OSError upon failure.

Usage

from pathlib import Path
import tempfile

with tempfile.TemporaryDirectory() as tmp:
    p = Path(tmp) / "hello.txt"
    with open(p, "w", encoding="utf-8") as f:
        f.write("line 1\n")
    with open(p, "r", encoding="utf-8") as f:
        assert f.read() == "line 1\n"

Gotchas

REPL Inspect

help(open)

Linked from

Related notes