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