Python 3.8+Linuxosfunction
os.memfd_create
os — special descriptors
Create an anonymous in-memory file and return its descriptor (3.8+). Availability: Linux.
Syntax
os.memfd_create(...)
It creates a file that lives only in memory and disappears once every descriptor is closed; nothing hits the disk. The name is for debugging only and need not be unique. The descriptor can be handed to another process or used with mmap — handy for sharing data without temporary files.
Examples
import os
fd = os.memfd_create('buf') # → ? только Linux: дескриптор анонимного файла в памяти, например 3
print(os.write(fd, b'hello')) # → 5
print(os.pread(fd, 5, 0)) # → b'hello' — читаем с нулевого смещения, не двигая курсор
print(os.fstat(fd).st_size) # → 5 — ведёт себя как обычный файл, но живёт только в памяти
os.close(fd) # → ? только Linux: буфер освобождён; пути в файловой системе у memfd нет, на диске следов не остаётся
See also
Open in the full glossarydocs.python.orgReport an inaccuracy