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