posix —- The most common POSIX system calls
This module provides access to operating system functionality that isstandardized by the C Standard and the POSIX standard (a thinly disguised Unixinterface).
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 operatingsystems the posix
module is not available, but a subset is alwaysavailable through the os
interface. Once os
is imported, there isno performance penalty in using it instead of posix
. In addition,os
provides some additional functionality, such as automatically callingputenv()
when an entry in os.environ
is changed.
Errors are reported as exceptions; the usual exceptions are given for typeerrors, while errors reported by the system calls raise OSError
.
Large File Support
Several operating systems (including AIX, HP-UX, Irix and Solaris) providesupport for files that are larger than 2 GiB from a C programming model whereint
and long
are 32-bit values. This is typically accomplishedby defining the relevant size and offset types as 64-bit values. Such files aresometimes referred to as large files.
Large file support is enabled in Python when the size of an off_t
islarger than a long
and the long long
is at least as largeas an off_t
.It may be necessary to configure and compile Python with certain compiler flagsto enable this mode. For example, it is enabled by default with recent versionsof Irix, but with Solaris 2.6 and 2.7 you need to do something like:
- CFLAGS="`getconf LFS_CFLAGS`" OPT="-g -O2 $CFLAGS" \
- ./configure
On large-file-capable Linux systems, this might work:
- 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:
posix.
environ
- A dictionary representing the string environment at the time the interpreterwas started. Keys and values are bytes on Unix and str on Windows. Forexample,
environ[b'HOME']
(environ['HOME']
on Windows) is thepathname of your home directory, equivalent togetenv("HOME")
in C.
Modifying this dictionary does not affect the string environment passed on byexecv()
, popen()
or system()
; if you need tochange the environment, pass environ
to execve()
or addvariable assignments and export statements to the command string forsystem()
or popen()
.
在 3.2 版更改: On Unix, keys and values are bytes.
注解
The os
module provides an alternate implementation of environ
which updates the environment on modification. Note also that updatingos.environ
will render this dictionary obsolete. Use of theos
module version of this is recommended over direct access to theposix
module.