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. |
| 62 | |
| 63 | When exceptions are strings, the string for the exception is |
| 64 | \code{'OSError'}. |
| 65 | \end{excdesc} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 67 | \begin{datadesc}{name} |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 68 | The name of the operating system dependent module imported. The |
| 69 | following names have currently been registered: \code{'posix'}, \code{'nt'}, |
Fred Drake | 933d5a7 | 1999-09-17 14:38:39 +0000 | [diff] [blame] | 70 | \code{'dos'}, \code{'mac'}, \code{'os2'}, \code{'ce'}, \code{'java'}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 71 | \end{datadesc} |
| 72 | |
| 73 | \begin{datadesc}{path} |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 74 | The corresponding operating system dependent standard module for pathname |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 75 | operations, such as \module{posixpath} or \module{macpath}. Thus, |
| 76 | given the proper imports, \code{os.path.split(\var{file})} is |
| 77 | equivalent to but more portable than |
| 78 | \code{posixpath.split(\var{file})}. Note that this is also an |
| 79 | importable module: it may be imported directly as |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 80 | \refmodule{os.path}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 81 | \end{datadesc} |
| 82 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 83 | |
| 84 | |
| 85 | \subsection{Process Parameters \label{os-procinfo}} |
| 86 | |
| 87 | These functions and data items provide information and operate on the |
| 88 | current process and user. |
| 89 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 90 | \begin{datadesc}{environ} |
Fred Drake | 0e1de8b | 1999-04-29 12:57:32 +0000 | [diff] [blame] | 91 | A mapping object representing the string environment. For example, |
| 92 | \code{environ['HOME']} is the pathname of your home directory (on some |
| 93 | platforms), and is equivalent to \code{getenv("HOME")} in C. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 94 | |
| 95 | If the platform supports the \function{putenv()} function, this |
| 96 | mapping may be used to modify the environment as well as query the |
| 97 | environment. \function{putenv()} will be called automatically when |
| 98 | the mapping is modified. |
| 99 | |
| 100 | If \function{putenv()} is not provided, this mapping may be passed to |
| 101 | the appropriate process-creation functions to cause child processes to |
| 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} |
| 106 | \funclineni{getcwd}{} |
| 107 | These functions are described in ``Files and Directories'' (section |
| 108 | \ref{os-file-dir}). |
| 109 | \end{funcdescni} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 110 | |
Fred Drake | 18f7a45 | 1999-12-09 22:11:43 +0000 | [diff] [blame] | 111 | \begin{funcdesc}{ctermid}{} |
| 112 | Return the filename corresponding to the controlling terminal of the |
| 113 | process. |
| 114 | Availability: \UNIX{}. |
| 115 | \end{funcdesc} |
| 116 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 117 | \begin{funcdesc}{getegid}{} |
| 118 | Return the current process' effective group id. |
| 119 | Availability: \UNIX{}. |
| 120 | \end{funcdesc} |
| 121 | |
| 122 | \begin{funcdesc}{geteuid}{} |
Fred Drake | 6b330ba8 | 1999-05-25 13:42:26 +0000 | [diff] [blame] | 123 | \index{user!effective id} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 124 | Return the current process' effective user id. |
| 125 | Availability: \UNIX{}. |
| 126 | \end{funcdesc} |
| 127 | |
| 128 | \begin{funcdesc}{getgid}{} |
Fred Drake | 6b330ba8 | 1999-05-25 13:42:26 +0000 | [diff] [blame] | 129 | \index{process!group} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 130 | Return the current process' group id. |
| 131 | Availability: \UNIX{}. |
| 132 | \end{funcdesc} |
| 133 | |
Fred Drake | 88f6ca2 | 1999-12-15 19:39:04 +0000 | [diff] [blame] | 134 | \begin{funcdesc}{getgroups}{} |
| 135 | Return list of supplemental group ids associated with the current |
| 136 | process. |
| 137 | Availability: \UNIX{}. |
| 138 | \end{funcdesc} |
| 139 | |
| 140 | \begin{funcdesc}{getlogin}{} |
| 141 | Return the actual login name for the current process, even if there |
| 142 | are multiple login names which map to the same user id. |
| 143 | Availability: \UNIX{}. |
| 144 | \end{funcdesc} |
| 145 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 146 | \begin{funcdesc}{getpgrp}{} |
| 147 | \index{process!group} |
| 148 | Return the current process group id. |
| 149 | Availability: \UNIX{}. |
| 150 | \end{funcdesc} |
| 151 | |
| 152 | \begin{funcdesc}{getpid}{} |
| 153 | \index{process!id} |
| 154 | Return the current process id. |
| 155 | Availability: \UNIX{}, Windows. |
| 156 | \end{funcdesc} |
| 157 | |
| 158 | \begin{funcdesc}{getppid}{} |
| 159 | \index{process!id of parent} |
| 160 | Return the parent's process id. |
| 161 | Availability: \UNIX{}. |
| 162 | \end{funcdesc} |
| 163 | |
| 164 | \begin{funcdesc}{getuid}{} |
Fred Drake | 6b330ba8 | 1999-05-25 13:42:26 +0000 | [diff] [blame] | 165 | \index{user!id} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 166 | Return the current process' user id. |
| 167 | Availability: \UNIX{}. |
| 168 | \end{funcdesc} |
| 169 | |
Fred Drake | 81e142b | 2001-05-31 20:27:46 +0000 | [diff] [blame] | 170 | \begin{funcdesc}{getenv}{varname\optional{, value}} |
| 171 | Return the value of the environment variable \var{varname} if it |
| 172 | exists, or \var{value} if it doesn't. \var{value} defaults to |
| 173 | \code{None}. |
| 174 | Availability: most flavors of \UNIX{}, Windows. |
| 175 | \end{funcdesc} |
| 176 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 177 | \begin{funcdesc}{putenv}{varname, value} |
| 178 | \index{environment variables!setting} |
| 179 | Set the environment variable named \var{varname} to the string |
| 180 | \var{value}. Such changes to the environment affect subprocesses |
| 181 | started with \function{os.system()}, \function{popen()} or |
| 182 | \function{fork()} and \function{execv()}. |
| 183 | Availability: most flavors of \UNIX{}, Windows. |
| 184 | |
| 185 | When \function{putenv()} is |
| 186 | supported, assignments to items in \code{os.environ} are automatically |
| 187 | translated into corresponding calls to \function{putenv()}; however, |
| 188 | calls to \function{putenv()} don't update \code{os.environ}, so it is |
| 189 | actually preferable to assign to items of \code{os.environ}. |
| 190 | \end{funcdesc} |
| 191 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 192 | \begin{funcdesc}{setegid}{egid} |
| 193 | Set the current process's effective group id. |
| 194 | Availability: \UNIX{}. |
| 195 | \end{funcdesc} |
| 196 | |
| 197 | \begin{funcdesc}{seteuid}{euid} |
| 198 | Set the current process's effective user id. |
| 199 | Availability: \UNIX{}. |
| 200 | \end{funcdesc} |
| 201 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 202 | \begin{funcdesc}{setgid}{gid} |
| 203 | Set the current process' group id. |
| 204 | Availability: \UNIX{}. |
| 205 | \end{funcdesc} |
| 206 | |
| 207 | \begin{funcdesc}{setpgrp}{} |
| 208 | Calls the system call \cfunction{setpgrp()} or \cfunction{setpgrp(0, |
| 209 | 0)} depending on which version is implemented (if any). See the |
| 210 | \UNIX{} manual for the semantics. |
| 211 | Availability: \UNIX{}. |
| 212 | \end{funcdesc} |
| 213 | |
| 214 | \begin{funcdesc}{setpgid}{pid, pgrp} |
| 215 | Calls the system call \cfunction{setpgid()}. See the \UNIX{} manual |
| 216 | for the semantics. |
| 217 | Availability: \UNIX{}. |
| 218 | \end{funcdesc} |
| 219 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 220 | \begin{funcdesc}{setreuid}{ruid, euid} |
| 221 | Set the current process's real and effective user ids. |
| 222 | Availability: \UNIX{}. |
| 223 | \end{funcdesc} |
| 224 | |
| 225 | \begin{funcdesc}{setregid}{rgid, egid} |
| 226 | Set the current process's real and effective group ids. |
| 227 | Availability: \UNIX{}. |
| 228 | \end{funcdesc} |
| 229 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 230 | \begin{funcdesc}{setsid}{} |
| 231 | Calls the system call \cfunction{setsid()}. See the \UNIX{} manual |
| 232 | for the semantics. |
| 233 | Availability: \UNIX{}. |
| 234 | \end{funcdesc} |
| 235 | |
| 236 | \begin{funcdesc}{setuid}{uid} |
Fred Drake | 6b330ba8 | 1999-05-25 13:42:26 +0000 | [diff] [blame] | 237 | \index{user!id, setting} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 238 | Set the current process' user id. |
| 239 | Availability: \UNIX{}. |
| 240 | \end{funcdesc} |
| 241 | |
| 242 | % placed in this section since it relates to errno.... a little weak ;-( |
| 243 | \begin{funcdesc}{strerror}{code} |
| 244 | Return the error message corresponding to the error code in |
| 245 | \var{code}. |
| 246 | Availability: \UNIX{}, Windows. |
| 247 | \end{funcdesc} |
| 248 | |
| 249 | \begin{funcdesc}{umask}{mask} |
| 250 | Set the current numeric umask and returns the previous umask. |
| 251 | Availability: \UNIX{}, Windows. |
| 252 | \end{funcdesc} |
| 253 | |
| 254 | \begin{funcdesc}{uname}{} |
| 255 | Return a 5-tuple containing information identifying the current |
| 256 | operating system. The tuple contains 5 strings: |
| 257 | \code{(\var{sysname}, \var{nodename}, \var{release}, \var{version}, |
| 258 | \var{machine})}. Some systems truncate the nodename to 8 |
| 259 | characters or to the leading component; a better way to get the |
| 260 | hostname is \function{socket.gethostname()} |
| 261 | \withsubitem{(in module socket)}{\ttindex{gethostname()}} |
| 262 | or even |
| 263 | \withsubitem{(in module socket)}{\ttindex{gethostbyaddr()}} |
| 264 | \code{socket.gethostbyaddr(socket.gethostname())}. |
| 265 | Availability: recent flavors of \UNIX{}. |
| 266 | \end{funcdesc} |
| 267 | |
| 268 | |
| 269 | |
| 270 | \subsection{File Object Creation \label{os-newstreams}} |
| 271 | |
| 272 | These functions create new file objects. |
| 273 | |
| 274 | |
| 275 | \begin{funcdesc}{fdopen}{fd\optional{, mode\optional{, bufsize}}} |
| 276 | Return an open file object connected to the file descriptor \var{fd}. |
Fred Drake | 8c9fc00 | 1999-08-05 13:41:31 +0000 | [diff] [blame] | 277 | \index{I/O control!buffering} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 278 | The \var{mode} and \var{bufsize} arguments have the same meaning as |
| 279 | the corresponding arguments to the built-in \function{open()} |
| 280 | function. |
| 281 | Availability: Macintosh, \UNIX{}, Windows. |
| 282 | \end{funcdesc} |
| 283 | |
| 284 | \begin{funcdesc}{popen}{command\optional{, mode\optional{, bufsize}}} |
| 285 | Open a pipe to or from \var{command}. The return value is an open |
| 286 | file object connected to the pipe, which can be read or written |
| 287 | depending on whether \var{mode} is \code{'r'} (default) or \code{'w'}. |
| 288 | The \var{bufsize} argument has the same meaning as the corresponding |
| 289 | argument to the built-in \function{open()} function. The exit status of |
| 290 | the command (encoded in the format specified for \function{wait()}) is |
| 291 | available as the return value of the \method{close()} method of the file |
| 292 | object, except that when the exit status is zero (termination without |
Fred Drake | 1319e3e | 2000-10-03 17:14:27 +0000 | [diff] [blame] | 293 | errors), \code{None} is returned. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 294 | Availability: \UNIX{}, Windows. |
Fred Drake | c71c23e | 2000-10-04 13:57:27 +0000 | [diff] [blame] | 295 | |
| 296 | \versionchanged[This function worked unreliably under Windows in |
| 297 | earlier versions of Python. This was due to the use of the |
| 298 | \cfunction{_popen()} function from the libraries provided with |
| 299 | Windows. Newer versions of Python do not use the broken |
| 300 | implementation from the Windows libraries]{2.0} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 301 | \end{funcdesc} |
| 302 | |
Fred Drake | 18f7a45 | 1999-12-09 22:11:43 +0000 | [diff] [blame] | 303 | \begin{funcdesc}{tmpfile}{} |
| 304 | Return a new file object opened in update mode (\samp{w+}). The file |
| 305 | has no directory entries associated with it and will be automatically |
| 306 | deleted once there are no file descriptors for the file. |
Fred Drake | efaef13 | 2001-07-17 20:39:18 +0000 | [diff] [blame] | 307 | Availability: \UNIX{}, Windows. |
Fred Drake | 18f7a45 | 1999-12-09 22:11:43 +0000 | [diff] [blame] | 308 | \end{funcdesc} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 309 | |
| 310 | |
Fred Drake | 8a9db99 | 2000-09-28 20:27:51 +0000 | [diff] [blame] | 311 | For each of these \function{popen()} variants, if \var{bufsize} is |
| 312 | specified, it specifies the buffer size for the I/O pipes. |
| 313 | \var{mode}, if provided, should be the string \code{'b'} or |
| 314 | \code{'t'}; on Windows this is needed to determine whether the file |
| 315 | objects should be opened in binary or text mode. The default value |
| 316 | for \var{mode} is \code{'t'}. |
| 317 | |
Fred Drake | 098d7fa | 2001-09-11 19:56:51 +0000 | [diff] [blame] | 318 | These methods do not make it possible to retrieve the return code from |
| 319 | the child processes. The only way to control the input and output |
| 320 | streams and also retrieve the return codes is to use the |
| 321 | \class{Popen3} and \class{Popen4} classes from the \refmodule{popen2} |
| 322 | module; these are only available on \UNIX. |
| 323 | |
Fred Drake | 046f4d8 | 2001-06-11 15:21:48 +0000 | [diff] [blame] | 324 | \begin{funcdesc}{popen2}{cmd\optional{, mode\optional{, bufsize}}} |
Fred Drake | 8a9db99 | 2000-09-28 20:27:51 +0000 | [diff] [blame] | 325 | Executes \var{cmd} as a sub-process. Returns the file objects |
| 326 | \code{(\var{child_stdin}, \var{child_stdout})}. |
Fred Drake | 0b9bc20 | 2001-06-11 18:25:34 +0000 | [diff] [blame] | 327 | Availability: \UNIX{}, Windows. |
Fred Drake | 8a9db99 | 2000-09-28 20:27:51 +0000 | [diff] [blame] | 328 | \versionadded{2.0} |
| 329 | \end{funcdesc} |
| 330 | |
Fred Drake | 046f4d8 | 2001-06-11 15:21:48 +0000 | [diff] [blame] | 331 | \begin{funcdesc}{popen3}{cmd\optional{, mode\optional{, bufsize}}} |
Fred Drake | 8a9db99 | 2000-09-28 20:27:51 +0000 | [diff] [blame] | 332 | Executes \var{cmd} as a sub-process. Returns the file objects |
| 333 | \code{(\var{child_stdin}, \var{child_stdout}, \var{child_stderr})}. |
Fred Drake | 0b9bc20 | 2001-06-11 18:25:34 +0000 | [diff] [blame] | 334 | Availability: \UNIX{}, Windows. |
Fred Drake | 8a9db99 | 2000-09-28 20:27:51 +0000 | [diff] [blame] | 335 | \versionadded{2.0} |
| 336 | \end{funcdesc} |
| 337 | |
Fred Drake | 046f4d8 | 2001-06-11 15:21:48 +0000 | [diff] [blame] | 338 | \begin{funcdesc}{popen4}{cmd\optional{, mode\optional{, bufsize}}} |
Fred Drake | 8a9db99 | 2000-09-28 20:27:51 +0000 | [diff] [blame] | 339 | Executes \var{cmd} as a sub-process. Returns the file objects |
| 340 | \code{(\var{child_stdin}, \var{child_stdout_and_stderr})}. |
Fred Drake | 0b9bc20 | 2001-06-11 18:25:34 +0000 | [diff] [blame] | 341 | Availability: \UNIX{}, Windows. |
Fred Drake | 8a9db99 | 2000-09-28 20:27:51 +0000 | [diff] [blame] | 342 | \versionadded{2.0} |
| 343 | \end{funcdesc} |
| 344 | |
| 345 | This functionality is also available in the \refmodule{popen2} module |
| 346 | using functions of the same names, but the return values of those |
| 347 | functions have a different order. |
| 348 | |
| 349 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 350 | \subsection{File Descriptor Operations \label{os-fd-ops}} |
| 351 | |
| 352 | These functions operate on I/O streams referred to |
| 353 | using file descriptors. |
| 354 | |
| 355 | |
| 356 | \begin{funcdesc}{close}{fd} |
| 357 | Close file descriptor \var{fd}. |
| 358 | Availability: Macintosh, \UNIX{}, Windows. |
| 359 | |
| 360 | Note: this function is intended for low-level I/O and must be applied |
| 361 | to a file descriptor as returned by \function{open()} or |
| 362 | \function{pipe()}. To close a ``file object'' returned by the |
| 363 | built-in function \function{open()} or by \function{popen()} or |
| 364 | \function{fdopen()}, use its \method{close()} method. |
| 365 | \end{funcdesc} |
| 366 | |
| 367 | \begin{funcdesc}{dup}{fd} |
| 368 | Return a duplicate of file descriptor \var{fd}. |
| 369 | Availability: Macintosh, \UNIX{}, Windows. |
| 370 | \end{funcdesc} |
| 371 | |
| 372 | \begin{funcdesc}{dup2}{fd, fd2} |
| 373 | Duplicate file descriptor \var{fd} to \var{fd2}, closing the latter |
| 374 | first if necessary. |
| 375 | Availability: \UNIX{}, Windows. |
| 376 | \end{funcdesc} |
| 377 | |
Fred Drake | 88f6ca2 | 1999-12-15 19:39:04 +0000 | [diff] [blame] | 378 | \begin{funcdesc}{fpathconf}{fd, name} |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 379 | Return system configuration information relevant to an open file. |
Fred Drake | 88f6ca2 | 1999-12-15 19:39:04 +0000 | [diff] [blame] | 380 | \var{name} specifies the configuration value to retrieve; it may be a |
| 381 | string which is the name of a defined system value; these names are |
| 382 | specified in a number of standards (\POSIX.1, Unix95, Unix98, and |
| 383 | others). Some platforms define additional names as well. The names |
| 384 | known to the host operating system are given in the |
| 385 | \code{pathconf_names} dictionary. For configuration variables not |
| 386 | included in that mapping, passing an integer for \var{name} is also |
| 387 | accepted. |
| 388 | Availability: \UNIX{}. |
| 389 | |
| 390 | If \var{name} is a string and is not known, \exception{ValueError} is |
| 391 | raised. If a specific value for \var{name} is not supported by the |
| 392 | host system, even if it is included in \code{pathconf_names}, an |
| 393 | \exception{OSError} is raised with \constant{errno.EINVAL} for the |
| 394 | error number. |
| 395 | \end{funcdesc} |
| 396 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 397 | \begin{funcdesc}{fstat}{fd} |
| 398 | Return status for file descriptor \var{fd}, like \function{stat()}. |
| 399 | Availability: \UNIX{}, Windows. |
| 400 | \end{funcdesc} |
| 401 | |
| 402 | \begin{funcdesc}{fstatvfs}{fd} |
| 403 | Return information about the filesystem containing the file associated |
| 404 | with file descriptor \var{fd}, like \function{statvfs()}. |
| 405 | Availability: \UNIX{}. |
| 406 | \end{funcdesc} |
| 407 | |
| 408 | \begin{funcdesc}{ftruncate}{fd, length} |
| 409 | Truncate the file corresponding to file descriptor \var{fd}, |
| 410 | so that it is at most \var{length} bytes in size. |
| 411 | Availability: \UNIX{}. |
| 412 | \end{funcdesc} |
| 413 | |
Skip Montanaro | d372521 | 2000-07-19 17:30:58 +0000 | [diff] [blame] | 414 | \begin{funcdesc}{isatty}{fd} |
| 415 | Return \code{1} if the file descriptor \var{fd} is open and connected to a |
| 416 | tty(-like) device, else \code{0}. |
| 417 | Availability: \UNIX{} |
| 418 | \end{funcdesc} |
| 419 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 420 | \begin{funcdesc}{lseek}{fd, pos, how} |
| 421 | Set the current position of file descriptor \var{fd} to position |
| 422 | \var{pos}, modified by \var{how}: \code{0} to set the position |
| 423 | relative to the beginning of the file; \code{1} to set it relative to |
| 424 | the current position; \code{2} to set it relative to the end of the |
| 425 | file. |
| 426 | Availability: Macintosh, \UNIX{}, Windows. |
| 427 | \end{funcdesc} |
| 428 | |
| 429 | \begin{funcdesc}{open}{file, flags\optional{, mode}} |
| 430 | Open the file \var{file} and set various flags according to |
| 431 | \var{flags} and possibly its mode according to \var{mode}. |
| 432 | The default \var{mode} is \code{0777} (octal), and the current umask |
| 433 | value is first masked out. Return the file descriptor for the newly |
| 434 | opened file. |
| 435 | Availability: Macintosh, \UNIX{}, Windows. |
| 436 | |
| 437 | For a description of the flag and mode values, see the C run-time |
| 438 | documentation; flag constants (like \constant{O_RDONLY} and |
| 439 | \constant{O_WRONLY}) are defined in this module too (see below). |
| 440 | |
| 441 | Note: this function is intended for low-level I/O. For normal usage, |
| 442 | use the built-in function \function{open()}, which returns a ``file |
| 443 | object'' with \method{read()} and \method{write()} methods (and many |
| 444 | more). |
| 445 | \end{funcdesc} |
| 446 | |
Fred Drake | c82634c | 2000-06-28 17:27:48 +0000 | [diff] [blame] | 447 | \begin{funcdesc}{openpty}{} |
| 448 | Open a new pseudo-terminal pair. Return a pair of file descriptors |
| 449 | \code{(\var{master}, \var{slave})} for the pty and the tty, |
| 450 | respectively. For a (slightly) more portable approach, use the |
| 451 | \refmodule{pty}\refstmodindex{pty} module. |
| 452 | Availability: Some flavors of \UNIX{} |
| 453 | \end{funcdesc} |
| 454 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 455 | \begin{funcdesc}{pipe}{} |
| 456 | Create a pipe. Return a pair of file descriptors \code{(\var{r}, |
| 457 | \var{w})} usable for reading and writing, respectively. |
| 458 | Availability: \UNIX{}, Windows. |
| 459 | \end{funcdesc} |
| 460 | |
| 461 | \begin{funcdesc}{read}{fd, n} |
| 462 | Read at most \var{n} bytes from file descriptor \var{fd}. |
| 463 | Return a string containing the bytes read. |
| 464 | Availability: Macintosh, \UNIX{}, Windows. |
| 465 | |
| 466 | Note: this function is intended for low-level I/O and must be applied |
| 467 | to a file descriptor as returned by \function{open()} or |
| 468 | \function{pipe()}. To read a ``file object'' returned by the |
| 469 | built-in function \function{open()} or by \function{popen()} or |
| 470 | \function{fdopen()}, or \code{sys.stdin}, use its |
| 471 | \method{read()} or \method{readline()} methods. |
| 472 | \end{funcdesc} |
| 473 | |
| 474 | \begin{funcdesc}{tcgetpgrp}{fd} |
| 475 | Return the process group associated with the terminal given by |
| 476 | \var{fd} (an open file descriptor as returned by \function{open()}). |
| 477 | Availability: \UNIX{}. |
| 478 | \end{funcdesc} |
| 479 | |
| 480 | \begin{funcdesc}{tcsetpgrp}{fd, pg} |
| 481 | Set the process group associated with the terminal given by |
| 482 | \var{fd} (an open file descriptor as returned by \function{open()}) |
| 483 | to \var{pg}. |
| 484 | Availability: \UNIX{}. |
| 485 | \end{funcdesc} |
| 486 | |
| 487 | \begin{funcdesc}{ttyname}{fd} |
| 488 | Return a string which specifies the terminal device associated with |
| 489 | file-descriptor \var{fd}. If \var{fd} is not associated with a terminal |
| 490 | device, an exception is raised. |
| 491 | Availability: \UNIX{}. |
| 492 | \end{funcdesc} |
| 493 | |
| 494 | \begin{funcdesc}{write}{fd, str} |
| 495 | Write the string \var{str} to file descriptor \var{fd}. |
| 496 | Return the number of bytes actually written. |
| 497 | Availability: Macintosh, \UNIX{}, Windows. |
| 498 | |
| 499 | Note: this function is intended for low-level I/O and must be applied |
| 500 | to a file descriptor as returned by \function{open()} or |
| 501 | \function{pipe()}. To write a ``file object'' returned by the |
| 502 | built-in function \function{open()} or by \function{popen()} or |
| 503 | \function{fdopen()}, or \code{sys.stdout} or \code{sys.stderr}, use |
| 504 | its \method{write()} method. |
| 505 | \end{funcdesc} |
| 506 | |
| 507 | |
| 508 | The following data items are available for use in constructing the |
| 509 | \var{flags} parameter to the \function{open()} function. |
| 510 | |
| 511 | \begin{datadesc}{O_RDONLY} |
| 512 | \dataline{O_WRONLY} |
| 513 | \dataline{O_RDWR} |
| 514 | \dataline{O_NDELAY} |
| 515 | \dataline{O_NONBLOCK} |
| 516 | \dataline{O_APPEND} |
| 517 | \dataline{O_DSYNC} |
| 518 | \dataline{O_RSYNC} |
| 519 | \dataline{O_SYNC} |
| 520 | \dataline{O_NOCTTY} |
| 521 | \dataline{O_CREAT} |
| 522 | \dataline{O_EXCL} |
| 523 | \dataline{O_TRUNC} |
| 524 | Options for the \var{flag} argument to the \function{open()} function. |
| 525 | These can be bit-wise OR'd together. |
| 526 | Availability: Macintosh, \UNIX{}, Windows. |
| 527 | \end{datadesc} |
| 528 | |
Fred Drake | 3ac977e | 2000-08-11 20:19:51 +0000 | [diff] [blame] | 529 | \begin{datadesc}{O_BINARY} |
| 530 | Option for the \var{flag} argument to the \function{open()} function. |
| 531 | This can be bit-wise OR'd together with those listed above. |
| 532 | Availability: Macintosh, Windows. |
| 533 | % XXX need to check on the availability of this one. |
| 534 | \end{datadesc} |
| 535 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 536 | |
| 537 | \subsection{Files and Directories \label{os-file-dir}} |
| 538 | |
| 539 | \begin{funcdesc}{access}{path, mode} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 540 | Check read/write/execute permissions for this process or existence of |
| 541 | file \var{path}. \var{mode} should be \constant{F_OK} to test the |
| 542 | existence of \var{path}, or it can be the inclusive OR of one or more |
| 543 | of \constant{R_OK}, \constant{W_OK}, and \constant{X_OK} to test |
| 544 | permissions. Return \code{1} if access is allowed, \code{0} if not. |
| 545 | See the \UNIX{} man page \manpage{access}{2} for more information. |
Fred Drake | 3ac977e | 2000-08-11 20:19:51 +0000 | [diff] [blame] | 546 | Availability: \UNIX{}, Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 547 | \end{funcdesc} |
| 548 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 549 | \begin{datadesc}{F_OK} |
| 550 | Value to pass as the \var{mode} parameter of \function{access()} to |
| 551 | test the existence of \var{path}. |
| 552 | \end{datadesc} |
| 553 | |
| 554 | \begin{datadesc}{R_OK} |
| 555 | Value to include in the \var{mode} parameter of \function{access()} |
| 556 | to test the readability of \var{path}. |
| 557 | \end{datadesc} |
| 558 | |
| 559 | \begin{datadesc}{W_OK} |
| 560 | Value to include in the \var{mode} parameter of \function{access()} |
| 561 | to test the writability of \var{path}. |
| 562 | \end{datadesc} |
| 563 | |
| 564 | \begin{datadesc}{X_OK} |
| 565 | Value to include in the \var{mode} parameter of \function{access()} |
| 566 | to determine if \var{path} can be executed. |
| 567 | \end{datadesc} |
| 568 | |
Fred Drake | 6db897c | 1999-07-12 16:49:30 +0000 | [diff] [blame] | 569 | \begin{funcdesc}{chdir}{path} |
| 570 | \index{directory!changing} |
| 571 | Change the current working directory to \var{path}. |
| 572 | Availability: Macintosh, \UNIX{}, Windows. |
| 573 | \end{funcdesc} |
| 574 | |
| 575 | \begin{funcdesc}{getcwd}{} |
| 576 | Return a string representing the current working directory. |
| 577 | Availability: Macintosh, \UNIX{}, Windows. |
| 578 | \end{funcdesc} |
| 579 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 580 | \begin{funcdesc}{chroot}{path} |
| 581 | Change the root directory of the current process to \var{path}. |
| 582 | Availability: \UNIX{}. |
| 583 | \versionadded{2.2} |
| 584 | \end{funcdesc} |
| 585 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 586 | \begin{funcdesc}{chmod}{path, mode} |
| 587 | Change the mode of \var{path} to the numeric \var{mode}. |
| 588 | Availability: \UNIX{}, Windows. |
| 589 | \end{funcdesc} |
| 590 | |
| 591 | \begin{funcdesc}{chown}{path, uid, gid} |
| 592 | Change the owner and group id of \var{path} to the numeric \var{uid} |
| 593 | and \var{gid}. |
| 594 | Availability: \UNIX{}. |
| 595 | \end{funcdesc} |
| 596 | |
| 597 | \begin{funcdesc}{link}{src, dst} |
| 598 | Create a hard link pointing to \var{src} named \var{dst}. |
| 599 | Availability: \UNIX{}. |
| 600 | \end{funcdesc} |
| 601 | |
| 602 | \begin{funcdesc}{listdir}{path} |
| 603 | Return a list containing the names of the entries in the directory. |
| 604 | The list is in arbitrary order. It does not include the special |
| 605 | entries \code{'.'} and \code{'..'} even if they are present in the |
| 606 | directory. |
| 607 | Availability: Macintosh, \UNIX{}, Windows. |
| 608 | \end{funcdesc} |
| 609 | |
| 610 | \begin{funcdesc}{lstat}{path} |
| 611 | Like \function{stat()}, but do not follow symbolic links. |
| 612 | Availability: \UNIX{}. |
| 613 | \end{funcdesc} |
| 614 | |
| 615 | \begin{funcdesc}{mkfifo}{path\optional{, mode}} |
| 616 | Create a FIFO (a named pipe) named \var{path} with numeric mode |
| 617 | \var{mode}. The default \var{mode} is \code{0666} (octal). The current |
| 618 | umask value is first masked out from the mode. |
| 619 | Availability: \UNIX{}. |
| 620 | |
| 621 | FIFOs are pipes that can be accessed like regular files. FIFOs exist |
| 622 | until they are deleted (for example with \function{os.unlink()}). |
| 623 | Generally, FIFOs are used as rendezvous between ``client'' and |
| 624 | ``server'' type processes: the server opens the FIFO for reading, and |
| 625 | the client opens it for writing. Note that \function{mkfifo()} |
| 626 | doesn't open the FIFO --- it just creates the rendezvous point. |
| 627 | \end{funcdesc} |
| 628 | |
| 629 | \begin{funcdesc}{mkdir}{path\optional{, mode}} |
| 630 | Create a directory named \var{path} with numeric mode \var{mode}. |
| 631 | The default \var{mode} is \code{0777} (octal). On some systems, |
| 632 | \var{mode} is ignored. Where it is used, the current umask value is |
| 633 | first masked out. |
| 634 | Availability: Macintosh, \UNIX{}, Windows. |
| 635 | \end{funcdesc} |
| 636 | |
| 637 | \begin{funcdesc}{makedirs}{path\optional{, mode}} |
| 638 | \index{directory!creating} |
| 639 | Recursive directory creation function. Like \function{mkdir()}, |
| 640 | but makes all intermediate-level directories needed to contain the |
| 641 | leaf directory. Throws an \exception{error} exception if the leaf |
| 642 | directory already exists or cannot be created. The default \var{mode} |
Fred Drake | bbf7a40 | 2001-09-28 16:14:18 +0000 | [diff] [blame] | 643 | is \code{0777} (octal). This function does not properly handle UNC |
| 644 | paths (only relevant on Windows systems). |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 645 | \versionadded{1.5.2} |
| 646 | \end{funcdesc} |
| 647 | |
Fred Drake | 88f6ca2 | 1999-12-15 19:39:04 +0000 | [diff] [blame] | 648 | \begin{funcdesc}{pathconf}{path, name} |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 649 | Return system configuration information relevant to a named file. |
Fred Drake | 88f6ca2 | 1999-12-15 19:39:04 +0000 | [diff] [blame] | 650 | \var{name} specifies the configuration value to retrieve; it may be a |
| 651 | string which is the name of a defined system value; these names are |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 652 | 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] | 653 | others). Some platforms define additional names as well. The names |
| 654 | known to the host operating system are given in the |
| 655 | \code{pathconf_names} dictionary. For configuration variables not |
| 656 | included in that mapping, passing an integer for \var{name} is also |
| 657 | accepted. |
| 658 | Availability: \UNIX{}. |
| 659 | |
| 660 | If \var{name} is a string and is not known, \exception{ValueError} is |
| 661 | raised. If a specific value for \var{name} is not supported by the |
| 662 | host system, even if it is included in \code{pathconf_names}, an |
| 663 | \exception{OSError} is raised with \constant{errno.EINVAL} for the |
| 664 | error number. |
| 665 | \end{funcdesc} |
| 666 | |
| 667 | \begin{datadesc}{pathconf_names} |
| 668 | Dictionary mapping names accepted by \function{pathconf()} and |
| 669 | \function{fpathconf()} to the integer values defined for those names |
| 670 | by the host operating system. This can be used to determine the set |
| 671 | of names known to the system. |
| 672 | Availability: \UNIX. |
| 673 | \end{datadesc} |
| 674 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 675 | \begin{funcdesc}{readlink}{path} |
| 676 | Return a string representing the path to which the symbolic link |
Fred Drake | dc9e7e4 | 2001-05-29 18:13:06 +0000 | [diff] [blame] | 677 | points. The result may be either an absolute or relative pathname; if |
| 678 | it is relative, it may be converted to an absolute pathname using |
| 679 | \code{os.path.join(os.path.dirname(\var{path}), \var{result})}. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 680 | Availability: \UNIX{}. |
| 681 | \end{funcdesc} |
| 682 | |
| 683 | \begin{funcdesc}{remove}{path} |
Fred Drake | dc9e7e4 | 2001-05-29 18:13:06 +0000 | [diff] [blame] | 684 | Remove the file \var{path}. If \var{path} is a directory, |
| 685 | \exception{OSError} is raised; see \function{rmdir()} below to remove |
| 686 | a directory. This is identical to the \function{unlink()} function |
| 687 | documented below. On Windows, attempting to remove a file that is in |
| 688 | use causes an exception to be raised; on \UNIX, the directory entry is |
| 689 | removed but the storage allocated to the file is not made available |
| 690 | until the original file is no longer in use. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 691 | Availability: Macintosh, \UNIX{}, Windows. |
| 692 | \end{funcdesc} |
| 693 | |
| 694 | \begin{funcdesc}{removedirs}{path} |
| 695 | \index{directory!deleting} |
| 696 | Recursive directory removal function. Works like |
| 697 | \function{rmdir()} except that, if the leaf directory is |
| 698 | successfully removed, directories corresponding to rightmost path |
| 699 | segments will be pruned way until either the whole path is consumed or |
| 700 | an error is raised (which is ignored, because it generally means that |
| 701 | a parent directory is not empty). Throws an \exception{error} |
| 702 | exception if the leaf directory could not be successfully removed. |
| 703 | \versionadded{1.5.2} |
| 704 | \end{funcdesc} |
| 705 | |
| 706 | \begin{funcdesc}{rename}{src, dst} |
Fred Drake | dc9e7e4 | 2001-05-29 18:13:06 +0000 | [diff] [blame] | 707 | Rename the file or directory \var{src} to \var{dst}. If \var{dst} is |
| 708 | a directory, \exception{OSError} will be raised. On \UNIX, if |
| 709 | \var{dst} exists and is a file, it will be removed silently if the |
| 710 | user has permission. The operation may fail on some \UNIX{} flavors |
Skip Montanaro | b9d973d | 2001-06-04 15:31:17 +0000 | [diff] [blame] | 711 | if \var{src} and \var{dst} are on different filesystems. If |
Fred Drake | dc9e7e4 | 2001-05-29 18:13:06 +0000 | [diff] [blame] | 712 | successful, the renaming will be an atomic operation (this is a |
| 713 | \POSIX{} requirement). On Windows, if \var{dst} already exists, |
| 714 | \exception{OSError} will be raised even if it is a file; there may be |
| 715 | no way to implement an atomic rename when \var{dst} names an existing |
| 716 | file. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 717 | Availability: Macintosh, \UNIX{}, Windows. |
| 718 | \end{funcdesc} |
| 719 | |
| 720 | \begin{funcdesc}{renames}{old, new} |
| 721 | Recursive directory or file renaming function. |
| 722 | Works like \function{rename()}, except creation of any intermediate |
| 723 | directories needed to make the new pathname good is attempted first. |
| 724 | After the rename, directories corresponding to rightmost path segments |
| 725 | of the old name will be pruned away using \function{removedirs()}. |
| 726 | |
| 727 | Note: this function can fail with the new directory structure made if |
| 728 | you lack permissions needed to remove the leaf directory or file. |
| 729 | \versionadded{1.5.2} |
| 730 | \end{funcdesc} |
| 731 | |
| 732 | \begin{funcdesc}{rmdir}{path} |
| 733 | Remove the directory \var{path}. |
| 734 | Availability: Macintosh, \UNIX{}, Windows. |
| 735 | \end{funcdesc} |
| 736 | |
| 737 | \begin{funcdesc}{stat}{path} |
| 738 | Perform a \cfunction{stat()} system call on the given path. The |
| 739 | return value is a tuple of at least 10 integers giving the most |
| 740 | important (and portable) members of the \emph{stat} structure, in the |
| 741 | order |
| 742 | \code{st_mode}, |
| 743 | \code{st_ino}, |
| 744 | \code{st_dev}, |
| 745 | \code{st_nlink}, |
| 746 | \code{st_uid}, |
| 747 | \code{st_gid}, |
| 748 | \code{st_size}, |
| 749 | \code{st_atime}, |
| 750 | \code{st_mtime}, |
| 751 | \code{st_ctime}. |
Fred Drake | 21c9df7 | 2000-10-14 05:46:11 +0000 | [diff] [blame] | 752 | More items may be added at the end by some implementations. Note that |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 753 | on the Mac OS, the time values are floating point values, like all |
| 754 | time values on the Mac OS. |
| 755 | (On Windows, some items are filled with dummy values.) |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 756 | Availability: Macintosh, \UNIX{}, Windows. |
| 757 | |
| 758 | Note: The standard module \refmodule{stat}\refstmodindex{stat} defines |
| 759 | functions and constants that are useful for extracting information |
| 760 | from a \ctype{stat} structure. |
| 761 | \end{funcdesc} |
| 762 | |
| 763 | \begin{funcdesc}{statvfs}{path} |
| 764 | Perform a \cfunction{statvfs()} system call on the given path. The |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 765 | return value is a tuple of 10 integers giving the most common |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 766 | members of the \ctype{statvfs} structure, in the order |
| 767 | \code{f_bsize}, |
| 768 | \code{f_frsize}, |
| 769 | \code{f_blocks}, |
| 770 | \code{f_bfree}, |
| 771 | \code{f_bavail}, |
| 772 | \code{f_files}, |
| 773 | \code{f_ffree}, |
| 774 | \code{f_favail}, |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 775 | \code{f_flag}, |
| 776 | \code{f_namemax}. |
| 777 | Availability: \UNIX{}. |
| 778 | |
| 779 | Note: The standard module \module{statvfs}\refstmodindex{statvfs} |
| 780 | defines constants that are useful for extracting information |
| 781 | from a \ctype{statvfs} structure. |
| 782 | \end{funcdesc} |
| 783 | |
| 784 | \begin{funcdesc}{symlink}{src, dst} |
| 785 | Create a symbolic link pointing to \var{src} named \var{dst}. |
| 786 | Availability: \UNIX{}. |
| 787 | \end{funcdesc} |
| 788 | |
Fred Drake | 18f7a45 | 1999-12-09 22:11:43 +0000 | [diff] [blame] | 789 | \begin{funcdesc}{tempnam}{\optional{dir\optional{, prefix}}} |
| 790 | Return a unique path name that is reasonable for creating a temporary |
| 791 | file. This will be an absolute path that names a potential directory |
| 792 | entry in the directory \var{dir} or a common location for temporary |
| 793 | files if \var{dir} is omitted or \code{None}. If given and not |
| 794 | \code{None}, \var{prefix} is used to provide a short prefix to the |
| 795 | filename. Applications are responsible for properly creating and |
| 796 | managing files created using paths returned by \function{tempnam()}; |
| 797 | no automatic cleanup is provided. |
Fred Drake | efaef13 | 2001-07-17 20:39:18 +0000 | [diff] [blame] | 798 | Availability: \UNIX, Windows. |
Fred Drake | 18f7a45 | 1999-12-09 22:11:43 +0000 | [diff] [blame] | 799 | \end{funcdesc} |
| 800 | |
| 801 | \begin{funcdesc}{tmpnam}{} |
| 802 | Return a unique path name that is reasonable for creating a temporary |
| 803 | file. This will be an absolute path that names a potential directory |
| 804 | entry in a common location for temporary files. Applications are |
| 805 | responsible for properly creating and managing files created using |
| 806 | paths returned by \function{tmpnam()}; no automatic cleanup is |
| 807 | provided. |
Fred Drake | efaef13 | 2001-07-17 20:39:18 +0000 | [diff] [blame] | 808 | Availability: \UNIX, Windows. |
Fred Drake | 18f7a45 | 1999-12-09 22:11:43 +0000 | [diff] [blame] | 809 | \end{funcdesc} |
| 810 | |
| 811 | \begin{datadesc}{TMP_MAX} |
| 812 | The maximum number of unique names that \function{tmpnam()} will |
| 813 | generate before reusing names. |
| 814 | \end{datadesc} |
| 815 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 816 | \begin{funcdesc}{unlink}{path} |
| 817 | Remove the file \var{path}. This is the same function as |
| 818 | \function{remove()}; the \function{unlink()} name is its traditional |
| 819 | \UNIX{} name. |
| 820 | Availability: Macintosh, \UNIX{}, Windows. |
| 821 | \end{funcdesc} |
| 822 | |
Barry Warsaw | 93a8eac | 2000-05-01 16:18:22 +0000 | [diff] [blame] | 823 | \begin{funcdesc}{utime}{path, times} |
| 824 | Set the access and modified times of the file specified by \var{path}. |
| 825 | If \var{times} is \code{None}, then the file's access and modified |
| 826 | 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] | 827 | 2-tuple of numbers, of the form \code{(\var{atime}, \var{mtime})} |
| 828 | which is used to set the access and modified times, respectively. |
Fred Drake | 4a15263 | 2000-10-19 05:33:46 +0000 | [diff] [blame] | 829 | \versionchanged[Added support for \code{None} for \var{times}]{2.0} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 830 | Availability: Macintosh, \UNIX{}, Windows. |
| 831 | \end{funcdesc} |
| 832 | |
| 833 | |
| 834 | \subsection{Process Management \label{os-process}} |
| 835 | |
Fred Drake | 18f7a45 | 1999-12-09 22:11:43 +0000 | [diff] [blame] | 836 | These functions may be used to create and manage processes. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 837 | |
Fred Drake | 7be3115 | 2000-09-23 05:22:07 +0000 | [diff] [blame] | 838 | The various \function{exec*()} functions take a list of arguments for |
| 839 | the new program loaded into the process. In each case, the first of |
| 840 | these arguments is passed to the new program as its own name rather |
| 841 | than as an argument a user may have typed on a command line. For the |
| 842 | C programmer, this is the \code{argv[0]} passed to a program's |
| 843 | \cfunction{main()}. For example, \samp{os.execv('/bin/echo', ['foo', |
| 844 | 'bar'])} will only print \samp{bar} on standard output; \samp{foo} |
| 845 | will seem to be ignored. |
| 846 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 847 | |
Fred Drake | 18f7a45 | 1999-12-09 22:11:43 +0000 | [diff] [blame] | 848 | \begin{funcdesc}{abort}{} |
| 849 | Generate a \constant{SIGABRT} signal to the current process. On |
| 850 | \UNIX, the default behavior is to produce a core dump; on Windows, the |
| 851 | process immediately returns an exit code of \code{3}. Be aware that |
| 852 | programs which use \function{signal.signal()} to register a handler |
| 853 | for \constant{SIGABRT} will behave differently. |
| 854 | Availability: \UNIX, Windows. |
| 855 | \end{funcdesc} |
| 856 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 857 | \begin{funcdesc}{execl}{path, arg0, arg1, ...} |
| 858 | This is equivalent to |
| 859 | \samp{execv(\var{path}, (\var{arg0}, \var{arg1}, ...))}. |
| 860 | Availability: \UNIX{}, Windows. |
| 861 | \end{funcdesc} |
| 862 | |
| 863 | \begin{funcdesc}{execle}{path, arg0, arg1, ..., env} |
| 864 | This is equivalent to |
| 865 | \samp{execve(\var{path}, (\var{arg0}, \var{arg1}, ...), \var{env})}. |
| 866 | Availability: \UNIX{}, Windows. |
| 867 | \end{funcdesc} |
| 868 | |
| 869 | \begin{funcdesc}{execlp}{path, arg0, arg1, ...} |
| 870 | This is equivalent to |
| 871 | \samp{execvp(\var{path}, (\var{arg0}, \var{arg1}, ...))}. |
| 872 | Availability: \UNIX{}, Windows. |
| 873 | \end{funcdesc} |
| 874 | |
| 875 | \begin{funcdesc}{execv}{path, args} |
| 876 | Execute the executable \var{path} with argument list \var{args}, |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 877 | replacing the current process (the Python interpreter). |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 878 | The argument list may be a tuple or list of strings. |
| 879 | Availability: \UNIX{}, Windows. |
| 880 | \end{funcdesc} |
| 881 | |
| 882 | \begin{funcdesc}{execve}{path, args, env} |
| 883 | Execute the executable \var{path} with argument list \var{args}, |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 884 | and environment \var{env}, replacing the current process (the Python |
| 885 | interpreter). |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 886 | The argument list may be a tuple or list of strings. |
| 887 | The environment must be a dictionary mapping strings to strings. |
| 888 | Availability: \UNIX{}, Windows. |
| 889 | \end{funcdesc} |
| 890 | |
| 891 | \begin{funcdesc}{execvp}{path, args} |
| 892 | This is like \samp{execv(\var{path}, \var{args})} but duplicates |
| 893 | the shell's actions in searching for an executable file in a list of |
| 894 | directories. The directory list is obtained from |
| 895 | \code{environ['PATH']}. |
| 896 | Availability: \UNIX{}, Windows. |
| 897 | \end{funcdesc} |
| 898 | |
| 899 | \begin{funcdesc}{execvpe}{path, args, env} |
| 900 | This is a cross between \function{execve()} and \function{execvp()}. |
| 901 | The directory list is obtained from \code{\var{env}['PATH']}. |
| 902 | Availability: \UNIX{}, Windows. |
| 903 | \end{funcdesc} |
| 904 | |
| 905 | \begin{funcdesc}{_exit}{n} |
| 906 | Exit to the system with status \var{n}, without calling cleanup |
| 907 | handlers, flushing stdio buffers, etc. |
| 908 | Availability: \UNIX{}, Windows. |
| 909 | |
| 910 | Note: the standard way to exit is \code{sys.exit(\var{n})}. |
| 911 | \function{_exit()} should normally only be used in the child process |
| 912 | after a \function{fork()}. |
| 913 | \end{funcdesc} |
| 914 | |
| 915 | \begin{funcdesc}{fork}{} |
| 916 | Fork a child process. Return \code{0} in the child, the child's |
| 917 | process id in the parent. |
| 918 | Availability: \UNIX{}. |
| 919 | \end{funcdesc} |
| 920 | |
Fred Drake | c82634c | 2000-06-28 17:27:48 +0000 | [diff] [blame] | 921 | \begin{funcdesc}{forkpty}{} |
| 922 | Fork a child process, using a new pseudo-terminal as the child's |
| 923 | controlling terminal. Return a pair of \code{(\var{pid}, \var{fd})}, |
| 924 | where \var{pid} is \code{0} in the child, the new child's process id |
| 925 | in the parent, and \code{fd} is the file descriptor of the master end |
| 926 | of the pseudo-terminal. For a more portable approach, use the |
| 927 | \refmodule{pty} module. |
| 928 | Availability: Some flavors of \UNIX{} |
| 929 | \end{funcdesc} |
| 930 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 931 | \begin{funcdesc}{kill}{pid, sig} |
| 932 | \index{process!killing} |
| 933 | \index{process!signalling} |
| 934 | Kill the process \var{pid} with signal \var{sig}. |
| 935 | Availability: \UNIX{}. |
| 936 | \end{funcdesc} |
| 937 | |
| 938 | \begin{funcdesc}{nice}{increment} |
| 939 | Add \var{increment} to the process's ``niceness''. Return the new |
| 940 | niceness. |
| 941 | Availability: \UNIX{}. |
| 942 | \end{funcdesc} |
| 943 | |
| 944 | \begin{funcdesc}{plock}{op} |
| 945 | Lock program segments into memory. The value of \var{op} |
| 946 | (defined in \code{<sys/lock.h>}) determines which segments are locked. |
Fred Drake | 3906363 | 1999-02-26 14:05:02 +0000 | [diff] [blame] | 947 | Availability: \UNIX{}. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 948 | \end{funcdesc} |
| 949 | |
Fred Drake | 046f4d8 | 2001-06-11 15:21:48 +0000 | [diff] [blame] | 950 | \begin{funcdescni}{popen}{\unspecified} |
| 951 | \funclineni{popen2}{\unspecified} |
| 952 | \funclineni{popen3}{\unspecified} |
| 953 | \funclineni{popen4}{\unspecified} |
| 954 | Run child processes, returning opened pipes for communications. These |
| 955 | functions are described in section \ref{os-newstreams}. |
| 956 | \end{funcdescni} |
| 957 | |
Fred Drake | 739282d | 2001-08-16 21:21:28 +0000 | [diff] [blame] | 958 | \begin{funcdesc}{spawnl}{mode, path, \moreargs} |
| 959 | \funcline{spawnle}{mode, path, \moreargs, env} |
| 960 | \funcline{spawnlp}{mode, path, \moreargs} |
| 961 | \funcline{spawnlpe}{mode, path, \moreargs, env} |
| 962 | \funcline{spawnv}{mode, path, args} |
| 963 | \funcline{spawnve}{mode, path, args, env} |
| 964 | \funcline{spawnvp}{mode, path, args} |
| 965 | \funcline{spawnvpe}{mode, path, args, env} |
| 966 | Execute the program \var{path} in a new process. If \var{mode} is |
| 967 | \constant{P_NOWAIT}, this function returns the process ID of the new |
| 968 | process; it \var{mode} is \constant{P_WAIT}, returns the process's |
| 969 | exit code if it exits normally, or \code{-\var{signal}}, where |
| 970 | \var{signal} is the signal that killed the process. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 971 | |
Fred Drake | 739282d | 2001-08-16 21:21:28 +0000 | [diff] [blame] | 972 | For \function{spawnle()}, \function{spawnlpe()}, \function{spawnve()}, |
| 973 | and \function{spawnvpe()} (note that these all end in \character{e}), |
| 974 | the \var{env} parameter must be a mapping which is used to define the |
| 975 | environment variables for the new process; the \function{spawnl()}, |
| 976 | \function{spawnlp()}, \function{spawnv()}, and \function{spawnvp()} |
| 977 | all cause the new process to inherit the environment of the current |
| 978 | process. |
| 979 | |
| 980 | The variants which include a second \character{p} near the end |
| 981 | (\function{spawnlp()}, \function{spawnlpe()}, \function{spawnvp()}, |
| 982 | and \function{spawnvpe()}) will use the \envvar{PATH} environment |
| 983 | variable to locate the program \var{path}. The other variants, |
| 984 | \function{spawnl()}, \function{spawnle()}, \function{spawnv()}, and |
| 985 | \function{spawnve()}, will not use the \envvar{PATH} variable to |
| 986 | locate the executable. |
| 987 | |
| 988 | The \character{l} and \character{v} variants of the |
| 989 | \function{spawn*()} functions differ in how command-line arguments are |
| 990 | passed. The \character{l} variants are perhaps the easiest to work |
| 991 | with if the number of parameters is fixed when the code is written; |
| 992 | the individual parameters simply become additional parameters to the |
| 993 | \function{spawnl*()} functions. The \character{v} variants are good |
| 994 | when the number of parameters is variable, with the arguments being |
| 995 | passed in a list or tuple as the \var{args} parameter. In either |
| 996 | case, the arguments to the child process must start with the name of |
| 997 | the command being run. |
| 998 | |
| 999 | As an example, the following calls to \function{spawnlp()} and |
| 1000 | \function{spawnvpe()} are equivalent: |
| 1001 | |
| 1002 | \begin{verbatim} |
| 1003 | import os |
| 1004 | os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null') |
| 1005 | |
| 1006 | L = ['cp', 'index.html', '/dev/null'] |
| 1007 | os.spawnvpe(os.P_WAIT, 'cp', L, os.environ) |
| 1008 | \end{verbatim} |
| 1009 | |
Fred Drake | 15861b2 | 2000-02-29 05:19:38 +0000 | [diff] [blame] | 1010 | Availability: \UNIX{}, Windows. |
Fred Drake | 0b9bc20 | 2001-06-11 18:25:34 +0000 | [diff] [blame] | 1011 | \versionadded{1.6} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1012 | \end{funcdesc} |
| 1013 | |
Fred Drake | 9329e5e | 1999-02-16 19:40:19 +0000 | [diff] [blame] | 1014 | \begin{datadesc}{P_WAIT} |
| 1015 | \dataline{P_NOWAIT} |
| 1016 | \dataline{P_NOWAITO} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1017 | Possible values for the \var{mode} parameter to \function{spawnv()} |
| 1018 | and \function{spawnve()}. |
Fred Drake | 15861b2 | 2000-02-29 05:19:38 +0000 | [diff] [blame] | 1019 | Availability: \UNIX{}, Windows. |
Fred Drake | 0b9bc20 | 2001-06-11 18:25:34 +0000 | [diff] [blame] | 1020 | \versionadded{1.6} |
Fred Drake | 15861b2 | 2000-02-29 05:19:38 +0000 | [diff] [blame] | 1021 | \end{datadesc} |
| 1022 | |
| 1023 | \begin{datadesc}{P_OVERLAY} |
| 1024 | \dataline{P_DETACH} |
| 1025 | Possible values for the \var{mode} parameter to \function{spawnv()} |
| 1026 | and \function{spawnve()}. These are less portable than those listed |
| 1027 | above. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1028 | Availability: Windows. |
Fred Drake | 0b9bc20 | 2001-06-11 18:25:34 +0000 | [diff] [blame] | 1029 | \versionadded{1.6} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1030 | \end{datadesc} |
| 1031 | |
Fred Drake | 4ce4f2e | 2000-09-29 04:15:19 +0000 | [diff] [blame] | 1032 | \begin{funcdesc}{startfile}{path} |
| 1033 | Start a file with its associated application. This acts like |
| 1034 | double-clicking the file in Windows Explorer, or giving the file name |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 1035 | as an argument to the \program{start} command from the interactive |
| 1036 | command shell: the file is opened with whatever application (if any) |
| 1037 | its extension is associated. |
Fred Drake | 4ce4f2e | 2000-09-29 04:15:19 +0000 | [diff] [blame] | 1038 | |
| 1039 | \function{startfile()} returns as soon as the associated application |
| 1040 | is launched. There is no option to wait for the application to close, |
| 1041 | and no way to retrieve the application's exit status. The \var{path} |
| 1042 | parameter is relative to the current directory. If you want to use an |
| 1043 | absolute path, make sure the first character is not a slash |
| 1044 | (\character{/}); the underlying Win32 \cfunction{ShellExecute()} |
Fred Drake | 8a2adcf | 2001-07-23 19:20:56 +0000 | [diff] [blame] | 1045 | 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] | 1046 | function to ensure that the path is properly encoded for Win32. |
| 1047 | Availability: Windows. |
| 1048 | \versionadded{2.0} |
| 1049 | \end{funcdesc} |
| 1050 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1051 | \begin{funcdesc}{system}{command} |
| 1052 | Execute the command (a string) in a subshell. This is implemented by |
| 1053 | calling the Standard C function \cfunction{system()}, and has the |
Fred Drake | ec6baaf | 1999-04-21 18:13:31 +0000 | [diff] [blame] | 1054 | same limitations. Changes to \code{posix.environ}, \code{sys.stdin}, |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1055 | etc.\ are not reflected in the environment of the executed command. |
| 1056 | The return value is the exit status of the process encoded in the |
Fred Drake | 7a62128 | 1999-06-10 15:07:05 +0000 | [diff] [blame] | 1057 | format specified for \function{wait()}, except on Windows 95 and 98, |
Fred Drake | a88ef00 | 1999-06-18 19:11:25 +0000 | [diff] [blame] | 1058 | where it is always \code{0}. Note that \POSIX{} does not specify the |
| 1059 | meaning of the return value of the C \cfunction{system()} function, |
| 1060 | so the return value of the Python function is system-dependent. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1061 | Availability: \UNIX{}, Windows. |
| 1062 | \end{funcdesc} |
| 1063 | |
| 1064 | \begin{funcdesc}{times}{} |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 1065 | Return a 5-tuple of floating point numbers indicating accumulated |
| 1066 | (processor or other) |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1067 | times, in seconds. The items are: user time, system time, children's |
| 1068 | 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] | 1069 | point in the past, in that order. See the \UNIX{} manual page |
| 1070 | \manpage{times}{2} or the corresponding Windows Platform API |
| 1071 | documentation. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1072 | Availability: \UNIX{}, Windows. |
| 1073 | \end{funcdesc} |
| 1074 | |
| 1075 | \begin{funcdesc}{wait}{} |
| 1076 | Wait for completion of a child process, and return a tuple containing |
| 1077 | its pid and exit status indication: a 16-bit number, whose low byte is |
| 1078 | the signal number that killed the process, and whose high byte is the |
| 1079 | exit status (if the signal number is zero); the high bit of the low |
| 1080 | byte is set if a core file was produced. |
| 1081 | Availability: \UNIX{}. |
| 1082 | \end{funcdesc} |
| 1083 | |
| 1084 | \begin{funcdesc}{waitpid}{pid, options} |
Fred Drake | 31e5e37 | 1999-08-13 13:36:33 +0000 | [diff] [blame] | 1085 | Wait for completion of a child process given by process id \var{pid}, |
| 1086 | and return a tuple containing its process id and exit status |
| 1087 | indication (encoded as for \function{wait()}). The semantics of the |
| 1088 | call are affected by the value of the integer \var{options}, which |
| 1089 | should be \code{0} for normal operation. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1090 | Availability: \UNIX{}. |
Fred Drake | 31e5e37 | 1999-08-13 13:36:33 +0000 | [diff] [blame] | 1091 | |
| 1092 | If \var{pid} is greater than \code{0}, \function{waitpid()} requests |
| 1093 | status information for that specific process. If \var{pid} is |
| 1094 | \code{0}, the request is for the status of any child in the process |
| 1095 | group of the current process. If \var{pid} is \code{-1}, the request |
| 1096 | pertains to any child of the current process. If \var{pid} is less |
| 1097 | than \code{-1}, status is requested for any process in the process |
| 1098 | group \code{-\var{pid}} (the absolute value of \var{pid}). |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1099 | \end{funcdesc} |
| 1100 | |
| 1101 | \begin{datadesc}{WNOHANG} |
| 1102 | The option for \function{waitpid()} to avoid hanging if no child |
| 1103 | process status is available immediately. |
| 1104 | Availability: \UNIX{}. |
| 1105 | \end{datadesc} |
| 1106 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 1107 | The following functions take a process status code as returned by |
| 1108 | \function{system()}, \function{wait()}, or \function{waitpid()} as a |
| 1109 | parameter. They may be used to determine the disposition of a |
| 1110 | process. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1111 | |
| 1112 | \begin{funcdesc}{WIFSTOPPED}{status} |
| 1113 | Return true if the process has been stopped. |
| 1114 | Availability: \UNIX{}. |
| 1115 | \end{funcdesc} |
| 1116 | |
| 1117 | \begin{funcdesc}{WIFSIGNALED}{status} |
| 1118 | Return true if the process exited due to a signal. |
| 1119 | Availability: \UNIX{}. |
| 1120 | \end{funcdesc} |
| 1121 | |
| 1122 | \begin{funcdesc}{WIFEXITED}{status} |
| 1123 | Return true if the process exited using the \manpage{exit}{2} system |
| 1124 | call. |
| 1125 | Availability: \UNIX{}. |
| 1126 | \end{funcdesc} |
| 1127 | |
| 1128 | \begin{funcdesc}{WEXITSTATUS}{status} |
| 1129 | If \code{WIFEXITED(\var{status})} is true, return the integer |
| 1130 | parameter to the \manpage{exit}{2} system call. Otherwise, the return |
| 1131 | value is meaningless. |
| 1132 | Availability: \UNIX{}. |
| 1133 | \end{funcdesc} |
| 1134 | |
| 1135 | \begin{funcdesc}{WSTOPSIG}{status} |
Fred Drake | 35c3ffd | 1999-03-04 14:08:10 +0000 | [diff] [blame] | 1136 | Return the signal which caused the process to stop. |
| 1137 | Availability: \UNIX{}. |
| 1138 | \end{funcdesc} |
| 1139 | |
| 1140 | \begin{funcdesc}{WTERMSIG}{status} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1141 | Return the signal which caused the process to exit. |
| 1142 | Availability: \UNIX{}. |
| 1143 | \end{funcdesc} |
| 1144 | |
| 1145 | |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 1146 | \subsection{Miscellaneous System Information \label{os-path}} |
Fred Drake | 88f6ca2 | 1999-12-15 19:39:04 +0000 | [diff] [blame] | 1147 | |
| 1148 | |
| 1149 | \begin{funcdesc}{confstr}{name} |
| 1150 | Return string-valued system configuration values. |
| 1151 | \var{name} specifies the configuration value to retrieve; it may be a |
| 1152 | string which is the name of a defined system value; these names are |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 1153 | specified in a number of standards (\POSIX, \UNIX 95, \UNIX 98, and |
Fred Drake | 88f6ca2 | 1999-12-15 19:39:04 +0000 | [diff] [blame] | 1154 | others). Some platforms define additional names as well. The names |
| 1155 | known to the host operating system are given in the |
| 1156 | \code{confstr_names} dictionary. For configuration variables not |
| 1157 | included in that mapping, passing an integer for \var{name} is also |
| 1158 | accepted. |
| 1159 | Availability: \UNIX{}. |
| 1160 | |
| 1161 | If the configuration value specified by \var{name} isn't defined, the |
| 1162 | empty string is returned. |
| 1163 | |
| 1164 | If \var{name} is a string and is not known, \exception{ValueError} is |
| 1165 | raised. If a specific value for \var{name} is not supported by the |
| 1166 | host system, even if it is included in \code{confstr_names}, an |
| 1167 | \exception{OSError} is raised with \constant{errno.EINVAL} for the |
| 1168 | error number. |
| 1169 | \end{funcdesc} |
| 1170 | |
| 1171 | \begin{datadesc}{confstr_names} |
| 1172 | Dictionary mapping names accepted by \function{confstr()} to the |
| 1173 | integer values defined for those names by the host operating system. |
| 1174 | This can be used to determine the set of names known to the system. |
| 1175 | Availability: \UNIX. |
| 1176 | \end{datadesc} |
| 1177 | |
| 1178 | \begin{funcdesc}{sysconf}{name} |
| 1179 | Return integer-valued system configuration values. |
| 1180 | If the configuration value specified by \var{name} isn't defined, |
| 1181 | \code{-1} is returned. The comments regarding the \var{name} |
| 1182 | parameter for \function{confstr()} apply here as well; the dictionary |
| 1183 | that provides information on the known names is given by |
| 1184 | \code{sysconf_names}. |
| 1185 | Availability: \UNIX{}. |
| 1186 | \end{funcdesc} |
| 1187 | |
| 1188 | \begin{datadesc}{sysconf_names} |
| 1189 | Dictionary mapping names accepted by \function{sysconf()} to the |
| 1190 | integer values defined for those names by the host operating system. |
| 1191 | This can be used to determine the set of names known to the system. |
| 1192 | Availability: \UNIX. |
| 1193 | \end{datadesc} |
| 1194 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1195 | |
| 1196 | The follow data values are used to support path manipulation |
| 1197 | operations. These are defined for all platforms. |
| 1198 | |
| 1199 | Higher-level operations on pathnames are defined in the |
| 1200 | \refmodule{os.path} module. |
| 1201 | |
| 1202 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1203 | \begin{datadesc}{curdir} |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 1204 | The constant string used by the operating system to refer to the current |
| 1205 | directory. |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 1206 | For example: \code{'.'} for \POSIX{} or \code{':'} for the Macintosh. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1207 | \end{datadesc} |
| 1208 | |
| 1209 | \begin{datadesc}{pardir} |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 1210 | The constant string used by the operating system to refer to the parent |
| 1211 | directory. |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 1212 | For example: \code{'..'} for \POSIX{} or \code{'::'} for the Macintosh. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1213 | \end{datadesc} |
| 1214 | |
| 1215 | \begin{datadesc}{sep} |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 1216 | The character used by the operating system to separate pathname components, |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 1217 | for example, \character{/} for \POSIX{} or \character{:} for the |
| 1218 | Macintosh. Note that knowing this is not sufficient to be able to |
| 1219 | parse or concatenate pathnames --- use \function{os.path.split()} and |
Fred Drake | 1a3c2a0 | 1998-08-06 15:18:23 +0000 | [diff] [blame] | 1220 | \function{os.path.join()} --- but it is occasionally useful. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1221 | \end{datadesc} |
| 1222 | |
Guido van Rossum | b2afc81 | 1997-08-29 22:37:44 +0000 | [diff] [blame] | 1223 | \begin{datadesc}{altsep} |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 1224 | An alternative character used by the operating system to separate pathname |
| 1225 | components, or \code{None} if only one separator character exists. This is |
| 1226 | set to \character{/} on DOS and Windows systems where \code{sep} is a |
| 1227 | backslash. |
Guido van Rossum | b2afc81 | 1997-08-29 22:37:44 +0000 | [diff] [blame] | 1228 | \end{datadesc} |
| 1229 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 1230 | \begin{datadesc}{pathsep} |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 1231 | The character conventionally used by the operating system to separate |
| 1232 | search patch components (as in \envvar{PATH}), such as \character{:} for |
| 1233 | \POSIX{} or \character{;} for DOS and Windows. |
Guido van Rossum | 9c59ce9 | 1998-06-30 15:54:27 +0000 | [diff] [blame] | 1234 | \end{datadesc} |
| 1235 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 1236 | \begin{datadesc}{defpath} |
Fred Drake | 1a3c2a0 | 1998-08-06 15:18:23 +0000 | [diff] [blame] | 1237 | The default search path used by \function{exec*p*()} if the environment |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 1238 | doesn't have a \code{'PATH'} key. |
| 1239 | \end{datadesc} |
| 1240 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1241 | \begin{datadesc}{linesep} |
| 1242 | The string used to separate (or, rather, terminate) lines on the |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 1243 | current platform. This may be a single character, such as \code{'\e |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 1244 | n'} for \POSIX{} or \code{'\e r'} for the Mac OS, or multiple characters, |
| 1245 | for example, \code{'\e r\e n'} for DOS and Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 1246 | \end{datadesc} |