blob: 41198ec9527de254ee78eaac241927a9ffa82531 [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 Drake1a3c2a01998-08-06 15:18:23 +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 Drake1a3c2a01998-08-06 15:18:23 +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 Drake1a3c2a01998-08-06 15:18:23 +000057e.g.\ \character{/} for \POSIX{} or \character{:} for the Macintosh.
58Note that knowing this is not sufficient to be able to parse or
59concatenate pathnames --- 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
Fred Drake1a3c2a01998-08-06 15:18:23 +000066\character{/} on DOS/Windows systems where \code{sep} is a backslash.
Guido van Rossumb2afc811997-08-29 22:37:44 +000067\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 Drake1a3c2a01998-08-06 15:18:23 +000071components (as in \envvar{PATH}), e.g.\ \character{:} for \POSIX{} or
72\character{;} for MS-DOS.
Guido van Rossum470be141995-03-17 16:07:09 +000073\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 Drake1a3c2a01998-08-06 15:18:23 +000083The default search path used by \function{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}}
Fred Drake56fa8a71998-08-06 13:45:42 +000088\versionadded{1.5.2}
Guido van Rossum89a79d11998-07-24 20:48:20 +000089Recursive directory creation function. Like \function{mkdir()},
90but makes all intermediate-level directories needed to contain the
91leaf directory. Throws an \exception{os.error} exception if the leaf
92directory already exists or cannot be created. The default \var{mode}
93is \code{0777} (octal).
94\end{funcdesc}
95
96\begin{funcdesc}{removedirs}{path}
Fred Drake56fa8a71998-08-06 13:45:42 +000097\versionadded{1.5.2}
Guido van Rossum89a79d11998-07-24 20:48:20 +000098Recursive directory removal function. Works like
99\function{rmdir()} except that, if the leaf directory is
100successfully removed, directories corresponding to rightmost path
101segments will be pruned way until either the whole path is consumed or
102an error is raised (which is ignored, because it generally means that
103a parent directory is not empty). Throws an \exception{os.error}
104exception if the leaf directory could not be successfully removed.
105\end{funcdesc}
106
107\begin{funcdesc}{renames}{path}
Fred Drake56fa8a71998-08-06 13:45:42 +0000108\versionadded{1.5.2}
Guido van Rossum89a79d11998-07-24 20:48:20 +0000109Recursive directory or file renaming function.
110Works like \function{rename()}, except creation of any intermediate
111directories needed to make the new pathname good is attempted first.
112After the rename, directories corresponding to rightmost path segments
113of the old name will be pruned away using \function{removedirs()}.
114
115Note: this function can fail with the new directory structure made if
116you lack permissions needed to remove the leaf directory or file.
117\end{funcdesc}
118
Fred Drakec4f15af1998-03-10 03:17:26 +0000119\begin{funcdesc}{execl}{path, arg0, arg1, ...}
Guido van Rossum470be141995-03-17 16:07:09 +0000120This is equivalent to
Fred Drake1a3c2a01998-08-06 15:18:23 +0000121\samp{execv(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000122\end{funcdesc}
123
Fred Drakec4f15af1998-03-10 03:17:26 +0000124\begin{funcdesc}{execle}{path, arg0, arg1, ..., env}
Guido van Rossum470be141995-03-17 16:07:09 +0000125This is equivalent to
Fred Drake1a3c2a01998-08-06 15:18:23 +0000126\samp{execve(\var{path}, (\var{arg0}, \var{arg1}, ...), \var{env})}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000127\end{funcdesc}
128
Fred Drakec4f15af1998-03-10 03:17:26 +0000129\begin{funcdesc}{execlp}{path, arg0, arg1, ...}
Guido van Rossum470be141995-03-17 16:07:09 +0000130This is equivalent to
Fred Drake1a3c2a01998-08-06 15:18:23 +0000131\samp{execvp(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000132\end{funcdesc}
133
Fred Drakec4f15af1998-03-10 03:17:26 +0000134\begin{funcdesc}{execvp}{path, args}
Fred Drake1a3c2a01998-08-06 15:18:23 +0000135This is like \samp{execv(\var{path}, \var{args})} but duplicates
Guido van Rossum470be141995-03-17 16:07:09 +0000136the shell's actions in searching for an executable file in a list of
137directories. The directory list is obtained from
Fred Drakec4f15af1998-03-10 03:17:26 +0000138\code{environ['PATH']}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000139\end{funcdesc}
Guido van Rossum470be141995-03-17 16:07:09 +0000140
Fred Drakec4f15af1998-03-10 03:17:26 +0000141\begin{funcdesc}{execvpe}{path, args, env}
142This is a cross between \function{execve()} and \function{execvp()}.
Guido van Rossum470be141995-03-17 16:07:09 +0000143The directory list is obtained from \code{\var{env}['PATH']}.
144\end{funcdesc}
145
Fred Drake1a3c2a01998-08-06 15:18:23 +0000146(The functions \function{execv()} and \function{execve()} are not
Guido van Rossum470be141995-03-17 16:07:09 +0000147documented here, since they are implemented by the OS dependent
148module. If the OS dependent module doesn't define either of these,
149the functions that rely on it will raise an exception. They are
Fred Drakec4f15af1998-03-10 03:17:26 +0000150documented in the section on module \module{posix}, together with all
151other functions that \module{os} imports from the OS dependent module.)