Python 3.3+Linux · macOSosfunction
os.pread
os — positional I/O
Read n bytes from a descriptor at a given offset without moving the position. Availability: Unix.
Syntax
os.pread(...)
It leaves the shared descriptor position untouched, so several threads can read different parts of one file without locking around seek plus read. Like os.read it may return fewer bytes than requested; an empty result means the offset is past the end. It is not available on Windows.
Examples
import os
fd = os.open('pread_demo.txt', os.O_RDWR | os.O_CREAT) # временный файл только для примера
print(os.write(fd, b'Hello, world!')) # → 13 — записали 13 байт, текущее смещение стало 13
print(os.pread(fd, 5, 0)) # → b'Hello' — читаем 5 байт с позиции 0
print(os.lseek(fd, 0, os.SEEK_CUR)) # → 13 — pread не сдвинул текущее смещение файла, в отличие от os.read
print(os.pread(fd, 5, 1000)) # → b'' — чтение за концом файла возвращает пустые байты, а не ошибку
os.close(fd) # закрываем дескриптор
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy