blob: 75d41828f270543c2e4ccea9e1f263c9e3ea3403 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{os} ---
2 Miscellaneous OS interfaces.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{os}
4
5\modulesynopsis{Miscellaneous OS interfaces.}
6
Fred Drakec4f15af1998-03-10 03:17:26 +00007
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00008This module provides a more portable way of using operating system
9(OS) dependent functionality than importing an OS dependent built-in
Fred Drakec4f15af1998-03-10 03:17:26 +000010module like \module{posix}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000011
Fred Drakec4f15af1998-03-10 03:17:26 +000012When the optional built-in module \module{posix} is available, this
13module exports the same functions and data as \module{posix}; otherwise,
14it searches for an OS dependent built-in module like \module{mac} and
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000015exports the same functions and data as found there. The design of all
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000016Python's built-in OS dependent modules is such that as long as the same
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000017functionality is available, it uses the same interface; e.g., the
Fred Drakec4f15af1998-03-10 03:17:26 +000018function \code{os.stat(\var{file})} returns stat info about \var{file}
19in a format compatible with the \POSIX{} interface.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000020
21Extensions peculiar to a particular OS are also available through the
Fred Drakec4f15af1998-03-10 03:17:26 +000022\module{os} module, but using them is of course a threat to
23portability!
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000024
Fred Drakec4f15af1998-03-10 03:17:26 +000025Note that after the first time \module{os} is imported, there is
26\emph{no} performance penalty in using functions from \module{os}
27instead of directly from the OS dependent built-in module, so there
28should be \emph{no} reason not to use \module{os}!
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000029
30In addition to whatever the correct OS dependent module exports, the
Fred Drakec4f15af1998-03-10 03:17:26 +000031following variables and functions are always exported by \module{os}:
Guido van Rossum470be141995-03-17 16:07:09 +000032
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000033\begin{datadesc}{name}
Guido van Rossum470be141995-03-17 16:07:09 +000034The name of the OS dependent module imported. The following names
35have currently been registered: \code{'posix'}, \code{'nt'},
36\code{'dos'}, \code{'mac'}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000037\end{datadesc}
38
39\begin{datadesc}{path}
40The corresponding OS dependent standard module for pathname
Fred Drakec4f15af1998-03-10 03:17:26 +000041operations, e.g., \module{posixpath} or \module{macpath}. Thus, (given
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000042the proper imports), \code{os.path.split(\var{file})} is equivalent to but
43more portable than \code{posixpath.split(\var{file})}.
44\end{datadesc}
45
46\begin{datadesc}{curdir}
47The constant string used by the OS to refer to the current directory,
Fred Drake1a3c2a01998-08-06 15:18:23 +000048e.g.\ \code{'.'} for \POSIX{} or \code{':'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000049\end{datadesc}
50
51\begin{datadesc}{pardir}
52The constant string used by the OS to refer to the parent directory,
Fred Drake1a3c2a01998-08-06 15:18:23 +000053e.g.\ \code{'..'} for \POSIX{} or \code{'::'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000054\end{datadesc}
55
56\begin{datadesc}{sep}
Guido van Rossumb2afc811997-08-29 22:37:44 +000057The character used by the OS to separate pathname components,
Fred Drake1a3c2a01998-08-06 15:18:23 +000058e.g.\ \character{/} for \POSIX{} or \character{:} for the Macintosh.
59Note that knowing this is not sufficient to be able to parse or
60concatenate pathnames --- use \function{os.path.split()} and
61\function{os.path.join()} --- but it is occasionally useful.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000062\end{datadesc}
63
Guido van Rossumb2afc811997-08-29 22:37:44 +000064\begin{datadesc}{altsep}
65An alternative character used by the OS to separate pathname components,
66or \code{None} if only one separator character exists. This is set to
Fred Drake1a3c2a01998-08-06 15:18:23 +000067\character{/} on DOS/Windows systems where \code{sep} is a backslash.
Guido van Rossumb2afc811997-08-29 22:37:44 +000068\end{datadesc}
69
Guido van Rossum470be141995-03-17 16:07:09 +000070\begin{datadesc}{pathsep}
71The character conventionally used by the OS to separate search patch
Fred Drake1a3c2a01998-08-06 15:18:23 +000072components (as in \envvar{PATH}), e.g.\ \character{:} for \POSIX{} or
73\character{;} for MS-DOS.
Guido van Rossum470be141995-03-17 16:07:09 +000074\end{datadesc}
75
Guido van Rossum9c59ce91998-06-30 15:54:27 +000076\begin{datadesc}{linesep}
77The string used to separate (or, rather, terminate) lines on the
78current platform. This may be a single character, e.g. \code{'\e n'}
79for \POSIX{} or \code{'\e r'} for MacOS, or multiple characters,
80e.g. \code{'\e r\e n'} for MS-DOS.
81\end{datadesc}
82
Guido van Rossum470be141995-03-17 16:07:09 +000083\begin{datadesc}{defpath}
Fred Drake1a3c2a01998-08-06 15:18:23 +000084The default search path used by \function{exec*p*()} if the environment
Guido van Rossum470be141995-03-17 16:07:09 +000085doesn't have a \code{'PATH'} key.
86\end{datadesc}
87
Guido van Rossum89a79d11998-07-24 20:48:20 +000088\begin{funcdesc}{makedirs}{path\optional{, mode}}
Fred Drake56fa8a71998-08-06 13:45:42 +000089\versionadded{1.5.2}
Guido van Rossum89a79d11998-07-24 20:48:20 +000090Recursive directory creation function. Like \function{mkdir()},
91but makes all intermediate-level directories needed to contain the
92leaf directory. Throws an \exception{os.error} exception if the leaf
93directory already exists or cannot be created. The default \var{mode}
94is \code{0777} (octal).
95\end{funcdesc}
96
97\begin{funcdesc}{removedirs}{path}
Fred Drake56fa8a71998-08-06 13:45:42 +000098\versionadded{1.5.2}
Guido van Rossum89a79d11998-07-24 20:48:20 +000099Recursive directory removal function. Works like
100\function{rmdir()} except that, if the leaf directory is
101successfully removed, directories corresponding to rightmost path
102segments will be pruned way until either the whole path is consumed or
103an error is raised (which is ignored, because it generally means that
104a parent directory is not empty). Throws an \exception{os.error}
105exception if the leaf directory could not be successfully removed.
106\end{funcdesc}
107
108\begin{funcdesc}{renames}{path}
Fred Drake56fa8a71998-08-06 13:45:42 +0000109\versionadded{1.5.2}
Guido van Rossum89a79d11998-07-24 20:48:20 +0000110Recursive directory or file renaming function.
111Works like \function{rename()}, except creation of any intermediate
112directories needed to make the new pathname good is attempted first.
113After the rename, directories corresponding to rightmost path segments
114of the old name will be pruned away using \function{removedirs()}.
115
116Note: this function can fail with the new directory structure made if
117you lack permissions needed to remove the leaf directory or file.
118\end{funcdesc}
119
Fred Drakec4f15af1998-03-10 03:17:26 +0000120\begin{funcdesc}{execl}{path, arg0, arg1, ...}
Guido van Rossum470be141995-03-17 16:07:09 +0000121This is equivalent to
Fred Drake1a3c2a01998-08-06 15:18:23 +0000122\samp{execv(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000123\end{funcdesc}
124
Fred Drakec4f15af1998-03-10 03:17:26 +0000125\begin{funcdesc}{execle}{path, arg0, arg1, ..., env}
Guido van Rossum470be141995-03-17 16:07:09 +0000126This is equivalent to
Fred Drake1a3c2a01998-08-06 15:18:23 +0000127\samp{execve(\var{path}, (\var{arg0}, \var{arg1}, ...), \var{env})}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000128\end{funcdesc}
129
Fred Drakec4f15af1998-03-10 03:17:26 +0000130\begin{funcdesc}{execlp}{path, arg0, arg1, ...}
Guido van Rossum470be141995-03-17 16:07:09 +0000131This is equivalent to
Fred Drake1a3c2a01998-08-06 15:18:23 +0000132\samp{execvp(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000133\end{funcdesc}
134
Fred Drakec4f15af1998-03-10 03:17:26 +0000135\begin{funcdesc}{execvp}{path, args}
Fred Drake1a3c2a01998-08-06 15:18:23 +0000136This is like \samp{execv(\var{path}, \var{args})} but duplicates
Guido van Rossum470be141995-03-17 16:07:09 +0000137the shell's actions in searching for an executable file in a list of
138directories. The directory list is obtained from
Fred Drakec4f15af1998-03-10 03:17:26 +0000139\code{environ['PATH']}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000140\end{funcdesc}
Guido van Rossum470be141995-03-17 16:07:09 +0000141
Fred Drakec4f15af1998-03-10 03:17:26 +0000142\begin{funcdesc}{execvpe}{path, args, env}
143This is a cross between \function{execve()} and \function{execvp()}.
Guido van Rossum470be141995-03-17 16:07:09 +0000144The directory list is obtained from \code{\var{env}['PATH']}.
145\end{funcdesc}
146
Fred Drake1a3c2a01998-08-06 15:18:23 +0000147(The functions \function{execv()} and \function{execve()} are not
Guido van Rossum470be141995-03-17 16:07:09 +0000148documented here, since they are implemented by the OS dependent
149module. If the OS dependent module doesn't define either of these,
150the functions that rely on it will raise an exception. They are
Fred Drakec4f15af1998-03-10 03:17:26 +0000151documented in the section on module \module{posix}, together with all
152other functions that \module{os} imports from the OS dependent module.)