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