blob: b0ca13f49856a9861019d7e92d35b794f8cd3c4a [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
Guido van Rossum89a79d11998-07-24 20:48:20 +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
Guido van Rossum89a79d11998-07-24 20:48:20 +000087\begin{funcdesc}{makedirs}{path\optional{, mode}}
88Recursive directory creation function. Like \function{mkdir()},
89but makes all intermediate-level directories needed to contain the
90leaf directory. Throws an \exception{os.error} exception if the leaf
91directory already exists or cannot be created. The default \var{mode}
92is \code{0777} (octal).
93\end{funcdesc}
94
95\begin{funcdesc}{removedirs}{path}
96Recursive directory removal function. Works like
97\function{rmdir()} except that, if the leaf directory is
98successfully removed, directories corresponding to rightmost path
99segments will be pruned way until either the whole path is consumed or
100an error is raised (which is ignored, because it generally means that
101a parent directory is not empty). Throws an \exception{os.error}
102exception if the leaf directory could not be successfully removed.
103\end{funcdesc}
104
105\begin{funcdesc}{renames}{path}
106Recursive directory or file renaming function.
107Works like \function{rename()}, except creation of any intermediate
108directories needed to make the new pathname good is attempted first.
109After the rename, directories corresponding to rightmost path segments
110of the old name will be pruned away using \function{removedirs()}.
111
112Note: this function can fail with the new directory structure made if
113you lack permissions needed to remove the leaf directory or file.
114\end{funcdesc}
115
Fred Drakec4f15af1998-03-10 03:17:26 +0000116\begin{funcdesc}{execl}{path, arg0, arg1, ...}
Guido van Rossum470be141995-03-17 16:07:09 +0000117This is equivalent to
Fred Drakec4f15af1998-03-10 03:17:26 +0000118\code{execv(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000119\end{funcdesc}
120
Fred Drakec4f15af1998-03-10 03:17:26 +0000121\begin{funcdesc}{execle}{path, arg0, arg1, ..., env}
Guido van Rossum470be141995-03-17 16:07:09 +0000122This is equivalent to
Fred Drakec4f15af1998-03-10 03:17:26 +0000123\code{execve(\var{path}, (\var{arg0}, \var{arg1}, ...), \var{env})}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000124\end{funcdesc}
125
Fred Drakec4f15af1998-03-10 03:17:26 +0000126\begin{funcdesc}{execlp}{path, arg0, arg1, ...}
Guido van Rossum470be141995-03-17 16:07:09 +0000127This is equivalent to
Fred Drakec4f15af1998-03-10 03:17:26 +0000128\code{execvp(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000129\end{funcdesc}
130
Fred Drakec4f15af1998-03-10 03:17:26 +0000131\begin{funcdesc}{execvp}{path, args}
132This is like \code{execv(\var{path}, \var{args})} but duplicates
Guido van Rossum470be141995-03-17 16:07:09 +0000133the shell's actions in searching for an executable file in a list of
134directories. The directory list is obtained from
Fred Drakec4f15af1998-03-10 03:17:26 +0000135\code{environ['PATH']}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000136\end{funcdesc}
Guido van Rossum470be141995-03-17 16:07:09 +0000137
Fred Drakec4f15af1998-03-10 03:17:26 +0000138\begin{funcdesc}{execvpe}{path, args, env}
139This is a cross between \function{execve()} and \function{execvp()}.
Guido van Rossum470be141995-03-17 16:07:09 +0000140The directory list is obtained from \code{\var{env}['PATH']}.
141\end{funcdesc}
142
Fred Drakec4f15af1998-03-10 03:17:26 +0000143(The functions \code{execv()} and \code{execve()} are not
Guido van Rossum470be141995-03-17 16:07:09 +0000144documented here, since they are implemented by the OS dependent
145module. If the OS dependent module doesn't define either of these,
146the functions that rely on it will raise an exception. They are
Fred Drakec4f15af1998-03-10 03:17:26 +0000147documented in the section on module \module{posix}, together with all
148other functions that \module{os} imports from the OS dependent module.)