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