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