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