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