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