blob: b9f3f20f5dec620a610c8d1c7884d60bb16d7edb [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Standard Module \module{os}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{standard}{os}
3
4\modulesynopsis{Miscellaneous OS interfaces.}
5
Fred Drakec4f15af1998-03-10 03:17:26 +00006
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00007This module provides a more portable way of using operating system
8(OS) dependent functionality than importing an OS dependent built-in
Fred Drakec4f15af1998-03-10 03:17:26 +00009module like \module{posix}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000010
Fred Drakec4f15af1998-03-10 03:17:26 +000011When the optional built-in module \module{posix} is available, this
12module exports the same functions and data as \module{posix}; otherwise,
13it searches for an OS dependent built-in module like \module{mac} and
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000014exports the same functions and data as found there. The design of all
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000015Python's built-in OS dependent modules is such that as long as the same
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000016functionality is available, it uses the same interface; e.g., the
Fred Drakec4f15af1998-03-10 03:17:26 +000017function \code{os.stat(\var{file})} returns stat info about \var{file}
18in a format compatible with the \POSIX{} interface.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000019
20Extensions peculiar to a particular OS are also available through the
Fred Drakec4f15af1998-03-10 03:17:26 +000021\module{os} module, but using them is of course a threat to
22portability!
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000023
Fred Drakec4f15af1998-03-10 03:17:26 +000024Note that after the first time \module{os} is imported, there is
25\emph{no} performance penalty in using functions from \module{os}
26instead of directly from the OS dependent built-in module, so there
27should be \emph{no} reason not to use \module{os}!
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000028
29In addition to whatever the correct OS dependent module exports, the
Fred Drakec4f15af1998-03-10 03:17:26 +000030following variables and functions are always exported by \module{os}:
Guido van Rossum470be141995-03-17 16:07:09 +000031
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000032\begin{datadesc}{name}
Guido van Rossum470be141995-03-17 16:07:09 +000033The name of the OS dependent module imported. The following names
34have currently been registered: \code{'posix'}, \code{'nt'},
35\code{'dos'}, \code{'mac'}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000036\end{datadesc}
37
38\begin{datadesc}{path}
39The corresponding OS dependent standard module for pathname
Fred Drakec4f15af1998-03-10 03:17:26 +000040operations, e.g., \module{posixpath} or \module{macpath}. Thus, (given
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000041the proper imports), \code{os.path.split(\var{file})} is equivalent to but
42more portable than \code{posixpath.split(\var{file})}.
43\end{datadesc}
44
45\begin{datadesc}{curdir}
46The constant string used by the OS to refer to the current directory,
Fred Drakec4f15af1998-03-10 03:17:26 +000047e.g. \code{'.'} for \POSIX{} or \code{':'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000048\end{datadesc}
49
50\begin{datadesc}{pardir}
51The constant string used by the OS to refer to the parent directory,
Fred Drakec4f15af1998-03-10 03:17:26 +000052e.g. \code{'..'} for \POSIX{} or \code{'::'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000053\end{datadesc}
54
55\begin{datadesc}{sep}
Guido van Rossumb2afc811997-08-29 22:37:44 +000056The character used by the OS to separate pathname components,
Fred Drakec4f15af1998-03-10 03:17:26 +000057e.g. \code{'/'} for \POSIX{} or \code{':'} for the Macintosh. Note that
Fred Drake65b32f71998-02-09 20:27:12 +000058knowing this is not sufficient to be able to parse or concatenate
Fred Drakec4f15af1998-03-10 03:17:26 +000059pathnames --- better use \function{os.path.split()} and
60\function{os.path.join()}---but it is occasionally useful.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000061\end{datadesc}
62
Guido van Rossumb2afc811997-08-29 22:37:44 +000063\begin{datadesc}{altsep}
64An alternative character used by the OS to separate pathname components,
65or \code{None} if only one separator character exists. This is set to
66\code{'/'} on DOS/Windows systems where \code{sep} is a backslash.
67\end{datadesc}
68
Guido van Rossum470be141995-03-17 16:07:09 +000069\begin{datadesc}{pathsep}
70The character conventionally used by the OS to separate search patch
Fred Drake65b32f71998-02-09 20:27:12 +000071components (as in \code{\$PATH}), e.g.\ \code{':'} for \POSIX{} or
Guido van Rossum470be141995-03-17 16:07:09 +000072\code{';'} for MS-DOS.
73\end{datadesc}
74
Guido van Rossum9c59ce91998-06-30 15:54:27 +000075\begin{datadesc}{linesep}
76The string used to separate (or, rather, terminate) lines on the
77current platform. This may be a single character, e.g. \code{'\e n'}
78for \POSIX{} or \code{'\e r'} for MacOS, or multiple characters,
79e.g. \code{'\e r\e n'} for MS-DOS.
80\end{datadesc}
81
Guido van Rossum470be141995-03-17 16:07:09 +000082\begin{datadesc}{defpath}
Fred Drakec4f15af1998-03-10 03:17:26 +000083The default search path used by \code{exec*p*()} if the environment
Guido van Rossum470be141995-03-17 16:07:09 +000084doesn't have a \code{'PATH'} key.
85\end{datadesc}
86
Fred Drakec4f15af1998-03-10 03:17:26 +000087\begin{funcdesc}{execl}{path, arg0, arg1, ...}
Guido van Rossum470be141995-03-17 16:07:09 +000088This is equivalent to
Fred Drakec4f15af1998-03-10 03:17:26 +000089\code{execv(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000090\end{funcdesc}
91
Fred Drakec4f15af1998-03-10 03:17:26 +000092\begin{funcdesc}{execle}{path, arg0, arg1, ..., env}
Guido van Rossum470be141995-03-17 16:07:09 +000093This is equivalent to
Fred Drakec4f15af1998-03-10 03:17:26 +000094\code{execve(\var{path}, (\var{arg0}, \var{arg1}, ...), \var{env})}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000095\end{funcdesc}
96
Fred Drakec4f15af1998-03-10 03:17:26 +000097\begin{funcdesc}{execlp}{path, arg0, arg1, ...}
Guido van Rossum470be141995-03-17 16:07:09 +000098This is equivalent to
Fred Drakec4f15af1998-03-10 03:17:26 +000099\code{execvp(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000100\end{funcdesc}
101
Fred Drakec4f15af1998-03-10 03:17:26 +0000102\begin{funcdesc}{execvp}{path, args}
103This is like \code{execv(\var{path}, \var{args})} but duplicates
Guido van Rossum470be141995-03-17 16:07:09 +0000104the shell's actions in searching for an executable file in a list of
105directories. The directory list is obtained from
Fred Drakec4f15af1998-03-10 03:17:26 +0000106\code{environ['PATH']}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000107\end{funcdesc}
Guido van Rossum470be141995-03-17 16:07:09 +0000108
Fred Drakec4f15af1998-03-10 03:17:26 +0000109\begin{funcdesc}{execvpe}{path, args, env}
110This is a cross between \function{execve()} and \function{execvp()}.
Guido van Rossum470be141995-03-17 16:07:09 +0000111The directory list is obtained from \code{\var{env}['PATH']}.
112\end{funcdesc}
113
Fred Drakec4f15af1998-03-10 03:17:26 +0000114(The functions \code{execv()} and \code{execve()} are not
Guido van Rossum470be141995-03-17 16:07:09 +0000115documented here, since they are implemented by the OS dependent
116module. If the OS dependent module doesn't define either of these,
117the functions that rely on it will raise an exception. They are
Fred Drakec4f15af1998-03-10 03:17:26 +0000118documented in the section on module \module{posix}, together with all
119other functions that \module{os} imports from the OS dependent module.)