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).
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 is os.environ
is changed.
The descriptions below are very terse; refer to the corresponding Unix manual (or POSIX documentation) entry for more information. Arguments called path refer to a pathname given as a string.
Errors are reported as exceptions; the usual exceptions are given
for type errors, while errors reported by the system calls raise
posix.error
, described below.
Module posix
defines the following data items:
posix.environ['HOME']
is the pathname of your home directory, equivalent to
getenv("HOME")
in C.
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()
.
However: If you are using this module via the os
module
(as you should - see the introduction above), environ
is a
a mapping object that behaves almost like a dictionary but invokes
putenv()
automatically called whenever an item is changed.
'posix.error'
. The accompanying value is a
pair containing the numeric error code from errno
and the
corresponding string, as would be printed by the C function
perror()
.
It defines the following functions and constants:
Note: this function is intended for low-level I/O and must be applied
to a file descriptor as returned by posix.open()
or
posix.pipe()
. To close a ``file object'' returned by the
built-in function open
or by posix.popen
or
posix.fdopen
, use its close()
method.
None
.
Note: the standard way to exit is sys.exit(n)
.
posix._exit()
should normally only be used in the child process
after a fork()
.
open()
function.
stat()
.
'.'
and '..'
even if they are present in the
directory.
stat()
, but do not follow symbolic links. (On systems
without symbolic links, this is identical to posix.stat()
.)
FIFOs are pipes that can be accessed like regular files. FIFOs exist
until they are deleted (for example with os.unlink
).
Generally, FIFOs are used as rendez-vous between ``client'' and
``server'' type processes: the server opens the FIFO for reading, and
the client opens it for writing. Note that mkfifo()
doesn't
open the FIFO - it just creates the rendez-vous point.
For a description of the flag and mode values, see the Unix or C
run-time documentation; flag constants (like O_RDONLY
and
O_WRONLY
) are defined in this module too (see below).
Note: this function is intended for low-level I/O. For normal usage,
use the built-in function open
, which returns a ``file object''
with read()
and write()
methods (and many more).
(r, w)
usable for reading and writing, respectively.
(Not on MS-DOS.)
<sys/lock.h>
) determines which segments are locked.
(Not on MS-DOS.)
'r'
(default) or 'w'
.
The bufsize argument has the same meaning as the corresponding
argument to the built-in open()
function. The exit status of
the command (encoded in the format specified for wait()
) is
available as the return value of the close()
method of the file
object.
(Not on MS-DOS.)
os.system()
, os.popen()
or
os.fork()
and os.execv()
. (Not on all systems.)
When putenv()
is
supported, assignments to items in os.environ
are automatically
translated into corresponding calls to os.putenv()
; however,
calls to os.putenv()
don't update os.environ
, so it is
actually preferable to assign to items of os.environ
.
Note: this function is intended for low-level I/O and must be applied
to a file descriptor as returned by posix.open()
or
posix.pipe()
. To read a ``file object'' returned by the
built-in function open
or by posix.popen
or
posix.fdopen
, or sys.stdin
, use its
read()
or readline()
methods.
posix.error
.)
rmdir
below to remove a directory.
This is identical to the unlink
function documented below.
setpgrp()
or setpgrp(0, 0)
depending on which version is implemented (if any). See the Unix
manual for the semantics.
(Not on MS-DOS.)
setpgid()
. See the Unix manual for
the semantics.
(Not on MS-DOS.)
setsid()
. See the Unix manual for the
semantics.
(Not on MS-DOS.)
st_mode
,
st_ino
,
st_dev
,
st_nlink
,
st_uid
,
st_gid
,
st_size
,
st_atime
,
st_mtime
,
st_ctime
.
More items may be added at the end by some implementations.
(On MS-DOS, some items are filled with dummy values.)
Note: The standard module stat
defines functions and constants
that are useful for extracting information from a stat structure.
posix.error
.)
system()
, and has the same
limitations. Changes to posix.environ
, sys.stdin
etc. are
not reflected in the environment of the executed command. The return
value is the exit status of the process encoded in the format
specified for wait()
.
posix.open()
).
(Not on MS-DOS.)
posix.open()
)
to pg.
(Not on MS-DOS.)
(sysname, nodename, release, version, machine)
.
Some systems truncate the nodename to 8
characters or to the leading component; a better way to get the
hostname is socket.gethostname()
. (Not on MS-DOS, nor on older
Unix systems.)
remove
;
the unlink
name is its traditional Unix name.
wait()
). The semantics of the call are affected by the value of
the integer options, which should be 0 for normal operation. (If the
system does not support waitpid()
, this always raises
posix.error
. Not on MS-DOS.)
Note: this function is intended for low-level I/O and must be applied
to a file descriptor as returned by posix.open()
or
posix.pipe()
. To write a ``file object'' returned by the
built-in function open
or by posix.popen
or
posix.fdopen
, or sys.stdout
or sys.stderr
, use
its write()
method.
waitpid()
to avoid hanging if no child process
status is available immediately.
flag
argument to the open()
function.
These can be bit-wise OR'd together.