On this page
typing.BinaryIO
Prev: typing.Awaitable | Up: typing | Next: typing.ByteString
Rel: typing.AbstractSet, typing.Annotated, typing.Any, typing.assert_never
Ref: docs.python.org — typing.BinaryIO
Tags: #public #python3 #stdlib #typing
Import
import typing
from typing import BinaryIO
Signature & Overview
typing.BinaryIO()
Typed approximation of the return of open() in binary mode.
Methods & Attributes
BinaryIO.close(self) -> None—BinaryIO.closemethod/property.BinaryIO.closed(...)—BinaryIO.closedmethod/property.BinaryIO.fileno(self) -> int—BinaryIO.filenomethod/property.BinaryIO.flush(self) -> None—BinaryIO.flushmethod/property.BinaryIO.isatty(self) -> bool—BinaryIO.isattymethod/property.BinaryIO.mode(...)—BinaryIO.modemethod/property.BinaryIO.name(...)—BinaryIO.namemethod/property.BinaryIO.read(self, n: int = -1) -> ~AnyStr—BinaryIO.readmethod/property.BinaryIO.readable(self) -> bool—BinaryIO.readablemethod/property.BinaryIO.readline(self, limit: int = -1) -> ~AnyStr—BinaryIO.readlinemethod/property.BinaryIO.readlines(self, hint: int = -1) -> list[~AnyStr]—BinaryIO.readlinesmethod/property.BinaryIO.seek(self, offset: int, whence: int = 0) -> int—BinaryIO.seekmethod/property.BinaryIO.seekable(self) -> bool—BinaryIO.seekablemethod/property.BinaryIO.tell(self) -> int—BinaryIO.tellmethod/property.BinaryIO.truncate(self, size: int | None = None) -> int—BinaryIO.truncatemethod/property.BinaryIO.writable(self) -> bool—BinaryIO.writablemethod/property.BinaryIO.write(self, s: bytes | bytearray) -> int—BinaryIO.writemethod/property.BinaryIO.writelines(self, lines: list[~AnyStr]) -> None—BinaryIO.writelinesmethod/property.
Usage
import io
from typing import BinaryIO
def read_header(fh: BinaryIO) -> bytes:
return fh.read(4)
assert read_header(io.BytesIO(b"PNG\r\n")) == b"PNG\r"
Gotchas
- For binary file-like objects (
bytes): UseTextIOforstrstreams, or a small customProtocolif you only need.read().
REPL Inspect
from typing import BinaryIO
help(BinaryIO)
Linked from
Related notes
- tuts/python3/stdlib_module/typing/Awaitable Linked here; links to this page; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/ByteString Linked here; links to this page; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/typing/AbstractSet Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/Annotated Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/Any Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/assert_never Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/assert_type shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/AsyncContextManager shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/AsyncGenerator shared tags: #stdlib, #typing