blob: 763d2f394123dad125e4cf56f817a9737e55ab8a [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
Fred Drake65b32f71998-02-09 20:27:12 +000016format 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
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,
Fred Drake65b32f71998-02-09 20:27:12 +000046e.g. \code{'.'} for \POSIX{} or \code{':'} for the Mac.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000047\end{datadesc}
48
49\begin{datadesc}{pardir}
50The constant string used by the OS to refer to the parent directory,
Fred Drake65b32f71998-02-09 20:27:12 +000051e.g. \code{'..'} for \POSIX{} or \code{'::'} for the Mac.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000052\end{datadesc}
53
54\begin{datadesc}{sep}
Guido van Rossumb2afc811997-08-29 22:37:44 +000055The character used by the OS to separate pathname components,
Fred Drake65b32f71998-02-09 20:27:12 +000056e.g. \code{'/'} for \POSIX{} or \code{':'} for the Mac. Note that
57knowing this is not sufficient to be able to parse or concatenate
58pathnames --- better use \code{os.path.split()} and
59\code{os.path.join()}---but it is occasionally useful.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000060\end{datadesc}
61
Guido van Rossumb2afc811997-08-29 22:37:44 +000062\begin{datadesc}{altsep}
63An alternative character used by the OS to separate pathname components,
64or \code{None} if only one separator character exists. This is set to
65\code{'/'} on DOS/Windows systems where \code{sep} is a backslash.
66\end{datadesc}
67
Guido van Rossum470be141995-03-17 16:07:09 +000068\begin{datadesc}{pathsep}
69The character conventionally used by the OS to separate search patch
Fred Drake65b32f71998-02-09 20:27:12 +000070components (as in \code{\$PATH}), e.g.\ \code{':'} for \POSIX{} or
Guido van Rossum470be141995-03-17 16:07:09 +000071\code{';'} for MS-DOS.
72\end{datadesc}
73
74\begin{datadesc}{defpath}
75The default search path used by \code{os.exec*p*()} if the environment
76doesn't have a \code{'PATH'} key.
77\end{datadesc}
78
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000079\begin{funcdesc}{execl}{path\, arg0\, arg1\, ...}
Guido van Rossum470be141995-03-17 16:07:09 +000080This is equivalent to
81\code{os.execv(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000082\end{funcdesc}
83
84\begin{funcdesc}{execle}{path\, arg0\, arg1\, ...\, env}
Guido van Rossum470be141995-03-17 16:07:09 +000085This is equivalent to
86\code{os.execve(\var{path}, (\var{arg0}, \var{arg1}, ...), \var{env})}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000087\end{funcdesc}
88
89\begin{funcdesc}{execlp}{path\, arg0\, arg1\, ...}
Guido van Rossum470be141995-03-17 16:07:09 +000090This is equivalent to
91\code{os.execvp(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000092\end{funcdesc}
93
Guido van Rossum470be141995-03-17 16:07:09 +000094\begin{funcdesc}{execvp}{path\, args}
95This is like \code{os.execv(\var{path}, \var{args})} but duplicates
96the shell's actions in searching for an executable file in a list of
97directories. The directory list is obtained from
98\code{os.environ['PATH']}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000099\end{funcdesc}
Guido van Rossum470be141995-03-17 16:07:09 +0000100
101\begin{funcdesc}{execvpe}{path\, args\, env}
102This is a cross between \code{os.execve()} and \code{os.execvp()}.
103The directory list is obtained from \code{\var{env}['PATH']}.
104\end{funcdesc}
105
106(The functions \code{os.execv()} and \code{execve()} are not
107documented here, since they are implemented by the OS dependent
108module. If the OS dependent module doesn't define either of these,
109the functions that rely on it will raise an exception. They are
110documented in the section on module \code{posix}, together with all
111other functions that \code{os} imports from the OS dependent module.)