Fred Drake | 3a0351c | 1998-04-04 07:23:21 +0000 | [diff] [blame] | 1 | \section{Standard Module \module{os}} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 2 | \declaremodule{standard}{os} |
| 3 | |
| 4 | \modulesynopsis{Miscellaneous OS interfaces.} |
| 5 | |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 7 | This module provides a more portable way of using operating system |
| 8 | (OS) dependent functionality than importing an OS dependent built-in |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 9 | module like \module{posix}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 10 | |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 11 | When the optional built-in module \module{posix} is available, this |
| 12 | module exports the same functions and data as \module{posix}; otherwise, |
| 13 | it searches for an OS dependent built-in module like \module{mac} and |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 14 | exports the same functions and data as found there. The design of all |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 15 | Python's built-in OS dependent modules is such that as long as the same |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 16 | functionality is available, it uses the same interface; e.g., the |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 17 | function \code{os.stat(\var{file})} returns stat info about \var{file} |
| 18 | in a format compatible with the \POSIX{} interface. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 19 | |
| 20 | Extensions peculiar to a particular OS are also available through the |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 21 | \module{os} module, but using them is of course a threat to |
| 22 | portability! |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 23 | |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 24 | Note that after the first time \module{os} is imported, there is |
| 25 | \emph{no} performance penalty in using functions from \module{os} |
| 26 | instead of directly from the OS dependent built-in module, so there |
| 27 | should be \emph{no} reason not to use \module{os}! |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 28 | |
| 29 | In addition to whatever the correct OS dependent module exports, the |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 30 | following variables and functions are always exported by \module{os}: |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 32 | \begin{datadesc}{name} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 33 | The name of the OS dependent module imported. The following names |
| 34 | have currently been registered: \code{'posix'}, \code{'nt'}, |
| 35 | \code{'dos'}, \code{'mac'}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 36 | \end{datadesc} |
| 37 | |
| 38 | \begin{datadesc}{path} |
| 39 | The corresponding OS dependent standard module for pathname |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 40 | operations, e.g., \module{posixpath} or \module{macpath}. Thus, (given |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 41 | the proper imports), \code{os.path.split(\var{file})} is equivalent to but |
| 42 | more portable than \code{posixpath.split(\var{file})}. |
| 43 | \end{datadesc} |
| 44 | |
| 45 | \begin{datadesc}{curdir} |
| 46 | The constant string used by the OS to refer to the current directory, |
Fred Drake | 1a3c2a0 | 1998-08-06 15:18:23 +0000 | [diff] [blame] | 47 | e.g.\ \code{'.'} for \POSIX{} or \code{':'} for the Macintosh. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 48 | \end{datadesc} |
| 49 | |
| 50 | \begin{datadesc}{pardir} |
| 51 | The constant string used by the OS to refer to the parent directory, |
Fred Drake | 1a3c2a0 | 1998-08-06 15:18:23 +0000 | [diff] [blame] | 52 | e.g.\ \code{'..'} for \POSIX{} or \code{'::'} for the Macintosh. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 53 | \end{datadesc} |
| 54 | |
| 55 | \begin{datadesc}{sep} |
Guido van Rossum | b2afc81 | 1997-08-29 22:37:44 +0000 | [diff] [blame] | 56 | The character used by the OS to separate pathname components, |
Fred Drake | 1a3c2a0 | 1998-08-06 15:18:23 +0000 | [diff] [blame] | 57 | e.g.\ \character{/} for \POSIX{} or \character{:} for the Macintosh. |
| 58 | Note that knowing this is not sufficient to be able to parse or |
| 59 | concatenate pathnames --- use \function{os.path.split()} and |
| 60 | \function{os.path.join()} --- but it is occasionally useful. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 61 | \end{datadesc} |
| 62 | |
Guido van Rossum | b2afc81 | 1997-08-29 22:37:44 +0000 | [diff] [blame] | 63 | \begin{datadesc}{altsep} |
| 64 | An alternative character used by the OS to separate pathname components, |
| 65 | or \code{None} if only one separator character exists. This is set to |
Fred Drake | 1a3c2a0 | 1998-08-06 15:18:23 +0000 | [diff] [blame] | 66 | \character{/} on DOS/Windows systems where \code{sep} is a backslash. |
Guido van Rossum | b2afc81 | 1997-08-29 22:37:44 +0000 | [diff] [blame] | 67 | \end{datadesc} |
| 68 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 69 | \begin{datadesc}{pathsep} |
| 70 | The character conventionally used by the OS to separate search patch |
Fred Drake | 1a3c2a0 | 1998-08-06 15:18:23 +0000 | [diff] [blame] | 71 | components (as in \envvar{PATH}), e.g.\ \character{:} for \POSIX{} or |
| 72 | \character{;} for MS-DOS. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 73 | \end{datadesc} |
| 74 | |
Guido van Rossum | 9c59ce9 | 1998-06-30 15:54:27 +0000 | [diff] [blame] | 75 | \begin{datadesc}{linesep} |
| 76 | The string used to separate (or, rather, terminate) lines on the |
| 77 | current platform. This may be a single character, e.g. \code{'\e n'} |
| 78 | for \POSIX{} or \code{'\e r'} for MacOS, or multiple characters, |
| 79 | e.g. \code{'\e r\e n'} for MS-DOS. |
| 80 | \end{datadesc} |
| 81 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 82 | \begin{datadesc}{defpath} |
Fred Drake | 1a3c2a0 | 1998-08-06 15:18:23 +0000 | [diff] [blame] | 83 | The default search path used by \function{exec*p*()} if the environment |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 84 | doesn't have a \code{'PATH'} key. |
| 85 | \end{datadesc} |
| 86 | |
Guido van Rossum | 89a79d1 | 1998-07-24 20:48:20 +0000 | [diff] [blame] | 87 | \begin{funcdesc}{makedirs}{path\optional{, mode}} |
Fred Drake | 56fa8a7 | 1998-08-06 13:45:42 +0000 | [diff] [blame] | 88 | \versionadded{1.5.2} |
Guido van Rossum | 89a79d1 | 1998-07-24 20:48:20 +0000 | [diff] [blame] | 89 | Recursive directory creation function. Like \function{mkdir()}, |
| 90 | but makes all intermediate-level directories needed to contain the |
| 91 | leaf directory. Throws an \exception{os.error} exception if the leaf |
| 92 | directory already exists or cannot be created. The default \var{mode} |
| 93 | is \code{0777} (octal). |
| 94 | \end{funcdesc} |
| 95 | |
| 96 | \begin{funcdesc}{removedirs}{path} |
Fred Drake | 56fa8a7 | 1998-08-06 13:45:42 +0000 | [diff] [blame] | 97 | \versionadded{1.5.2} |
Guido van Rossum | 89a79d1 | 1998-07-24 20:48:20 +0000 | [diff] [blame] | 98 | Recursive directory removal function. Works like |
| 99 | \function{rmdir()} except that, if the leaf directory is |
| 100 | successfully removed, directories corresponding to rightmost path |
| 101 | segments will be pruned way until either the whole path is consumed or |
| 102 | an error is raised (which is ignored, because it generally means that |
| 103 | a parent directory is not empty). Throws an \exception{os.error} |
| 104 | exception if the leaf directory could not be successfully removed. |
| 105 | \end{funcdesc} |
| 106 | |
| 107 | \begin{funcdesc}{renames}{path} |
Fred Drake | 56fa8a7 | 1998-08-06 13:45:42 +0000 | [diff] [blame] | 108 | \versionadded{1.5.2} |
Guido van Rossum | 89a79d1 | 1998-07-24 20:48:20 +0000 | [diff] [blame] | 109 | Recursive directory or file renaming function. |
| 110 | Works like \function{rename()}, except creation of any intermediate |
| 111 | directories needed to make the new pathname good is attempted first. |
| 112 | After the rename, directories corresponding to rightmost path segments |
| 113 | of the old name will be pruned away using \function{removedirs()}. |
| 114 | |
| 115 | Note: this function can fail with the new directory structure made if |
| 116 | you lack permissions needed to remove the leaf directory or file. |
| 117 | \end{funcdesc} |
| 118 | |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 119 | \begin{funcdesc}{execl}{path, arg0, arg1, ...} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 120 | This is equivalent to |
Fred Drake | 1a3c2a0 | 1998-08-06 15:18:23 +0000 | [diff] [blame] | 121 | \samp{execv(\var{path}, (\var{arg0}, \var{arg1}, ...))}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 122 | \end{funcdesc} |
| 123 | |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 124 | \begin{funcdesc}{execle}{path, arg0, arg1, ..., env} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 125 | This is equivalent to |
Fred Drake | 1a3c2a0 | 1998-08-06 15:18:23 +0000 | [diff] [blame] | 126 | \samp{execve(\var{path}, (\var{arg0}, \var{arg1}, ...), \var{env})}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 127 | \end{funcdesc} |
| 128 | |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 129 | \begin{funcdesc}{execlp}{path, arg0, arg1, ...} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 130 | This is equivalent to |
Fred Drake | 1a3c2a0 | 1998-08-06 15:18:23 +0000 | [diff] [blame] | 131 | \samp{execvp(\var{path}, (\var{arg0}, \var{arg1}, ...))}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 132 | \end{funcdesc} |
| 133 | |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 134 | \begin{funcdesc}{execvp}{path, args} |
Fred Drake | 1a3c2a0 | 1998-08-06 15:18:23 +0000 | [diff] [blame] | 135 | This is like \samp{execv(\var{path}, \var{args})} but duplicates |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 136 | the shell's actions in searching for an executable file in a list of |
| 137 | directories. The directory list is obtained from |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 138 | \code{environ['PATH']}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 139 | \end{funcdesc} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 140 | |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 141 | \begin{funcdesc}{execvpe}{path, args, env} |
| 142 | This is a cross between \function{execve()} and \function{execvp()}. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 143 | The directory list is obtained from \code{\var{env}['PATH']}. |
| 144 | \end{funcdesc} |
| 145 | |
Fred Drake | 1a3c2a0 | 1998-08-06 15:18:23 +0000 | [diff] [blame] | 146 | (The functions \function{execv()} and \function{execve()} are not |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 147 | documented here, since they are implemented by the OS dependent |
| 148 | module. If the OS dependent module doesn't define either of these, |
| 149 | the functions that rely on it will raise an exception. They are |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 150 | documented in the section on module \module{posix}, together with all |
| 151 | other functions that \module{os} imports from the OS dependent module.) |