Python Glossary Русский

all Python 3Linux · macOSosfunction

os.mknod

os — special files

Create a filesystem node (device special file, FIFO, etc.). Availability: Unix.

Syntax

os.mknod(...)

Creating device files requires root; without it only regular files and pipes are possible. The node type is set by bits in mode (stat.S_IFCHR, stat.S_IFBLK), and the device number is built with os.makedev. For pipes os.mkfifo reads more clearly in everyday code.

Examples

import os, stat
os.mknod('plain.txt')   # → ? только Unix: создан пустой обычный файл с режимом по умолчанию 0o600
os.mknod('node.fifo', stat.S_IFIFO | 0o600)   # → ? только Unix: тип узла задаётся битами режима — здесь получился FIFO, как от os.mkfifo
print(stat.S_ISFIFO(os.stat('node.fifo').st_mode))   # → True
os.mknod('blockdev', stat.S_IFBLK | 0o600, os.makedev(8, 0))   # → ? только Unix и только под root: узел блочного устройства major=8, minor=0; без прав — PermissionError
os.mknod('node.fifo', stat.S_IFIFO | 0o600)   # → FileExistsError: [Errno 17] File exists: 'node.fifo'

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy