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