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