Python 3.14+osfunction
os.readinto()
os — file descriptors
Reads from a file descriptor into a pre-allocated mutable buffer (bytearray/memoryview) and returns the number of bytes read, without creating a new bytes object. Python 3.14+.
Syntax
os.readinto(fd, buffer, /)
It writes data straight into the given bytearray or memoryview and returns the byte count — no new bytes object per read. That saves memory and time in loops over large streams, especially with a memoryview slice over the free part of the buffer. Like os.read it may fill less than the buffer; 0 means end of file.
Examples
import os
r, w = os.pipe()
os.write(w, b'hello world')
buf = bytearray(5)
print(os.readinto(r, buf)) # → 5 (сколько байт реально прочитано)
print(bytes(buf)) # → b'hello'
print(os.readinto(r, bytearray(0))) # → 0 (буфер нулевой длины)
os.close(r); os.close(w)
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy