Python Glossary Русский

all Python 3ioterm

io.BufferedRandom

binary streams

A buffered random-access stream (reading, writing and seek) over a raw stream, used for files opened in 'r+b' mode.

Syntax

io.BufferedRandom(raw)

You rarely construct this yourself: open(path, 'r+b') gives you one, and the mode matters more than the class name, since 'r+b' needs an existing file and leaves it intact while 'w+b' truncates it on open. Writes land in a buffer first, so until flush() or close() another process, or even another handle on the same file, still sees the old bytes; call flush() explicitly when the update has to be visible right away.

Examples

import io
print(issubclass(io.BufferedRandom, io.BufferedIOBase))   # → True
b = io.BufferedRandom(io.BytesIO(b'abcdef'))
print(b.read(3))   # → b'abc'
print(b.tell())   # → 3
print(b.seek(0))   # → 0
print(b.write(b'XY'))   # → 2
b.seek(0)
print(b.read())   # → b'XYcdef'
print(b.readable(), b.writable(), b.seekable())   # → True True True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy