Fred Drake | 3a0351c | 1998-04-04 07:23:21 +0000 | [diff] [blame] | 1 | \section{Standard Module \module{os}} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 2 | \label{module-os} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 3 | \stmodindex{os} |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 5 | This module provides a more portable way of using operating system |
| 6 | (OS) dependent functionality than importing an OS dependent built-in |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 7 | module like \module{posix}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 8 | |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 9 | When the optional built-in module \module{posix} is available, this |
| 10 | module exports the same functions and data as \module{posix}; otherwise, |
| 11 | 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] | 12 | 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] | 13 | 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] | 14 | functionality is available, it uses the same interface; e.g., the |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 15 | function \code{os.stat(\var{file})} returns stat info about \var{file} |
| 16 | in a format compatible with the \POSIX{} interface. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 17 | |
| 18 | Extensions peculiar to a particular OS are also available through the |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 19 | \module{os} module, but using them is of course a threat to |
| 20 | portability! |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 21 | |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 22 | Note that after the first time \module{os} is imported, there is |
| 23 | \emph{no} performance penalty in using functions from \module{os} |
| 24 | instead of directly from the OS dependent built-in module, so there |
| 25 | should be \emph{no} reason not to use \module{os}! |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 26 | |
| 27 | In addition to whatever the correct OS dependent module exports, the |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 28 | following variables and functions are always exported by \module{os}: |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 30 | \begin{datadesc}{name} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 31 | The name of the OS dependent module imported. The following names |
| 32 | have currently been registered: \code{'posix'}, \code{'nt'}, |
| 33 | \code{'dos'}, \code{'mac'}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 34 | \end{datadesc} |
| 35 | |
| 36 | \begin{datadesc}{path} |
| 37 | The corresponding OS dependent standard module for pathname |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 38 | operations, e.g., \module{posixpath} or \module{macpath}. Thus, (given |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 39 | the proper imports), \code{os.path.split(\var{file})} is equivalent to but |
| 40 | more portable than \code{posixpath.split(\var{file})}. |
| 41 | \end{datadesc} |
| 42 | |
| 43 | \begin{datadesc}{curdir} |
| 44 | The constant string used by the OS to refer to the current directory, |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 45 | e.g. \code{'.'} for \POSIX{} or \code{':'} for the Macintosh. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 46 | \end{datadesc} |
| 47 | |
| 48 | \begin{datadesc}{pardir} |
| 49 | The constant string used by the OS to refer to the parent directory, |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 50 | e.g. \code{'..'} for \POSIX{} or \code{'::'} for the Macintosh. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 51 | \end{datadesc} |
| 52 | |
| 53 | \begin{datadesc}{sep} |
Guido van Rossum | b2afc81 | 1997-08-29 22:37:44 +0000 | [diff] [blame] | 54 | The character used by the OS to separate pathname components, |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 55 | e.g. \code{'/'} for \POSIX{} or \code{':'} for the Macintosh. Note that |
Fred Drake | 65b32f7 | 1998-02-09 20:27:12 +0000 | [diff] [blame] | 56 | knowing this is not sufficient to be able to parse or concatenate |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 57 | pathnames --- better use \function{os.path.split()} and |
| 58 | \function{os.path.join()}---but it is occasionally useful. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 59 | \end{datadesc} |
| 60 | |
Guido van Rossum | b2afc81 | 1997-08-29 22:37:44 +0000 | [diff] [blame] | 61 | \begin{datadesc}{altsep} |
| 62 | An alternative character used by the OS to separate pathname components, |
| 63 | or \code{None} if only one separator character exists. This is set to |
| 64 | \code{'/'} on DOS/Windows systems where \code{sep} is a backslash. |
| 65 | \end{datadesc} |
| 66 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 67 | \begin{datadesc}{pathsep} |
| 68 | The character conventionally used by the OS to separate search patch |
Fred Drake | 65b32f7 | 1998-02-09 20:27:12 +0000 | [diff] [blame] | 69 | components (as in \code{\$PATH}), e.g.\ \code{':'} for \POSIX{} or |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 70 | \code{';'} for MS-DOS. |
| 71 | \end{datadesc} |
| 72 | |
Guido van Rossum | 9c59ce9 | 1998-06-30 15:54:27 +0000 | [diff] [blame] | 73 | \begin{datadesc}{linesep} |
| 74 | The string used to separate (or, rather, terminate) lines on the |
| 75 | current platform. This may be a single character, e.g. \code{'\e n'} |
| 76 | for \POSIX{} or \code{'\e r'} for MacOS, or multiple characters, |
| 77 | e.g. \code{'\e r\e n'} for MS-DOS. |
| 78 | \end{datadesc} |
| 79 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 80 | \begin{datadesc}{defpath} |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 81 | The default search path used by \code{exec*p*()} if the environment |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 82 | doesn't have a \code{'PATH'} key. |
| 83 | \end{datadesc} |
| 84 | |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 85 | \begin{funcdesc}{execl}{path, arg0, arg1, ...} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 86 | This is equivalent to |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 87 | \code{execv(\var{path}, (\var{arg0}, \var{arg1}, ...))}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 88 | \end{funcdesc} |
| 89 | |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 90 | \begin{funcdesc}{execle}{path, arg0, arg1, ..., env} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 91 | This is equivalent to |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 92 | \code{execve(\var{path}, (\var{arg0}, \var{arg1}, ...), \var{env})}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 93 | \end{funcdesc} |
| 94 | |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 95 | \begin{funcdesc}{execlp}{path, arg0, arg1, ...} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 96 | This is equivalent to |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 97 | \code{execvp(\var{path}, (\var{arg0}, \var{arg1}, ...))}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 98 | \end{funcdesc} |
| 99 | |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 100 | \begin{funcdesc}{execvp}{path, args} |
| 101 | This is like \code{execv(\var{path}, \var{args})} but duplicates |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 102 | the shell's actions in searching for an executable file in a list of |
| 103 | directories. The directory list is obtained from |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 104 | \code{environ['PATH']}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 105 | \end{funcdesc} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 106 | |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 107 | \begin{funcdesc}{execvpe}{path, args, env} |
| 108 | This is a cross between \function{execve()} and \function{execvp()}. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 109 | The directory list is obtained from \code{\var{env}['PATH']}. |
| 110 | \end{funcdesc} |
| 111 | |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 112 | (The functions \code{execv()} and \code{execve()} are not |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 113 | documented here, since they are implemented by the OS dependent |
| 114 | module. If the OS dependent module doesn't define either of these, |
| 115 | the functions that rely on it will raise an exception. They are |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 116 | documented in the section on module \module{posix}, together with all |
| 117 | other functions that \module{os} imports from the OS dependent module.) |