posix --- 最常见的 POSIX 系统调用


此模块提供了对基于 C 标准和 POSIX 标准(一种稍加修改的 Unix 接口)进行标准化的系统功能的访问。

Availability: Unix.

Do not import this module directly. Instead, import the module os, which provides a portable version of this interface. On Unix, the os module provides a superset of the posix interface. On non-Unix operating systems the posix module is not available, but a subset is always available through the os interface. Once os is imported, there is no performance penalty in using it instead of posix. In addition, os provides some additional functionality, such as automatically calling putenv() when an entry in os.environ is changed.

错误将作为异常被报告;对于类型错误会给出普通异常,而系统调用所报告的异常则会引发 OSError

大文件支持

某些操作系统(包括 AIX 和 Solaris)可对 intlong 为 32 位值的 C 编程模型提供大于 2 GiB 文件的支持。 这在通常情况下是以将相关数据的大小和偏移量类型定义为 64 位值的方式来实现的。 这样的文件有时被称为 大文件

Python 中的大文件支持会在 off_t 的大小超过 longlong long 的大小至少与 off_t 一样时被启用。 要启用此模式可能必须在启用特定编译旗标的情况下配置和编译 Python。 例如,在 Solaris 2.6 和 2.7 中你需要执行这样的操作:

CFLAGS="`getconf LFS_CFLAGS`" OPT="-g -O2 $CFLAGS" \
        ./configure

在支持大文件的 Linux 系统中,可以这样做:

CFLAGS='-D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64' OPT="-g -O2 $CFLAGS" \
        ./configure

重要的模块内容

In addition to many functions described in the os module documentation, posix defines the following data item:

posix.environ

一个表示解释器启动时间点的字符串环境的字典。 键和值的类型在Unix 上为 bytes 而在 Windows 上为 str。 例如,environ[b'HOME'] (Windows 上的 environ['HOME']) 是你的家目录的路径名,等价于 C 中的 getenv("HOME")

修改此字典不会影响由 execv(), popen()system() 所传入的字符串环境;如果你需要修改环境,请将 environ 传给 execve() 或者为 system()popen() 的命令字符串添加变量赋值和 export 语句。

在 3.2 版本发生变更: 在 Unix 上,键和值为 bytes 类型。

备注

The os module provides an alternate implementation of environ which updates the environment on modification. Note also that updating os.environ will render this dictionary obsolete. Use of the os module version of this is recommended over direct access to the posix module.