posixThe most common POSIX system calls最常见的POSIX系统调用


This module provides access to operating system functionality that is standardized by the C Standard and the POSIX standard (a thinly disguised Unix interface).该模块提供了对C标准和POSIX标准(一种薄薄的Unix接口)标准化的操作系统功能的访问。

Do not import this module directly.请勿直接导入此模块。 Instead, import the module os, which provides a portable version of this interface. 相反,导入模块os,它提供了该接口的可移植版本。On Unix, the os module provides a superset of the posix interface. 在Unix上,os模块提供了posix接口的超集。On non-Unix operating systems the posix module is not available, but a subset is always available through the os interface. 在非Unix操作系统中,posix模块不可用,但通过操作系统接口始终可以使用子集。Once os is imported, there is no performance penalty in using it instead of posix. 一旦导入os,使用它而不是posix就不会对性能造成影响。In addition, os provides some additional functionality, such as automatically calling putenv() when an entry in os.environ is changed.此外,os还提供了一些额外的功能,例如当os.environ中的一个条目发生更改时自动调用putenv()

Errors are reported as exceptions; the usual exceptions are given for type errors, while errors reported by the system calls raise OSError.错误报告为异常;通常的异常是针对类型错误给出的,而系统调用报告的错误会引发OSError

Large File Support大文件支持

Several operating systems (including AIX, HP-UX and Solaris) provide support for files that are larger than 2 GiB from a C programming model where int and long are 32-bit values. 一些操作系统(包括AIX、HP-UX和Solaris)支持C编程模型中大于2GiB的文件,其中intlong是32位值。This is typically accomplished by defining the relevant size and offset types as 64-bit values. 这通常是通过将相关的大小和偏移类型定义为64位值来实现的。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 is at least as large as an off_t. off_t的大小大于long并且long long至少与off_t一样大时,Python中启用了大文件支持。It may be necessary to configure and compile Python with certain compiler flags to enable this mode. 可能需要使用某些编译器标志来配置和编译Python以启用此模式。For example, with Solaris 2.6 and 2.7 you need to do something like:例如,对于Solaris 2.6和2.7,您需要执行以下操作:

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

On large-file-capable Linux systems, this might work:在支持大型文件的Linux系统上,这可能会起作用:

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

Notable Module Contents值得注意的模块内容

In addition to many functions described in the os module documentation, posix defines the following data item:除了os模块文档中描述的许多功能外,posix还定义了以下数据项:

posix.environ

A dictionary representing the string environment at the time the interpreter was started. 表示解释器启动时的字符串环境的字典。Keys and values are bytes on Unix and str on Windows. 键和值在Unix上是字节,在Windows上是str。For example, environ[b'HOME'] (environ['HOME'] on Windows) is the pathname of your home directory, equivalent to getenv("HOME") in C.例如,environ[b'HOME'](在Windows上为environ['HOME'])是主目录的路径名,相当于C中的getenv("HOME")

Modifying this dictionary does not affect the string environment passed on by execv(), popen() or system(); if you need to change the environment, pass environ to execve() or add variable assignments and export statements to the command string for system() or popen().修改此字典不会影响execv()popen()system()传递的字符串环境;如果需要更改环境,请将environ传递给execve(),或者将变量赋值和导出语句添加到system()popen()的命令字符串中。

Changed in version 3.2:版本3.2中更改: On Unix, keys and values are bytes.在Unix上,键和值是字节。

Note

The os module provides an alternate implementation of environ which updates the environment on modification. os模块提供了environ的替代实现,该实现在修改时更新环境。Note also that updating os.environ will render this dictionary obsolete. 还要注意的是,更新os.environ会使该字典过时。Use of the os module version of this is recommended over direct access to the posix module.建议使用os模块版本,而不是直接访问posix模块。