36.1. posix
— 最常见的 POSIX 系统调用¶
此模块提供了对基于 C 标准和 POSIX 标准(一种稍加修改的 Unix 接口)进行标准化的系统功能的访问。
请勿直接导入此模块。 而应导入 os
模块,它提供了此接口的 可移植 版本。 在 Unix 上,os
模块提供了 posix
接口的一个超集。 在非 Unix 操作系统上 posix
模块将不可用,但会通过 os
接口提供它的一个可用子集。 一旦导入了 os
,用它替代 posix
时就 没有 性能惩罚。 此外,os
还提供了一些附加功能,例如在 os.environ
中的某个条目被修改时会自动调用 putenv()
。
错误将作为异常被报告;对于类型错误会给出普通异常,而系统调用所报告的异常则会引发 OSError
。
36.1.1. 大文件支持¶
Several operating systems (including AIX, HP-UX, Irix and Solaris) provide
support for files that are larger than 2 GB from a C programming model where
int
and long
are 32-bit values. This is typically accomplished
by defining the relevant size and offset types as 64-bit values. Such files are
sometimes referred to as large files.
Large file support is enabled in Python when the size of an off_t
is
larger than a long
and the long long
type is available and is
at least as large as an off_t
. Python longs are then used to represent
file sizes, offsets and other values that can exceed the range of a Python int.
It may be necessary to configure and compile Python with certain compiler flags
to enable this mode. For example, it is enabled by default with recent versions
of Irix, but with Solaris 2.6 and 2.7 you need to do something like:
CFLAGS="`getconf LFS_CFLAGS`" OPT="-g -O2 $CFLAGS" \
./configure
在支持大文件的 Linux 系统中,可以这样做:
CFLAGS='-D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64' OPT="-g -O2 $CFLAGS" \
./configure
36.1.2. 重要的模块内容¶
除了 os
模块文档已说明的许多函数,posix
还定义了下列数据项:
-
posix.
environ
¶ A dictionary representing the string environment at the time the interpreter was started. For example,
environ['HOME']
is the pathname of your home directory, equivalent togetenv("HOME")
in C.修改此字典不会影响由
execv()
,popen()
或system()
所传入的字符串环境;如果你需要修改环境,请将environ
传给execve()
或者为system()
或popen()
的命令字符串添加变量赋值和 export 语句。