blob: f17ce9574f6e670015c8fc97168e18a2c75fa825 [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
4\stmodindex{os}
5This module provides a more portable way of using operating system
6(OS) dependent functionality than importing an OS dependent built-in
7module like \code{posix}.
8
9When the optional built-in module \code{posix} is available, this
10module exports the same functions and data as \code{posix}; otherwise,
11it searches for an OS dependent built-in module like \code{mac} and
12exports 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
15function \code{os.stat(\var{file})} returns stat info about a \var{file} in a
16format compatible with the POSIX interface.
17
18Extensions peculiar to a particular OS are also available through the
19\code{os} module, but using them is of course a threat to portability!
20
21Note that after the first time \code{os} is imported, there is \emph{no}
22performance penalty in using functions from \code{os} instead of
23directly from the OS dependent built-in module, so there should be
24\emph{no} reason not to use \code{os}!
25
26In addition to whatever the correct OS dependent module exports, the
27following variables and functions are always exported by \code{os}:
28
29\renewcommand{\indexsubitem}{(in module os)}
Guido van Rossum470be141995-03-17 16:07:09 +000030
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000031\begin{datadesc}{name}
Guido van Rossum470be141995-03-17 16:07:09 +000032The name of the OS dependent module imported. The following names
33have currently been registered: \code{'posix'}, \code{'nt'},
34\code{'dos'}, \code{'mac'}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000035\end{datadesc}
36
37\begin{datadesc}{path}
38The corresponding OS dependent standard module for pathname
39operations, e.g., \code{posixpath} or \code{macpath}. Thus, (given
40the proper imports), \code{os.path.split(\var{file})} is equivalent to but
41more portable than \code{posixpath.split(\var{file})}.
42\end{datadesc}
43
44\begin{datadesc}{curdir}
45The constant string used by the OS to refer to the current directory,
46e.g. \code{'.'} for POSIX or \code{':'} for the Mac.
47\end{datadesc}
48
49\begin{datadesc}{pardir}
50The constant string used by the OS to refer to the parent directory,
51e.g. \code{'..'} for POSIX or \code{'::'} for the Mac.
52\end{datadesc}
53
54\begin{datadesc}{sep}
Guido van Rossum470be141995-03-17 16:07:09 +000055The character used by the OS to separate pathname components, e.g.\
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000056\code{'/'} for POSIX or \code{':'} for the Mac. Note that knowing this
57is not sufficient to be able to parse or concatenate pathnames---better
58use \code{os.path.split()} and \code{os.path.join()}---but it is
59occasionally useful.
60\end{datadesc}
61
Guido van Rossum470be141995-03-17 16:07:09 +000062\begin{datadesc}{pathsep}
63The character conventionally used by the OS to separate search patch
64components (as in \code{\$PATH}), e.g.\ \code{':'} for POSIX or
65\code{';'} for MS-DOS.
66\end{datadesc}
67
68\begin{datadesc}{defpath}
69The default search path used by \code{os.exec*p*()} if the environment
70doesn't have a \code{'PATH'} key.
71\end{datadesc}
72
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000073\begin{funcdesc}{execl}{path\, arg0\, arg1\, ...}
Guido van Rossum470be141995-03-17 16:07:09 +000074This is equivalent to
75\code{os.execv(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000076\end{funcdesc}
77
78\begin{funcdesc}{execle}{path\, arg0\, arg1\, ...\, env}
Guido van Rossum470be141995-03-17 16:07:09 +000079This is equivalent to
80\code{os.execve(\var{path}, (\var{arg0}, \var{arg1}, ...), \var{env})}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000081\end{funcdesc}
82
83\begin{funcdesc}{execlp}{path\, arg0\, arg1\, ...}
Guido van Rossum470be141995-03-17 16:07:09 +000084This is equivalent to
85\code{os.execvp(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000086\end{funcdesc}
87
Guido van Rossum470be141995-03-17 16:07:09 +000088\begin{funcdesc}{execvp}{path\, args}
89This is like \code{os.execv(\var{path}, \var{args})} but duplicates
90the shell's actions in searching for an executable file in a list of
91directories. The directory list is obtained from
92\code{os.environ['PATH']}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000093\end{funcdesc}
Guido van Rossum470be141995-03-17 16:07:09 +000094
95\begin{funcdesc}{execvpe}{path\, args\, env}
96This is a cross between \code{os.execve()} and \code{os.execvp()}.
97The directory list is obtained from \code{\var{env}['PATH']}.
98\end{funcdesc}
99
100(The functions \code{os.execv()} and \code{execve()} are not
101documented here, since they are implemented by the OS dependent
102module. If the OS dependent module doesn't define either of these,
103the functions that rely on it will raise an exception. They are
104documented in the section on module \code{posix}, together with all
105other functions that \code{os} imports from the OS dependent module.)