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