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