Python Glossary Русский

Python 3.8+Linuxosfunction

os.copy_file_range

os — efficient copying

Copy a range of bytes between files in the kernel (3.8+). Availability: Linux.

Syntax

os.copy_file_range(...)

It copies data inside the kernel, and on filesystems with reflink support such as Btrfs and XFS it may share blocks instead of copying them. Since Python 3.14, shutil.copyfile on Linux uses it automatically when possible. It may copy less than requested — call it in a loop until it returns zero.

Examples

import os
src = os.open('copy_src.txt', os.O_RDWR | os.O_CREAT | os.O_TRUNC)   # файл-источник в текущем каталоге
print(os.write(src, b'hello world!'))   # → 12 — записали 12 байт без буферизации Python
dst = os.open('copy_dst.txt', os.O_WRONLY | os.O_CREAT | os.O_TRUNC)   # файл-приёмник в текущем каталоге
print(os.copy_file_range(src, dst, 12, offset_src=0))   # → 12 — ядро скопировало байты, минуя буфер Python
print(os.copy_file_range(src, dst, 12, offset_src=12))   # → 0 — за концом файла копировать нечего

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy