On this page
io.open
Prev: io.IOBase | Up: io | Next: io.open_code
Rel: io.BlockingIOError, io.BufferedIOBase, io.BufferedRandom, io.BufferedReader
Ref: docs.python.org — io.open
Tags: #public #python3 #stdlib #io
Import
import io
from io import open
Signature & Overview
io.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
import io
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
p = Path(tmp) / "note.txt"
with io.open(p, "w", encoding="utf-8") as f:
f.write("hello")
assert p.read_text(encoding="utf-8") == "hello"
Gotchas
- Identical to built-in
open()in Python 3:io.open is openisTrue; always specifyencoding="utf-8"for text files.
REPL Inspect
from io import open
help(open)
Linked from
Related notes
- tuts/python3/stdlib_module/io/IOBase Linked here; links to this page; shared tags: #io, #stdlib
- tuts/python3/stdlib_module/io/open_code 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