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 moduleos,which provides aportable version of this interface. On Unix, theosmodule provides a superset of theposix interface. On non-Unix operatingsystems theposix module is not available, but a subset is alwaysavailable through theos interface. Onceos is imported, there isno performance penalty in using it instead ofposix. In addition,os provides some additional functionality, such as automatically callingputenv() when an entry inos.environ is changed.
Errors are reported as exceptions; the usual exceptions are given for typeerrors, while errors reported by the system calls raiseOSError.
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 andlong are 32-bit values. This is typically accomplishedby defining the relevant size and offset types as 64-bit values. Such files aresometimes referred to aslarge files.
Large file support is enabled in Python when the size of anoff_t islarger than along and thelonglong is at least as largeas anoff_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 theos 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 by
execv(),popen()orsystem(); if you need tochange the environment, passenvirontoexecve()or addvariable assignments and export statements to the command string forsystem()orpopen().Changed in version 3.2:On Unix, keys and values are bytes.
Note
The
osmodule provides an alternate implementation ofenvironwhich updates the environment on modification. Note also that updatingos.environwill render this dictionary obsolete. Use of theosmodule version of this is recommended over direct access to theposixmodule.