blob: 80955afd4a012bc3ccfe805eef418fca17ddd0f7 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`os` --- Miscellaneous operating system interfaces
2=======================================================
3
4.. module:: os
5 :synopsis: Miscellaneous operating system interfaces.
6
7
Christian Heimesa62da1d2008-01-12 19:39:10 +00008This module provides a portable way of using operating system dependent
9functionality. If you just want to read or write a file see :func:`open`, if
10you want to manipulate paths, see the :mod:`os.path` module, and if you want to
11read all the lines in all the files on the command line see the :mod:`fileinput`
12module. For creating temporary files and directories see the :mod:`tempfile`
13module, and for high-level file and directory handling see the :mod:`shutil`
14module.
Georg Brandl116aa622007-08-15 14:28:22 +000015
Benjamin Peterson68dbebc2009-12-31 03:30:26 +000016Notes on the availability of these functions:
Georg Brandl116aa622007-08-15 14:28:22 +000017
Benjamin Peterson68dbebc2009-12-31 03:30:26 +000018* The design of all built-in operating system dependent modules of Python is
19 such that as long as the same functionality is available, it uses the same
20 interface; for example, the function ``os.stat(path)`` returns stat
21 information about *path* in the same format (which happens to have originated
22 with the POSIX interface).
Georg Brandl116aa622007-08-15 14:28:22 +000023
Benjamin Peterson68dbebc2009-12-31 03:30:26 +000024* Extensions peculiar to a particular operating system are also available
25 through the :mod:`os` module, but using them is of course a threat to
26 portability.
Georg Brandl116aa622007-08-15 14:28:22 +000027
Benjamin Peterson68dbebc2009-12-31 03:30:26 +000028* All functions accepting path or file names accept both bytes and string
29 objects, and result in an object of the same type, if a path or file name is
30 returned.
Georg Brandl76e55382008-10-08 16:34:57 +000031
32.. note::
33
Georg Brandlc575c902008-09-13 17:46:05 +000034 If not separately noted, all functions that claim "Availability: Unix" are
35 supported on Mac OS X, which builds on a Unix core.
36
Benjamin Peterson68dbebc2009-12-31 03:30:26 +000037* An "Availability: Unix" note means that this function is commonly found on
38 Unix systems. It does not make any claims about its existence on a specific
39 operating system.
40
41* If not separately noted, all functions that claim "Availability: Unix" are
42 supported on Mac OS X, which builds on a Unix core.
43
Benjamin Petersond91203b2010-05-06 23:20:40 +000044.. Availability notes get their own line and occur at the end of the function
45.. documentation.
46
Georg Brandlc575c902008-09-13 17:46:05 +000047.. note::
48
Christian Heimesa62da1d2008-01-12 19:39:10 +000049 All functions in this module raise :exc:`OSError` in the case of invalid or
50 inaccessible file names and paths, or other arguments that have the correct
51 type, but are not accepted by the operating system.
Georg Brandl116aa622007-08-15 14:28:22 +000052
Georg Brandl116aa622007-08-15 14:28:22 +000053.. exception:: error
54
Christian Heimesa62da1d2008-01-12 19:39:10 +000055 An alias for the built-in :exc:`OSError` exception.
Georg Brandl116aa622007-08-15 14:28:22 +000056
57
58.. data:: name
59
Benjamin Peterson68dbebc2009-12-31 03:30:26 +000060 The name of the operating system dependent module imported. The following
61 names have currently been registered: ``'posix'``, ``'nt'``, ``'mac'``,
62 ``'os2'``, ``'ce'``, ``'java'``.
Georg Brandl116aa622007-08-15 14:28:22 +000063
64
Martin v. Löwis011e8422009-05-05 04:43:17 +000065.. _os-filenames:
66
67File Names, Command Line Arguments, and Environment Variables
68-------------------------------------------------------------
69
70In Python, file names, command line arguments, and environment
71variables are represented using the string type. On some systems,
72decoding these strings to and from bytes is necessary before passing
73them to the operating system. Python uses the file system encoding to
74perform this conversion (see :func:`sys.getfilesystemencoding`).
75
76.. versionchanged:: 3.1
77 On some systems, conversion using the file system encoding may
Martin v. Löwis43c57782009-05-10 08:15:24 +000078 fail. In this case, Python uses the ``surrogateescape`` encoding
79 error handler, which means that undecodable bytes are replaced by a
Martin v. Löwis011e8422009-05-05 04:43:17 +000080 Unicode character U+DCxx on decoding, and these are again
81 translated to the original byte on encoding.
82
83
84The file system encoding must guarantee to successfully decode all
85bytes below 128. If the file system encoding fails to provide this
86guarantee, API functions may raise UnicodeErrors.
87
88
Georg Brandl116aa622007-08-15 14:28:22 +000089.. _os-procinfo:
90
91Process Parameters
92------------------
93
94These functions and data items provide information and operate on the current
95process and user.
96
97
98.. data:: environ
99
100 A mapping object representing the string environment. For example,
101 ``environ['HOME']`` is the pathname of your home directory (on some platforms),
102 and is equivalent to ``getenv("HOME")`` in C.
103
104 This mapping is captured the first time the :mod:`os` module is imported,
105 typically during Python startup as part of processing :file:`site.py`. Changes
106 to the environment made after this time are not reflected in ``os.environ``,
107 except for changes made by modifying ``os.environ`` directly.
108
109 If the platform supports the :func:`putenv` function, this mapping may be used
110 to modify the environment as well as query the environment. :func:`putenv` will
111 be called automatically when the mapping is modified.
112
113 .. note::
114
115 Calling :func:`putenv` directly does not change ``os.environ``, so it's better
116 to modify ``os.environ``.
117
118 .. note::
119
Georg Brandlc575c902008-09-13 17:46:05 +0000120 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
121 cause memory leaks. Refer to the system documentation for
122 :cfunc:`putenv`.
Georg Brandl116aa622007-08-15 14:28:22 +0000123
124 If :func:`putenv` is not provided, a modified copy of this mapping may be
125 passed to the appropriate process-creation functions to cause child processes
126 to use a modified environment.
127
Georg Brandl9afde1c2007-11-01 20:32:30 +0000128 If the platform supports the :func:`unsetenv` function, you can delete items in
Georg Brandl116aa622007-08-15 14:28:22 +0000129 this mapping to unset environment variables. :func:`unsetenv` will be called
Georg Brandl9afde1c2007-11-01 20:32:30 +0000130 automatically when an item is deleted from ``os.environ``, and when
131 one of the :meth:`pop` or :meth:`clear` methods is called.
132
Georg Brandl116aa622007-08-15 14:28:22 +0000133
134.. function:: chdir(path)
135 fchdir(fd)
136 getcwd()
137 :noindex:
138
139 These functions are described in :ref:`os-file-dir`.
140
141
142.. function:: ctermid()
143
144 Return the filename corresponding to the controlling terminal of the process.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000145
Georg Brandl116aa622007-08-15 14:28:22 +0000146 Availability: Unix.
147
148
149.. function:: getegid()
150
151 Return the effective group id of the current process. This corresponds to the
Benjamin Petersond91203b2010-05-06 23:20:40 +0000152 "set id" bit on the file being executed in the current process.
153
154 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000155
156
157.. function:: geteuid()
158
159 .. index:: single: user; effective id
160
Benjamin Petersond91203b2010-05-06 23:20:40 +0000161 Return the current process's effective user id.
162
163 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000164
165
166.. function:: getgid()
167
168 .. index:: single: process; group
169
Benjamin Petersond91203b2010-05-06 23:20:40 +0000170 Return the real group id of the current process.
171
172 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000173
174
175.. function:: getgroups()
176
177 Return list of supplemental group ids associated with the current process.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000178
Georg Brandl116aa622007-08-15 14:28:22 +0000179 Availability: Unix.
180
181
182.. function:: getlogin()
183
184 Return the name of the user logged in on the controlling terminal of the
185 process. For most purposes, it is more useful to use the environment variable
186 :envvar:`LOGNAME` to find out who the user is, or
187 ``pwd.getpwuid(os.getuid())[0]`` to get the login name of the currently
Benjamin Petersond91203b2010-05-06 23:20:40 +0000188 effective user id.
189
190 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000191
192
193.. function:: getpgid(pid)
194
195 Return the process group id of the process with process id *pid*. If *pid* is 0,
Benjamin Petersond91203b2010-05-06 23:20:40 +0000196 the process group id of the current process is returned.
Georg Brandl116aa622007-08-15 14:28:22 +0000197
Benjamin Petersond91203b2010-05-06 23:20:40 +0000198 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000199
200.. function:: getpgrp()
201
202 .. index:: single: process; group
203
Benjamin Petersond91203b2010-05-06 23:20:40 +0000204 Return the id of the current process group.
205
206 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000207
208
209.. function:: getpid()
210
211 .. index:: single: process; id
212
Benjamin Petersond91203b2010-05-06 23:20:40 +0000213 Return the current process id.
214
215 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000216
217
218.. function:: getppid()
219
220 .. index:: single: process; id of parent
221
Benjamin Petersond91203b2010-05-06 23:20:40 +0000222 Return the parent's process id.
223
224 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000225
226
227.. function:: getuid()
228
229 .. index:: single: user; id
230
Benjamin Petersond91203b2010-05-06 23:20:40 +0000231 Return the current process's user id.
232
233 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000234
235
236.. function:: getenv(varname[, value])
237
238 Return the value of the environment variable *varname* if it exists, or *value*
Benjamin Petersond91203b2010-05-06 23:20:40 +0000239 if it doesn't. *value* defaults to ``None``.
240
241 Availability: most flavors of Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000242
243
244.. function:: putenv(varname, value)
245
246 .. index:: single: environment variables; setting
247
248 Set the environment variable named *varname* to the string *value*. Such
249 changes to the environment affect subprocesses started with :func:`os.system`,
Benjamin Petersond91203b2010-05-06 23:20:40 +0000250 :func:`popen` or :func:`fork` and :func:`execv`.
251
252 Availability: most flavors of Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000253
254 .. note::
255
Georg Brandlc575c902008-09-13 17:46:05 +0000256 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
257 cause memory leaks. Refer to the system documentation for putenv.
Georg Brandl116aa622007-08-15 14:28:22 +0000258
259 When :func:`putenv` is supported, assignments to items in ``os.environ`` are
260 automatically translated into corresponding calls to :func:`putenv`; however,
261 calls to :func:`putenv` don't update ``os.environ``, so it is actually
262 preferable to assign to items of ``os.environ``.
263
264
265.. function:: setegid(egid)
266
Benjamin Petersond91203b2010-05-06 23:20:40 +0000267 Set the current process's effective group id.
268
269 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000270
271
272.. function:: seteuid(euid)
273
Benjamin Petersond91203b2010-05-06 23:20:40 +0000274 Set the current process's effective user id.
275
276 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000277
278
279.. function:: setgid(gid)
280
Benjamin Petersond91203b2010-05-06 23:20:40 +0000281 Set the current process' group id.
282
283 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000284
285
286.. function:: setgroups(groups)
287
288 Set the list of supplemental group ids associated with the current process to
289 *groups*. *groups* must be a sequence, and each element must be an integer
Christian Heimesfaf2f632008-01-06 16:59:19 +0000290 identifying a group. This operation is typically available only to the superuser.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000291
Georg Brandl116aa622007-08-15 14:28:22 +0000292 Availability: Unix.
293
Georg Brandl116aa622007-08-15 14:28:22 +0000294
295.. function:: setpgrp()
296
Christian Heimesfaf2f632008-01-06 16:59:19 +0000297 Call the system call :cfunc:`setpgrp` or :cfunc:`setpgrp(0, 0)` depending on
Georg Brandl116aa622007-08-15 14:28:22 +0000298 which version is implemented (if any). See the Unix manual for the semantics.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000299
Georg Brandl116aa622007-08-15 14:28:22 +0000300 Availability: Unix.
301
302
303.. function:: setpgid(pid, pgrp)
304
Christian Heimesfaf2f632008-01-06 16:59:19 +0000305 Call the system call :cfunc:`setpgid` to set the process group id of the
Georg Brandl116aa622007-08-15 14:28:22 +0000306 process with id *pid* to the process group with id *pgrp*. See the Unix manual
Benjamin Petersond91203b2010-05-06 23:20:40 +0000307 for the semantics.
308
309 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000310
311
312.. function:: setreuid(ruid, euid)
313
Benjamin Petersond91203b2010-05-06 23:20:40 +0000314
315 Set the current process's real and effective user ids.
316
317 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000318
319
320.. function:: setregid(rgid, egid)
321
Benjamin Petersond91203b2010-05-06 23:20:40 +0000322 Set the current process's real and effective group ids.
323
324 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000325
326
327.. function:: getsid(pid)
328
Christian Heimesfaf2f632008-01-06 16:59:19 +0000329 Call the system call :cfunc:`getsid`. See the Unix manual for the semantics.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000330
Georg Brandl116aa622007-08-15 14:28:22 +0000331 Availability: Unix.
332
Georg Brandl116aa622007-08-15 14:28:22 +0000333
334.. function:: setsid()
335
Christian Heimesfaf2f632008-01-06 16:59:19 +0000336 Call the system call :cfunc:`setsid`. See the Unix manual for the semantics.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000337
Georg Brandl116aa622007-08-15 14:28:22 +0000338 Availability: Unix.
339
340
341.. function:: setuid(uid)
342
343 .. index:: single: user; id, setting
344
Benjamin Petersond91203b2010-05-06 23:20:40 +0000345 Set the current process's user id.
346
347 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000348
Georg Brandl116aa622007-08-15 14:28:22 +0000349
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000350.. placed in this section since it relates to errno.... a little weak
Georg Brandl116aa622007-08-15 14:28:22 +0000351.. function:: strerror(code)
352
353 Return the error message corresponding to the error code in *code*.
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000354 On platforms where :cfunc:`strerror` returns ``NULL`` when given an unknown
Benjamin Petersond91203b2010-05-06 23:20:40 +0000355 error number, :exc:`ValueError` is raised.
356
357 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000358
359
360.. function:: umask(mask)
361
Benjamin Petersond91203b2010-05-06 23:20:40 +0000362 Set the current numeric umask and return the previous umask.
363
364 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000365
366
367.. function:: uname()
368
369 .. index::
370 single: gethostname() (in module socket)
371 single: gethostbyaddr() (in module socket)
372
373 Return a 5-tuple containing information identifying the current operating
374 system. The tuple contains 5 strings: ``(sysname, nodename, release, version,
375 machine)``. Some systems truncate the nodename to 8 characters or to the
376 leading component; a better way to get the hostname is
377 :func:`socket.gethostname` or even
Benjamin Petersond91203b2010-05-06 23:20:40 +0000378 ``socket.gethostbyaddr(socket.gethostname())``.
379
380 Availability: recent flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000381
382
383.. function:: unsetenv(varname)
384
385 .. index:: single: environment variables; deleting
386
387 Unset (delete) the environment variable named *varname*. Such changes to the
388 environment affect subprocesses started with :func:`os.system`, :func:`popen` or
Benjamin Petersond91203b2010-05-06 23:20:40 +0000389 :func:`fork` and :func:`execv`.
Georg Brandl116aa622007-08-15 14:28:22 +0000390
391 When :func:`unsetenv` is supported, deletion of items in ``os.environ`` is
392 automatically translated into a corresponding call to :func:`unsetenv`; however,
393 calls to :func:`unsetenv` don't update ``os.environ``, so it is actually
394 preferable to delete items of ``os.environ``.
395
Benjamin Petersond91203b2010-05-06 23:20:40 +0000396 Availability: most flavors of Unix, Windows.
397
Georg Brandl116aa622007-08-15 14:28:22 +0000398
399.. _os-newstreams:
400
401File Object Creation
402--------------------
403
404These functions create new file objects. (See also :func:`open`.)
405
406
407.. function:: fdopen(fd[, mode[, bufsize]])
408
409 .. index:: single: I/O control; buffering
410
411 Return an open file object connected to the file descriptor *fd*. The *mode*
412 and *bufsize* arguments have the same meaning as the corresponding arguments to
Benjamin Petersond91203b2010-05-06 23:20:40 +0000413 the built-in :func:`open` function.
Georg Brandl116aa622007-08-15 14:28:22 +0000414
Georg Brandl55ac8f02007-09-01 13:51:09 +0000415 When specified, the *mode* argument must start with one of the letters
416 ``'r'``, ``'w'``, or ``'a'``, otherwise a :exc:`ValueError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000417
Georg Brandl55ac8f02007-09-01 13:51:09 +0000418 On Unix, when the *mode* argument starts with ``'a'``, the *O_APPEND* flag is
419 set on the file descriptor (which the :cfunc:`fdopen` implementation already
420 does on most platforms).
Georg Brandl116aa622007-08-15 14:28:22 +0000421
Benjamin Petersond91203b2010-05-06 23:20:40 +0000422 Availability: Unix, Windows.
423
Georg Brandl116aa622007-08-15 14:28:22 +0000424
Georg Brandl116aa622007-08-15 14:28:22 +0000425.. _os-fd-ops:
426
427File Descriptor Operations
428--------------------------
429
430These functions operate on I/O streams referenced using file descriptors.
431
432File descriptors are small integers corresponding to a file that has been opened
433by the current process. For example, standard input is usually file descriptor
4340, standard output is 1, and standard error is 2. Further files opened by a
435process will then be assigned 3, 4, 5, and so forth. The name "file descriptor"
436is slightly deceptive; on Unix platforms, sockets and pipes are also referenced
437by file descriptors.
438
439
440.. function:: close(fd)
441
Benjamin Petersond91203b2010-05-06 23:20:40 +0000442 Close file descriptor *fd*.
443
444 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000445
446 .. note::
447
448 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000449 descriptor as returned by :func:`os.open` or :func:`pipe`. To close a "file
Georg Brandl116aa622007-08-15 14:28:22 +0000450 object" returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000451 :func:`fdopen`, use its :meth:`~file.close` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000452
453
Christian Heimesfdab48e2008-01-20 09:06:41 +0000454.. function:: closerange(fd_low, fd_high)
455
456 Close all file descriptors from *fd_low* (inclusive) to *fd_high* (exclusive),
Benjamin Petersond91203b2010-05-06 23:20:40 +0000457 ignoring errors. Equivalent to::
Christian Heimesfdab48e2008-01-20 09:06:41 +0000458
Georg Brandl7baf6252009-09-01 08:13:16 +0000459 for fd in range(fd_low, fd_high):
Christian Heimesfdab48e2008-01-20 09:06:41 +0000460 try:
461 os.close(fd)
462 except OSError:
463 pass
464
Benjamin Petersond91203b2010-05-06 23:20:40 +0000465 Availability: Unix, Windows.
466
Christian Heimesfdab48e2008-01-20 09:06:41 +0000467
Georg Brandl81f11302007-12-21 08:45:42 +0000468.. function:: device_encoding(fd)
469
470 Return a string describing the encoding of the device associated with *fd*
471 if it is connected to a terminal; else return :const:`None`.
472
473
Georg Brandl116aa622007-08-15 14:28:22 +0000474.. function:: dup(fd)
475
Benjamin Petersond91203b2010-05-06 23:20:40 +0000476 Return a duplicate of file descriptor *fd*.
477
478 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000479
480
481.. function:: dup2(fd, fd2)
482
483 Duplicate file descriptor *fd* to *fd2*, closing the latter first if necessary.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000484
Georg Brandlc575c902008-09-13 17:46:05 +0000485 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000486
487
Christian Heimes4e30a842007-11-30 22:12:06 +0000488.. function:: fchmod(fd, mode)
489
490 Change the mode of the file given by *fd* to the numeric *mode*. See the docs
Benjamin Petersond91203b2010-05-06 23:20:40 +0000491 for :func:`chmod` for possible values of *mode*.
492
493 Availability: Unix.
Christian Heimes4e30a842007-11-30 22:12:06 +0000494
495
496.. function:: fchown(fd, uid, gid)
497
498 Change the owner and group id of the file given by *fd* to the numeric *uid*
499 and *gid*. To leave one of the ids unchanged, set it to -1.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000500
Christian Heimes4e30a842007-11-30 22:12:06 +0000501 Availability: Unix.
502
503
Georg Brandl116aa622007-08-15 14:28:22 +0000504.. function:: fdatasync(fd)
505
506 Force write of file with filedescriptor *fd* to disk. Does not force update of
Benjamin Petersond91203b2010-05-06 23:20:40 +0000507 metadata.
508
509 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000510
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000511 .. note::
512 This function is not available on MacOS.
513
Georg Brandl116aa622007-08-15 14:28:22 +0000514
515.. function:: fpathconf(fd, name)
516
517 Return system configuration information relevant to an open file. *name*
518 specifies the configuration value to retrieve; it may be a string which is the
519 name of a defined system value; these names are specified in a number of
520 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
521 additional names as well. The names known to the host operating system are
522 given in the ``pathconf_names`` dictionary. For configuration variables not
523 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +0000524
525 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
526 specific value for *name* is not supported by the host system, even if it is
527 included in ``pathconf_names``, an :exc:`OSError` is raised with
528 :const:`errno.EINVAL` for the error number.
529
Benjamin Petersond91203b2010-05-06 23:20:40 +0000530 Availability: Unix.
531
Georg Brandl116aa622007-08-15 14:28:22 +0000532
533.. function:: fstat(fd)
534
Benjamin Petersond91203b2010-05-06 23:20:40 +0000535 Return status for file descriptor *fd*, like :func:`stat`.
536
537 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000538
539
540.. function:: fstatvfs(fd)
541
542 Return information about the filesystem containing the file associated with file
Benjamin Petersond91203b2010-05-06 23:20:40 +0000543 descriptor *fd*, like :func:`statvfs`.
544
545 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000546
547
548.. function:: fsync(fd)
549
550 Force write of file with filedescriptor *fd* to disk. On Unix, this calls the
551 native :cfunc:`fsync` function; on Windows, the MS :cfunc:`_commit` function.
552
553 If you're starting with a Python file object *f*, first do ``f.flush()``, and
554 then do ``os.fsync(f.fileno())``, to ensure that all internal buffers associated
Benjamin Petersond91203b2010-05-06 23:20:40 +0000555 with *f* are written to disk.
556
557 Availability: Unix, and Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000558
559
560.. function:: ftruncate(fd, length)
561
562 Truncate the file corresponding to file descriptor *fd*, so that it is at most
Benjamin Petersond91203b2010-05-06 23:20:40 +0000563 *length* bytes in size.
564
565 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000566
567
568.. function:: isatty(fd)
569
570 Return ``True`` if the file descriptor *fd* is open and connected to a
Benjamin Petersond91203b2010-05-06 23:20:40 +0000571 tty(-like) device, else ``False``.
572
573 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000574
575
576.. function:: lseek(fd, pos, how)
577
Christian Heimesfaf2f632008-01-06 16:59:19 +0000578 Set the current position of file descriptor *fd* to position *pos*, modified
579 by *how*: :const:`SEEK_SET` or ``0`` to set the position relative to the
580 beginning of the file; :const:`SEEK_CUR` or ``1`` to set it relative to the
581 current position; :const:`os.SEEK_END` or ``2`` to set it relative to the end of
Benjamin Petersond91203b2010-05-06 23:20:40 +0000582 the file.
583
584 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000585
586
Georg Brandl6c8583f2010-05-19 21:22:58 +0000587.. data:: SEEK_SET
588 SEEK_CUR
589 SEEK_END
590
591 Parameters to the :func:`lseek` function. Their values are 0, 1, and 2,
592 respectively. Availability: Windows, Unix.
593
594
Georg Brandl116aa622007-08-15 14:28:22 +0000595.. function:: open(file, flags[, mode])
596
Georg Brandlf4a41232008-05-26 17:55:52 +0000597 Open the file *file* and set various flags according to *flags* and possibly
598 its mode according to *mode*. The default *mode* is ``0o777`` (octal), and
599 the current umask value is first masked out. Return the file descriptor for
Benjamin Petersond91203b2010-05-06 23:20:40 +0000600 the newly opened file.
Georg Brandl116aa622007-08-15 14:28:22 +0000601
602 For a description of the flag and mode values, see the C run-time documentation;
603 flag constants (like :const:`O_RDONLY` and :const:`O_WRONLY`) are defined in
Georg Brandl6c8583f2010-05-19 21:22:58 +0000604 this module too (see :ref:`open-constants`). In particular, on Windows adding
605 :const:`O_BINARY` is needed to open files in binary mode.
Georg Brandl116aa622007-08-15 14:28:22 +0000606
Benjamin Petersond91203b2010-05-06 23:20:40 +0000607 Availability: Unix, Windows.
608
Georg Brandl116aa622007-08-15 14:28:22 +0000609 .. note::
610
Georg Brandlc5605df2009-08-13 08:26:44 +0000611 This function is intended for low-level I/O. For normal usage, use the
612 built-in function :func:`open`, which returns a "file object" with
Benjamin Petersond91203b2010-05-06 23:20:40 +0000613 :meth:`~file.read` and :meth:`~file.wprite` methods (and many more). To
Georg Brandlc5605df2009-08-13 08:26:44 +0000614 wrap a file descriptor in a "file object", use :func:`fdopen`.
Georg Brandl116aa622007-08-15 14:28:22 +0000615
616
617.. function:: openpty()
618
619 .. index:: module: pty
620
621 Open a new pseudo-terminal pair. Return a pair of file descriptors ``(master,
622 slave)`` for the pty and the tty, respectively. For a (slightly) more portable
Benjamin Petersond91203b2010-05-06 23:20:40 +0000623 approach, use the :mod:`pty` module.
624
625 Availability: some flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000626
627
628.. function:: pipe()
629
630 Create a pipe. Return a pair of file descriptors ``(r, w)`` usable for reading
Benjamin Petersond91203b2010-05-06 23:20:40 +0000631 and writing, respectively.
632
633 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000634
635
636.. function:: read(fd, n)
637
Georg Brandlc5605df2009-08-13 08:26:44 +0000638 Read at most *n* bytes from file descriptor *fd*. Return a bytestring containing the
Georg Brandl116aa622007-08-15 14:28:22 +0000639 bytes read. If the end of the file referred to by *fd* has been reached, an
Benjamin Petersond91203b2010-05-06 23:20:40 +0000640 empty bytes object is returned.
641
642 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000643
644 .. note::
645
646 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000647 descriptor as returned by :func:`os.open` or :func:`pipe`. To read a "file object"
Georg Brandl116aa622007-08-15 14:28:22 +0000648 returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000649 :func:`fdopen`, or :data:`sys.stdin`, use its :meth:`~file.read` or
650 :meth:`~file.readline` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000651
652
653.. function:: tcgetpgrp(fd)
654
655 Return the process group associated with the terminal given by *fd* (an open
Benjamin Petersond91203b2010-05-06 23:20:40 +0000656 file descriptor as returned by :func:`os.open`).
657
658 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000659
660
661.. function:: tcsetpgrp(fd, pg)
662
663 Set the process group associated with the terminal given by *fd* (an open file
Benjamin Petersond91203b2010-05-06 23:20:40 +0000664 descriptor as returned by :func:`os.open`) to *pg*.
665
666 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000667
668
669.. function:: ttyname(fd)
670
671 Return a string which specifies the terminal device associated with
Georg Brandl9afde1c2007-11-01 20:32:30 +0000672 file descriptor *fd*. If *fd* is not associated with a terminal device, an
Benjamin Petersond91203b2010-05-06 23:20:40 +0000673 exception is raised.
674
675 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000676
677
678.. function:: write(fd, str)
679
Georg Brandlc5605df2009-08-13 08:26:44 +0000680 Write the bytestring in *str* to file descriptor *fd*. Return the number of
Benjamin Petersond91203b2010-05-06 23:20:40 +0000681 bytes actually written.
682
683 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000684
685 .. note::
686
687 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000688 descriptor as returned by :func:`os.open` or :func:`pipe`. To write a "file
Georg Brandl116aa622007-08-15 14:28:22 +0000689 object" returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000690 :func:`fdopen`, or :data:`sys.stdout` or :data:`sys.stderr`, use its
691 :meth:`~file.write` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000692
Georg Brandl6c8583f2010-05-19 21:22:58 +0000693
694.. _open-constants:
695
696``open()`` flag constants
697~~~~~~~~~~~~~~~~~~~~~~~~~
698
Georg Brandlaf265f42008-12-07 15:06:20 +0000699The following constants are options for the *flags* parameter to the
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000700:func:`~os.open` function. They can be combined using the bitwise OR operator
Georg Brandlaf265f42008-12-07 15:06:20 +0000701``|``. Some of them are not available on all platforms. For descriptions of
702their availability and use, consult the :manpage:`open(2)` manual page on Unix
Doug Hellmann14214262009-09-21 12:16:43 +0000703or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000704
705
706.. data:: O_RDONLY
707 O_WRONLY
708 O_RDWR
709 O_APPEND
710 O_CREAT
711 O_EXCL
712 O_TRUNC
713
Georg Brandlaf265f42008-12-07 15:06:20 +0000714 These constants are available on Unix and Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000715
716
717.. data:: O_DSYNC
718 O_RSYNC
719 O_SYNC
720 O_NDELAY
721 O_NONBLOCK
722 O_NOCTTY
723 O_SHLOCK
724 O_EXLOCK
725
Georg Brandlaf265f42008-12-07 15:06:20 +0000726 These constants are only available on Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000727
728
729.. data:: O_BINARY
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000730 O_NOINHERIT
Georg Brandl116aa622007-08-15 14:28:22 +0000731 O_SHORT_LIVED
732 O_TEMPORARY
733 O_RANDOM
734 O_SEQUENTIAL
735 O_TEXT
736
Georg Brandlaf265f42008-12-07 15:06:20 +0000737 These constants are only available on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000738
739
Alexandre Vassalottibee32532008-05-16 18:15:12 +0000740.. data:: O_ASYNC
741 O_DIRECT
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000742 O_DIRECTORY
743 O_NOFOLLOW
744 O_NOATIME
745
Georg Brandlaf265f42008-12-07 15:06:20 +0000746 These constants are GNU extensions and not present if they are not defined by
747 the C library.
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000748
749
Georg Brandl116aa622007-08-15 14:28:22 +0000750.. _os-file-dir:
751
752Files and Directories
753---------------------
754
Georg Brandl116aa622007-08-15 14:28:22 +0000755.. function:: access(path, mode)
756
757 Use the real uid/gid to test for access to *path*. Note that most operations
758 will use the effective uid/gid, therefore this routine can be used in a
759 suid/sgid environment to test if the invoking user has the specified access to
760 *path*. *mode* should be :const:`F_OK` to test the existence of *path*, or it
761 can be the inclusive OR of one or more of :const:`R_OK`, :const:`W_OK`, and
762 :const:`X_OK` to test permissions. Return :const:`True` if access is allowed,
763 :const:`False` if not. See the Unix man page :manpage:`access(2)` for more
Benjamin Petersond91203b2010-05-06 23:20:40 +0000764 information.
765
766 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000767
768 .. note::
769
Georg Brandlc5605df2009-08-13 08:26:44 +0000770 Using :func:`access` to check if a user is authorized to e.g. open a file
771 before actually doing so using :func:`open` creates a security hole,
772 because the user might exploit the short time interval between checking
773 and opening the file to manipulate it.
Georg Brandl116aa622007-08-15 14:28:22 +0000774
775 .. note::
776
777 I/O operations may fail even when :func:`access` indicates that they would
778 succeed, particularly for operations on network filesystems which may have
779 permissions semantics beyond the usual POSIX permission-bit model.
780
781
782.. data:: F_OK
783
784 Value to pass as the *mode* parameter of :func:`access` to test the existence of
785 *path*.
786
787
788.. data:: R_OK
789
790 Value to include in the *mode* parameter of :func:`access` to test the
791 readability of *path*.
792
793
794.. data:: W_OK
795
796 Value to include in the *mode* parameter of :func:`access` to test the
797 writability of *path*.
798
799
800.. data:: X_OK
801
802 Value to include in the *mode* parameter of :func:`access` to determine if
803 *path* can be executed.
804
805
806.. function:: chdir(path)
807
808 .. index:: single: directory; changing
809
Benjamin Petersond91203b2010-05-06 23:20:40 +0000810 Change the current working directory to *path*.
811
812 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000813
814
815.. function:: fchdir(fd)
816
817 Change the current working directory to the directory represented by the file
818 descriptor *fd*. The descriptor must refer to an opened directory, not an open
Benjamin Petersond91203b2010-05-06 23:20:40 +0000819 file.
820
821 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000822
Georg Brandl116aa622007-08-15 14:28:22 +0000823
824.. function:: getcwd()
825
Martin v. Löwis011e8422009-05-05 04:43:17 +0000826 Return a string representing the current working directory.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000827
Martin v. Löwis011e8422009-05-05 04:43:17 +0000828 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000829
Benjamin Petersond91203b2010-05-06 23:20:40 +0000830
Martin v. Löwisa731b992008-10-07 06:36:31 +0000831.. function:: getcwdb()
Georg Brandl116aa622007-08-15 14:28:22 +0000832
Georg Brandl76e55382008-10-08 16:34:57 +0000833 Return a bytestring representing the current working directory.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000834
Georg Brandlc575c902008-09-13 17:46:05 +0000835 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000836
Georg Brandl116aa622007-08-15 14:28:22 +0000837
838.. function:: chflags(path, flags)
839
840 Set the flags of *path* to the numeric *flags*. *flags* may take a combination
841 (bitwise OR) of the following values (as defined in the :mod:`stat` module):
842
843 * ``UF_NODUMP``
844 * ``UF_IMMUTABLE``
845 * ``UF_APPEND``
846 * ``UF_OPAQUE``
847 * ``UF_NOUNLINK``
848 * ``SF_ARCHIVED``
849 * ``SF_IMMUTABLE``
850 * ``SF_APPEND``
851 * ``SF_NOUNLINK``
852 * ``SF_SNAPSHOT``
853
Georg Brandlc575c902008-09-13 17:46:05 +0000854 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000855
Georg Brandl116aa622007-08-15 14:28:22 +0000856
857.. function:: chroot(path)
858
859 Change the root directory of the current process to *path*. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +0000860 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000861
Georg Brandl116aa622007-08-15 14:28:22 +0000862
863.. function:: chmod(path, mode)
864
865 Change the mode of *path* to the numeric *mode*. *mode* may take one of the
Christian Heimesfaf2f632008-01-06 16:59:19 +0000866 following values (as defined in the :mod:`stat` module) or bitwise ORed
Georg Brandl116aa622007-08-15 14:28:22 +0000867 combinations of them:
868
R. David Murrayba426142009-07-21 14:29:59 +0000869 * :data:`stat.S_ISUID`
870 * :data:`stat.S_ISGID`
871 * :data:`stat.S_ENFMT`
872 * :data:`stat.S_ISVTX`
873 * :data:`stat.S_IREAD`
874 * :data:`stat.S_IWRITE`
875 * :data:`stat.S_IEXEC`
876 * :data:`stat.S_IRWXU`
877 * :data:`stat.S_IRUSR`
878 * :data:`stat.S_IWUSR`
879 * :data:`stat.S_IXUSR`
880 * :data:`stat.S_IRWXG`
881 * :data:`stat.S_IRGRP`
882 * :data:`stat.S_IWGRP`
883 * :data:`stat.S_IXGRP`
884 * :data:`stat.S_IRWXO`
885 * :data:`stat.S_IROTH`
886 * :data:`stat.S_IWOTH`
887 * :data:`stat.S_IXOTH`
Georg Brandl116aa622007-08-15 14:28:22 +0000888
Georg Brandlc575c902008-09-13 17:46:05 +0000889 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000890
891 .. note::
892
893 Although Windows supports :func:`chmod`, you can only set the file's read-only
894 flag with it (via the ``stat.S_IWRITE`` and ``stat.S_IREAD``
895 constants or a corresponding integer value). All other bits are
896 ignored.
897
898
899.. function:: chown(path, uid, gid)
900
901 Change the owner and group id of *path* to the numeric *uid* and *gid*. To leave
Benjamin Petersond91203b2010-05-06 23:20:40 +0000902 one of the ids unchanged, set it to -1.
903
904 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000905
906
907.. function:: lchflags(path, flags)
908
909 Set the flags of *path* to the numeric *flags*, like :func:`chflags`, but do not
Benjamin Petersond91203b2010-05-06 23:20:40 +0000910 follow symbolic links.
911
912 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000913
Georg Brandl116aa622007-08-15 14:28:22 +0000914
Christian Heimes93852662007-12-01 12:22:32 +0000915.. function:: lchmod(path, mode)
916
917 Change the mode of *path* to the numeric *mode*. If path is a symlink, this
918 affects the symlink rather than the target. See the docs for :func:`chmod`
Benjamin Petersond91203b2010-05-06 23:20:40 +0000919 for possible values of *mode*.
920
921 Availability: Unix.
Christian Heimes93852662007-12-01 12:22:32 +0000922
Christian Heimes93852662007-12-01 12:22:32 +0000923
Georg Brandl116aa622007-08-15 14:28:22 +0000924.. function:: lchown(path, uid, gid)
925
Christian Heimesfaf2f632008-01-06 16:59:19 +0000926 Change the owner and group id of *path* to the numeric *uid* and *gid*. This
Benjamin Petersond91203b2010-05-06 23:20:40 +0000927 function will not follow symbolic links.
928
929 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000930
Georg Brandl116aa622007-08-15 14:28:22 +0000931
Benjamin Peterson5879d412009-03-30 14:51:56 +0000932.. function:: link(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +0000933
Benjamin Petersond91203b2010-05-06 23:20:40 +0000934 Create a hard link pointing to *source* named *link_name*.
935
936 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000937
938
939.. function:: listdir(path)
940
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000941 Return a list containing the names of the entries in the directory given by
942 *path*. The list is in arbitrary order. It does not include the special
943 entries ``'.'`` and ``'..'`` even if they are present in the directory.
Georg Brandl116aa622007-08-15 14:28:22 +0000944
Martin v. Löwis011e8422009-05-05 04:43:17 +0000945 This function can be called with a bytes or string argument, and returns
946 filenames of the same datatype.
Georg Brandl116aa622007-08-15 14:28:22 +0000947
Benjamin Petersond91203b2010-05-06 23:20:40 +0000948 Availability: Unix, Windows.
949
Georg Brandl116aa622007-08-15 14:28:22 +0000950
951.. function:: lstat(path)
952
Guido van Rossum2cc30da2007-11-02 23:46:40 +0000953 Like :func:`stat`, but do not follow symbolic links. This is an alias for
954 :func:`stat` on platforms that do not support symbolic links, such as
955 Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000956
957
958.. function:: mkfifo(path[, mode])
959
Georg Brandlf4a41232008-05-26 17:55:52 +0000960 Create a FIFO (a named pipe) named *path* with numeric mode *mode*. The
961 default *mode* is ``0o666`` (octal). The current umask value is first masked
Benjamin Petersond91203b2010-05-06 23:20:40 +0000962 out from the mode.
Georg Brandl116aa622007-08-15 14:28:22 +0000963
964 FIFOs are pipes that can be accessed like regular files. FIFOs exist until they
965 are deleted (for example with :func:`os.unlink`). Generally, FIFOs are used as
966 rendezvous between "client" and "server" type processes: the server opens the
967 FIFO for reading, and the client opens it for writing. Note that :func:`mkfifo`
968 doesn't open the FIFO --- it just creates the rendezvous point.
969
Benjamin Petersond91203b2010-05-06 23:20:40 +0000970 Availability: Unix.
971
Georg Brandl116aa622007-08-15 14:28:22 +0000972
Georg Brandlf4a41232008-05-26 17:55:52 +0000973.. function:: mknod(filename[, mode=0o600, device])
Georg Brandl116aa622007-08-15 14:28:22 +0000974
975 Create a filesystem node (file, device special file or named pipe) named
976 *filename*. *mode* specifies both the permissions to use and the type of node to
977 be created, being combined (bitwise OR) with one of ``stat.S_IFREG``,
978 ``stat.S_IFCHR``, ``stat.S_IFBLK``,
979 and ``stat.S_IFIFO`` (those constants are available in :mod:`stat`).
980 For ``stat.S_IFCHR`` and
981 ``stat.S_IFBLK``, *device* defines the newly created device special file (probably using
982 :func:`os.makedev`), otherwise it is ignored.
983
Georg Brandl116aa622007-08-15 14:28:22 +0000984
985.. function:: major(device)
986
Christian Heimesfaf2f632008-01-06 16:59:19 +0000987 Extract the device major number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000988 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
989
Georg Brandl116aa622007-08-15 14:28:22 +0000990
991.. function:: minor(device)
992
Christian Heimesfaf2f632008-01-06 16:59:19 +0000993 Extract the device minor number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000994 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
995
Georg Brandl116aa622007-08-15 14:28:22 +0000996
997.. function:: makedev(major, minor)
998
Christian Heimesfaf2f632008-01-06 16:59:19 +0000999 Compose a raw device number from the major and minor device numbers.
Georg Brandl116aa622007-08-15 14:28:22 +00001000
Georg Brandl116aa622007-08-15 14:28:22 +00001001
1002.. function:: mkdir(path[, mode])
1003
Georg Brandlf4a41232008-05-26 17:55:52 +00001004 Create a directory named *path* with numeric mode *mode*. The default *mode*
1005 is ``0o777`` (octal). On some systems, *mode* is ignored. Where it is used,
Georg Brandlc62efa82010-07-11 10:41:07 +00001006 the current umask value is first masked out. If the directory already
1007 exists, :exc:`OSError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +00001008
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001009 It is also possible to create temporary directories; see the
1010 :mod:`tempfile` module's :func:`tempfile.mkdtemp` function.
1011
Benjamin Petersond91203b2010-05-06 23:20:40 +00001012 Availability: Unix, Windows.
1013
Georg Brandl116aa622007-08-15 14:28:22 +00001014
1015.. function:: makedirs(path[, mode])
1016
1017 .. index::
1018 single: directory; creating
1019 single: UNC paths; and os.makedirs()
1020
1021 Recursive directory creation function. Like :func:`mkdir`, but makes all
Georg Brandlf4a41232008-05-26 17:55:52 +00001022 intermediate-level directories needed to contain the leaf directory. Throws
1023 an :exc:`error` exception if the leaf directory already exists or cannot be
1024 created. The default *mode* is ``0o777`` (octal). On some systems, *mode*
1025 is ignored. Where it is used, the current umask value is first masked out.
Georg Brandl116aa622007-08-15 14:28:22 +00001026
1027 .. note::
1028
1029 :func:`makedirs` will become confused if the path elements to create include
Christian Heimesfaf2f632008-01-06 16:59:19 +00001030 :data:`os.pardir`.
Georg Brandl116aa622007-08-15 14:28:22 +00001031
Georg Brandl55ac8f02007-09-01 13:51:09 +00001032 This function handles UNC paths correctly.
Georg Brandl116aa622007-08-15 14:28:22 +00001033
1034
1035.. function:: pathconf(path, name)
1036
1037 Return system configuration information relevant to a named file. *name*
1038 specifies the configuration value to retrieve; it may be a string which is the
1039 name of a defined system value; these names are specified in a number of
1040 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
1041 additional names as well. The names known to the host operating system are
1042 given in the ``pathconf_names`` dictionary. For configuration variables not
1043 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +00001044
1045 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1046 specific value for *name* is not supported by the host system, even if it is
1047 included in ``pathconf_names``, an :exc:`OSError` is raised with
1048 :const:`errno.EINVAL` for the error number.
1049
Benjamin Petersond91203b2010-05-06 23:20:40 +00001050 Availability: Unix.
1051
Georg Brandl116aa622007-08-15 14:28:22 +00001052
1053.. data:: pathconf_names
1054
1055 Dictionary mapping names accepted by :func:`pathconf` and :func:`fpathconf` to
1056 the integer values defined for those names by the host operating system. This
1057 can be used to determine the set of names known to the system. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001058 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001059
1060
1061.. function:: readlink(path)
1062
1063 Return a string representing the path to which the symbolic link points. The
1064 result may be either an absolute or relative pathname; if it is relative, it may
1065 be converted to an absolute pathname using ``os.path.join(os.path.dirname(path),
1066 result)``.
1067
Georg Brandl76e55382008-10-08 16:34:57 +00001068 If the *path* is a string object, the result will also be a string object,
1069 and the call may raise an UnicodeDecodeError. If the *path* is a bytes
1070 object, the result will be a bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +00001071
Georg Brandlc575c902008-09-13 17:46:05 +00001072 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001073
1074
1075.. function:: remove(path)
1076
Georg Brandl7baf6252009-09-01 08:13:16 +00001077 Remove (delete) the file *path*. If *path* is a directory, :exc:`OSError` is
1078 raised; see :func:`rmdir` below to remove a directory. This is identical to
1079 the :func:`unlink` function documented below. On Windows, attempting to
1080 remove a file that is in use causes an exception to be raised; on Unix, the
1081 directory entry is removed but the storage allocated to the file is not made
Benjamin Petersond91203b2010-05-06 23:20:40 +00001082 available until the original file is no longer in use.
1083
1084 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001085
1086
1087.. function:: removedirs(path)
1088
1089 .. index:: single: directory; deleting
1090
Christian Heimesfaf2f632008-01-06 16:59:19 +00001091 Remove directories recursively. Works like :func:`rmdir` except that, if the
Georg Brandl116aa622007-08-15 14:28:22 +00001092 leaf directory is successfully removed, :func:`removedirs` tries to
1093 successively remove every parent directory mentioned in *path* until an error
1094 is raised (which is ignored, because it generally means that a parent directory
1095 is not empty). For example, ``os.removedirs('foo/bar/baz')`` will first remove
1096 the directory ``'foo/bar/baz'``, and then remove ``'foo/bar'`` and ``'foo'`` if
1097 they are empty. Raises :exc:`OSError` if the leaf directory could not be
1098 successfully removed.
1099
Georg Brandl116aa622007-08-15 14:28:22 +00001100
1101.. function:: rename(src, dst)
1102
1103 Rename the file or directory *src* to *dst*. If *dst* is a directory,
1104 :exc:`OSError` will be raised. On Unix, if *dst* exists and is a file, it will
Christian Heimesfaf2f632008-01-06 16:59:19 +00001105 be replaced silently if the user has permission. The operation may fail on some
Georg Brandl116aa622007-08-15 14:28:22 +00001106 Unix flavors if *src* and *dst* are on different filesystems. If successful,
1107 the renaming will be an atomic operation (this is a POSIX requirement). On
1108 Windows, if *dst* already exists, :exc:`OSError` will be raised even if it is a
1109 file; there may be no way to implement an atomic rename when *dst* names an
Benjamin Petersond91203b2010-05-06 23:20:40 +00001110 existing file.
1111
1112 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001113
1114
1115.. function:: renames(old, new)
1116
1117 Recursive directory or file renaming function. Works like :func:`rename`, except
1118 creation of any intermediate directories needed to make the new pathname good is
1119 attempted first. After the rename, directories corresponding to rightmost path
1120 segments of the old name will be pruned away using :func:`removedirs`.
1121
Georg Brandl116aa622007-08-15 14:28:22 +00001122 .. note::
1123
1124 This function can fail with the new directory structure made if you lack
1125 permissions needed to remove the leaf directory or file.
1126
1127
1128.. function:: rmdir(path)
1129
Georg Brandl7baf6252009-09-01 08:13:16 +00001130 Remove (delete) the directory *path*. Only works when the directory is
1131 empty, otherwise, :exc:`OSError` is raised. In order to remove whole
Benjamin Petersond91203b2010-05-06 23:20:40 +00001132 directory trees, :func:`shutil.rmtree` can be used.
1133
1134 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001135
1136
1137.. function:: stat(path)
1138
1139 Perform a :cfunc:`stat` system call on the given path. The return value is an
1140 object whose attributes correspond to the members of the :ctype:`stat`
1141 structure, namely: :attr:`st_mode` (protection bits), :attr:`st_ino` (inode
1142 number), :attr:`st_dev` (device), :attr:`st_nlink` (number of hard links),
Christian Heimesfaf2f632008-01-06 16:59:19 +00001143 :attr:`st_uid` (user id of owner), :attr:`st_gid` (group id of owner),
Georg Brandl116aa622007-08-15 14:28:22 +00001144 :attr:`st_size` (size of file, in bytes), :attr:`st_atime` (time of most recent
1145 access), :attr:`st_mtime` (time of most recent content modification),
1146 :attr:`st_ctime` (platform dependent; time of most recent metadata change on
1147 Unix, or the time of creation on Windows)::
1148
1149 >>> import os
1150 >>> statinfo = os.stat('somefile.txt')
1151 >>> statinfo
Ezio Melotti9ce52612010-01-16 14:52:34 +00001152 (33188, 422511, 769, 1, 1032, 100, 926, 1105022698,1105022732, 1105022732)
Georg Brandl116aa622007-08-15 14:28:22 +00001153 >>> statinfo.st_size
Ezio Melotti9ce52612010-01-16 14:52:34 +00001154 926
Georg Brandl116aa622007-08-15 14:28:22 +00001155 >>>
1156
Georg Brandl116aa622007-08-15 14:28:22 +00001157
1158 On some Unix systems (such as Linux), the following attributes may also be
1159 available: :attr:`st_blocks` (number of blocks allocated for file),
1160 :attr:`st_blksize` (filesystem blocksize), :attr:`st_rdev` (type of device if an
1161 inode device). :attr:`st_flags` (user defined flags for file).
1162
1163 On other Unix systems (such as FreeBSD), the following attributes may be
1164 available (but may be only filled out if root tries to use them): :attr:`st_gen`
1165 (file generation number), :attr:`st_birthtime` (time of file creation).
1166
1167 On Mac OS systems, the following attributes may also be available:
1168 :attr:`st_rsize`, :attr:`st_creator`, :attr:`st_type`.
1169
Georg Brandl116aa622007-08-15 14:28:22 +00001170 .. index:: module: stat
1171
1172 For backward compatibility, the return value of :func:`stat` is also accessible
1173 as a tuple of at least 10 integers giving the most important (and portable)
1174 members of the :ctype:`stat` structure, in the order :attr:`st_mode`,
1175 :attr:`st_ino`, :attr:`st_dev`, :attr:`st_nlink`, :attr:`st_uid`,
1176 :attr:`st_gid`, :attr:`st_size`, :attr:`st_atime`, :attr:`st_mtime`,
1177 :attr:`st_ctime`. More items may be added at the end by some implementations.
1178 The standard module :mod:`stat` defines functions and constants that are useful
1179 for extracting information from a :ctype:`stat` structure. (On Windows, some
1180 items are filled with dummy values.)
1181
1182 .. note::
1183
1184 The exact meaning and resolution of the :attr:`st_atime`, :attr:`st_mtime`, and
1185 :attr:`st_ctime` members depends on the operating system and the file system.
1186 For example, on Windows systems using the FAT or FAT32 file systems,
1187 :attr:`st_mtime` has 2-second resolution, and :attr:`st_atime` has only 1-day
1188 resolution. See your operating system documentation for details.
1189
Georg Brandlc575c902008-09-13 17:46:05 +00001190 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001191
Georg Brandl116aa622007-08-15 14:28:22 +00001192
1193.. function:: stat_float_times([newvalue])
1194
1195 Determine whether :class:`stat_result` represents time stamps as float objects.
1196 If *newvalue* is ``True``, future calls to :func:`stat` return floats, if it is
1197 ``False``, future calls return ints. If *newvalue* is omitted, return the
1198 current setting.
1199
1200 For compatibility with older Python versions, accessing :class:`stat_result` as
1201 a tuple always returns integers.
1202
Georg Brandl55ac8f02007-09-01 13:51:09 +00001203 Python now returns float values by default. Applications which do not work
1204 correctly with floating point time stamps can use this function to restore the
1205 old behaviour.
Georg Brandl116aa622007-08-15 14:28:22 +00001206
1207 The resolution of the timestamps (that is the smallest possible fraction)
1208 depends on the system. Some systems only support second resolution; on these
1209 systems, the fraction will always be zero.
1210
1211 It is recommended that this setting is only changed at program startup time in
1212 the *__main__* module; libraries should never change this setting. If an
1213 application uses a library that works incorrectly if floating point time stamps
1214 are processed, this application should turn the feature off until the library
1215 has been corrected.
1216
1217
1218.. function:: statvfs(path)
1219
1220 Perform a :cfunc:`statvfs` system call on the given path. The return value is
1221 an object whose attributes describe the filesystem on the given path, and
1222 correspond to the members of the :ctype:`statvfs` structure, namely:
1223 :attr:`f_bsize`, :attr:`f_frsize`, :attr:`f_blocks`, :attr:`f_bfree`,
1224 :attr:`f_bavail`, :attr:`f_files`, :attr:`f_ffree`, :attr:`f_favail`,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001225 :attr:`f_flag`, :attr:`f_namemax`.
1226
1227 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001228
Georg Brandl116aa622007-08-15 14:28:22 +00001229
Benjamin Peterson5879d412009-03-30 14:51:56 +00001230.. function:: symlink(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +00001231
Benjamin Petersond91203b2010-05-06 23:20:40 +00001232 Create a symbolic link pointing to *source* named *link_name*.
1233
1234 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001235
1236
Georg Brandl116aa622007-08-15 14:28:22 +00001237.. function:: unlink(path)
1238
Georg Brandl7baf6252009-09-01 08:13:16 +00001239 Remove (delete) the file *path*. This is the same function as
1240 :func:`remove`; the :func:`unlink` name is its traditional Unix
Benjamin Petersond91203b2010-05-06 23:20:40 +00001241 name.
1242
1243 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001244
1245
1246.. function:: utime(path, times)
1247
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001248 Set the access and modified times of the file specified by *path*. If *times*
1249 is ``None``, then the file's access and modified times are set to the current
1250 time. (The effect is similar to running the Unix program :program:`touch` on
1251 the path.) Otherwise, *times* must be a 2-tuple of numbers, of the form
1252 ``(atime, mtime)`` which is used to set the access and modified times,
1253 respectively. Whether a directory can be given for *path* depends on whether
1254 the operating system implements directories as files (for example, Windows
1255 does not). Note that the exact times you set here may not be returned by a
1256 subsequent :func:`stat` call, depending on the resolution with which your
1257 operating system records access and modification times; see :func:`stat`.
Georg Brandl116aa622007-08-15 14:28:22 +00001258
Georg Brandlc575c902008-09-13 17:46:05 +00001259 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001260
1261
1262.. function:: walk(top[, topdown=True [, onerror=None[, followlinks=False]]])
1263
1264 .. index::
1265 single: directory; walking
1266 single: directory; traversal
1267
Christian Heimesfaf2f632008-01-06 16:59:19 +00001268 Generate the file names in a directory tree by walking the tree
1269 either top-down or bottom-up. For each directory in the tree rooted at directory
Georg Brandl116aa622007-08-15 14:28:22 +00001270 *top* (including *top* itself), it yields a 3-tuple ``(dirpath, dirnames,
1271 filenames)``.
1272
1273 *dirpath* is a string, the path to the directory. *dirnames* is a list of the
1274 names of the subdirectories in *dirpath* (excluding ``'.'`` and ``'..'``).
1275 *filenames* is a list of the names of the non-directory files in *dirpath*.
1276 Note that the names in the lists contain no path components. To get a full path
1277 (which begins with *top*) to a file or directory in *dirpath*, do
1278 ``os.path.join(dirpath, name)``.
1279
Christian Heimesfaf2f632008-01-06 16:59:19 +00001280 If optional argument *topdown* is ``True`` or not specified, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001281 directory is generated before the triples for any of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001282 (directories are generated top-down). If *topdown* is ``False``, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001283 directory is generated after the triples for all of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001284 (directories are generated bottom-up).
Georg Brandl116aa622007-08-15 14:28:22 +00001285
Christian Heimesfaf2f632008-01-06 16:59:19 +00001286 When *topdown* is ``True``, the caller can modify the *dirnames* list in-place
Georg Brandl116aa622007-08-15 14:28:22 +00001287 (perhaps using :keyword:`del` or slice assignment), and :func:`walk` will only
1288 recurse into the subdirectories whose names remain in *dirnames*; this can be
1289 used to prune the search, impose a specific order of visiting, or even to inform
1290 :func:`walk` about directories the caller creates or renames before it resumes
Christian Heimesfaf2f632008-01-06 16:59:19 +00001291 :func:`walk` again. Modifying *dirnames* when *topdown* is ``False`` is
Georg Brandl116aa622007-08-15 14:28:22 +00001292 ineffective, because in bottom-up mode the directories in *dirnames* are
1293 generated before *dirpath* itself is generated.
1294
Christian Heimesfaf2f632008-01-06 16:59:19 +00001295 By default errors from the :func:`listdir` call are ignored. If optional
Georg Brandl116aa622007-08-15 14:28:22 +00001296 argument *onerror* is specified, it should be a function; it will be called with
1297 one argument, an :exc:`OSError` instance. It can report the error to continue
1298 with the walk, or raise the exception to abort the walk. Note that the filename
1299 is available as the ``filename`` attribute of the exception object.
1300
1301 By default, :func:`walk` will not walk down into symbolic links that resolve to
Christian Heimesfaf2f632008-01-06 16:59:19 +00001302 directories. Set *followlinks* to ``True`` to visit directories pointed to by
Georg Brandl116aa622007-08-15 14:28:22 +00001303 symlinks, on systems that support them.
1304
Georg Brandl116aa622007-08-15 14:28:22 +00001305 .. note::
1306
Christian Heimesfaf2f632008-01-06 16:59:19 +00001307 Be aware that setting *followlinks* to ``True`` can lead to infinite recursion if a
Georg Brandl116aa622007-08-15 14:28:22 +00001308 link points to a parent directory of itself. :func:`walk` does not keep track of
1309 the directories it visited already.
1310
1311 .. note::
1312
1313 If you pass a relative pathname, don't change the current working directory
1314 between resumptions of :func:`walk`. :func:`walk` never changes the current
1315 directory, and assumes that its caller doesn't either.
1316
1317 This example displays the number of bytes taken by non-directory files in each
1318 directory under the starting directory, except that it doesn't look under any
1319 CVS subdirectory::
1320
1321 import os
1322 from os.path import join, getsize
1323 for root, dirs, files in os.walk('python/Lib/email'):
Georg Brandl6911e3c2007-09-04 07:15:32 +00001324 print(root, "consumes", end=" ")
1325 print(sum(getsize(join(root, name)) for name in files), end=" ")
1326 print("bytes in", len(files), "non-directory files")
Georg Brandl116aa622007-08-15 14:28:22 +00001327 if 'CVS' in dirs:
1328 dirs.remove('CVS') # don't visit CVS directories
1329
Christian Heimesfaf2f632008-01-06 16:59:19 +00001330 In the next example, walking the tree bottom-up is essential: :func:`rmdir`
Georg Brandl116aa622007-08-15 14:28:22 +00001331 doesn't allow deleting a directory before the directory is empty::
1332
Christian Heimesfaf2f632008-01-06 16:59:19 +00001333 # Delete everything reachable from the directory named in "top",
Georg Brandl116aa622007-08-15 14:28:22 +00001334 # assuming there are no symbolic links.
1335 # CAUTION: This is dangerous! For example, if top == '/', it
1336 # could delete all your disk files.
1337 import os
1338 for root, dirs, files in os.walk(top, topdown=False):
1339 for name in files:
1340 os.remove(os.path.join(root, name))
1341 for name in dirs:
1342 os.rmdir(os.path.join(root, name))
1343
Georg Brandl116aa622007-08-15 14:28:22 +00001344
1345.. _os-process:
1346
1347Process Management
1348------------------
1349
1350These functions may be used to create and manage processes.
1351
1352The various :func:`exec\*` functions take a list of arguments for the new
1353program loaded into the process. In each case, the first of these arguments is
1354passed to the new program as its own name rather than as an argument a user may
1355have typed on a command line. For the C programmer, this is the ``argv[0]``
1356passed to a program's :cfunc:`main`. For example, ``os.execv('/bin/echo',
1357['foo', 'bar'])`` will only print ``bar`` on standard output; ``foo`` will seem
1358to be ignored.
1359
1360
1361.. function:: abort()
1362
1363 Generate a :const:`SIGABRT` signal to the current process. On Unix, the default
1364 behavior is to produce a core dump; on Windows, the process immediately returns
1365 an exit code of ``3``. Be aware that programs which use :func:`signal.signal`
1366 to register a handler for :const:`SIGABRT` will behave differently.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001367
Georg Brandlc575c902008-09-13 17:46:05 +00001368 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001369
1370
1371.. function:: execl(path, arg0, arg1, ...)
1372 execle(path, arg0, arg1, ..., env)
1373 execlp(file, arg0, arg1, ...)
1374 execlpe(file, arg0, arg1, ..., env)
1375 execv(path, args)
1376 execve(path, args, env)
1377 execvp(file, args)
1378 execvpe(file, args, env)
1379
1380 These functions all execute a new program, replacing the current process; they
1381 do not return. On Unix, the new executable is loaded into the current process,
Christian Heimesfaf2f632008-01-06 16:59:19 +00001382 and will have the same process id as the caller. Errors will be reported as
Georg Brandl48310cd2009-01-03 21:18:54 +00001383 :exc:`OSError` exceptions.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001384
1385 The current process is replaced immediately. Open file objects and
1386 descriptors are not flushed, so if there may be data buffered
1387 on these open files, you should flush them using
1388 :func:`sys.stdout.flush` or :func:`os.fsync` before calling an
1389 :func:`exec\*` function.
Georg Brandl116aa622007-08-15 14:28:22 +00001390
Christian Heimesfaf2f632008-01-06 16:59:19 +00001391 The "l" and "v" variants of the :func:`exec\*` functions differ in how
1392 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001393 to work with if the number of parameters is fixed when the code is written; the
1394 individual parameters simply become additional parameters to the :func:`execl\*`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001395 functions. The "v" variants are good when the number of parameters is
Georg Brandl116aa622007-08-15 14:28:22 +00001396 variable, with the arguments being passed in a list or tuple as the *args*
1397 parameter. In either case, the arguments to the child process should start with
1398 the name of the command being run, but this is not enforced.
1399
Christian Heimesfaf2f632008-01-06 16:59:19 +00001400 The variants which include a "p" near the end (:func:`execlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001401 :func:`execlpe`, :func:`execvp`, and :func:`execvpe`) will use the
1402 :envvar:`PATH` environment variable to locate the program *file*. When the
1403 environment is being replaced (using one of the :func:`exec\*e` variants,
1404 discussed in the next paragraph), the new environment is used as the source of
1405 the :envvar:`PATH` variable. The other variants, :func:`execl`, :func:`execle`,
1406 :func:`execv`, and :func:`execve`, will not use the :envvar:`PATH` variable to
1407 locate the executable; *path* must contain an appropriate absolute or relative
1408 path.
1409
1410 For :func:`execle`, :func:`execlpe`, :func:`execve`, and :func:`execvpe` (note
Christian Heimesfaf2f632008-01-06 16:59:19 +00001411 that these all end in "e"), the *env* parameter must be a mapping which is
Christian Heimesa342c012008-04-20 21:01:16 +00001412 used to define the environment variables for the new process (these are used
1413 instead of the current process' environment); the functions :func:`execl`,
Georg Brandl116aa622007-08-15 14:28:22 +00001414 :func:`execlp`, :func:`execv`, and :func:`execvp` all cause the new process to
Georg Brandl48310cd2009-01-03 21:18:54 +00001415 inherit the environment of the current process.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001416
1417 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001418
1419
1420.. function:: _exit(n)
1421
1422 Exit to the system with status *n*, without calling cleanup handlers, flushing
Benjamin Petersond91203b2010-05-06 23:20:40 +00001423 stdio buffers, etc.
1424
1425 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001426
1427 .. note::
1428
1429 The standard way to exit is ``sys.exit(n)``. :func:`_exit` should normally only
1430 be used in the child process after a :func:`fork`.
1431
Christian Heimesfaf2f632008-01-06 16:59:19 +00001432The following exit codes are defined and can be used with :func:`_exit`,
Georg Brandl116aa622007-08-15 14:28:22 +00001433although they are not required. These are typically used for system programs
1434written in Python, such as a mail server's external command delivery program.
1435
1436.. note::
1437
1438 Some of these may not be available on all Unix platforms, since there is some
1439 variation. These constants are defined where they are defined by the underlying
1440 platform.
1441
1442
1443.. data:: EX_OK
1444
Benjamin Petersond91203b2010-05-06 23:20:40 +00001445 Exit code that means no error occurred.
1446
1447 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001448
Georg Brandl116aa622007-08-15 14:28:22 +00001449
1450.. data:: EX_USAGE
1451
1452 Exit code that means the command was used incorrectly, such as when the wrong
Benjamin Petersond91203b2010-05-06 23:20:40 +00001453 number of arguments are given.
1454
1455 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001456
Georg Brandl116aa622007-08-15 14:28:22 +00001457
1458.. data:: EX_DATAERR
1459
Benjamin Petersond91203b2010-05-06 23:20:40 +00001460 Exit code that means the input data was incorrect.
1461
1462 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001463
Georg Brandl116aa622007-08-15 14:28:22 +00001464
1465.. data:: EX_NOINPUT
1466
1467 Exit code that means an input file did not exist or was not readable.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001468
Georg Brandlc575c902008-09-13 17:46:05 +00001469 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001470
Georg Brandl116aa622007-08-15 14:28:22 +00001471
1472.. data:: EX_NOUSER
1473
Benjamin Petersond91203b2010-05-06 23:20:40 +00001474 Exit code that means a specified user did not exist.
1475
1476 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001477
Georg Brandl116aa622007-08-15 14:28:22 +00001478
1479.. data:: EX_NOHOST
1480
Benjamin Petersond91203b2010-05-06 23:20:40 +00001481 Exit code that means a specified host did not exist.
1482
1483 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001484
Georg Brandl116aa622007-08-15 14:28:22 +00001485
1486.. data:: EX_UNAVAILABLE
1487
Benjamin Petersond91203b2010-05-06 23:20:40 +00001488 Exit code that means that a required service is unavailable.
1489
1490 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001491
Georg Brandl116aa622007-08-15 14:28:22 +00001492
1493.. data:: EX_SOFTWARE
1494
Benjamin Petersond91203b2010-05-06 23:20:40 +00001495 Exit code that means an internal software error was detected.
1496
1497 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001498
Georg Brandl116aa622007-08-15 14:28:22 +00001499
1500.. data:: EX_OSERR
1501
1502 Exit code that means an operating system error was detected, such as the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001503 inability to fork or create a pipe.
1504
1505 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001506
Georg Brandl116aa622007-08-15 14:28:22 +00001507
1508.. data:: EX_OSFILE
1509
1510 Exit code that means some system file did not exist, could not be opened, or had
Benjamin Petersond91203b2010-05-06 23:20:40 +00001511 some other kind of error.
1512
1513 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001514
Georg Brandl116aa622007-08-15 14:28:22 +00001515
1516.. data:: EX_CANTCREAT
1517
1518 Exit code that means a user specified output file could not be created.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001519
Georg Brandlc575c902008-09-13 17:46:05 +00001520 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001521
Georg Brandl116aa622007-08-15 14:28:22 +00001522
1523.. data:: EX_IOERR
1524
1525 Exit code that means that an error occurred while doing I/O on some file.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001526
Georg Brandlc575c902008-09-13 17:46:05 +00001527 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001528
Georg Brandl116aa622007-08-15 14:28:22 +00001529
1530.. data:: EX_TEMPFAIL
1531
1532 Exit code that means a temporary failure occurred. This indicates something
1533 that may not really be an error, such as a network connection that couldn't be
Benjamin Petersond91203b2010-05-06 23:20:40 +00001534 made during a retryable operation.
1535
1536 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001537
Georg Brandl116aa622007-08-15 14:28:22 +00001538
1539.. data:: EX_PROTOCOL
1540
1541 Exit code that means that a protocol exchange was illegal, invalid, or not
Benjamin Petersond91203b2010-05-06 23:20:40 +00001542 understood.
1543
1544 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001545
Georg Brandl116aa622007-08-15 14:28:22 +00001546
1547.. data:: EX_NOPERM
1548
1549 Exit code that means that there were insufficient permissions to perform the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001550 operation (but not intended for file system problems).
1551
1552 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001553
Georg Brandl116aa622007-08-15 14:28:22 +00001554
1555.. data:: EX_CONFIG
1556
1557 Exit code that means that some kind of configuration error occurred.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001558
Georg Brandlc575c902008-09-13 17:46:05 +00001559 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001560
Georg Brandl116aa622007-08-15 14:28:22 +00001561
1562.. data:: EX_NOTFOUND
1563
Benjamin Petersond91203b2010-05-06 23:20:40 +00001564 Exit code that means something like "an entry was not found".
1565
1566 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001567
Georg Brandl116aa622007-08-15 14:28:22 +00001568
1569.. function:: fork()
1570
Christian Heimesfaf2f632008-01-06 16:59:19 +00001571 Fork a child process. Return ``0`` in the child and the child's process id in the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001572 parent. If an error occurs :exc:`OSError` is raised.
Benjamin Petersonbcd8ac32008-10-10 22:20:52 +00001573
1574 Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have
1575 known issues when using fork() from a thread.
1576
Georg Brandlc575c902008-09-13 17:46:05 +00001577 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001578
1579
1580.. function:: forkpty()
1581
1582 Fork a child process, using a new pseudo-terminal as the child's controlling
1583 terminal. Return a pair of ``(pid, fd)``, where *pid* is ``0`` in the child, the
1584 new child's process id in the parent, and *fd* is the file descriptor of the
1585 master end of the pseudo-terminal. For a more portable approach, use the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001586 :mod:`pty` module. If an error occurs :exc:`OSError` is raised.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001587
Georg Brandlc575c902008-09-13 17:46:05 +00001588 Availability: some flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001589
1590
1591.. function:: kill(pid, sig)
1592
1593 .. index::
1594 single: process; killing
1595 single: process; signalling
1596
1597 Send signal *sig* to the process *pid*. Constants for the specific signals
1598 available on the host platform are defined in the :mod:`signal` module.
Georg Brandlc575c902008-09-13 17:46:05 +00001599 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001600
1601
1602.. function:: killpg(pgid, sig)
1603
1604 .. index::
1605 single: process; killing
1606 single: process; signalling
1607
Benjamin Petersond91203b2010-05-06 23:20:40 +00001608 Send the signal *sig* to the process group *pgid*.
1609
1610 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001611
Georg Brandl116aa622007-08-15 14:28:22 +00001612
1613.. function:: nice(increment)
1614
1615 Add *increment* to the process's "niceness". Return the new niceness.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001616
Georg Brandlc575c902008-09-13 17:46:05 +00001617 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001618
1619
1620.. function:: plock(op)
1621
1622 Lock program segments into memory. The value of *op* (defined in
Benjamin Petersond91203b2010-05-06 23:20:40 +00001623 ``<sys/lock.h>``) determines which segments are locked.
1624
1625 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001626
1627
1628.. function:: popen(...)
1629 :noindex:
1630
1631 Run child processes, returning opened pipes for communications. These functions
1632 are described in section :ref:`os-newstreams`.
1633
1634
1635.. function:: spawnl(mode, path, ...)
1636 spawnle(mode, path, ..., env)
1637 spawnlp(mode, file, ...)
1638 spawnlpe(mode, file, ..., env)
1639 spawnv(mode, path, args)
1640 spawnve(mode, path, args, env)
1641 spawnvp(mode, file, args)
1642 spawnvpe(mode, file, args, env)
1643
1644 Execute the program *path* in a new process.
1645
1646 (Note that the :mod:`subprocess` module provides more powerful facilities for
1647 spawning new processes and retrieving their results; using that module is
Benjamin Peterson87c8d872009-06-11 22:54:11 +00001648 preferable to using these functions. Check especially the
1649 :ref:`subprocess-replacements` section.)
Georg Brandl116aa622007-08-15 14:28:22 +00001650
Christian Heimesfaf2f632008-01-06 16:59:19 +00001651 If *mode* is :const:`P_NOWAIT`, this function returns the process id of the new
Georg Brandl116aa622007-08-15 14:28:22 +00001652 process; if *mode* is :const:`P_WAIT`, returns the process's exit code if it
1653 exits normally, or ``-signal``, where *signal* is the signal that killed the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001654 process. On Windows, the process id will actually be the process handle, so can
Georg Brandl116aa622007-08-15 14:28:22 +00001655 be used with the :func:`waitpid` function.
1656
Christian Heimesfaf2f632008-01-06 16:59:19 +00001657 The "l" and "v" variants of the :func:`spawn\*` functions differ in how
1658 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001659 to work with if the number of parameters is fixed when the code is written; the
1660 individual parameters simply become additional parameters to the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001661 :func:`spawnl\*` functions. The "v" variants are good when the number of
Georg Brandl116aa622007-08-15 14:28:22 +00001662 parameters is variable, with the arguments being passed in a list or tuple as
1663 the *args* parameter. In either case, the arguments to the child process must
1664 start with the name of the command being run.
1665
Christian Heimesfaf2f632008-01-06 16:59:19 +00001666 The variants which include a second "p" near the end (:func:`spawnlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001667 :func:`spawnlpe`, :func:`spawnvp`, and :func:`spawnvpe`) will use the
1668 :envvar:`PATH` environment variable to locate the program *file*. When the
1669 environment is being replaced (using one of the :func:`spawn\*e` variants,
1670 discussed in the next paragraph), the new environment is used as the source of
1671 the :envvar:`PATH` variable. The other variants, :func:`spawnl`,
1672 :func:`spawnle`, :func:`spawnv`, and :func:`spawnve`, will not use the
1673 :envvar:`PATH` variable to locate the executable; *path* must contain an
1674 appropriate absolute or relative path.
1675
1676 For :func:`spawnle`, :func:`spawnlpe`, :func:`spawnve`, and :func:`spawnvpe`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001677 (note that these all end in "e"), the *env* parameter must be a mapping
Christian Heimesa342c012008-04-20 21:01:16 +00001678 which is used to define the environment variables for the new process (they are
1679 used instead of the current process' environment); the functions
Georg Brandl116aa622007-08-15 14:28:22 +00001680 :func:`spawnl`, :func:`spawnlp`, :func:`spawnv`, and :func:`spawnvp` all cause
Benjamin Petersond23f8222009-04-05 19:13:16 +00001681 the new process to inherit the environment of the current process. Note that
1682 keys and values in the *env* dictionary must be strings; invalid keys or
1683 values will cause the function to fail, with a return value of ``127``.
Georg Brandl116aa622007-08-15 14:28:22 +00001684
1685 As an example, the following calls to :func:`spawnlp` and :func:`spawnvpe` are
1686 equivalent::
1687
1688 import os
1689 os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1690
1691 L = ['cp', 'index.html', '/dev/null']
1692 os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1693
1694 Availability: Unix, Windows. :func:`spawnlp`, :func:`spawnlpe`, :func:`spawnvp`
1695 and :func:`spawnvpe` are not available on Windows.
1696
Georg Brandl116aa622007-08-15 14:28:22 +00001697
1698.. data:: P_NOWAIT
1699 P_NOWAITO
1700
1701 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1702 functions. If either of these values is given, the :func:`spawn\*` functions
Christian Heimesfaf2f632008-01-06 16:59:19 +00001703 will return as soon as the new process has been created, with the process id as
Benjamin Petersond91203b2010-05-06 23:20:40 +00001704 the return value.
1705
1706 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001707
Georg Brandl116aa622007-08-15 14:28:22 +00001708
1709.. data:: P_WAIT
1710
1711 Possible value for the *mode* parameter to the :func:`spawn\*` family of
1712 functions. If this is given as *mode*, the :func:`spawn\*` functions will not
1713 return until the new process has run to completion and will return the exit code
1714 of the process the run is successful, or ``-signal`` if a signal kills the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001715 process.
1716
1717 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001718
Georg Brandl116aa622007-08-15 14:28:22 +00001719
1720.. data:: P_DETACH
1721 P_OVERLAY
1722
1723 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1724 functions. These are less portable than those listed above. :const:`P_DETACH`
1725 is similar to :const:`P_NOWAIT`, but the new process is detached from the
1726 console of the calling process. If :const:`P_OVERLAY` is used, the current
1727 process will be replaced; the :func:`spawn\*` function will not return.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001728
Georg Brandl116aa622007-08-15 14:28:22 +00001729 Availability: Windows.
1730
Georg Brandl116aa622007-08-15 14:28:22 +00001731
1732.. function:: startfile(path[, operation])
1733
1734 Start a file with its associated application.
1735
1736 When *operation* is not specified or ``'open'``, this acts like double-clicking
1737 the file in Windows Explorer, or giving the file name as an argument to the
1738 :program:`start` command from the interactive command shell: the file is opened
1739 with whatever application (if any) its extension is associated.
1740
1741 When another *operation* is given, it must be a "command verb" that specifies
1742 what should be done with the file. Common verbs documented by Microsoft are
1743 ``'print'`` and ``'edit'`` (to be used on files) as well as ``'explore'`` and
1744 ``'find'`` (to be used on directories).
1745
1746 :func:`startfile` returns as soon as the associated application is launched.
1747 There is no option to wait for the application to close, and no way to retrieve
1748 the application's exit status. The *path* parameter is relative to the current
1749 directory. If you want to use an absolute path, make sure the first character
1750 is not a slash (``'/'``); the underlying Win32 :cfunc:`ShellExecute` function
1751 doesn't work if it is. Use the :func:`os.path.normpath` function to ensure that
Benjamin Petersond91203b2010-05-06 23:20:40 +00001752 the path is properly encoded for Win32.
1753
1754 Availability: Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001755
Georg Brandl116aa622007-08-15 14:28:22 +00001756
1757.. function:: system(command)
1758
1759 Execute the command (a string) in a subshell. This is implemented by calling
Georg Brandl628e6f92009-10-27 20:24:45 +00001760 the Standard C function :cfunc:`system`, and has the same limitations.
1761 Changes to :data:`sys.stdin`, etc. are not reflected in the environment of the
1762 executed command.
Georg Brandl116aa622007-08-15 14:28:22 +00001763
1764 On Unix, the return value is the exit status of the process encoded in the
1765 format specified for :func:`wait`. Note that POSIX does not specify the meaning
1766 of the return value of the C :cfunc:`system` function, so the return value of
1767 the Python function is system-dependent.
1768
1769 On Windows, the return value is that returned by the system shell after running
1770 *command*, given by the Windows environment variable :envvar:`COMSPEC`: on
1771 :program:`command.com` systems (Windows 95, 98 and ME) this is always ``0``; on
1772 :program:`cmd.exe` systems (Windows NT, 2000 and XP) this is the exit status of
1773 the command run; on systems using a non-native shell, consult your shell
1774 documentation.
1775
Georg Brandl116aa622007-08-15 14:28:22 +00001776 The :mod:`subprocess` module provides more powerful facilities for spawning new
1777 processes and retrieving their results; using that module is preferable to using
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001778 this function. Use the :mod:`subprocess` module. Check especially the
1779 :ref:`subprocess-replacements` section.
Georg Brandl116aa622007-08-15 14:28:22 +00001780
Benjamin Petersond91203b2010-05-06 23:20:40 +00001781 Availability: Unix, Windows.
1782
Georg Brandl116aa622007-08-15 14:28:22 +00001783
1784.. function:: times()
1785
Benjamin Petersond91203b2010-05-06 23:20:40 +00001786 Return a 5-tuple of floating point numbers indicating accumulated (processor
1787 or other) times, in seconds. The items are: user time, system time,
1788 children's user time, children's system time, and elapsed real time since a
1789 fixed point in the past, in that order. See the Unix manual page
1790 :manpage:`times(2)` or the corresponding Windows Platform API documentation.
1791 On Windows, only the first two items are filled, the others are zero.
1792
1793 Availability: Unix, Windows
Georg Brandl116aa622007-08-15 14:28:22 +00001794
1795
1796.. function:: wait()
1797
1798 Wait for completion of a child process, and return a tuple containing its pid
1799 and exit status indication: a 16-bit number, whose low byte is the signal number
1800 that killed the process, and whose high byte is the exit status (if the signal
1801 number is zero); the high bit of the low byte is set if a core file was
Benjamin Petersond91203b2010-05-06 23:20:40 +00001802 produced.
1803
1804 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001805
1806
1807.. function:: waitpid(pid, options)
1808
1809 The details of this function differ on Unix and Windows.
1810
1811 On Unix: Wait for completion of a child process given by process id *pid*, and
1812 return a tuple containing its process id and exit status indication (encoded as
1813 for :func:`wait`). The semantics of the call are affected by the value of the
1814 integer *options*, which should be ``0`` for normal operation.
1815
1816 If *pid* is greater than ``0``, :func:`waitpid` requests status information for
1817 that specific process. If *pid* is ``0``, the request is for the status of any
1818 child in the process group of the current process. If *pid* is ``-1``, the
1819 request pertains to any child of the current process. If *pid* is less than
1820 ``-1``, status is requested for any process in the process group ``-pid`` (the
1821 absolute value of *pid*).
1822
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001823 An :exc:`OSError` is raised with the value of errno when the syscall
1824 returns -1.
1825
Georg Brandl116aa622007-08-15 14:28:22 +00001826 On Windows: Wait for completion of a process given by process handle *pid*, and
1827 return a tuple containing *pid*, and its exit status shifted left by 8 bits
1828 (shifting makes cross-platform use of the function easier). A *pid* less than or
1829 equal to ``0`` has no special meaning on Windows, and raises an exception. The
1830 value of integer *options* has no effect. *pid* can refer to any process whose
1831 id is known, not necessarily a child process. The :func:`spawn` functions called
1832 with :const:`P_NOWAIT` return suitable process handles.
1833
1834
1835.. function:: wait3([options])
1836
1837 Similar to :func:`waitpid`, except no process id argument is given and a
1838 3-element tuple containing the child's process id, exit status indication, and
1839 resource usage information is returned. Refer to :mod:`resource`.\
1840 :func:`getrusage` for details on resource usage information. The option
1841 argument is the same as that provided to :func:`waitpid` and :func:`wait4`.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001842
Georg Brandl116aa622007-08-15 14:28:22 +00001843 Availability: Unix.
1844
Georg Brandl116aa622007-08-15 14:28:22 +00001845
1846.. function:: wait4(pid, options)
1847
1848 Similar to :func:`waitpid`, except a 3-element tuple, containing the child's
1849 process id, exit status indication, and resource usage information is returned.
1850 Refer to :mod:`resource`.\ :func:`getrusage` for details on resource usage
1851 information. The arguments to :func:`wait4` are the same as those provided to
Benjamin Petersond91203b2010-05-06 23:20:40 +00001852 :func:`waitpid`.
1853
1854 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001855
Georg Brandl116aa622007-08-15 14:28:22 +00001856
1857.. data:: WNOHANG
1858
1859 The option for :func:`waitpid` to return immediately if no child process status
1860 is available immediately. The function returns ``(0, 0)`` in this case.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001861
Georg Brandlc575c902008-09-13 17:46:05 +00001862 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001863
1864
1865.. data:: WCONTINUED
1866
1867 This option causes child processes to be reported if they have been continued
Benjamin Petersond91203b2010-05-06 23:20:40 +00001868 from a job control stop since their status was last reported.
1869
1870 Availability: Some Unix systems.
Georg Brandl116aa622007-08-15 14:28:22 +00001871
Georg Brandl116aa622007-08-15 14:28:22 +00001872
1873.. data:: WUNTRACED
1874
1875 This option causes child processes to be reported if they have been stopped but
Benjamin Petersond91203b2010-05-06 23:20:40 +00001876 their current state has not been reported since they were stopped.
1877
1878 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001879
Georg Brandl116aa622007-08-15 14:28:22 +00001880
1881The following functions take a process status code as returned by
1882:func:`system`, :func:`wait`, or :func:`waitpid` as a parameter. They may be
1883used to determine the disposition of a process.
1884
Georg Brandl116aa622007-08-15 14:28:22 +00001885.. function:: WCOREDUMP(status)
1886
Christian Heimesfaf2f632008-01-06 16:59:19 +00001887 Return ``True`` if a core dump was generated for the process, otherwise
Benjamin Petersond91203b2010-05-06 23:20:40 +00001888 return ``False``.
1889
1890 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001891
Georg Brandl116aa622007-08-15 14:28:22 +00001892
1893.. function:: WIFCONTINUED(status)
1894
Christian Heimesfaf2f632008-01-06 16:59:19 +00001895 Return ``True`` if the process has been continued from a job control stop,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001896 otherwise return ``False``.
1897
1898 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001899
Georg Brandl116aa622007-08-15 14:28:22 +00001900
1901.. function:: WIFSTOPPED(status)
1902
Christian Heimesfaf2f632008-01-06 16:59:19 +00001903 Return ``True`` if the process has been stopped, otherwise return
Benjamin Petersond91203b2010-05-06 23:20:40 +00001904 ``False``.
1905
1906 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001907
1908
1909.. function:: WIFSIGNALED(status)
1910
Christian Heimesfaf2f632008-01-06 16:59:19 +00001911 Return ``True`` if the process exited due to a signal, otherwise return
Benjamin Petersond91203b2010-05-06 23:20:40 +00001912 ``False``.
1913
1914 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001915
1916
1917.. function:: WIFEXITED(status)
1918
Christian Heimesfaf2f632008-01-06 16:59:19 +00001919 Return ``True`` if the process exited using the :manpage:`exit(2)` system call,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001920 otherwise return ``False``.
1921
1922 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001923
1924
1925.. function:: WEXITSTATUS(status)
1926
1927 If ``WIFEXITED(status)`` is true, return the integer parameter to the
1928 :manpage:`exit(2)` system call. Otherwise, the return value is meaningless.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001929
Georg Brandlc575c902008-09-13 17:46:05 +00001930 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001931
1932
1933.. function:: WSTOPSIG(status)
1934
Benjamin Petersond91203b2010-05-06 23:20:40 +00001935 Return the signal which caused the process to stop.
1936
1937 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001938
1939
1940.. function:: WTERMSIG(status)
1941
Benjamin Petersond91203b2010-05-06 23:20:40 +00001942 Return the signal which caused the process to exit.
1943
1944 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001945
1946
1947.. _os-path:
1948
1949Miscellaneous System Information
1950--------------------------------
1951
1952
1953.. function:: confstr(name)
1954
1955 Return string-valued system configuration values. *name* specifies the
1956 configuration value to retrieve; it may be a string which is the name of a
1957 defined system value; these names are specified in a number of standards (POSIX,
1958 Unix 95, Unix 98, and others). Some platforms define additional names as well.
1959 The names known to the host operating system are given as the keys of the
1960 ``confstr_names`` dictionary. For configuration variables not included in that
Benjamin Petersond91203b2010-05-06 23:20:40 +00001961 mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +00001962
1963 If the configuration value specified by *name* isn't defined, ``None`` is
1964 returned.
1965
1966 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1967 specific value for *name* is not supported by the host system, even if it is
1968 included in ``confstr_names``, an :exc:`OSError` is raised with
1969 :const:`errno.EINVAL` for the error number.
1970
Benjamin Petersond91203b2010-05-06 23:20:40 +00001971 Availability: Unix
1972
Georg Brandl116aa622007-08-15 14:28:22 +00001973
1974.. data:: confstr_names
1975
1976 Dictionary mapping names accepted by :func:`confstr` to the integer values
1977 defined for those names by the host operating system. This can be used to
Benjamin Petersond91203b2010-05-06 23:20:40 +00001978 determine the set of names known to the system.
1979
1980 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001981
1982
1983.. function:: getloadavg()
1984
Christian Heimesa62da1d2008-01-12 19:39:10 +00001985 Return the number of processes in the system run queue averaged over the last
1986 1, 5, and 15 minutes or raises :exc:`OSError` if the load average was
Benjamin Petersond91203b2010-05-06 23:20:40 +00001987 unobtainable.
1988
1989 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001990
Georg Brandl116aa622007-08-15 14:28:22 +00001991
1992.. function:: sysconf(name)
1993
1994 Return integer-valued system configuration values. If the configuration value
1995 specified by *name* isn't defined, ``-1`` is returned. The comments regarding
1996 the *name* parameter for :func:`confstr` apply here as well; the dictionary that
1997 provides information on the known names is given by ``sysconf_names``.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001998
Georg Brandlc575c902008-09-13 17:46:05 +00001999 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002000
2001
2002.. data:: sysconf_names
2003
2004 Dictionary mapping names accepted by :func:`sysconf` to the integer values
2005 defined for those names by the host operating system. This can be used to
Benjamin Petersond91203b2010-05-06 23:20:40 +00002006 determine the set of names known to the system.
2007
2008 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002009
Christian Heimesfaf2f632008-01-06 16:59:19 +00002010The following data values are used to support path manipulation operations. These
Georg Brandl116aa622007-08-15 14:28:22 +00002011are defined for all platforms.
2012
2013Higher-level operations on pathnames are defined in the :mod:`os.path` module.
2014
2015
2016.. data:: curdir
2017
2018 The constant string used by the operating system to refer to the current
Georg Brandlc575c902008-09-13 17:46:05 +00002019 directory. This is ``'.'`` for Windows and POSIX. Also available via
2020 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002021
2022
2023.. data:: pardir
2024
2025 The constant string used by the operating system to refer to the parent
Georg Brandlc575c902008-09-13 17:46:05 +00002026 directory. This is ``'..'`` for Windows and POSIX. Also available via
2027 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002028
2029
2030.. data:: sep
2031
Georg Brandlc575c902008-09-13 17:46:05 +00002032 The character used by the operating system to separate pathname components.
2033 This is ``'/'`` for POSIX and ``'\\'`` for Windows. Note that knowing this
2034 is not sufficient to be able to parse or concatenate pathnames --- use
Georg Brandl116aa622007-08-15 14:28:22 +00002035 :func:`os.path.split` and :func:`os.path.join` --- but it is occasionally
2036 useful. Also available via :mod:`os.path`.
2037
2038
2039.. data:: altsep
2040
2041 An alternative character used by the operating system to separate pathname
2042 components, or ``None`` if only one separator character exists. This is set to
2043 ``'/'`` on Windows systems where ``sep`` is a backslash. Also available via
2044 :mod:`os.path`.
2045
2046
2047.. data:: extsep
2048
2049 The character which separates the base filename from the extension; for example,
2050 the ``'.'`` in :file:`os.py`. Also available via :mod:`os.path`.
2051
Georg Brandl116aa622007-08-15 14:28:22 +00002052
2053.. data:: pathsep
2054
2055 The character conventionally used by the operating system to separate search
2056 path components (as in :envvar:`PATH`), such as ``':'`` for POSIX or ``';'`` for
2057 Windows. Also available via :mod:`os.path`.
2058
2059
2060.. data:: defpath
2061
2062 The default search path used by :func:`exec\*p\*` and :func:`spawn\*p\*` if the
2063 environment doesn't have a ``'PATH'`` key. Also available via :mod:`os.path`.
2064
2065
2066.. data:: linesep
2067
2068 The string used to separate (or, rather, terminate) lines on the current
Georg Brandlc575c902008-09-13 17:46:05 +00002069 platform. This may be a single character, such as ``'\n'`` for POSIX, or
2070 multiple characters, for example, ``'\r\n'`` for Windows. Do not use
2071 *os.linesep* as a line terminator when writing files opened in text mode (the
2072 default); use a single ``'\n'`` instead, on all platforms.
Georg Brandl116aa622007-08-15 14:28:22 +00002073
2074
2075.. data:: devnull
2076
Georg Brandlc52eeab2010-05-21 22:05:15 +00002077 The file path of the null device. For example: ``'/dev/null'`` for
2078 POSIX, ``'nul'`` for Windows. Also available via :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002079
Georg Brandl116aa622007-08-15 14:28:22 +00002080
2081.. _os-miscfunc:
2082
2083Miscellaneous Functions
2084-----------------------
2085
2086
2087.. function:: urandom(n)
2088
2089 Return a string of *n* random bytes suitable for cryptographic use.
2090
2091 This function returns random bytes from an OS-specific randomness source. The
2092 returned data should be unpredictable enough for cryptographic applications,
2093 though its exact quality depends on the OS implementation. On a UNIX-like
2094 system this will query /dev/urandom, and on Windows it will use CryptGenRandom.
2095 If a randomness source is not found, :exc:`NotImplementedError` will be raised.