blob: 953e1171eb98d381d032e18bc825a537f0e4c84b [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Standard Module \sectcode{os}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-os}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00003\stmodindex{os}
Fred Drakec4f15af1998-03-10 03:17:26 +00004
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00005This module provides a more portable way of using operating system
6(OS) dependent functionality than importing an OS dependent built-in
Fred Drakec4f15af1998-03-10 03:17:26 +00007module like \module{posix}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00008
Fred Drakec4f15af1998-03-10 03:17:26 +00009When the optional built-in module \module{posix} is available, this
10module exports the same functions and data as \module{posix}; otherwise,
11it searches for an OS dependent built-in module like \module{mac} and
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000012exports the same functions and data as found there. The design of all
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000013Python's built-in OS dependent modules is such that as long as the same
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000014functionality is available, it uses the same interface; e.g., the
Fred Drakec4f15af1998-03-10 03:17:26 +000015function \code{os.stat(\var{file})} returns stat info about \var{file}
16in a format compatible with the \POSIX{} interface.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000017
18Extensions peculiar to a particular OS are also available through the
Fred Drakec4f15af1998-03-10 03:17:26 +000019\module{os} module, but using them is of course a threat to
20portability!
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000021
Fred Drakec4f15af1998-03-10 03:17:26 +000022Note that after the first time \module{os} is imported, there is
23\emph{no} performance penalty in using functions from \module{os}
24instead of directly from the OS dependent built-in module, so there
25should be \emph{no} reason not to use \module{os}!
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000026
27In addition to whatever the correct OS dependent module exports, the
Fred Drakec4f15af1998-03-10 03:17:26 +000028following variables and functions are always exported by \module{os}:
Guido van Rossum470be141995-03-17 16:07:09 +000029
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000030\begin{datadesc}{name}
Guido van Rossum470be141995-03-17 16:07:09 +000031The name of the OS dependent module imported. The following names
32have currently been registered: \code{'posix'}, \code{'nt'},
33\code{'dos'}, \code{'mac'}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000034\end{datadesc}
35
36\begin{datadesc}{path}
37The corresponding OS dependent standard module for pathname
Fred Drakec4f15af1998-03-10 03:17:26 +000038operations, e.g., \module{posixpath} or \module{macpath}. Thus, (given
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000039the proper imports), \code{os.path.split(\var{file})} is equivalent to but
40more portable than \code{posixpath.split(\var{file})}.
41\end{datadesc}
42
43\begin{datadesc}{curdir}
44The constant string used by the OS to refer to the current directory,
Fred Drakec4f15af1998-03-10 03:17:26 +000045e.g. \code{'.'} for \POSIX{} or \code{':'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000046\end{datadesc}
47
48\begin{datadesc}{pardir}
49The constant string used by the OS to refer to the parent directory,
Fred Drakec4f15af1998-03-10 03:17:26 +000050e.g. \code{'..'} for \POSIX{} or \code{'::'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000051\end{datadesc}
52
53\begin{datadesc}{sep}
Guido van Rossumb2afc811997-08-29 22:37:44 +000054The character used by the OS to separate pathname components,
Fred Drakec4f15af1998-03-10 03:17:26 +000055e.g. \code{'/'} for \POSIX{} or \code{':'} for the Macintosh. Note that
Fred Drake65b32f71998-02-09 20:27:12 +000056knowing this is not sufficient to be able to parse or concatenate
Fred Drakec4f15af1998-03-10 03:17:26 +000057pathnames --- better use \function{os.path.split()} and
58\function{os.path.join()}---but it is occasionally useful.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000059\end{datadesc}
60
Guido van Rossumb2afc811997-08-29 22:37:44 +000061\begin{datadesc}{altsep}
62An alternative character used by the OS to separate pathname components,
63or \code{None} if only one separator character exists. This is set to
64\code{'/'} on DOS/Windows systems where \code{sep} is a backslash.
65\end{datadesc}
66
Guido van Rossum470be141995-03-17 16:07:09 +000067\begin{datadesc}{pathsep}
68The character conventionally used by the OS to separate search patch
Fred Drake65b32f71998-02-09 20:27:12 +000069components (as in \code{\$PATH}), e.g.\ \code{':'} for \POSIX{} or
Guido van Rossum470be141995-03-17 16:07:09 +000070\code{';'} for MS-DOS.
71\end{datadesc}
72
73\begin{datadesc}{defpath}
Fred Drakec4f15af1998-03-10 03:17:26 +000074The default search path used by \code{exec*p*()} if the environment
Guido van Rossum470be141995-03-17 16:07:09 +000075doesn't have a \code{'PATH'} key.
76\end{datadesc}
77
Fred Drakec4f15af1998-03-10 03:17:26 +000078\begin{funcdesc}{execl}{path, arg0, arg1, ...}
Guido van Rossum470be141995-03-17 16:07:09 +000079This is equivalent to
Fred Drakec4f15af1998-03-10 03:17:26 +000080\code{execv(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000081\end{funcdesc}
82
Fred Drakec4f15af1998-03-10 03:17:26 +000083\begin{funcdesc}{execle}{path, arg0, arg1, ..., env}
Guido van Rossum470be141995-03-17 16:07:09 +000084This is equivalent to
Fred Drakec4f15af1998-03-10 03:17:26 +000085\code{execve(\var{path}, (\var{arg0}, \var{arg1}, ...), \var{env})}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000086\end{funcdesc}
87
Fred Drakec4f15af1998-03-10 03:17:26 +000088\begin{funcdesc}{execlp}{path, arg0, arg1, ...}
Guido van Rossum470be141995-03-17 16:07:09 +000089This is equivalent to
Fred Drakec4f15af1998-03-10 03:17:26 +000090\code{execvp(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000091\end{funcdesc}
92
Fred Drakec4f15af1998-03-10 03:17:26 +000093\begin{funcdesc}{execvp}{path, args}
94This is like \code{execv(\var{path}, \var{args})} but duplicates
Guido van Rossum470be141995-03-17 16:07:09 +000095the shell's actions in searching for an executable file in a list of
96directories. The directory list is obtained from
Fred Drakec4f15af1998-03-10 03:17:26 +000097\code{environ['PATH']}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000098\end{funcdesc}
Guido van Rossum470be141995-03-17 16:07:09 +000099
Fred Drakec4f15af1998-03-10 03:17:26 +0000100\begin{funcdesc}{execvpe}{path, args, env}
101This is a cross between \function{execve()} and \function{execvp()}.
Guido van Rossum470be141995-03-17 16:07:09 +0000102The directory list is obtained from \code{\var{env}['PATH']}.
103\end{funcdesc}
104
Fred Drakec4f15af1998-03-10 03:17:26 +0000105(The functions \code{execv()} and \code{execve()} are not
Guido van Rossum470be141995-03-17 16:07:09 +0000106documented here, since they are implemented by the OS dependent
107module. If the OS dependent module doesn't define either of these,
108the functions that rely on it will raise an exception. They are
Fred Drakec4f15af1998-03-10 03:17:26 +0000109documented in the section on module \module{posix}, together with all
110other functions that \module{os} imports from the OS dependent module.)