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

io.open_code

Prev: io.open | Up: io | Next: io.RawIOBase
Rel: io.BlockingIOError, io.BufferedIOBase, io.BufferedRandom, io.BufferedReader
Ref: docs.python.org — io.open_code
Tags: #public #python3 #stdlib #io


Import

import io
from io import open_code

Signature & Overview

io.open_code(path)

Opens the provided file with the intent to import the contents.

Usage

import io
from pathlib import Path
import tempfile

with tempfile.TemporaryDirectory() as tmp:
    p = Path(tmp) / "script.py"
    p.write_text("x = 1\n", encoding="utf-8")
    with io.open_code(str(p)) as f:
        assert f.read() == b"x = 1\n"

Gotchas

REPL Inspect

from io import open_code
help(open_code)

Linked from

Related notes