Python Glossary Русский

Python 3.3+Linux · macOSosfunction

os.pwrite

os — positional I/O

Write bytes to a descriptor at a given offset without moving the position. Availability: Unix.

Syntax

os.pwrite(...)

It writes at the given offset without moving the shared position — handy for writing different regions of a file from several threads. If the file was opened with O_APPEND, Linux still appends the data at the end, ignoring the offset. It may write only part of the data — check the returned count.

Examples

import os
fd = os.open('pwrite-demo.bin', os.O_RDWR | os.O_CREAT | os.O_TRUNC)  # временный файл для опытов
print(os.pwrite(fd, b'0123456789', 0))  # → 10 — записано байт начиная со смещения 0
print(os.pwrite(fd, b'XY', 4))  # → 2 — точечная правка двух байтов в середине файла
print(os.pread(fd, 10, 0))  # → b'0123XY6789'
print(os.lseek(fd, 0, os.SEEK_CUR))  # → 0 — pwrite не двигает позицию fd, в отличие от os.write

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy