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