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