Python 3.3+Linux · macOSosfunction
os.readv
os — positional I/O
Read from a descriptor into multiple buffers (scatter read). Availability: Unix.
Syntax
os.readv(...)
It fills the given mutable buffers in order — say, one bytearray for a fixed-size header and another for the body — and returns the total bytes read. The buffers are reused, so no extra copies are made. If there is less data than room, the later buffers stay partly filled.
Examples
import os
fd = os.open('readv-demo.bin', os.O_RDWR | os.O_CREAT | os.O_TRUNC) # временный файл для опытов
print(os.pwrite(fd, b'HEADERBODY', 0)) # → 10 — подготовили данные, позиция fd осталась в начале
bufs = [bytearray(6), bytearray(4)] # scatter-чтение: заголовок и тело в разные буферы
print(os.readv(fd, bufs), bytes(bufs[0]), bytes(bufs[1])) # → ? только Unix: 10 b'HEADER' b'BODY'
print(os.readv(fd, [bytearray(4)])) # → 0 — данные кончились: readv читает с текущей позиции и сдвигает её
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy