Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{os} --- |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 2 | Miscellaneous operating system interfaces} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | ec6baaf | 1999-04-21 18:13:31 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{os} |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 5 | \modulesynopsis{Miscellaneous operating system interfaces.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 6 | |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 8 | This module provides a more portable way of using operating system |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 9 | dependent functionality than importing a operating system dependent |
| 10 | built-in module like \refmodule{posix} or \module{nt}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 11 | |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 12 | This module searches for an operating system dependent built-in module like |
Fred Drake | 2f97901 | 1999-06-11 18:28:37 +0000 | [diff] [blame] | 13 | \module{mac} or \refmodule{posix} and exports the same functions and data |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 14 | as found there. The design of all Python's built-in operating system dependent |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 15 | modules is such that as long as the same functionality is available, |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 16 | it uses the same interface; for example, the function |
Fred Drake | ec6baaf | 1999-04-21 18:13:31 +0000 | [diff] [blame] | 17 | \code{os.stat(\var{path})} returns stat information about \var{path} in |
| 18 | the same format (which happens to have originated with the |
| 19 | \POSIX{} interface). |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 20 | |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 21 | Extensions peculiar to a particular operating system are also |
| 22 | available through the \module{os} module, but using them is of course a |
| 23 | threat to portability! |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 24 | |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 25 | Note that after the first time \module{os} is imported, there is |
| 26 | \emph{no} performance penalty in using functions from \module{os} |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 27 | instead of directly from the operating system dependent built-in module, |
| 28 | so there should be \emph{no} reason not to use \module{os}! |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 29 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 30 | |
Fred Drake | 859dc53 | 1999-07-01 13:54:40 +0000 | [diff] [blame] | 31 | % Frank Stajano <fstajano@uk.research.att.com> complained that it |
| 32 | % wasn't clear that the entries described in the subsections were all |
| 33 | % available at the module level (most uses of subsections are |
| 34 | % different); I think this is only a problem for the HTML version, |
| 35 | % where the relationship may not be as clear. |
| 36 | % |
| 37 | \ifhtml |
| 38 | The \module{os} module contains many functions and data values. |
| 39 | The items below and in the following sub-sections are all available |
| 40 | directly from the \module{os} module. |
| 41 | \fi |
| 42 | |
| 43 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 44 | \begin{excdesc}{error} |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 45 | This exception is raised when a function returns a system-related |
| 46 | error (not for illegal argument types or other incidental errors). |
| 47 | This is also known as the built-in exception \exception{OSError}. The |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 48 | accompanying value is a pair containing the numeric error code from |
| 49 | \cdata{errno} and the corresponding string, as would be printed by the |
| 50 | C function \cfunction{perror()}. See the module |
| 51 | \refmodule{errno}\refbimodindex{errno}, which contains names for the |
| 52 | error codes defined by the underlying operating system. |
| 53 | |
| 54 | When exceptions are classes, this exception carries two attributes, |
| 55 | \member{errno} and \member{strerror}. The first holds the value of |
| 56 | the C \cdata{errno} variable, and the latter holds the corresponding |
| 57 | error message from \cfunction{strerror()}. For exceptions that |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 58 | involve a file system path (such as \function{chdir()} or |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 59 | \function{unlink()}), the exception instance will contain a third |
| 60 | attribute, \member{filename}, which is the file name passed to the |
| 61 | function. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 62 | \end{excdesc} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 64 | \begin{datadesc}{name} |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 65 | The name of the operating system dependent module imported. The |
Fred Drake | 6995bb6 | 2001-11-29 20:48:44 +0000 | [diff] [blame] | 66 | following names have currently been registered: \code{'posix'}, |
Martin v. Löwis | 36a4d8c | 2002-10-10 18:24:54 +0000 | [diff] [blame] | 67 | \code{'nt'}, \code{'mac'}, \code{'os2'}, \code{'ce'}, |
Fred Drake | 6995bb6 | 2001-11-29 20:48:44 +0000 | [diff] [blame] | 68 | \code{'java'}, \code{'riscos'}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 69 | \end{datadesc} |
| 70 | |
| 71 | \begin{datadesc}{path} |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 72 | The corresponding operating system dependent standard module for pathname |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 73 | operations, such as \module{posixpath} or \module{macpath}. Thus, |
| 74 | given the proper imports, \code{os.path.split(\var{file})} is |
| 75 | equivalent to but more portable than |
| 76 | \code{posixpath.split(\var{file})}. Note that this is also an |
| 77 | importable module: it may be imported directly as |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 78 | \refmodule{os.path}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 79 | \end{datadesc} |
| 80 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 81 | |
| 82 | |
| 83 | \subsection{Process Parameters \label{os-procinfo}} |
| 84 | |
| 85 | These functions and data items provide information and operate on the |
| 86 | current process and user. |
| 87 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 88 | \begin{datadesc}{environ} |
Fred Drake | 0e1de8b | 1999-04-29 12:57:32 +0000 | [diff] [blame] | 89 | A mapping object representing the string environment. For example, |
| 90 | \code{environ['HOME']} is the pathname of your home directory (on some |
| 91 | platforms), and is equivalent to \code{getenv("HOME")} in C. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 92 | |
| 93 | If the platform supports the \function{putenv()} function, this |
| 94 | mapping may be used to modify the environment as well as query the |
| 95 | environment. \function{putenv()} will be called automatically when |
Neal Norwitz | 2b09bc4 | 2003-02-07 02:27:36 +0000 | [diff] [blame] | 96 | the mapping is modified. \note{On some platforms, including |
| 97 | FreeBSD and Mac OS X, setting \code{environ} may cause memory leaks. |
| 98 | Refer to the system documentation for putenv.} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 99 | |
| 100 | If \function{putenv()} is not provided, this mapping may be passed to |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 101 | the appropriate process-creation functions to cause child processes to |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 102 | use a modified environment. |
| 103 | \end{datadesc} |
| 104 | |
Fred Drake | 6db897c | 1999-07-12 16:49:30 +0000 | [diff] [blame] | 105 | \begin{funcdescni}{chdir}{path} |
Fred Drake | e19a5bc | 2002-04-15 19:46:40 +0000 | [diff] [blame] | 106 | \funclineni{fchdir}{fd} |
Fred Drake | 6db897c | 1999-07-12 16:49:30 +0000 | [diff] [blame] | 107 | \funclineni{getcwd}{} |
| 108 | These functions are described in ``Files and Directories'' (section |
| 109 | \ref{os-file-dir}). |
| 110 | \end{funcdescni} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 111 | |
Fred Drake | 18f7a45 | 1999-12-09 22:11:43 +0000 | [diff] [blame] | 112 | \begin{funcdesc}{ctermid}{} |
| 113 | Return the filename corresponding to the controlling terminal of the |
| 114 | process. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 115 | Availability: \UNIX. |
Fred Drake | 18f7a45 | 1999-12-09 22:11:43 +0000 | [diff] [blame] | 116 | \end{funcdesc} |
| 117 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 118 | \begin{funcdesc}{getegid}{} |
Fred Drake | d3e6678 | 2002-04-26 20:59:40 +0000 | [diff] [blame] | 119 | Return the effective group id of the current process. This |
| 120 | corresponds to the `set id' bit on the file being executed in the |
| 121 | current process. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 122 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 123 | \end{funcdesc} |
| 124 | |
| 125 | \begin{funcdesc}{geteuid}{} |
Fred Drake | 6b330ba8 | 1999-05-25 13:42:26 +0000 | [diff] [blame] | 126 | \index{user!effective id} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 127 | Return the current process' effective user id. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 128 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 129 | \end{funcdesc} |
| 130 | |
| 131 | \begin{funcdesc}{getgid}{} |
Fred Drake | 6b330ba8 | 1999-05-25 13:42:26 +0000 | [diff] [blame] | 132 | \index{process!group} |
Fred Drake | d3e6678 | 2002-04-26 20:59:40 +0000 | [diff] [blame] | 133 | Return the real group id of the current process. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 134 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 135 | \end{funcdesc} |
| 136 | |
Fred Drake | 88f6ca2 | 1999-12-15 19:39:04 +0000 | [diff] [blame] | 137 | \begin{funcdesc}{getgroups}{} |
| 138 | Return list of supplemental group ids associated with the current |
| 139 | process. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 140 | Availability: \UNIX. |
Fred Drake | 88f6ca2 | 1999-12-15 19:39:04 +0000 | [diff] [blame] | 141 | \end{funcdesc} |
| 142 | |
| 143 | \begin{funcdesc}{getlogin}{} |
Jeremy Hylton | 403e351 | 2002-07-24 15:32:25 +0000 | [diff] [blame] | 144 | Return the name of the user logged in on the controlling terminal of |
| 145 | the process. For most purposes, it is more useful to use the |
Andrew M. Kuchling | 4b37364 | 2003-02-03 15:36:26 +0000 | [diff] [blame] | 146 | environment variable \envvar{LOGNAME} to find out who the user is, |
| 147 | or \code{pwd.getpwuid(os.getuid())[0]} to get the login name |
| 148 | of the currently effective user ID. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 149 | Availability: \UNIX. |
Fred Drake | 88f6ca2 | 1999-12-15 19:39:04 +0000 | [diff] [blame] | 150 | \end{funcdesc} |
| 151 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 152 | \begin{funcdesc}{getpgid}{pid} |
| 153 | Return the process group id of the process with process id \var{pid}. |
| 154 | If \var{pid} is 0, the process group id of the current process is |
| 155 | returned. Availability: \UNIX. |
Neal Norwitz | cc5c694 | 2002-06-13 21:19:25 +0000 | [diff] [blame] | 156 | \versionadded{2.3} |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 157 | \end{funcdesc} |
| 158 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 159 | \begin{funcdesc}{getpgrp}{} |
| 160 | \index{process!group} |
Fred Drake | d3e6678 | 2002-04-26 20:59:40 +0000 | [diff] [blame] | 161 | Return the id of the current process group. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 162 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 163 | \end{funcdesc} |
| 164 | |
| 165 | \begin{funcdesc}{getpid}{} |
| 166 | \index{process!id} |
| 167 | Return the current process id. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 168 | Availability: \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 169 | \end{funcdesc} |
| 170 | |
| 171 | \begin{funcdesc}{getppid}{} |
| 172 | \index{process!id of parent} |
| 173 | Return the parent's process id. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 174 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 175 | \end{funcdesc} |
| 176 | |
| 177 | \begin{funcdesc}{getuid}{} |
Fred Drake | 6b330ba8 | 1999-05-25 13:42:26 +0000 | [diff] [blame] | 178 | \index{user!id} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 179 | Return the current process' user id. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 180 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 181 | \end{funcdesc} |
| 182 | |
Fred Drake | 81e142b | 2001-05-31 20:27:46 +0000 | [diff] [blame] | 183 | \begin{funcdesc}{getenv}{varname\optional{, value}} |
| 184 | Return the value of the environment variable \var{varname} if it |
| 185 | exists, or \var{value} if it doesn't. \var{value} defaults to |
| 186 | \code{None}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 187 | Availability: most flavors of \UNIX, Windows. |
Fred Drake | 81e142b | 2001-05-31 20:27:46 +0000 | [diff] [blame] | 188 | \end{funcdesc} |
| 189 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 190 | \begin{funcdesc}{putenv}{varname, value} |
| 191 | \index{environment variables!setting} |
| 192 | Set the environment variable named \var{varname} to the string |
| 193 | \var{value}. Such changes to the environment affect subprocesses |
| 194 | started with \function{os.system()}, \function{popen()} or |
| 195 | \function{fork()} and \function{execv()}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 196 | Availability: most flavors of \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 197 | |
Neal Norwitz | 2b09bc4 | 2003-02-07 02:27:36 +0000 | [diff] [blame] | 198 | \note{On some platforms, including FreeBSD and Mac OS X, |
| 199 | setting \code{environ} may cause memory leaks. |
| 200 | Refer to the system documentation for putenv.} |
| 201 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 202 | When \function{putenv()} is |
| 203 | supported, assignments to items in \code{os.environ} are automatically |
| 204 | translated into corresponding calls to \function{putenv()}; however, |
| 205 | calls to \function{putenv()} don't update \code{os.environ}, so it is |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 206 | actually preferable to assign to items of \code{os.environ}. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 207 | \end{funcdesc} |
| 208 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 209 | \begin{funcdesc}{setegid}{egid} |
| 210 | Set the current process's effective group id. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 211 | Availability: \UNIX. |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 212 | \end{funcdesc} |
| 213 | |
| 214 | \begin{funcdesc}{seteuid}{euid} |
| 215 | Set the current process's effective user id. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 216 | Availability: \UNIX. |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 217 | \end{funcdesc} |
| 218 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 219 | \begin{funcdesc}{setgid}{gid} |
| 220 | Set the current process' group id. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 221 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 222 | \end{funcdesc} |
| 223 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 224 | \begin{funcdesc}{setgroups}{groups} |
Martin v. Löwis | c405133 | 2001-10-18 14:07:12 +0000 | [diff] [blame] | 225 | Set the list of supplemental group ids associated with the current |
| 226 | process to \var{groups}. \var{groups} must be a sequence, and each |
| 227 | element must be an integer identifying a group. This operation is |
| 228 | typical available only to the superuser. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 229 | Availability: \UNIX. |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 230 | \versionadded{2.2} |
| 231 | \end{funcdesc} |
| 232 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 233 | \begin{funcdesc}{setpgrp}{} |
| 234 | Calls the system call \cfunction{setpgrp()} or \cfunction{setpgrp(0, |
| 235 | 0)} depending on which version is implemented (if any). See the |
| 236 | \UNIX{} manual for the semantics. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 237 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 238 | \end{funcdesc} |
| 239 | |
Fred Drake | d3e6678 | 2002-04-26 20:59:40 +0000 | [diff] [blame] | 240 | \begin{funcdesc}{setpgid}{pid, pgrp} Calls the system call |
| 241 | \cfunction{setpgid()} to set the process group id of the process with |
| 242 | id \var{pid} to the process group with id \var{pgrp}. See the \UNIX{} |
| 243 | manual for the semantics. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 244 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 245 | \end{funcdesc} |
| 246 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 247 | \begin{funcdesc}{setreuid}{ruid, euid} |
| 248 | Set the current process's real and effective user ids. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 249 | Availability: \UNIX. |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 250 | \end{funcdesc} |
| 251 | |
| 252 | \begin{funcdesc}{setregid}{rgid, egid} |
| 253 | Set the current process's real and effective group ids. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 254 | Availability: \UNIX. |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 255 | \end{funcdesc} |
| 256 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 257 | \begin{funcdesc}{setsid}{} |
| 258 | Calls the system call \cfunction{setsid()}. See the \UNIX{} manual |
| 259 | for the semantics. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 260 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 261 | \end{funcdesc} |
| 262 | |
| 263 | \begin{funcdesc}{setuid}{uid} |
Fred Drake | 6b330ba8 | 1999-05-25 13:42:26 +0000 | [diff] [blame] | 264 | \index{user!id, setting} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 265 | Set the current process' user id. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 266 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 267 | \end{funcdesc} |
| 268 | |
| 269 | % placed in this section since it relates to errno.... a little weak ;-( |
| 270 | \begin{funcdesc}{strerror}{code} |
| 271 | Return the error message corresponding to the error code in |
| 272 | \var{code}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 273 | Availability: \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 274 | \end{funcdesc} |
| 275 | |
| 276 | \begin{funcdesc}{umask}{mask} |
| 277 | Set the current numeric umask and returns the previous umask. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 278 | Availability: \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 279 | \end{funcdesc} |
| 280 | |
| 281 | \begin{funcdesc}{uname}{} |
| 282 | Return a 5-tuple containing information identifying the current |
| 283 | operating system. The tuple contains 5 strings: |
| 284 | \code{(\var{sysname}, \var{nodename}, \var{release}, \var{version}, |
| 285 | \var{machine})}. Some systems truncate the nodename to 8 |
| 286 | characters or to the leading component; a better way to get the |
| 287 | hostname is \function{socket.gethostname()} |
| 288 | \withsubitem{(in module socket)}{\ttindex{gethostname()}} |
| 289 | or even |
| 290 | \withsubitem{(in module socket)}{\ttindex{gethostbyaddr()}} |
| 291 | \code{socket.gethostbyaddr(socket.gethostname())}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 292 | Availability: recent flavors of \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 293 | \end{funcdesc} |
| 294 | |
| 295 | |
| 296 | |
| 297 | \subsection{File Object Creation \label{os-newstreams}} |
| 298 | |
| 299 | These functions create new file objects. |
| 300 | |
| 301 | |
| 302 | \begin{funcdesc}{fdopen}{fd\optional{, mode\optional{, bufsize}}} |
| 303 | Return an open file object connected to the file descriptor \var{fd}. |
Fred Drake | 8c9fc00 | 1999-08-05 13:41:31 +0000 | [diff] [blame] | 304 | \index{I/O control!buffering} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 305 | The \var{mode} and \var{bufsize} arguments have the same meaning as |
| 306 | the corresponding arguments to the built-in \function{open()} |
| 307 | function. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 308 | Availability: Macintosh, \UNIX, Windows. |
Thomas Heller | 5b470e0 | 2002-11-07 16:33:44 +0000 | [diff] [blame] | 309 | |
| 310 | \versionchanged[When specified, the \var{mode} argument must now start |
Fred Drake | b5f41de | 2002-11-07 17:13:03 +0000 | [diff] [blame] | 311 | with one of the letters \character{r}, \character{w}, or \character{a}, |
| 312 | otherwise a \exception{ValueError} is raised]{2.3} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 313 | \end{funcdesc} |
| 314 | |
| 315 | \begin{funcdesc}{popen}{command\optional{, mode\optional{, bufsize}}} |
| 316 | Open a pipe to or from \var{command}. The return value is an open |
| 317 | file object connected to the pipe, which can be read or written |
| 318 | depending on whether \var{mode} is \code{'r'} (default) or \code{'w'}. |
| 319 | The \var{bufsize} argument has the same meaning as the corresponding |
| 320 | argument to the built-in \function{open()} function. The exit status of |
| 321 | the command (encoded in the format specified for \function{wait()}) is |
| 322 | available as the return value of the \method{close()} method of the file |
| 323 | object, except that when the exit status is zero (termination without |
Fred Drake | 1319e3e | 2000-10-03 17:14:27 +0000 | [diff] [blame] | 324 | errors), \code{None} is returned. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 325 | Availability: \UNIX, Windows. |
Fred Drake | c71c23e | 2000-10-04 13:57:27 +0000 | [diff] [blame] | 326 | |
| 327 | \versionchanged[This function worked unreliably under Windows in |
| 328 | earlier versions of Python. This was due to the use of the |
| 329 | \cfunction{_popen()} function from the libraries provided with |
| 330 | Windows. Newer versions of Python do not use the broken |
| 331 | implementation from the Windows libraries]{2.0} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 332 | \end{funcdesc} |
| 333 | |
Fred Drake | 18f7a45 | 1999-12-09 22:11:43 +0000 | [diff] [blame] | 334 | \begin{funcdesc}{tmpfile}{} |
Guido van Rossum | db9198a | 2002-06-10 19:23:22 +0000 | [diff] [blame] | 335 | Return a new file object opened in update mode (\samp{w+b}). The file |
Fred Drake | 18f7a45 | 1999-12-09 22:11:43 +0000 | [diff] [blame] | 336 | has no directory entries associated with it and will be automatically |
| 337 | deleted once there are no file descriptors for the file. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 338 | Availability: \UNIX, Windows. |
Fred Drake | 18f7a45 | 1999-12-09 22:11:43 +0000 | [diff] [blame] | 339 | \end{funcdesc} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 340 | |
| 341 | |
Fred Drake | 8a9db99 | 2000-09-28 20:27:51 +0000 | [diff] [blame] | 342 | For each of these \function{popen()} variants, if \var{bufsize} is |
| 343 | specified, it specifies the buffer size for the I/O pipes. |
| 344 | \var{mode}, if provided, should be the string \code{'b'} or |
| 345 | \code{'t'}; on Windows this is needed to determine whether the file |
| 346 | objects should be opened in binary or text mode. The default value |
| 347 | for \var{mode} is \code{'t'}. |
| 348 | |
Fred Drake | 098d7fa | 2001-09-11 19:56:51 +0000 | [diff] [blame] | 349 | These methods do not make it possible to retrieve the return code from |
| 350 | the child processes. The only way to control the input and output |
| 351 | streams and also retrieve the return codes is to use the |
| 352 | \class{Popen3} and \class{Popen4} classes from the \refmodule{popen2} |
| 353 | module; these are only available on \UNIX. |
| 354 | |
Fred Drake | 08d10f9 | 2002-12-06 16:45:05 +0000 | [diff] [blame] | 355 | For a discussion of possible deadlock conditions related to the use |
Fred Drake | 9ea01d4 | 2002-06-18 20:30:37 +0000 | [diff] [blame] | 356 | of these functions, see ``\ulink{Flow Control |
| 357 | Issues}{popen2-flow-control.html}'' |
| 358 | (section~\ref{popen2-flow-control}). |
| 359 | |
Fred Drake | 046f4d8 | 2001-06-11 15:21:48 +0000 | [diff] [blame] | 360 | \begin{funcdesc}{popen2}{cmd\optional{, mode\optional{, bufsize}}} |
Fred Drake | 8a9db99 | 2000-09-28 20:27:51 +0000 | [diff] [blame] | 361 | Executes \var{cmd} as a sub-process. Returns the file objects |
| 362 | \code{(\var{child_stdin}, \var{child_stdout})}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 363 | Availability: \UNIX, Windows. |
Fred Drake | 8a9db99 | 2000-09-28 20:27:51 +0000 | [diff] [blame] | 364 | \versionadded{2.0} |
| 365 | \end{funcdesc} |
| 366 | |
Fred Drake | 046f4d8 | 2001-06-11 15:21:48 +0000 | [diff] [blame] | 367 | \begin{funcdesc}{popen3}{cmd\optional{, mode\optional{, bufsize}}} |
Fred Drake | 8a9db99 | 2000-09-28 20:27:51 +0000 | [diff] [blame] | 368 | Executes \var{cmd} as a sub-process. Returns the file objects |
| 369 | \code{(\var{child_stdin}, \var{child_stdout}, \var{child_stderr})}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 370 | Availability: \UNIX, Windows. |
Fred Drake | 8a9db99 | 2000-09-28 20:27:51 +0000 | [diff] [blame] | 371 | \versionadded{2.0} |
| 372 | \end{funcdesc} |
| 373 | |
Fred Drake | 046f4d8 | 2001-06-11 15:21:48 +0000 | [diff] [blame] | 374 | \begin{funcdesc}{popen4}{cmd\optional{, mode\optional{, bufsize}}} |
Fred Drake | 8a9db99 | 2000-09-28 20:27:51 +0000 | [diff] [blame] | 375 | Executes \var{cmd} as a sub-process. Returns the file objects |
| 376 | \code{(\var{child_stdin}, \var{child_stdout_and_stderr})}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 377 | Availability: \UNIX, Windows. |
Fred Drake | 8a9db99 | 2000-09-28 20:27:51 +0000 | [diff] [blame] | 378 | \versionadded{2.0} |
| 379 | \end{funcdesc} |
| 380 | |
| 381 | This functionality is also available in the \refmodule{popen2} module |
| 382 | using functions of the same names, but the return values of those |
| 383 | functions have a different order. |
| 384 | |
| 385 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 386 | \subsection{File Descriptor Operations \label{os-fd-ops}} |
| 387 | |
| 388 | These functions operate on I/O streams referred to |
| 389 | using file descriptors. |
| 390 | |
| 391 | |
| 392 | \begin{funcdesc}{close}{fd} |
| 393 | Close file descriptor \var{fd}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 394 | Availability: Macintosh, \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 395 | |
| 396 | Note: this function is intended for low-level I/O and must be applied |
| 397 | to a file descriptor as returned by \function{open()} or |
| 398 | \function{pipe()}. To close a ``file object'' returned by the |
| 399 | built-in function \function{open()} or by \function{popen()} or |
| 400 | \function{fdopen()}, use its \method{close()} method. |
| 401 | \end{funcdesc} |
| 402 | |
| 403 | \begin{funcdesc}{dup}{fd} |
| 404 | Return a duplicate of file descriptor \var{fd}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 405 | Availability: Macintosh, \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 406 | \end{funcdesc} |
| 407 | |
| 408 | \begin{funcdesc}{dup2}{fd, fd2} |
| 409 | Duplicate file descriptor \var{fd} to \var{fd2}, closing the latter |
| 410 | first if necessary. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 411 | Availability: \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 412 | \end{funcdesc} |
| 413 | |
Raymond Hettinger | 3cfdc34 | 2002-08-07 15:48:17 +0000 | [diff] [blame] | 414 | \begin{funcdesc}{fdatasync}{fd} |
| 415 | Force write of file with filedescriptor \var{fd} to disk. |
| 416 | Does not force update of metadata. |
| 417 | Availability: \UNIX. |
| 418 | \end{funcdesc} |
| 419 | |
Fred Drake | 88f6ca2 | 1999-12-15 19:39:04 +0000 | [diff] [blame] | 420 | \begin{funcdesc}{fpathconf}{fd, name} |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 421 | Return system configuration information relevant to an open file. |
Fred Drake | 88f6ca2 | 1999-12-15 19:39:04 +0000 | [diff] [blame] | 422 | \var{name} specifies the configuration value to retrieve; it may be a |
| 423 | string which is the name of a defined system value; these names are |
Raymond Hettinger | b67449d | 2003-09-08 18:52:18 +0000 | [diff] [blame] | 424 | specified in a number of standards (\POSIX.1, \UNIX{} 95, \UNIX{} 98, and |
Fred Drake | 88f6ca2 | 1999-12-15 19:39:04 +0000 | [diff] [blame] | 425 | others). Some platforms define additional names as well. The names |
| 426 | known to the host operating system are given in the |
| 427 | \code{pathconf_names} dictionary. For configuration variables not |
| 428 | included in that mapping, passing an integer for \var{name} is also |
| 429 | accepted. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 430 | Availability: \UNIX. |
Fred Drake | 88f6ca2 | 1999-12-15 19:39:04 +0000 | [diff] [blame] | 431 | |
| 432 | If \var{name} is a string and is not known, \exception{ValueError} is |
| 433 | raised. If a specific value for \var{name} is not supported by the |
| 434 | host system, even if it is included in \code{pathconf_names}, an |
| 435 | \exception{OSError} is raised with \constant{errno.EINVAL} for the |
| 436 | error number. |
| 437 | \end{funcdesc} |
| 438 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 439 | \begin{funcdesc}{fstat}{fd} |
| 440 | Return status for file descriptor \var{fd}, like \function{stat()}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 441 | Availability: \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 442 | \end{funcdesc} |
| 443 | |
| 444 | \begin{funcdesc}{fstatvfs}{fd} |
| 445 | Return information about the filesystem containing the file associated |
| 446 | with file descriptor \var{fd}, like \function{statvfs()}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 447 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 448 | \end{funcdesc} |
| 449 | |
Raymond Hettinger | 3cfdc34 | 2002-08-07 15:48:17 +0000 | [diff] [blame] | 450 | \begin{funcdesc}{fsync}{fd} |
Tim Peters | 2d1c846 | 2003-04-23 19:47:14 +0000 | [diff] [blame] | 451 | Force write of file with filedescriptor \var{fd} to disk. On \UNIX, |
| 452 | this calls the native \cfunction{fsync()} function; on Windows, the |
| 453 | MS \cfunction{_commit()} function. |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 454 | |
Tim Peters | 2d1c846 | 2003-04-23 19:47:14 +0000 | [diff] [blame] | 455 | If you're starting with a Python file object \var{f}, first do |
Raymond Hettinger | 52136a8 | 2003-05-10 03:35:37 +0000 | [diff] [blame] | 456 | \code{\var{f}.flush()}, and then do \code{os.fsync(\var{f}.fileno())}, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 457 | to ensure that all internal buffers associated with \var{f} are written |
| 458 | to disk. |
Tim Peters | 2d1c846 | 2003-04-23 19:47:14 +0000 | [diff] [blame] | 459 | Availability: \UNIX, and Windows starting in 2.2.3. |
Raymond Hettinger | 3cfdc34 | 2002-08-07 15:48:17 +0000 | [diff] [blame] | 460 | \end{funcdesc} |
| 461 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 462 | \begin{funcdesc}{ftruncate}{fd, length} |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 463 | Truncate the file corresponding to file descriptor \var{fd}, |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 464 | so that it is at most \var{length} bytes in size. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 465 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 466 | \end{funcdesc} |
| 467 | |
Skip Montanaro | d372521 | 2000-07-19 17:30:58 +0000 | [diff] [blame] | 468 | \begin{funcdesc}{isatty}{fd} |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 469 | Return \code{True} if the file descriptor \var{fd} is open and |
| 470 | connected to a tty(-like) device, else \code{False}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 471 | Availability: \UNIX. |
Skip Montanaro | d372521 | 2000-07-19 17:30:58 +0000 | [diff] [blame] | 472 | \end{funcdesc} |
| 473 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 474 | \begin{funcdesc}{lseek}{fd, pos, how} |
| 475 | Set the current position of file descriptor \var{fd} to position |
| 476 | \var{pos}, modified by \var{how}: \code{0} to set the position |
| 477 | relative to the beginning of the file; \code{1} to set it relative to |
| 478 | the current position; \code{2} to set it relative to the end of the |
| 479 | file. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 480 | Availability: Macintosh, \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 481 | \end{funcdesc} |
| 482 | |
| 483 | \begin{funcdesc}{open}{file, flags\optional{, mode}} |
| 484 | Open the file \var{file} and set various flags according to |
| 485 | \var{flags} and possibly its mode according to \var{mode}. |
| 486 | The default \var{mode} is \code{0777} (octal), and the current umask |
| 487 | value is first masked out. Return the file descriptor for the newly |
| 488 | opened file. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 489 | Availability: Macintosh, \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 490 | |
| 491 | For a description of the flag and mode values, see the C run-time |
| 492 | documentation; flag constants (like \constant{O_RDONLY} and |
| 493 | \constant{O_WRONLY}) are defined in this module too (see below). |
| 494 | |
| 495 | Note: this function is intended for low-level I/O. For normal usage, |
| 496 | use the built-in function \function{open()}, which returns a ``file |
| 497 | object'' with \method{read()} and \method{write()} methods (and many |
| 498 | more). |
| 499 | \end{funcdesc} |
| 500 | |
Fred Drake | c82634c | 2000-06-28 17:27:48 +0000 | [diff] [blame] | 501 | \begin{funcdesc}{openpty}{} |
| 502 | Open a new pseudo-terminal pair. Return a pair of file descriptors |
| 503 | \code{(\var{master}, \var{slave})} for the pty and the tty, |
| 504 | respectively. For a (slightly) more portable approach, use the |
| 505 | \refmodule{pty}\refstmodindex{pty} module. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 506 | Availability: Some flavors of \UNIX. |
Fred Drake | c82634c | 2000-06-28 17:27:48 +0000 | [diff] [blame] | 507 | \end{funcdesc} |
| 508 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 509 | \begin{funcdesc}{pipe}{} |
| 510 | Create a pipe. Return a pair of file descriptors \code{(\var{r}, |
| 511 | \var{w})} usable for reading and writing, respectively. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 512 | Availability: \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 513 | \end{funcdesc} |
| 514 | |
| 515 | \begin{funcdesc}{read}{fd, n} |
| 516 | Read at most \var{n} bytes from file descriptor \var{fd}. |
Fred Drake | a65375c | 2002-05-01 03:31:42 +0000 | [diff] [blame] | 517 | Return a string containing the bytes read. If the end of the file |
| 518 | referred to by \var{fd} has been reached, an empty string is |
| 519 | returned. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 520 | Availability: Macintosh, \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 521 | |
| 522 | Note: this function is intended for low-level I/O and must be applied |
| 523 | to a file descriptor as returned by \function{open()} or |
| 524 | \function{pipe()}. To read a ``file object'' returned by the |
| 525 | built-in function \function{open()} or by \function{popen()} or |
| 526 | \function{fdopen()}, or \code{sys.stdin}, use its |
| 527 | \method{read()} or \method{readline()} methods. |
| 528 | \end{funcdesc} |
| 529 | |
| 530 | \begin{funcdesc}{tcgetpgrp}{fd} |
| 531 | Return the process group associated with the terminal given by |
| 532 | \var{fd} (an open file descriptor as returned by \function{open()}). |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 533 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 534 | \end{funcdesc} |
| 535 | |
| 536 | \begin{funcdesc}{tcsetpgrp}{fd, pg} |
| 537 | Set the process group associated with the terminal given by |
| 538 | \var{fd} (an open file descriptor as returned by \function{open()}) |
| 539 | to \var{pg}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 540 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 541 | \end{funcdesc} |
| 542 | |
| 543 | \begin{funcdesc}{ttyname}{fd} |
| 544 | Return a string which specifies the terminal device associated with |
| 545 | file-descriptor \var{fd}. If \var{fd} is not associated with a terminal |
| 546 | device, an exception is raised. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 547 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 548 | \end{funcdesc} |
| 549 | |
| 550 | \begin{funcdesc}{write}{fd, str} |
| 551 | Write the string \var{str} to file descriptor \var{fd}. |
| 552 | Return the number of bytes actually written. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 553 | Availability: Macintosh, \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 554 | |
| 555 | Note: this function is intended for low-level I/O and must be applied |
| 556 | to a file descriptor as returned by \function{open()} or |
| 557 | \function{pipe()}. To write a ``file object'' returned by the |
| 558 | built-in function \function{open()} or by \function{popen()} or |
| 559 | \function{fdopen()}, or \code{sys.stdout} or \code{sys.stderr}, use |
| 560 | its \method{write()} method. |
| 561 | \end{funcdesc} |
| 562 | |
| 563 | |
| 564 | The following data items are available for use in constructing the |
| 565 | \var{flags} parameter to the \function{open()} function. |
| 566 | |
| 567 | \begin{datadesc}{O_RDONLY} |
| 568 | \dataline{O_WRONLY} |
| 569 | \dataline{O_RDWR} |
| 570 | \dataline{O_NDELAY} |
| 571 | \dataline{O_NONBLOCK} |
| 572 | \dataline{O_APPEND} |
| 573 | \dataline{O_DSYNC} |
| 574 | \dataline{O_RSYNC} |
| 575 | \dataline{O_SYNC} |
| 576 | \dataline{O_NOCTTY} |
| 577 | \dataline{O_CREAT} |
| 578 | \dataline{O_EXCL} |
| 579 | \dataline{O_TRUNC} |
| 580 | Options for the \var{flag} argument to the \function{open()} function. |
| 581 | These can be bit-wise OR'd together. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 582 | Availability: Macintosh, \UNIX, Windows. |
Tim Peters | c48a3ca | 2002-01-30 05:49:46 +0000 | [diff] [blame] | 583 | % XXX O_NDELAY, O_NONBLOCK, O_DSYNC, O_RSYNC, O_SYNC, O_NOCTTY are not on Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 584 | \end{datadesc} |
| 585 | |
Fred Drake | 3ac977e | 2000-08-11 20:19:51 +0000 | [diff] [blame] | 586 | \begin{datadesc}{O_BINARY} |
| 587 | Option for the \var{flag} argument to the \function{open()} function. |
| 588 | This can be bit-wise OR'd together with those listed above. |
| 589 | Availability: Macintosh, Windows. |
| 590 | % XXX need to check on the availability of this one. |
| 591 | \end{datadesc} |
| 592 | |
Tim Peters | c48a3ca | 2002-01-30 05:49:46 +0000 | [diff] [blame] | 593 | \begin{datadesc}{O_NOINHERIT} |
| 594 | \dataline{O_SHORT_LIVED} |
| 595 | \dataline{O_TEMPORARY} |
| 596 | \dataline{O_RANDOM} |
| 597 | \dataline{O_SEQUENTIAL} |
| 598 | \dataline{O_TEXT} |
| 599 | Options for the \var{flag} argument to the \function{open()} function. |
| 600 | These can be bit-wise OR'd together. |
| 601 | Availability: Windows. |
| 602 | \end{datadesc} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 603 | |
| 604 | \subsection{Files and Directories \label{os-file-dir}} |
| 605 | |
| 606 | \begin{funcdesc}{access}{path, mode} |
Fred Drake | 7f59124 | 2002-06-18 16:15:51 +0000 | [diff] [blame] | 607 | Use the real uid/gid to test for access to \var{path}. Note that most |
| 608 | operations will use the effective uid/gid, therefore this routine can |
| 609 | be used in a suid/sgid environment to test if the invoking user has the |
| 610 | specified access to \var{path}. \var{mode} should be \constant{F_OK} |
| 611 | to test the existence of \var{path}, or it can be the inclusive OR of |
| 612 | one or more of \constant{R_OK}, \constant{W_OK}, and \constant{X_OK} to |
| 613 | test permissions. Return \code{1} if access is allowed, \code{0} if not. |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 614 | See the \UNIX{} man page \manpage{access}{2} for more information. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 615 | Availability: \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 616 | \end{funcdesc} |
| 617 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 618 | \begin{datadesc}{F_OK} |
| 619 | Value to pass as the \var{mode} parameter of \function{access()} to |
| 620 | test the existence of \var{path}. |
| 621 | \end{datadesc} |
| 622 | |
| 623 | \begin{datadesc}{R_OK} |
| 624 | Value to include in the \var{mode} parameter of \function{access()} |
| 625 | to test the readability of \var{path}. |
| 626 | \end{datadesc} |
| 627 | |
| 628 | \begin{datadesc}{W_OK} |
| 629 | Value to include in the \var{mode} parameter of \function{access()} |
| 630 | to test the writability of \var{path}. |
| 631 | \end{datadesc} |
| 632 | |
| 633 | \begin{datadesc}{X_OK} |
| 634 | Value to include in the \var{mode} parameter of \function{access()} |
| 635 | to determine if \var{path} can be executed. |
| 636 | \end{datadesc} |
| 637 | |
Fred Drake | 6db897c | 1999-07-12 16:49:30 +0000 | [diff] [blame] | 638 | \begin{funcdesc}{chdir}{path} |
| 639 | \index{directory!changing} |
| 640 | Change the current working directory to \var{path}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 641 | Availability: Macintosh, \UNIX, Windows. |
Fred Drake | 6db897c | 1999-07-12 16:49:30 +0000 | [diff] [blame] | 642 | \end{funcdesc} |
| 643 | |
Fred Drake | 1549855 | 2002-04-15 19:41:27 +0000 | [diff] [blame] | 644 | \begin{funcdesc}{fchdir}{fd} |
| 645 | Change the current working directory to the directory represented by |
| 646 | the file descriptor \var{fd}. The descriptor must refer to an opened |
| 647 | directory, not an open file. |
| 648 | Availability: \UNIX. |
| 649 | \versionadded{2.3} |
| 650 | \end{funcdesc} |
| 651 | |
Fred Drake | 6db897c | 1999-07-12 16:49:30 +0000 | [diff] [blame] | 652 | \begin{funcdesc}{getcwd}{} |
| 653 | Return a string representing the current working directory. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 654 | Availability: Macintosh, \UNIX, Windows. |
Fred Drake | 6db897c | 1999-07-12 16:49:30 +0000 | [diff] [blame] | 655 | \end{funcdesc} |
| 656 | |
Martin v. Löwis | a844f2d | 2002-10-05 09:46:48 +0000 | [diff] [blame] | 657 | \begin{funcdesc}{getcwdu}{} |
| 658 | Return a Unicode object representing the current working directory. |
| 659 | Availability: \UNIX, Windows. |
| 660 | \versionadded{2.3} |
| 661 | \end{funcdesc} |
| 662 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 663 | \begin{funcdesc}{chroot}{path} |
| 664 | Change the root directory of the current process to \var{path}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 665 | Availability: \UNIX. |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 666 | \versionadded{2.2} |
| 667 | \end{funcdesc} |
| 668 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 669 | \begin{funcdesc}{chmod}{path, mode} |
| 670 | Change the mode of \var{path} to the numeric \var{mode}. |
Raymond Hettinger | 0a6aa28 | 2003-08-31 05:09:52 +0000 | [diff] [blame] | 671 | \var{mode} may take one of the following values |
| 672 | (as defined in the \module{stat} module): |
Raymond Hettinger | 9f5b07d | 2003-01-06 13:31:26 +0000 | [diff] [blame] | 673 | \begin{itemize} |
| 674 | \item \code{S_ISUID} |
| 675 | \item \code{S_ISGID} |
| 676 | \item \code{S_ENFMT} |
| 677 | \item \code{S_ISVTX} |
| 678 | \item \code{S_IREAD} |
| 679 | \item \code{S_IWRITE} |
| 680 | \item \code{S_IEXEC} |
| 681 | \item \code{S_IRWXU} |
| 682 | \item \code{S_IRUSR} |
| 683 | \item \code{S_IWUSR} |
| 684 | \item \code{S_IXUSR} |
| 685 | \item \code{S_IRWXG} |
| 686 | \item \code{S_IRGRP} |
| 687 | \item \code{S_IWGRP} |
| 688 | \item \code{S_IXGRP} |
| 689 | \item \code{S_IRWXO} |
| 690 | \item \code{S_IROTH} |
| 691 | \item \code{S_IWOTH} |
| 692 | \item \code{S_IXOTH} |
| 693 | \end{itemize} |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 694 | Availability: \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 695 | \end{funcdesc} |
| 696 | |
| 697 | \begin{funcdesc}{chown}{path, uid, gid} |
| 698 | Change the owner and group id of \var{path} to the numeric \var{uid} |
| 699 | and \var{gid}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 700 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 701 | \end{funcdesc} |
| 702 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 703 | \begin{funcdesc}{lchown}{path, uid, gid} |
| 704 | Change the owner and group id of \var{path} to the numeric \var{uid} |
| 705 | and gid. This function will not follow symbolic links. |
| 706 | Availability: \UNIX. |
| 707 | \versionadded{2.3} |
| 708 | \end{funcdesc} |
| 709 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 710 | \begin{funcdesc}{link}{src, dst} |
| 711 | Create a hard link pointing to \var{src} named \var{dst}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 712 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 713 | \end{funcdesc} |
| 714 | |
| 715 | \begin{funcdesc}{listdir}{path} |
| 716 | Return a list containing the names of the entries in the directory. |
| 717 | The list is in arbitrary order. It does not include the special |
| 718 | entries \code{'.'} and \code{'..'} even if they are present in the |
| 719 | directory. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 720 | Availability: Macintosh, \UNIX, Windows. |
Martin v. Löwis | a844f2d | 2002-10-05 09:46:48 +0000 | [diff] [blame] | 721 | |
Fred Drake | 5c7b248 | 2003-03-20 17:39:38 +0000 | [diff] [blame] | 722 | \versionchanged[On Windows NT/2k/XP and Unix, if \var{path} is a Unicode |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 723 | object, the result will be a list of Unicode objects.]{2.3} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 724 | \end{funcdesc} |
| 725 | |
| 726 | \begin{funcdesc}{lstat}{path} |
| 727 | Like \function{stat()}, but do not follow symbolic links. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 728 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 729 | \end{funcdesc} |
| 730 | |
| 731 | \begin{funcdesc}{mkfifo}{path\optional{, mode}} |
| 732 | Create a FIFO (a named pipe) named \var{path} with numeric mode |
| 733 | \var{mode}. The default \var{mode} is \code{0666} (octal). The current |
| 734 | umask value is first masked out from the mode. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 735 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 736 | |
| 737 | FIFOs are pipes that can be accessed like regular files. FIFOs exist |
| 738 | until they are deleted (for example with \function{os.unlink()}). |
| 739 | Generally, FIFOs are used as rendezvous between ``client'' and |
| 740 | ``server'' type processes: the server opens the FIFO for reading, and |
| 741 | the client opens it for writing. Note that \function{mkfifo()} |
| 742 | doesn't open the FIFO --- it just creates the rendezvous point. |
| 743 | \end{funcdesc} |
| 744 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 745 | \begin{funcdesc}{mknod}{path\optional{, mode=0600, device}} |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 746 | Create a filesystem node (file, device special file or named pipe) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 747 | named filename. \var{mode} specifies both the permissions to use and |
| 748 | the type of node to be created, being combined (bitwise OR) with one |
| 749 | of S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO (those constants are |
| 750 | available in \module{stat}). For S_IFCHR and S_IFBLK, \var{device} |
| 751 | defines the newly created device special file (probably using |
| 752 | \function{os.makedev()}), otherwise it is ignored. |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 753 | \versionadded{2.3} |
| 754 | \end{funcdesc} |
| 755 | |
| 756 | \begin{funcdesc}{major}{device} |
| 757 | Extracts a device major number from a raw device number. |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 758 | \versionadded{2.3} |
| 759 | \end{funcdesc} |
| 760 | |
| 761 | \begin{funcdesc}{minor}{device} |
| 762 | Extracts a device minor number from a raw device number. |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 763 | \versionadded{2.3} |
| 764 | \end{funcdesc} |
| 765 | |
| 766 | \begin{funcdesc}{makedev}{major, minor} |
| 767 | Composes a raw device number from the major and minor device numbers. |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 768 | \versionadded{2.3} |
| 769 | \end{funcdesc} |
| 770 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 771 | \begin{funcdesc}{mkdir}{path\optional{, mode}} |
| 772 | Create a directory named \var{path} with numeric mode \var{mode}. |
| 773 | The default \var{mode} is \code{0777} (octal). On some systems, |
| 774 | \var{mode} is ignored. Where it is used, the current umask value is |
| 775 | first masked out. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 776 | Availability: Macintosh, \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 777 | \end{funcdesc} |
| 778 | |
| 779 | \begin{funcdesc}{makedirs}{path\optional{, mode}} |
Fred Drake | 5c7b248 | 2003-03-20 17:39:38 +0000 | [diff] [blame] | 780 | Recursive directory creation function.\index{directory!creating} |
| 781 | \index{UNC paths!and \function{os.makedirs()}} |
| 782 | Like \function{mkdir()}, |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 783 | but makes all intermediate-level directories needed to contain the |
| 784 | leaf directory. Throws an \exception{error} exception if the leaf |
| 785 | directory already exists or cannot be created. The default \var{mode} |
Fred Drake | bbf7a40 | 2001-09-28 16:14:18 +0000 | [diff] [blame] | 786 | is \code{0777} (octal). This function does not properly handle UNC |
Fred Drake | 5c7b248 | 2003-03-20 17:39:38 +0000 | [diff] [blame] | 787 | paths (only relevant on Windows systems; Universal Naming Convention |
| 788 | paths are those that use the `\code{\e\e host\e path}' syntax). |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 789 | \versionadded{1.5.2} |
| 790 | \end{funcdesc} |
| 791 | |
Fred Drake | 88f6ca2 | 1999-12-15 19:39:04 +0000 | [diff] [blame] | 792 | \begin{funcdesc}{pathconf}{path, name} |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 793 | Return system configuration information relevant to a named file. |
Fred Drake | 88f6ca2 | 1999-12-15 19:39:04 +0000 | [diff] [blame] | 794 | \var{name} specifies the configuration value to retrieve; it may be a |
| 795 | string which is the name of a defined system value; these names are |
Raymond Hettinger | b67449d | 2003-09-08 18:52:18 +0000 | [diff] [blame] | 796 | specified in a number of standards (\POSIX.1, \UNIX{} 95, \UNIX{} 98, and |
Fred Drake | 88f6ca2 | 1999-12-15 19:39:04 +0000 | [diff] [blame] | 797 | others). Some platforms define additional names as well. The names |
| 798 | known to the host operating system are given in the |
| 799 | \code{pathconf_names} dictionary. For configuration variables not |
| 800 | included in that mapping, passing an integer for \var{name} is also |
| 801 | accepted. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 802 | Availability: \UNIX. |
Fred Drake | 88f6ca2 | 1999-12-15 19:39:04 +0000 | [diff] [blame] | 803 | |
| 804 | If \var{name} is a string and is not known, \exception{ValueError} is |
| 805 | raised. If a specific value for \var{name} is not supported by the |
| 806 | host system, even if it is included in \code{pathconf_names}, an |
| 807 | \exception{OSError} is raised with \constant{errno.EINVAL} for the |
| 808 | error number. |
| 809 | \end{funcdesc} |
| 810 | |
| 811 | \begin{datadesc}{pathconf_names} |
| 812 | Dictionary mapping names accepted by \function{pathconf()} and |
| 813 | \function{fpathconf()} to the integer values defined for those names |
| 814 | by the host operating system. This can be used to determine the set |
| 815 | of names known to the system. |
| 816 | Availability: \UNIX. |
| 817 | \end{datadesc} |
| 818 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 819 | \begin{funcdesc}{readlink}{path} |
| 820 | Return a string representing the path to which the symbolic link |
Fred Drake | dc9e7e4 | 2001-05-29 18:13:06 +0000 | [diff] [blame] | 821 | points. The result may be either an absolute or relative pathname; if |
| 822 | it is relative, it may be converted to an absolute pathname using |
| 823 | \code{os.path.join(os.path.dirname(\var{path}), \var{result})}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 824 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 825 | \end{funcdesc} |
| 826 | |
| 827 | \begin{funcdesc}{remove}{path} |
Fred Drake | dc9e7e4 | 2001-05-29 18:13:06 +0000 | [diff] [blame] | 828 | Remove the file \var{path}. If \var{path} is a directory, |
| 829 | \exception{OSError} is raised; see \function{rmdir()} below to remove |
| 830 | a directory. This is identical to the \function{unlink()} function |
| 831 | documented below. On Windows, attempting to remove a file that is in |
| 832 | use causes an exception to be raised; on \UNIX, the directory entry is |
| 833 | removed but the storage allocated to the file is not made available |
| 834 | until the original file is no longer in use. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 835 | Availability: Macintosh, \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 836 | \end{funcdesc} |
| 837 | |
| 838 | \begin{funcdesc}{removedirs}{path} |
| 839 | \index{directory!deleting} |
Fred Drake | 2c22e85 | 2002-07-02 21:03:49 +0000 | [diff] [blame] | 840 | Removes directories recursively. Works like |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 841 | \function{rmdir()} except that, if the leaf directory is |
| 842 | successfully removed, directories corresponding to rightmost path |
| 843 | segments will be pruned way until either the whole path is consumed or |
| 844 | an error is raised (which is ignored, because it generally means that |
| 845 | a parent directory is not empty). Throws an \exception{error} |
| 846 | exception if the leaf directory could not be successfully removed. |
| 847 | \versionadded{1.5.2} |
| 848 | \end{funcdesc} |
| 849 | |
| 850 | \begin{funcdesc}{rename}{src, dst} |
Fred Drake | dc9e7e4 | 2001-05-29 18:13:06 +0000 | [diff] [blame] | 851 | Rename the file or directory \var{src} to \var{dst}. If \var{dst} is |
| 852 | a directory, \exception{OSError} will be raised. On \UNIX, if |
| 853 | \var{dst} exists and is a file, it will be removed silently if the |
| 854 | user has permission. The operation may fail on some \UNIX{} flavors |
Skip Montanaro | b9d973d | 2001-06-04 15:31:17 +0000 | [diff] [blame] | 855 | if \var{src} and \var{dst} are on different filesystems. If |
Fred Drake | dc9e7e4 | 2001-05-29 18:13:06 +0000 | [diff] [blame] | 856 | successful, the renaming will be an atomic operation (this is a |
| 857 | \POSIX{} requirement). On Windows, if \var{dst} already exists, |
| 858 | \exception{OSError} will be raised even if it is a file; there may be |
| 859 | no way to implement an atomic rename when \var{dst} names an existing |
| 860 | file. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 861 | Availability: Macintosh, \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 862 | \end{funcdesc} |
| 863 | |
| 864 | \begin{funcdesc}{renames}{old, new} |
| 865 | Recursive directory or file renaming function. |
| 866 | Works like \function{rename()}, except creation of any intermediate |
| 867 | directories needed to make the new pathname good is attempted first. |
| 868 | After the rename, directories corresponding to rightmost path segments |
| 869 | of the old name will be pruned away using \function{removedirs()}. |
| 870 | |
| 871 | Note: this function can fail with the new directory structure made if |
| 872 | you lack permissions needed to remove the leaf directory or file. |
| 873 | \versionadded{1.5.2} |
| 874 | \end{funcdesc} |
| 875 | |
| 876 | \begin{funcdesc}{rmdir}{path} |
| 877 | Remove the directory \var{path}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 878 | Availability: Macintosh, \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 879 | \end{funcdesc} |
| 880 | |
| 881 | \begin{funcdesc}{stat}{path} |
| 882 | Perform a \cfunction{stat()} system call on the given path. The |
Fred Drake | 6995bb6 | 2001-11-29 20:48:44 +0000 | [diff] [blame] | 883 | return value is an object whose attributes correspond to the members of |
| 884 | the \ctype{stat} structure, namely: |
| 885 | \member{st_mode} (protection bits), |
| 886 | \member{st_ino} (inode number), |
| 887 | \member{st_dev} (device), |
Raymond Hettinger | 52136a8 | 2003-05-10 03:35:37 +0000 | [diff] [blame] | 888 | \member{st_nlink} (number of hard links), |
Fred Drake | 6995bb6 | 2001-11-29 20:48:44 +0000 | [diff] [blame] | 889 | \member{st_uid} (user ID of owner), |
| 890 | \member{st_gid} (group ID of owner), |
| 891 | \member{st_size} (size of file, in bytes), |
| 892 | \member{st_atime} (time of most recent access), |
| 893 | \member{st_mtime} (time of most recent content modification), |
| 894 | \member{st_ctime} |
| 895 | (time of most recent content modification or metadata change). |
| 896 | |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 897 | \versionchanged [If \function{stat_float_times} returns true, the time |
| 898 | values are floats, measuring seconds. Fractions of a second may be |
| 899 | reported if the system supports that. On Mac OS, the times are always |
| 900 | floats. See \function{stat_float_times} for further discussion. ]{2.3} |
Martin v. Löwis | a32c994 | 2002-09-09 16:17:47 +0000 | [diff] [blame] | 901 | |
Fred Drake | 6995bb6 | 2001-11-29 20:48:44 +0000 | [diff] [blame] | 902 | On some Unix systems (such as Linux), the following attributes may |
| 903 | also be available: |
| 904 | \member{st_blocks} (number of blocks allocated for file), |
| 905 | \member{st_blksize} (filesystem blocksize), |
| 906 | \member{st_rdev} (type of device if an inode device). |
| 907 | |
| 908 | On Mac OS systems, the following attributes may also be available: |
| 909 | \member{st_rsize}, |
| 910 | \member{st_creator}, |
| 911 | \member{st_type}. |
| 912 | |
| 913 | On RISCOS systems, the following attributes are also available: |
| 914 | \member{st_ftype} (file type), |
| 915 | \member{st_attrs} (attributes), |
| 916 | \member{st_obtype} (object type). |
| 917 | |
| 918 | For backward compatibility, the return value of \function{stat()} is |
| 919 | also accessible as a tuple of at least 10 integers giving the most |
| 920 | important (and portable) members of the \ctype{stat} structure, in the |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 921 | order |
Fred Drake | 6995bb6 | 2001-11-29 20:48:44 +0000 | [diff] [blame] | 922 | \member{st_mode}, |
| 923 | \member{st_ino}, |
| 924 | \member{st_dev}, |
| 925 | \member{st_nlink}, |
| 926 | \member{st_uid}, |
| 927 | \member{st_gid}, |
| 928 | \member{st_size}, |
| 929 | \member{st_atime}, |
| 930 | \member{st_mtime}, |
| 931 | \member{st_ctime}. |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 932 | More items may be added at the end by some implementations. |
Fred Drake | 6995bb6 | 2001-11-29 20:48:44 +0000 | [diff] [blame] | 933 | The standard module \refmodule{stat}\refstmodindex{stat} defines |
| 934 | functions and constants that are useful for extracting information |
| 935 | from a \ctype{stat} structure. |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 936 | (On Windows, some items are filled with dummy values.) |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 937 | Availability: Macintosh, \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 938 | |
Fred Drake | 6995bb6 | 2001-11-29 20:48:44 +0000 | [diff] [blame] | 939 | \versionchanged |
| 940 | [Added access to values as attributes of the returned object]{2.2} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 941 | \end{funcdesc} |
| 942 | |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 943 | \begin{funcdesc}{stat_float_times}{\optional{newvalue}} |
| 944 | Determine whether \class{stat_result} represents time stamps as float |
| 945 | objects. If newval is True, future calls to stat() return floats, if |
| 946 | it is False, future calls return ints. If newval is omitted, return |
| 947 | the current setting. |
| 948 | |
| 949 | For compatibility with older Python versions, accessing |
| 950 | \class{stat_result} as a tuple always returns integers. For |
| 951 | compatibility with Python 2.2, accessing the time stamps by field name |
| 952 | also returns integers. Applications that want to determine the |
| 953 | fractions of a second in a time stamp can use this function to have |
| 954 | time stamps represented as floats. Whether they will actually observe |
| 955 | non-zero fractions depends on the system. |
| 956 | |
Neal Norwitz | 6d23b17 | 2003-01-05 22:20:51 +0000 | [diff] [blame] | 957 | Future Python releases will change the default of this setting; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 958 | applications that cannot deal with floating point time stamps can then |
| 959 | use this function to turn the feature off. |
| 960 | |
| 961 | It is recommended that this setting is only changed at program startup |
| 962 | time in the \var{__main__} module; libraries should never change this |
| 963 | setting. If an application uses a library that works incorrectly if |
| 964 | floating point time stamps are processed, this application should turn |
| 965 | the feature off until the library has been corrected. |
| 966 | |
| 967 | \end{funcdesc} |
| 968 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 969 | \begin{funcdesc}{statvfs}{path} |
| 970 | Perform a \cfunction{statvfs()} system call on the given path. The |
Fred Drake | 6995bb6 | 2001-11-29 20:48:44 +0000 | [diff] [blame] | 971 | return value is an object whose attributes describe the filesystem on |
| 972 | the given path, and correspond to the members of the |
| 973 | \ctype{statvfs} structure, namely: |
| 974 | \member{f_frsize}, |
| 975 | \member{f_blocks}, |
| 976 | \member{f_bfree}, |
| 977 | \member{f_bavail}, |
| 978 | \member{f_files}, |
| 979 | \member{f_ffree}, |
| 980 | \member{f_favail}, |
| 981 | \member{f_flag}, |
| 982 | \member{f_namemax}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 983 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 984 | |
Fred Drake | 6995bb6 | 2001-11-29 20:48:44 +0000 | [diff] [blame] | 985 | For backward compatibility, the return value is also accessible as a |
| 986 | tuple whose values correspond to the attributes, in the order given above. |
| 987 | The standard module \refmodule{statvfs}\refstmodindex{statvfs} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 988 | defines constants that are useful for extracting information |
Fred Drake | 6995bb6 | 2001-11-29 20:48:44 +0000 | [diff] [blame] | 989 | from a \ctype{statvfs} structure when accessing it as a sequence; this |
| 990 | remains useful when writing code that needs to work with versions of |
| 991 | Python that don't support accessing the fields as attributes. |
| 992 | |
| 993 | \versionchanged |
| 994 | [Added access to values as attributes of the returned object]{2.2} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 995 | \end{funcdesc} |
| 996 | |
| 997 | \begin{funcdesc}{symlink}{src, dst} |
| 998 | Create a symbolic link pointing to \var{src} named \var{dst}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 999 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1000 | \end{funcdesc} |
| 1001 | |
Fred Drake | 18f7a45 | 1999-12-09 22:11:43 +0000 | [diff] [blame] | 1002 | \begin{funcdesc}{tempnam}{\optional{dir\optional{, prefix}}} |
| 1003 | Return a unique path name that is reasonable for creating a temporary |
| 1004 | file. This will be an absolute path that names a potential directory |
| 1005 | entry in the directory \var{dir} or a common location for temporary |
| 1006 | files if \var{dir} is omitted or \code{None}. If given and not |
| 1007 | \code{None}, \var{prefix} is used to provide a short prefix to the |
| 1008 | filename. Applications are responsible for properly creating and |
| 1009 | managing files created using paths returned by \function{tempnam()}; |
| 1010 | no automatic cleanup is provided. |
Fred Drake | 4b9ed2f | 2002-11-12 22:07:11 +0000 | [diff] [blame] | 1011 | On \UNIX, the environment variable \envvar{TMPDIR} overrides |
| 1012 | \var{dir}, while on Windows the \envvar{TMP} is used. The specific |
| 1013 | behavior of this function depends on the C library implementation; |
| 1014 | some aspects are underspecified in system documentation. |
Fred Drake | 938a8d7 | 2001-10-09 18:07:04 +0000 | [diff] [blame] | 1015 | \warning{Use of \function{tempnam()} is vulnerable to symlink attacks; |
| 1016 | consider using \function{tmpfile()} instead.} |
Fred Drake | efaef13 | 2001-07-17 20:39:18 +0000 | [diff] [blame] | 1017 | Availability: \UNIX, Windows. |
Fred Drake | 18f7a45 | 1999-12-09 22:11:43 +0000 | [diff] [blame] | 1018 | \end{funcdesc} |
| 1019 | |
| 1020 | \begin{funcdesc}{tmpnam}{} |
| 1021 | Return a unique path name that is reasonable for creating a temporary |
| 1022 | file. This will be an absolute path that names a potential directory |
| 1023 | entry in a common location for temporary files. Applications are |
| 1024 | responsible for properly creating and managing files created using |
| 1025 | paths returned by \function{tmpnam()}; no automatic cleanup is |
| 1026 | provided. |
Fred Drake | 938a8d7 | 2001-10-09 18:07:04 +0000 | [diff] [blame] | 1027 | \warning{Use of \function{tmpnam()} is vulnerable to symlink attacks; |
| 1028 | consider using \function{tmpfile()} instead.} |
Tim Peters | 5501b5e | 2003-04-28 03:13:03 +0000 | [diff] [blame] | 1029 | Availability: \UNIX, Windows. This function probably shouldn't be used |
| 1030 | on Windows, though: Microsoft's implementation of \function{tmpnam()} |
| 1031 | always creates a name in the root directory of the current drive, and |
| 1032 | that's generally a poor location for a temp file (depending on |
| 1033 | privileges, you may not even be able to open a file using this name). |
Fred Drake | 18f7a45 | 1999-12-09 22:11:43 +0000 | [diff] [blame] | 1034 | \end{funcdesc} |
| 1035 | |
| 1036 | \begin{datadesc}{TMP_MAX} |
| 1037 | The maximum number of unique names that \function{tmpnam()} will |
| 1038 | generate before reusing names. |
| 1039 | \end{datadesc} |
| 1040 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1041 | \begin{funcdesc}{unlink}{path} |
| 1042 | Remove the file \var{path}. This is the same function as |
| 1043 | \function{remove()}; the \function{unlink()} name is its traditional |
| 1044 | \UNIX{} name. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 1045 | Availability: Macintosh, \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1046 | \end{funcdesc} |
| 1047 | |
Barry Warsaw | 93a8eac | 2000-05-01 16:18:22 +0000 | [diff] [blame] | 1048 | \begin{funcdesc}{utime}{path, times} |
| 1049 | Set the access and modified times of the file specified by \var{path}. |
| 1050 | If \var{times} is \code{None}, then the file's access and modified |
| 1051 | times are set to the current time. Otherwise, \var{times} must be a |
Fred Drake | e06d025 | 2000-05-02 17:29:35 +0000 | [diff] [blame] | 1052 | 2-tuple of numbers, of the form \code{(\var{atime}, \var{mtime})} |
| 1053 | which is used to set the access and modified times, respectively. |
Fred Drake | 4a15263 | 2000-10-19 05:33:46 +0000 | [diff] [blame] | 1054 | \versionchanged[Added support for \code{None} for \var{times}]{2.0} |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 1055 | Availability: Macintosh, \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1056 | \end{funcdesc} |
| 1057 | |
Guido van Rossum | bf1bef8 | 2003-05-13 18:01:19 +0000 | [diff] [blame] | 1058 | \begin{funcdesc}{walk}{top\optional{, topdown\code{=True} |
| 1059 | \optional{, onerror\code{=None}}}} |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 1060 | \index{directory!walking} |
| 1061 | \index{directory!traversal} |
Tim Peters | bf89b3a | 2003-04-28 02:09:43 +0000 | [diff] [blame] | 1062 | \function{walk()} generates the file names in a directory tree, by |
| 1063 | walking the tree either top down or bottom up. |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 1064 | For each directory in the tree rooted at directory \var{top} (including |
| 1065 | \var{top} itself), it yields a 3-tuple |
| 1066 | \code{(\var{dirpath}, \var{dirnames}, \var{filenames})}. |
| 1067 | |
| 1068 | \var{dirpath} is a string, the path to the directory. \var{dirnames} is |
| 1069 | a list of the names of the subdirectories in \var{dirpath} |
| 1070 | (excluding \code{'.'} and \code{'..'}). \var{filenames} is a list of |
| 1071 | the names of the non-directory files in \var{dirpath}. Note that the |
| 1072 | names in the lists contain no path components. To get a full |
Fred Drake | 2194a4e | 2003-04-25 14:50:06 +0000 | [diff] [blame] | 1073 | path (which begins with \var{top}) to a file or directory in |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 1074 | \var{dirpath}, do \code{os.path.join(\var{dirpath}, \var{name})}. |
| 1075 | |
| 1076 | If optional argument \var{topdown} is true or not specified, the triple |
| 1077 | for a directory is generated before the triples for any of its |
| 1078 | subdirectories (directories are generated top down). If \var{topdown} is |
| 1079 | false, the triple for a directory is generated after the triples for all |
| 1080 | of its subdirectories (directories are generated bottom up). |
| 1081 | |
| 1082 | When \var{topdown} is true, the caller can modify the \var{dirnames} list |
Raymond Hettinger | 9756f38 | 2003-09-10 00:11:28 +0000 | [diff] [blame] | 1083 | in-place (perhaps using \keyword{del} or slice assignment), and |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 1084 | \function{walk()} will only recurse into the subdirectories whose names |
| 1085 | remain in \var{dirnames}; this can be used to prune the search, |
| 1086 | impose a specific order of visiting, or even to inform \function{walk()} |
| 1087 | about directories the caller creates or renames before it resumes |
| 1088 | \function{walk()} again. Modifying \var{dirnames} when \var{topdown} is |
| 1089 | false is ineffective, because in bottom-up mode the directories in |
| 1090 | \var{dirnames} are generated before \var{dirnames} itself is generated. |
| 1091 | |
Guido van Rossum | bf1bef8 | 2003-05-13 18:01:19 +0000 | [diff] [blame] | 1092 | By default errors from the \code{os.listdir()} call are ignored. If |
| 1093 | optional argument \var{onerror} is specified, it should be a function; |
| 1094 | it will be called with one argument, an os.error instance. It can |
| 1095 | report the error to continue with the walk, or raise the exception |
| 1096 | to abort the walk. Note that the filename is available as the |
| 1097 | \code{filename} attribute of the exception object. |
| 1098 | |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 1099 | \begin{notice} |
| 1100 | If you pass a relative pathname, don't change the current working |
Fred Drake | 2194a4e | 2003-04-25 14:50:06 +0000 | [diff] [blame] | 1101 | directory between resumptions of \function{walk()}. \function{walk()} |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 1102 | never changes the current directory, and assumes that its caller |
| 1103 | doesn't either. |
| 1104 | \end{notice} |
| 1105 | |
| 1106 | \begin{notice} |
| 1107 | On systems that support symbolic links, links to subdirectories appear |
| 1108 | in \var{dirnames} lists, but \function{walk()} will not visit them |
| 1109 | (infinite loops are hard to avoid when following symbolic links). |
| 1110 | To visit linked directories, you can identify them with |
Fred Drake | 2194a4e | 2003-04-25 14:50:06 +0000 | [diff] [blame] | 1111 | \code{os.path.islink(\var{path})}, and invoke \code{walk(\var{path})} |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 1112 | on each directly. |
| 1113 | \end{notice} |
| 1114 | |
| 1115 | This example displays the number of bytes taken by non-directory files |
| 1116 | in each directory under the starting directory, except that it doesn't |
| 1117 | look under any CVS subdirectory: |
| 1118 | |
| 1119 | \begin{verbatim} |
| 1120 | import os |
| 1121 | from os.path import join, getsize |
| 1122 | for root, dirs, files in os.walk('python/Lib/email'): |
| 1123 | print root, "consumes", |
| 1124 | print sum([getsize(join(root, name)) for name in files]), |
| 1125 | print "bytes in", len(files), "non-directory files" |
| 1126 | if 'CVS' in dirs: |
| 1127 | dirs.remove('CVS') # don't visit CVS directories |
| 1128 | \end{verbatim} |
Tim Peters | bf89b3a | 2003-04-28 02:09:43 +0000 | [diff] [blame] | 1129 | |
| 1130 | In the next example, walking the tree bottom up is essential: |
| 1131 | \function{rmdir()} doesn't allow deleting a directory before the |
| 1132 | directory is empty: |
| 1133 | |
| 1134 | \begin{verbatim} |
| 1135 | import os |
| 1136 | from os.path import join |
| 1137 | # Delete everything reachable from the directory named in 'top'. |
Tim Peters | a390c6e | 2003-04-28 19:15:10 +0000 | [diff] [blame] | 1138 | # CAUTION: This is dangerous! For example, if top == '/', it |
| 1139 | # could delete all your disk files. |
Tim Peters | bf89b3a | 2003-04-28 02:09:43 +0000 | [diff] [blame] | 1140 | for root, dirs, files in os.walk(top, topdown=False): |
| 1141 | for name in files: |
| 1142 | os.remove(join(root, name)) |
| 1143 | for name in dirs: |
| 1144 | os.rmdir(join(root, name)) |
| 1145 | \end{verbatim} |
| 1146 | |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 1147 | \versionadded{2.3} |
| 1148 | \end{funcdesc} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1149 | |
| 1150 | \subsection{Process Management \label{os-process}} |
| 1151 | |
Fred Drake | 18f7a45 | 1999-12-09 22:11:43 +0000 | [diff] [blame] | 1152 | These functions may be used to create and manage processes. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1153 | |
Fred Drake | 7be3115 | 2000-09-23 05:22:07 +0000 | [diff] [blame] | 1154 | The various \function{exec*()} functions take a list of arguments for |
| 1155 | the new program loaded into the process. In each case, the first of |
| 1156 | these arguments is passed to the new program as its own name rather |
| 1157 | than as an argument a user may have typed on a command line. For the |
| 1158 | C programmer, this is the \code{argv[0]} passed to a program's |
| 1159 | \cfunction{main()}. For example, \samp{os.execv('/bin/echo', ['foo', |
| 1160 | 'bar'])} will only print \samp{bar} on standard output; \samp{foo} |
| 1161 | will seem to be ignored. |
| 1162 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1163 | |
Fred Drake | 18f7a45 | 1999-12-09 22:11:43 +0000 | [diff] [blame] | 1164 | \begin{funcdesc}{abort}{} |
| 1165 | Generate a \constant{SIGABRT} signal to the current process. On |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 1166 | \UNIX, the default behavior is to produce a core dump; on Windows, the |
Fred Drake | 18f7a45 | 1999-12-09 22:11:43 +0000 | [diff] [blame] | 1167 | process immediately returns an exit code of \code{3}. Be aware that |
| 1168 | programs which use \function{signal.signal()} to register a handler |
| 1169 | for \constant{SIGABRT} will behave differently. |
| 1170 | Availability: \UNIX, Windows. |
| 1171 | \end{funcdesc} |
| 1172 | |
Fred Drake | db7287c | 2001-10-18 18:58:30 +0000 | [diff] [blame] | 1173 | \begin{funcdesc}{execl}{path, arg0, arg1, \moreargs} |
| 1174 | \funcline{execle}{path, arg0, arg1, \moreargs, env} |
| 1175 | \funcline{execlp}{file, arg0, arg1, \moreargs} |
| 1176 | \funcline{execlpe}{file, arg0, arg1, \moreargs, env} |
| 1177 | \funcline{execv}{path, args} |
| 1178 | \funcline{execve}{path, args, env} |
| 1179 | \funcline{execvp}{file, args} |
| 1180 | \funcline{execvpe}{file, args, env} |
| 1181 | These functions all execute a new program, replacing the current |
| 1182 | process; they do not return. On \UNIX, the new executable is loaded |
| 1183 | into the current process, and will have the same process ID as the |
| 1184 | caller. Errors will be reported as \exception{OSError} exceptions. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1185 | |
Fred Drake | db7287c | 2001-10-18 18:58:30 +0000 | [diff] [blame] | 1186 | The \character{l} and \character{v} variants of the |
| 1187 | \function{exec*()} functions differ in how command-line arguments are |
| 1188 | passed. The \character{l} variants are perhaps the easiest to work |
| 1189 | with if the number of parameters is fixed when the code is written; |
| 1190 | the individual parameters simply become additional parameters to the |
| 1191 | \function{execl*()} functions. The \character{v} variants are good |
| 1192 | when the number of parameters is variable, with the arguments being |
| 1193 | passed in a list or tuple as the \var{args} parameter. In either |
| 1194 | case, the arguments to the child process must start with the name of |
| 1195 | the command being run. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1196 | |
Fred Drake | db7287c | 2001-10-18 18:58:30 +0000 | [diff] [blame] | 1197 | The variants which include a \character{p} near the end |
| 1198 | (\function{execlp()}, \function{execlpe()}, \function{execvp()}, |
| 1199 | and \function{execvpe()}) will use the \envvar{PATH} environment |
| 1200 | variable to locate the program \var{file}. When the environment is |
| 1201 | being replaced (using one of the \function{exec*e()} variants, |
| 1202 | discussed in the next paragraph), the |
| 1203 | new environment is used as the source of the \envvar{PATH} variable. |
| 1204 | The other variants, \function{execl()}, \function{execle()}, |
| 1205 | \function{execv()}, and \function{execve()}, will not use the |
| 1206 | \envvar{PATH} variable to locate the executable; \var{path} must |
| 1207 | contain an appropriate absolute or relative path. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1208 | |
Fred Drake | db7287c | 2001-10-18 18:58:30 +0000 | [diff] [blame] | 1209 | For \function{execle()}, \function{execlpe()}, \function{execve()}, |
| 1210 | and \function{execvpe()} (note that these all end in \character{e}), |
| 1211 | the \var{env} parameter must be a mapping which is used to define the |
| 1212 | environment variables for the new process; the \function{execl()}, |
| 1213 | \function{execlp()}, \function{execv()}, and \function{execvp()} |
| 1214 | all cause the new process to inherit the environment of the current |
| 1215 | process. |
| 1216 | Availability: \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1217 | \end{funcdesc} |
| 1218 | |
| 1219 | \begin{funcdesc}{_exit}{n} |
| 1220 | Exit to the system with status \var{n}, without calling cleanup |
| 1221 | handlers, flushing stdio buffers, etc. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 1222 | Availability: \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1223 | |
| 1224 | Note: the standard way to exit is \code{sys.exit(\var{n})}. |
| 1225 | \function{_exit()} should normally only be used in the child process |
| 1226 | after a \function{fork()}. |
| 1227 | \end{funcdesc} |
| 1228 | |
Barry Warsaw | b6604b3 | 2003-01-07 22:43:25 +0000 | [diff] [blame] | 1229 | The following exit codes are a defined, and can be used with |
| 1230 | \function{_exit()}, although they are not required. These are |
| 1231 | typically used for system programs written in Python, such as a |
| 1232 | mail server's external command delivery program. |
| 1233 | |
| 1234 | \begin{datadesc}{EX_OK} |
| 1235 | Exit code that means no error occurred. |
| 1236 | Availability: \UNIX. |
| 1237 | \versionadded{2.3} |
| 1238 | \end{datadesc} |
| 1239 | |
| 1240 | \begin{datadesc}{EX_USAGE} |
| 1241 | Exit code that means the command was used incorrectly, such as when |
| 1242 | the wrong number of arguments are given. |
| 1243 | Availability: \UNIX. |
| 1244 | \versionadded{2.3} |
| 1245 | \end{datadesc} |
| 1246 | |
| 1247 | \begin{datadesc}{EX_DATAERR} |
| 1248 | Exit code that means the input data was incorrect. |
| 1249 | Availability: \UNIX. |
| 1250 | \versionadded{2.3} |
| 1251 | \end{datadesc} |
| 1252 | |
| 1253 | \begin{datadesc}{EX_NOINPUT} |
| 1254 | Exit code that means an input file did not exist or was not readable. |
| 1255 | Availability: \UNIX. |
| 1256 | \versionadded{2.3} |
| 1257 | \end{datadesc} |
| 1258 | |
| 1259 | \begin{datadesc}{EX_NOUSER} |
| 1260 | Exit code that means a specified user did not exist. |
| 1261 | Availability: \UNIX. |
| 1262 | \versionadded{2.3} |
| 1263 | \end{datadesc} |
| 1264 | |
| 1265 | \begin{datadesc}{EX_NOHOST} |
| 1266 | Exit code that means a specified host did not exist. |
| 1267 | Availability: \UNIX. |
| 1268 | \versionadded{2.3} |
| 1269 | \end{datadesc} |
| 1270 | |
| 1271 | \begin{datadesc}{EX_UNAVAILABLE} |
| 1272 | Exit code that means that a required service is unavailable. |
| 1273 | Availability: \UNIX. |
| 1274 | \versionadded{2.3} |
| 1275 | \end{datadesc} |
| 1276 | |
| 1277 | \begin{datadesc}{EX_SOFTWARE} |
| 1278 | Exit code that means an internal software error was detected. |
| 1279 | Availability: \UNIX. |
| 1280 | \versionadded{2.3} |
| 1281 | \end{datadesc} |
| 1282 | |
| 1283 | \begin{datadesc}{EX_OSERR} |
| 1284 | Exit code that means an operating system error was detected, such as |
| 1285 | the inability to fork or create a pipe. |
| 1286 | Availability: \UNIX. |
| 1287 | \versionadded{2.3} |
| 1288 | \end{datadesc} |
| 1289 | |
| 1290 | \begin{datadesc}{EX_OSFILE} |
| 1291 | Exit code that means some system file did not exist, could not be |
| 1292 | opened, or had some other kind of error. |
| 1293 | Availability: \UNIX. |
| 1294 | \versionadded{2.3} |
| 1295 | \end{datadesc} |
| 1296 | |
| 1297 | \begin{datadesc}{EX_CANTCREAT} |
| 1298 | Exit code that means a user specified output file could not be created. |
| 1299 | Availability: \UNIX. |
| 1300 | \versionadded{2.3} |
| 1301 | \end{datadesc} |
| 1302 | |
| 1303 | \begin{datadesc}{EX_IOERR} |
| 1304 | Exit code that means that an error occurred while doing I/O on some file. |
| 1305 | Availability: \UNIX. |
| 1306 | \versionadded{2.3} |
| 1307 | \end{datadesc} |
| 1308 | |
| 1309 | \begin{datadesc}{EX_TEMPFAIL} |
| 1310 | Exit code that means a temporary failure occurred. This indicates |
| 1311 | something that may not really be an error, such as a network |
| 1312 | connection that couldn't be made during a retryable operation. |
| 1313 | Availability: \UNIX. |
| 1314 | \versionadded{2.3} |
| 1315 | \end{datadesc} |
| 1316 | |
| 1317 | \begin{datadesc}{EX_PROTOCOL} |
| 1318 | Exit code that means that a protocol exchange was illegal, invalid, or |
| 1319 | not understood. |
| 1320 | Availability: \UNIX. |
| 1321 | \versionadded{2.3} |
| 1322 | \end{datadesc} |
| 1323 | |
| 1324 | \begin{datadesc}{EX_NOPERM} |
| 1325 | Exit code that means that there were insufficient permissions to |
| 1326 | perform the operation (but not intended for file system problems). |
| 1327 | Availability: \UNIX. |
| 1328 | \versionadded{2.3} |
| 1329 | \end{datadesc} |
| 1330 | |
| 1331 | \begin{datadesc}{EX_CONFIG} |
| 1332 | Exit code that means that some kind of configuration error occurred. |
| 1333 | Availability: \UNIX. |
| 1334 | \versionadded{2.3} |
| 1335 | \end{datadesc} |
| 1336 | |
| 1337 | \begin{datadesc}{EX_NOTFOUND} |
| 1338 | Exit code that means something like ``an entry was not found''. |
| 1339 | Availability: \UNIX. |
| 1340 | \versionadded{2.3} |
| 1341 | \end{datadesc} |
| 1342 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1343 | \begin{funcdesc}{fork}{} |
| 1344 | Fork a child process. Return \code{0} in the child, the child's |
| 1345 | process id in the parent. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 1346 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1347 | \end{funcdesc} |
| 1348 | |
Fred Drake | c82634c | 2000-06-28 17:27:48 +0000 | [diff] [blame] | 1349 | \begin{funcdesc}{forkpty}{} |
| 1350 | Fork a child process, using a new pseudo-terminal as the child's |
| 1351 | controlling terminal. Return a pair of \code{(\var{pid}, \var{fd})}, |
| 1352 | where \var{pid} is \code{0} in the child, the new child's process id |
Fred Drake | 6995bb6 | 2001-11-29 20:48:44 +0000 | [diff] [blame] | 1353 | in the parent, and \var{fd} is the file descriptor of the master end |
Fred Drake | c82634c | 2000-06-28 17:27:48 +0000 | [diff] [blame] | 1354 | of the pseudo-terminal. For a more portable approach, use the |
| 1355 | \refmodule{pty} module. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 1356 | Availability: Some flavors of \UNIX. |
Fred Drake | c82634c | 2000-06-28 17:27:48 +0000 | [diff] [blame] | 1357 | \end{funcdesc} |
| 1358 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1359 | \begin{funcdesc}{kill}{pid, sig} |
| 1360 | \index{process!killing} |
| 1361 | \index{process!signalling} |
Fred Drake | 5c79831 | 2001-12-21 03:58:47 +0000 | [diff] [blame] | 1362 | Kill the process \var{pid} with signal \var{sig}. Constants for the |
| 1363 | specific signals available on the host platform are defined in the |
| 1364 | \refmodule{signal} module. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 1365 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1366 | \end{funcdesc} |
| 1367 | |
Martin v. Löwis | 33e9443 | 2002-12-27 10:21:19 +0000 | [diff] [blame] | 1368 | \begin{funcdesc}{killpg}{pgid, sig} |
| 1369 | \index{process!killing} |
| 1370 | \index{process!signalling} |
| 1371 | Kill the process group \var{pgid} with the signal \var{sig}. |
| 1372 | Availability: \UNIX. |
| 1373 | \versionadded{2.3} |
| 1374 | \end{funcdesc} |
| 1375 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1376 | \begin{funcdesc}{nice}{increment} |
| 1377 | Add \var{increment} to the process's ``niceness''. Return the new |
| 1378 | niceness. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 1379 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1380 | \end{funcdesc} |
| 1381 | |
| 1382 | \begin{funcdesc}{plock}{op} |
| 1383 | Lock program segments into memory. The value of \var{op} |
| 1384 | (defined in \code{<sys/lock.h>}) determines which segments are locked. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 1385 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1386 | \end{funcdesc} |
| 1387 | |
Fred Drake | 046f4d8 | 2001-06-11 15:21:48 +0000 | [diff] [blame] | 1388 | \begin{funcdescni}{popen}{\unspecified} |
| 1389 | \funclineni{popen2}{\unspecified} |
| 1390 | \funclineni{popen3}{\unspecified} |
| 1391 | \funclineni{popen4}{\unspecified} |
| 1392 | Run child processes, returning opened pipes for communications. These |
| 1393 | functions are described in section \ref{os-newstreams}. |
| 1394 | \end{funcdescni} |
| 1395 | |
Fred Drake | 739282d | 2001-08-16 21:21:28 +0000 | [diff] [blame] | 1396 | \begin{funcdesc}{spawnl}{mode, path, \moreargs} |
| 1397 | \funcline{spawnle}{mode, path, \moreargs, env} |
Fred Drake | db7287c | 2001-10-18 18:58:30 +0000 | [diff] [blame] | 1398 | \funcline{spawnlp}{mode, file, \moreargs} |
| 1399 | \funcline{spawnlpe}{mode, file, \moreargs, env} |
Fred Drake | 739282d | 2001-08-16 21:21:28 +0000 | [diff] [blame] | 1400 | \funcline{spawnv}{mode, path, args} |
| 1401 | \funcline{spawnve}{mode, path, args, env} |
Fred Drake | db7287c | 2001-10-18 18:58:30 +0000 | [diff] [blame] | 1402 | \funcline{spawnvp}{mode, file, args} |
| 1403 | \funcline{spawnvpe}{mode, file, args, env} |
Fred Drake | 739282d | 2001-08-16 21:21:28 +0000 | [diff] [blame] | 1404 | Execute the program \var{path} in a new process. If \var{mode} is |
| 1405 | \constant{P_NOWAIT}, this function returns the process ID of the new |
Tim Peters | b404145 | 2001-12-06 23:37:17 +0000 | [diff] [blame] | 1406 | process; if \var{mode} is \constant{P_WAIT}, returns the process's |
Fred Drake | 739282d | 2001-08-16 21:21:28 +0000 | [diff] [blame] | 1407 | exit code if it exits normally, or \code{-\var{signal}}, where |
Fred Drake | 4dfb7a8 | 2002-04-01 23:30:47 +0000 | [diff] [blame] | 1408 | \var{signal} is the signal that killed the process. On Windows, the |
| 1409 | process ID will actually be the process handle, so can be used with |
| 1410 | the \function{waitpid()} function. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1411 | |
Fred Drake | 739282d | 2001-08-16 21:21:28 +0000 | [diff] [blame] | 1412 | The \character{l} and \character{v} variants of the |
| 1413 | \function{spawn*()} functions differ in how command-line arguments are |
| 1414 | passed. The \character{l} variants are perhaps the easiest to work |
| 1415 | with if the number of parameters is fixed when the code is written; |
| 1416 | the individual parameters simply become additional parameters to the |
| 1417 | \function{spawnl*()} functions. The \character{v} variants are good |
| 1418 | when the number of parameters is variable, with the arguments being |
| 1419 | passed in a list or tuple as the \var{args} parameter. In either |
| 1420 | case, the arguments to the child process must start with the name of |
| 1421 | the command being run. |
| 1422 | |
Fred Drake | db7287c | 2001-10-18 18:58:30 +0000 | [diff] [blame] | 1423 | The variants which include a second \character{p} near the end |
| 1424 | (\function{spawnlp()}, \function{spawnlpe()}, \function{spawnvp()}, |
| 1425 | and \function{spawnvpe()}) will use the \envvar{PATH} environment |
| 1426 | variable to locate the program \var{file}. When the environment is |
| 1427 | being replaced (using one of the \function{spawn*e()} variants, |
| 1428 | discussed in the next paragraph), the new environment is used as the |
| 1429 | source of the \envvar{PATH} variable. The other variants, |
| 1430 | \function{spawnl()}, \function{spawnle()}, \function{spawnv()}, and |
| 1431 | \function{spawnve()}, will not use the \envvar{PATH} variable to |
| 1432 | locate the executable; \var{path} must contain an appropriate absolute |
| 1433 | or relative path. |
| 1434 | |
| 1435 | For \function{spawnle()}, \function{spawnlpe()}, \function{spawnve()}, |
| 1436 | and \function{spawnvpe()} (note that these all end in \character{e}), |
| 1437 | the \var{env} parameter must be a mapping which is used to define the |
| 1438 | environment variables for the new process; the \function{spawnl()}, |
| 1439 | \function{spawnlp()}, \function{spawnv()}, and \function{spawnvp()} |
| 1440 | all cause the new process to inherit the environment of the current |
| 1441 | process. |
| 1442 | |
Fred Drake | 739282d | 2001-08-16 21:21:28 +0000 | [diff] [blame] | 1443 | As an example, the following calls to \function{spawnlp()} and |
| 1444 | \function{spawnvpe()} are equivalent: |
| 1445 | |
| 1446 | \begin{verbatim} |
| 1447 | import os |
| 1448 | os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null') |
| 1449 | |
| 1450 | L = ['cp', 'index.html', '/dev/null'] |
| 1451 | os.spawnvpe(os.P_WAIT, 'cp', L, os.environ) |
| 1452 | \end{verbatim} |
| 1453 | |
Fred Drake | 8c8e871 | 2001-12-20 17:24:11 +0000 | [diff] [blame] | 1454 | Availability: \UNIX, Windows. \function{spawnlp()}, |
| 1455 | \function{spawnlpe()}, \function{spawnvp()} and \function{spawnvpe()} |
| 1456 | are not available on Windows. |
Fred Drake | 0b9bc20 | 2001-06-11 18:25:34 +0000 | [diff] [blame] | 1457 | \versionadded{1.6} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1458 | \end{funcdesc} |
| 1459 | |
Fred Drake | 938a8d7 | 2001-10-09 18:07:04 +0000 | [diff] [blame] | 1460 | \begin{datadesc}{P_NOWAIT} |
Fred Drake | 9329e5e | 1999-02-16 19:40:19 +0000 | [diff] [blame] | 1461 | \dataline{P_NOWAITO} |
Fred Drake | 938a8d7 | 2001-10-09 18:07:04 +0000 | [diff] [blame] | 1462 | Possible values for the \var{mode} parameter to the \function{spawn*()} |
| 1463 | family of functions. If either of these values is given, the |
| 1464 | \function{spawn*()} functions will return as soon as the new process |
| 1465 | has been created, with the process ID as the return value. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 1466 | Availability: \UNIX, Windows. |
Fred Drake | 0b9bc20 | 2001-06-11 18:25:34 +0000 | [diff] [blame] | 1467 | \versionadded{1.6} |
Fred Drake | 15861b2 | 2000-02-29 05:19:38 +0000 | [diff] [blame] | 1468 | \end{datadesc} |
| 1469 | |
Fred Drake | 938a8d7 | 2001-10-09 18:07:04 +0000 | [diff] [blame] | 1470 | \begin{datadesc}{P_WAIT} |
| 1471 | Possible value for the \var{mode} parameter to the \function{spawn*()} |
| 1472 | family of functions. If this is given as \var{mode}, the |
| 1473 | \function{spawn*()} functions will not return until the new process |
| 1474 | has run to completion and will return the exit code of the process the |
| 1475 | run is successful, or \code{-\var{signal}} if a signal kills the |
| 1476 | process. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 1477 | Availability: \UNIX, Windows. |
Fred Drake | 938a8d7 | 2001-10-09 18:07:04 +0000 | [diff] [blame] | 1478 | \versionadded{1.6} |
| 1479 | \end{datadesc} |
| 1480 | |
| 1481 | \begin{datadesc}{P_DETACH} |
| 1482 | \dataline{P_OVERLAY} |
| 1483 | Possible values for the \var{mode} parameter to the |
| 1484 | \function{spawn*()} family of functions. These are less portable than |
| 1485 | those listed above. |
| 1486 | \constant{P_DETACH} is similar to \constant{P_NOWAIT}, but the new |
| 1487 | process is detached from the console of the calling process. |
| 1488 | If \constant{P_OVERLAY} is used, the current process will be replaced; |
| 1489 | the \function{spawn*()} function will not return. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1490 | Availability: Windows. |
Fred Drake | 0b9bc20 | 2001-06-11 18:25:34 +0000 | [diff] [blame] | 1491 | \versionadded{1.6} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1492 | \end{datadesc} |
| 1493 | |
Fred Drake | 4ce4f2e | 2000-09-29 04:15:19 +0000 | [diff] [blame] | 1494 | \begin{funcdesc}{startfile}{path} |
| 1495 | Start a file with its associated application. This acts like |
| 1496 | double-clicking the file in Windows Explorer, or giving the file name |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 1497 | as an argument to the \program{start} command from the interactive |
| 1498 | command shell: the file is opened with whatever application (if any) |
| 1499 | its extension is associated. |
Fred Drake | 4ce4f2e | 2000-09-29 04:15:19 +0000 | [diff] [blame] | 1500 | |
| 1501 | \function{startfile()} returns as soon as the associated application |
| 1502 | is launched. There is no option to wait for the application to close, |
| 1503 | and no way to retrieve the application's exit status. The \var{path} |
| 1504 | parameter is relative to the current directory. If you want to use an |
| 1505 | absolute path, make sure the first character is not a slash |
| 1506 | (\character{/}); the underlying Win32 \cfunction{ShellExecute()} |
Fred Drake | 8a2adcf | 2001-07-23 19:20:56 +0000 | [diff] [blame] | 1507 | function doesn't work if it is. Use the \function{os.path.normpath()} |
Fred Drake | 4ce4f2e | 2000-09-29 04:15:19 +0000 | [diff] [blame] | 1508 | function to ensure that the path is properly encoded for Win32. |
| 1509 | Availability: Windows. |
| 1510 | \versionadded{2.0} |
| 1511 | \end{funcdesc} |
| 1512 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1513 | \begin{funcdesc}{system}{command} |
| 1514 | Execute the command (a string) in a subshell. This is implemented by |
| 1515 | calling the Standard C function \cfunction{system()}, and has the |
Fred Drake | ec6baaf | 1999-04-21 18:13:31 +0000 | [diff] [blame] | 1516 | same limitations. Changes to \code{posix.environ}, \code{sys.stdin}, |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1517 | etc.\ are not reflected in the environment of the executed command. |
Tim Peters | dbaf04e | 2003-05-20 16:15:58 +0000 | [diff] [blame] | 1518 | |
Fred Drake | 15eac1f | 2003-05-20 16:21:51 +0000 | [diff] [blame] | 1519 | On \UNIX, the return value is the exit status of the process encoded in the |
Tim Peters | dbaf04e | 2003-05-20 16:15:58 +0000 | [diff] [blame] | 1520 | format specified for \function{wait()}. Note that \POSIX{} does not |
| 1521 | specify the meaning of the return value of the C \cfunction{system()} |
| 1522 | function, so the return value of the Python function is system-dependent. |
| 1523 | |
Fred Drake | 15eac1f | 2003-05-20 16:21:51 +0000 | [diff] [blame] | 1524 | On Windows, the return value is that returned by the system shell after |
Tim Peters | dbaf04e | 2003-05-20 16:15:58 +0000 | [diff] [blame] | 1525 | running \var{command}, given by the Windows environment variable |
Fred Drake | 15eac1f | 2003-05-20 16:21:51 +0000 | [diff] [blame] | 1526 | \envvar{COMSPEC}: on \program{command.com} systems (Windows 95, 98 and ME) |
| 1527 | this is always \code{0}; on \program{cmd.exe} systems (Windows NT, 2000 |
Tim Peters | dbaf04e | 2003-05-20 16:15:58 +0000 | [diff] [blame] | 1528 | and XP) this is the exit status of the command run; on systems using |
| 1529 | a non-native shell, consult your shell documentation. |
| 1530 | |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 1531 | Availability: \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1532 | \end{funcdesc} |
| 1533 | |
| 1534 | \begin{funcdesc}{times}{} |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 1535 | Return a 5-tuple of floating point numbers indicating accumulated |
| 1536 | (processor or other) |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1537 | times, in seconds. The items are: user time, system time, children's |
| 1538 | user time, children's system time, and elapsed real time since a fixed |
Fred Drake | ec6baaf | 1999-04-21 18:13:31 +0000 | [diff] [blame] | 1539 | point in the past, in that order. See the \UNIX{} manual page |
| 1540 | \manpage{times}{2} or the corresponding Windows Platform API |
| 1541 | documentation. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 1542 | Availability: \UNIX, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1543 | \end{funcdesc} |
| 1544 | |
| 1545 | \begin{funcdesc}{wait}{} |
| 1546 | Wait for completion of a child process, and return a tuple containing |
| 1547 | its pid and exit status indication: a 16-bit number, whose low byte is |
| 1548 | the signal number that killed the process, and whose high byte is the |
| 1549 | exit status (if the signal number is zero); the high bit of the low |
| 1550 | byte is set if a core file was produced. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 1551 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1552 | \end{funcdesc} |
| 1553 | |
| 1554 | \begin{funcdesc}{waitpid}{pid, options} |
Fred Drake | 1f89e2a | 2002-05-10 12:37:56 +0000 | [diff] [blame] | 1555 | The details of this function differ on \UNIX{} and Windows. |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 1556 | |
| 1557 | On \UNIX: |
Fred Drake | 31e5e37 | 1999-08-13 13:36:33 +0000 | [diff] [blame] | 1558 | Wait for completion of a child process given by process id \var{pid}, |
| 1559 | and return a tuple containing its process id and exit status |
| 1560 | indication (encoded as for \function{wait()}). The semantics of the |
| 1561 | call are affected by the value of the integer \var{options}, which |
| 1562 | should be \code{0} for normal operation. |
Fred Drake | 31e5e37 | 1999-08-13 13:36:33 +0000 | [diff] [blame] | 1563 | |
| 1564 | If \var{pid} is greater than \code{0}, \function{waitpid()} requests |
| 1565 | status information for that specific process. If \var{pid} is |
| 1566 | \code{0}, the request is for the status of any child in the process |
| 1567 | group of the current process. If \var{pid} is \code{-1}, the request |
| 1568 | pertains to any child of the current process. If \var{pid} is less |
| 1569 | than \code{-1}, status is requested for any process in the process |
| 1570 | group \code{-\var{pid}} (the absolute value of \var{pid}). |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 1571 | |
| 1572 | On Windows: |
Fred Drake | 4dfb7a8 | 2002-04-01 23:30:47 +0000 | [diff] [blame] | 1573 | Wait for completion of a process given by process handle \var{pid}, |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 1574 | and return a tuple containing \var{pid}, |
| 1575 | and its exit status shifted left by 8 bits (shifting makes cross-platform |
| 1576 | use of the function easier). |
| 1577 | A \var{pid} less than or equal to \code{0} has no special meaning on |
| 1578 | Windows, and raises an exception. |
| 1579 | The value of integer \var{options} has no effect. |
| 1580 | \var{pid} can refer to any process whose id is known, not necessarily a |
| 1581 | child process. |
| 1582 | The \function{spawn()} functions called with \constant{P_NOWAIT} |
Fred Drake | 4dfb7a8 | 2002-04-01 23:30:47 +0000 | [diff] [blame] | 1583 | return suitable process handles. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1584 | \end{funcdesc} |
| 1585 | |
| 1586 | \begin{datadesc}{WNOHANG} |
| 1587 | The option for \function{waitpid()} to avoid hanging if no child |
| 1588 | process status is available immediately. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 1589 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1590 | \end{datadesc} |
| 1591 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 1592 | \begin{datadesc}{WCONTINUED} |
| 1593 | This option causes child processes to be reported if they have been |
| 1594 | continued from a job control stop since their status was last |
| 1595 | reported. |
| 1596 | Availability: Some \UNIX{} systems. |
| 1597 | \versionadded{2.3} |
| 1598 | \end{datadesc} |
| 1599 | |
| 1600 | \begin{datadesc}{WUNTRACED} |
| 1601 | This option causes child processes to be reported if they have been |
| 1602 | stopped but their current state has not been reported since they were |
| 1603 | stopped. |
| 1604 | Availability: \UNIX. |
| 1605 | \versionadded{2.3} |
| 1606 | \end{datadesc} |
| 1607 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 1608 | The following functions take a process status code as returned by |
| 1609 | \function{system()}, \function{wait()}, or \function{waitpid()} as a |
| 1610 | parameter. They may be used to determine the disposition of a |
| 1611 | process. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1612 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 1613 | \begin{funcdesc}{WCOREDUMP}{status} |
| 1614 | Returns \code{True} if a core dump was generated for the process, |
| 1615 | otherwise it returns \code{False}. |
| 1616 | Availability: \UNIX. |
| 1617 | \versionadded{2.3} |
| 1618 | \end{funcdesc} |
| 1619 | |
| 1620 | \begin{funcdesc}{WIFCONTINUED}{status} |
| 1621 | Returns \code{True} if the process has been continued from a job |
| 1622 | control stop, otherwise it returns \code{False}. |
| 1623 | Availability: \UNIX. |
| 1624 | \versionadded{2.3} |
| 1625 | \end{funcdesc} |
| 1626 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1627 | \begin{funcdesc}{WIFSTOPPED}{status} |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 1628 | Returns \code{True} if the process has been stopped, otherwise it |
| 1629 | returns \code{False}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 1630 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1631 | \end{funcdesc} |
| 1632 | |
| 1633 | \begin{funcdesc}{WIFSIGNALED}{status} |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 1634 | Returns \code{True} if the process exited due to a signal, otherwise |
| 1635 | it returns \code{False}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 1636 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1637 | \end{funcdesc} |
| 1638 | |
| 1639 | \begin{funcdesc}{WIFEXITED}{status} |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 1640 | Returns \code{True} if the process exited using the \manpage{exit}{2} |
| 1641 | system call, otherwise it returns \code{False}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 1642 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1643 | \end{funcdesc} |
| 1644 | |
| 1645 | \begin{funcdesc}{WEXITSTATUS}{status} |
| 1646 | If \code{WIFEXITED(\var{status})} is true, return the integer |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 1647 | parameter to the \manpage{exit}{2} system call. Otherwise, the return |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1648 | value is meaningless. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 1649 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1650 | \end{funcdesc} |
| 1651 | |
| 1652 | \begin{funcdesc}{WSTOPSIG}{status} |
Fred Drake | 35c3ffd | 1999-03-04 14:08:10 +0000 | [diff] [blame] | 1653 | Return the signal which caused the process to stop. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 1654 | Availability: \UNIX. |
Fred Drake | 35c3ffd | 1999-03-04 14:08:10 +0000 | [diff] [blame] | 1655 | \end{funcdesc} |
| 1656 | |
| 1657 | \begin{funcdesc}{WTERMSIG}{status} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1658 | Return the signal which caused the process to exit. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 1659 | Availability: \UNIX. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1660 | \end{funcdesc} |
| 1661 | |
| 1662 | |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 1663 | \subsection{Miscellaneous System Information \label{os-path}} |
Fred Drake | 88f6ca2 | 1999-12-15 19:39:04 +0000 | [diff] [blame] | 1664 | |
| 1665 | |
| 1666 | \begin{funcdesc}{confstr}{name} |
| 1667 | Return string-valued system configuration values. |
| 1668 | \var{name} specifies the configuration value to retrieve; it may be a |
| 1669 | string which is the name of a defined system value; these names are |
Raymond Hettinger | b67449d | 2003-09-08 18:52:18 +0000 | [diff] [blame] | 1670 | specified in a number of standards (\POSIX, \UNIX{} 95, \UNIX{} 98, and |
Fred Drake | 88f6ca2 | 1999-12-15 19:39:04 +0000 | [diff] [blame] | 1671 | others). Some platforms define additional names as well. The names |
| 1672 | known to the host operating system are given in the |
| 1673 | \code{confstr_names} dictionary. For configuration variables not |
| 1674 | included in that mapping, passing an integer for \var{name} is also |
| 1675 | accepted. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 1676 | Availability: \UNIX. |
Fred Drake | 88f6ca2 | 1999-12-15 19:39:04 +0000 | [diff] [blame] | 1677 | |
| 1678 | If the configuration value specified by \var{name} isn't defined, the |
| 1679 | empty string is returned. |
| 1680 | |
| 1681 | If \var{name} is a string and is not known, \exception{ValueError} is |
| 1682 | raised. If a specific value for \var{name} is not supported by the |
| 1683 | host system, even if it is included in \code{confstr_names}, an |
| 1684 | \exception{OSError} is raised with \constant{errno.EINVAL} for the |
| 1685 | error number. |
| 1686 | \end{funcdesc} |
| 1687 | |
| 1688 | \begin{datadesc}{confstr_names} |
| 1689 | Dictionary mapping names accepted by \function{confstr()} to the |
| 1690 | integer values defined for those names by the host operating system. |
| 1691 | This can be used to determine the set of names known to the system. |
| 1692 | Availability: \UNIX. |
| 1693 | \end{datadesc} |
| 1694 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 1695 | \begin{funcdesc}{getloadavg}{} |
| 1696 | Return the number of processes in the system run queue averaged over |
| 1697 | the last 1, 5, and 15 minutes or raises OSError if the load average |
| 1698 | was unobtainable. |
| 1699 | |
| 1700 | \versionadded{2.3} |
| 1701 | \end{funcdesc} |
| 1702 | |
Fred Drake | 88f6ca2 | 1999-12-15 19:39:04 +0000 | [diff] [blame] | 1703 | \begin{funcdesc}{sysconf}{name} |
| 1704 | Return integer-valued system configuration values. |
| 1705 | If the configuration value specified by \var{name} isn't defined, |
| 1706 | \code{-1} is returned. The comments regarding the \var{name} |
| 1707 | parameter for \function{confstr()} apply here as well; the dictionary |
| 1708 | that provides information on the known names is given by |
| 1709 | \code{sysconf_names}. |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 1710 | Availability: \UNIX. |
Fred Drake | 88f6ca2 | 1999-12-15 19:39:04 +0000 | [diff] [blame] | 1711 | \end{funcdesc} |
| 1712 | |
| 1713 | \begin{datadesc}{sysconf_names} |
| 1714 | Dictionary mapping names accepted by \function{sysconf()} to the |
| 1715 | integer values defined for those names by the host operating system. |
| 1716 | This can be used to determine the set of names known to the system. |
| 1717 | Availability: \UNIX. |
| 1718 | \end{datadesc} |
| 1719 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1720 | |
| 1721 | The follow data values are used to support path manipulation |
| 1722 | operations. These are defined for all platforms. |
| 1723 | |
| 1724 | Higher-level operations on pathnames are defined in the |
| 1725 | \refmodule{os.path} module. |
| 1726 | |
| 1727 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1728 | \begin{datadesc}{curdir} |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 1729 | The constant string used by the operating system to refer to the current |
| 1730 | directory. |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 1731 | For example: \code{'.'} for \POSIX{} or \code{':'} for the Macintosh. |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 1732 | Also available via \module{os.path}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1733 | \end{datadesc} |
| 1734 | |
| 1735 | \begin{datadesc}{pardir} |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 1736 | The constant string used by the operating system to refer to the parent |
| 1737 | directory. |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 1738 | For example: \code{'..'} for \POSIX{} or \code{'::'} for the Macintosh. |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 1739 | Also available via \module{os.path}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1740 | \end{datadesc} |
| 1741 | |
| 1742 | \begin{datadesc}{sep} |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 1743 | The character used by the operating system to separate pathname components, |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 1744 | for example, \character{/} for \POSIX{} or \character{:} for the |
| 1745 | Macintosh. Note that knowing this is not sufficient to be able to |
| 1746 | parse or concatenate pathnames --- use \function{os.path.split()} and |
Fred Drake | 1a3c2a0 | 1998-08-06 15:18:23 +0000 | [diff] [blame] | 1747 | \function{os.path.join()} --- but it is occasionally useful. |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 1748 | Also available via \module{os.path}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1749 | \end{datadesc} |
| 1750 | |
Guido van Rossum | b2afc81 | 1997-08-29 22:37:44 +0000 | [diff] [blame] | 1751 | \begin{datadesc}{altsep} |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 1752 | An alternative character used by the operating system to separate pathname |
| 1753 | components, or \code{None} if only one separator character exists. This is |
Martin v. Löwis | 36a4d8c | 2002-10-10 18:24:54 +0000 | [diff] [blame] | 1754 | set to \character{/} on Windows systems where \code{sep} is a |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 1755 | backslash. |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 1756 | Also available via \module{os.path}. |
Guido van Rossum | b2afc81 | 1997-08-29 22:37:44 +0000 | [diff] [blame] | 1757 | \end{datadesc} |
| 1758 | |
Skip Montanaro | 47e46e2 | 2003-02-14 05:45:31 +0000 | [diff] [blame] | 1759 | \begin{datadesc}{extsep} |
Fred Drake | 002a5de | 2003-02-14 06:39:37 +0000 | [diff] [blame] | 1760 | The character which separates the base filename from the extension; |
| 1761 | for example, the \character{.} in \file{os.py}. |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 1762 | Also available via \module{os.path}. |
Fred Drake | 002a5de | 2003-02-14 06:39:37 +0000 | [diff] [blame] | 1763 | \versionadded{2.2} |
Skip Montanaro | 47e46e2 | 2003-02-14 05:45:31 +0000 | [diff] [blame] | 1764 | \end{datadesc} |
| 1765 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 1766 | \begin{datadesc}{pathsep} |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 1767 | The character conventionally used by the operating system to separate |
| 1768 | search patch components (as in \envvar{PATH}), such as \character{:} for |
Martin v. Löwis | 36a4d8c | 2002-10-10 18:24:54 +0000 | [diff] [blame] | 1769 | \POSIX{} or \character{;} for Windows. |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 1770 | Also available via \module{os.path}. |
Guido van Rossum | 9c59ce9 | 1998-06-30 15:54:27 +0000 | [diff] [blame] | 1771 | \end{datadesc} |
| 1772 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 1773 | \begin{datadesc}{defpath} |
Fred Drake | 6995bb6 | 2001-11-29 20:48:44 +0000 | [diff] [blame] | 1774 | The default search path used by \function{exec*p*()} and |
| 1775 | \function{spawn*p*()} if the environment doesn't have a \code{'PATH'} |
| 1776 | key. |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 1777 | Also available via \module{os.path}. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 1778 | \end{datadesc} |
| 1779 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1780 | \begin{datadesc}{linesep} |
| 1781 | The string used to separate (or, rather, terminate) lines on the |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 1782 | current platform. This may be a single character, such as \code{'\e |
Fred Drake | 6995bb6 | 2001-11-29 20:48:44 +0000 | [diff] [blame] | 1783 | n'} for \POSIX{} or \code{'\e r'} for Mac OS, or multiple characters, |
Martin v. Löwis | 36a4d8c | 2002-10-10 18:24:54 +0000 | [diff] [blame] | 1784 | for example, \code{'\e r\e n'} for Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1785 | \end{datadesc} |