blob: 51442efcfcb14d32a9d188c25a98c8e6b9265d26 [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Standard Module \sectcode{os}}
2
3\stmodindex{os}
4This module provides a more portable way of using operating system
5(OS) dependent functionality than importing an OS dependent built-in
6module like \code{posix}.
7
8When the optional built-in module \code{posix} is available, this
9module exports the same functions and data as \code{posix}; otherwise,
10it searches for an OS dependent built-in module like \code{mac} and
11exports the same functions and data as found there. The design of all
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000012Python's built-in OS dependent modules is such that as long as the same
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000013functionality is available, it uses the same interface; e.g., the
14function \code{os.stat(\var{file})} returns stat info about a \var{file} in a
15format compatible with the POSIX interface.
16
17Extensions peculiar to a particular OS are also available through the
18\code{os} module, but using them is of course a threat to portability!
19
20Note that after the first time \code{os} is imported, there is \emph{no}
21performance penalty in using functions from \code{os} instead of
22directly from the OS dependent built-in module, so there should be
23\emph{no} reason not to use \code{os}!
24
25In addition to whatever the correct OS dependent module exports, the
26following variables and functions are always exported by \code{os}:
27
28\renewcommand{\indexsubitem}{(in 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
38operations, e.g., \code{posixpath} or \code{macpath}. Thus, (given
39the 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,
45e.g. \code{'.'} for POSIX or \code{':'} for the Mac.
46\end{datadesc}
47
48\begin{datadesc}{pardir}
49The constant string used by the OS to refer to the parent directory,
50e.g. \code{'..'} for POSIX or \code{'::'} for the Mac.
51\end{datadesc}
52
53\begin{datadesc}{sep}
Guido van Rossum470be141995-03-17 16:07:09 +000054The character used by the OS to separate pathname components, e.g.\
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000055\code{'/'} for POSIX or \code{':'} for the Mac. Note that knowing this
56is not sufficient to be able to parse or concatenate pathnames---better
57use \code{os.path.split()} and \code{os.path.join()}---but it is
58occasionally useful.
59\end{datadesc}
60
Guido van Rossum470be141995-03-17 16:07:09 +000061\begin{datadesc}{pathsep}
62The character conventionally used by the OS to separate search patch
63components (as in \code{\$PATH}), e.g.\ \code{':'} for POSIX or
64\code{';'} for MS-DOS.
65\end{datadesc}
66
67\begin{datadesc}{defpath}
68The default search path used by \code{os.exec*p*()} if the environment
69doesn't have a \code{'PATH'} key.
70\end{datadesc}
71
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000072\begin{funcdesc}{execl}{path\, arg0\, arg1\, ...}
Guido van Rossum470be141995-03-17 16:07:09 +000073This is equivalent to
74\code{os.execv(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000075\end{funcdesc}
76
77\begin{funcdesc}{execle}{path\, arg0\, arg1\, ...\, env}
Guido van Rossum470be141995-03-17 16:07:09 +000078This is equivalent to
79\code{os.execve(\var{path}, (\var{arg0}, \var{arg1}, ...), \var{env})}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000080\end{funcdesc}
81
82\begin{funcdesc}{execlp}{path\, arg0\, arg1\, ...}
Guido van Rossum470be141995-03-17 16:07:09 +000083This is equivalent to
84\code{os.execvp(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000085\end{funcdesc}
86
Guido van Rossum470be141995-03-17 16:07:09 +000087\begin{funcdesc}{execvp}{path\, args}
88This is like \code{os.execv(\var{path}, \var{args})} but duplicates
89the shell's actions in searching for an executable file in a list of
90directories. The directory list is obtained from
91\code{os.environ['PATH']}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000092\end{funcdesc}
Guido van Rossum470be141995-03-17 16:07:09 +000093
94\begin{funcdesc}{execvpe}{path\, args\, env}
95This is a cross between \code{os.execve()} and \code{os.execvp()}.
96The directory list is obtained from \code{\var{env}['PATH']}.
97\end{funcdesc}
98
99(The functions \code{os.execv()} and \code{execve()} are not
100documented here, since they are implemented by the OS dependent
101module. If the OS dependent module doesn't define either of these,
102the functions that rely on it will raise an exception. They are
103documented in the section on module \code{posix}, together with all
104other functions that \code{os} imports from the OS dependent module.)