blob: 5568d4812c5cfb01d1e91374cf7792e1075ca0ed [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}
Fred Drake86929981998-12-28 21:58:15 +000090\index{directory!creating}
Guido van Rossum89a79d11998-07-24 20:48:20 +000091Recursive directory creation function. Like \function{mkdir()},
92but makes all intermediate-level directories needed to contain the
93leaf directory. Throws an \exception{os.error} exception if the leaf
94directory already exists or cannot be created. The default \var{mode}
95is \code{0777} (octal).
96\end{funcdesc}
97
98\begin{funcdesc}{removedirs}{path}
Fred Drake56fa8a71998-08-06 13:45:42 +000099\versionadded{1.5.2}
Fred Drake86929981998-12-28 21:58:15 +0000100\index{directory!deleting}
Guido van Rossum89a79d11998-07-24 20:48:20 +0000101Recursive directory removal function. Works like
102\function{rmdir()} except that, if the leaf directory is
103successfully removed, directories corresponding to rightmost path
104segments will be pruned way until either the whole path is consumed or
105an error is raised (which is ignored, because it generally means that
106a parent directory is not empty). Throws an \exception{os.error}
107exception if the leaf directory could not be successfully removed.
108\end{funcdesc}
109
Fred Drakee7210da1998-08-17 13:29:06 +0000110\begin{funcdesc}{renames}{old, new}
Fred Drake56fa8a71998-08-06 13:45:42 +0000111\versionadded{1.5.2}
Guido van Rossum89a79d11998-07-24 20:48:20 +0000112Recursive directory or file renaming function.
113Works like \function{rename()}, except creation of any intermediate
114directories needed to make the new pathname good is attempted first.
115After the rename, directories corresponding to rightmost path segments
116of the old name will be pruned away using \function{removedirs()}.
117
118Note: this function can fail with the new directory structure made if
119you lack permissions needed to remove the leaf directory or file.
120\end{funcdesc}
121
Fred Drakec4f15af1998-03-10 03:17:26 +0000122\begin{funcdesc}{execl}{path, arg0, arg1, ...}
Guido van Rossum470be141995-03-17 16:07:09 +0000123This is equivalent to
Fred Drake1a3c2a01998-08-06 15:18:23 +0000124\samp{execv(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000125\end{funcdesc}
126
Fred Drakec4f15af1998-03-10 03:17:26 +0000127\begin{funcdesc}{execle}{path, arg0, arg1, ..., env}
Guido van Rossum470be141995-03-17 16:07:09 +0000128This is equivalent to
Fred Drake1a3c2a01998-08-06 15:18:23 +0000129\samp{execve(\var{path}, (\var{arg0}, \var{arg1}, ...), \var{env})}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000130\end{funcdesc}
131
Fred Drakec4f15af1998-03-10 03:17:26 +0000132\begin{funcdesc}{execlp}{path, arg0, arg1, ...}
Guido van Rossum470be141995-03-17 16:07:09 +0000133This is equivalent to
Fred Drake1a3c2a01998-08-06 15:18:23 +0000134\samp{execvp(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000135\end{funcdesc}
136
Fred Drakec4f15af1998-03-10 03:17:26 +0000137\begin{funcdesc}{execvp}{path, args}
Fred Drake1a3c2a01998-08-06 15:18:23 +0000138This is like \samp{execv(\var{path}, \var{args})} but duplicates
Guido van Rossum470be141995-03-17 16:07:09 +0000139the shell's actions in searching for an executable file in a list of
140directories. The directory list is obtained from
Fred Drakec4f15af1998-03-10 03:17:26 +0000141\code{environ['PATH']}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000142\end{funcdesc}
Guido van Rossum470be141995-03-17 16:07:09 +0000143
Fred Drakec4f15af1998-03-10 03:17:26 +0000144\begin{funcdesc}{execvpe}{path, args, env}
145This is a cross between \function{execve()} and \function{execvp()}.
Guido van Rossum470be141995-03-17 16:07:09 +0000146The directory list is obtained from \code{\var{env}['PATH']}.
147\end{funcdesc}
148
Fred Drake1a3c2a01998-08-06 15:18:23 +0000149(The functions \function{execv()} and \function{execve()} are not
Guido van Rossum470be141995-03-17 16:07:09 +0000150documented here, since they are implemented by the OS dependent
151module. If the OS dependent module doesn't define either of these,
152the functions that rely on it will raise an exception. They are
Fred Drakec4f15af1998-03-10 03:17:26 +0000153documented in the section on module \module{posix}, together with all
154other functions that \module{os} imports from the OS dependent module.)