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