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