On this page
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
- For opening Python source files to be executed/compiled: Opens in
"rb"mode and triggers theopenaudit hook so security/enterprise embedders can inspect code loads.
REPL Inspect
from io import open_code
help(open_code)
Linked from
Related notes
- tuts/python3/stdlib_module/io/open Linked here; links to this page; shared tags: #io, #stdlib
- tuts/python3/stdlib_module/io/RawIOBase Linked here; links to this page; shared tags: #io, #stdlib
- tuts/python3/stdlib_module/io Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/io/BlockingIOError Linked here; shared tags: #io, #stdlib
- tuts/python3/stdlib_module/io/BufferedIOBase Linked here; shared tags: #io, #stdlib
- tuts/python3/stdlib_module/io/BufferedRandom Linked here; shared tags: #io, #stdlib
- tuts/python3/stdlib_module/io/BufferedReader Linked here; shared tags: #io, #stdlib
- tuts/c/stdlib/stdio_and_file_io shared tags: #io, #stdlib
- tuts/cpp/stdlib/io_and_filesystem shared tags: #io, #stdlib
- tuts/cpp/stdlib/io_and_filesystem/format_and_print shared tags: #io, #stdlib