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