Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1 | \section{Standard Module \sectcode{os}} |
| 2 | |
| 3 | \stmodindex{os} |
| 4 | This module provides a more portable way of using operating system |
| 5 | (OS) dependent functionality than importing an OS dependent built-in |
| 6 | module like \code{posix}. |
| 7 | |
| 8 | When the optional built-in module \code{posix} is available, this |
| 9 | module exports the same functions and data as \code{posix}; otherwise, |
| 10 | it searches for an OS dependent built-in module like \code{mac} and |
| 11 | 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] | 12 | 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] | 13 | functionality is available, it uses the same interface; e.g., the |
| 14 | function \code{os.stat(\var{file})} returns stat info about a \var{file} in a |
| 15 | format compatible with the POSIX interface. |
| 16 | |
| 17 | Extensions peculiar to a particular OS are also available through the |
| 18 | \code{os} module, but using them is of course a threat to portability! |
| 19 | |
| 20 | Note that after the first time \code{os} is imported, there is \emph{no} |
| 21 | performance penalty in using functions from \code{os} instead of |
| 22 | directly from the OS dependent built-in module, so there should be |
| 23 | \emph{no} reason not to use \code{os}! |
| 24 | |
| 25 | In addition to whatever the correct OS dependent module exports, the |
| 26 | following variables and functions are always exported by \code{os}: |
| 27 | |
| 28 | \renewcommand{\indexsubitem}{(in module os)} |
| 29 | \begin{datadesc}{name} |
| 30 | The name of the OS dependent module imported, e.g. \code{'posix'} or |
| 31 | \code{'mac'}. |
| 32 | \end{datadesc} |
| 33 | |
| 34 | \begin{datadesc}{path} |
| 35 | The corresponding OS dependent standard module for pathname |
| 36 | operations, e.g., \code{posixpath} or \code{macpath}. Thus, (given |
| 37 | the proper imports), \code{os.path.split(\var{file})} is equivalent to but |
| 38 | more portable than \code{posixpath.split(\var{file})}. |
| 39 | \end{datadesc} |
| 40 | |
| 41 | \begin{datadesc}{curdir} |
| 42 | The constant string used by the OS to refer to the current directory, |
| 43 | e.g. \code{'.'} for POSIX or \code{':'} for the Mac. |
| 44 | \end{datadesc} |
| 45 | |
| 46 | \begin{datadesc}{pardir} |
| 47 | The constant string used by the OS to refer to the parent directory, |
| 48 | e.g. \code{'..'} for POSIX or \code{'::'} for the Mac. |
| 49 | \end{datadesc} |
| 50 | |
| 51 | \begin{datadesc}{sep} |
| 52 | The character used by the OS to separate pathname components, e.g. |
| 53 | \code{'/'} for POSIX or \code{':'} for the Mac. Note that knowing this |
| 54 | is not sufficient to be able to parse or concatenate pathnames---better |
| 55 | use \code{os.path.split()} and \code{os.path.join()}---but it is |
| 56 | occasionally useful. |
| 57 | \end{datadesc} |
| 58 | |
| 59 | \begin{funcdesc}{execl}{path\, arg0\, arg1\, ...} |
| 60 | This is equivalent to a call to \code{os.execv} with an \var{argv} |
| 61 | of \code{[\var{arg0}, \var{arg1}, ...]}. |
| 62 | \end{funcdesc} |
| 63 | |
| 64 | \begin{funcdesc}{execle}{path\, arg0\, arg1\, ...\, env} |
| 65 | This is equivalent to a call to \code{os.execve} with an \var{argv} |
| 66 | of \code{[\var{arg0}, \var{arg1}, ...]}. |
| 67 | \end{funcdesc} |
| 68 | |
| 69 | \begin{funcdesc}{execlp}{path\, arg0\, arg1\, ...} |
| 70 | This is like \code{execl} but duplicates the shell's actions in |
| 71 | searching for an executable file in a list of directories. The |
| 72 | directory list is obtained from \code{environ['PATH']}. |
| 73 | \end{funcdesc} |
| 74 | |
| 75 | \begin{funcdesc}{execvp}{path\, arg0\, arg1\, ...} |
| 76 | \code{execvp} is for \code{execv} what \code{execlp} is for \code{execl}. |
| 77 | \end{funcdesc} |