Python os.mknod() Method

Python File(文件) 方法Python OS File/Directory Methods


Overview

The os.mknod() method is used to create a filesystem node with a specified filename (file, device special file, or named pipe).

Syntax

mknod()The method syntax is as follows:

os.mknod(filename[, mode=0600[, device=0]])

Parameters

  • filename-- the filesystem node to be created

  • mode-- mode specifies the permissions to use when creating or using the node, combined (or bitwise) with stat.S_IFREG, stat.S_IFCHR, stat.S_IFBLK, and stat.S_IFIFO (these constants are in the stat module). For stat.S_IFCHR and stat.S_IFBLK, device defines the newly created device special file (possibly using os.makedev()); all others are ignored.

  • device-- optional, specifies the device of the created file

Return Value

This method has no return value.

Example

The following example demonstrates the use of the mknod() method:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os
import stat

filename = '/tmp/tmpfile'
mode = 0600|stat.S_IRUSR

# 文件系统节点指定不同模式
os.mknod(filename, mode)

The output after executing the above program is:

-rw-------. 1 root   root         0 Apr 30 02:38 tmpfile

Python File(文件) 方法Python OS File/Directory Methods

Other Extensions