blob: 63bacd3dd117cd2bff53005324ed62220c88e47d [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,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001006 the current umask value is first masked out.
Georg Brandl116aa622007-08-15 14:28:22 +00001007
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001008 It is also possible to create temporary directories; see the
1009 :mod:`tempfile` module's :func:`tempfile.mkdtemp` function.
1010
Benjamin Petersond91203b2010-05-06 23:20:40 +00001011 Availability: Unix, Windows.
1012
Georg Brandl116aa622007-08-15 14:28:22 +00001013
1014.. function:: makedirs(path[, mode])
1015
1016 .. index::
1017 single: directory; creating
1018 single: UNC paths; and os.makedirs()
1019
1020 Recursive directory creation function. Like :func:`mkdir`, but makes all
Georg Brandlf4a41232008-05-26 17:55:52 +00001021 intermediate-level directories needed to contain the leaf directory. Throws
1022 an :exc:`error` exception if the leaf directory already exists or cannot be
1023 created. The default *mode* is ``0o777`` (octal). On some systems, *mode*
1024 is ignored. Where it is used, the current umask value is first masked out.
Georg Brandl116aa622007-08-15 14:28:22 +00001025
1026 .. note::
1027
1028 :func:`makedirs` will become confused if the path elements to create include
Christian Heimesfaf2f632008-01-06 16:59:19 +00001029 :data:`os.pardir`.
Georg Brandl116aa622007-08-15 14:28:22 +00001030
Georg Brandl55ac8f02007-09-01 13:51:09 +00001031 This function handles UNC paths correctly.
Georg Brandl116aa622007-08-15 14:28:22 +00001032
1033
1034.. function:: pathconf(path, name)
1035
1036 Return system configuration information relevant to a named file. *name*
1037 specifies the configuration value to retrieve; it may be a string which is the
1038 name of a defined system value; these names are specified in a number of
1039 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
1040 additional names as well. The names known to the host operating system are
1041 given in the ``pathconf_names`` dictionary. For configuration variables not
1042 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +00001043
1044 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1045 specific value for *name* is not supported by the host system, even if it is
1046 included in ``pathconf_names``, an :exc:`OSError` is raised with
1047 :const:`errno.EINVAL` for the error number.
1048
Benjamin Petersond91203b2010-05-06 23:20:40 +00001049 Availability: Unix.
1050
Georg Brandl116aa622007-08-15 14:28:22 +00001051
1052.. data:: pathconf_names
1053
1054 Dictionary mapping names accepted by :func:`pathconf` and :func:`fpathconf` to
1055 the integer values defined for those names by the host operating system. This
1056 can be used to determine the set of names known to the system. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001057 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001058
1059
1060.. function:: readlink(path)
1061
1062 Return a string representing the path to which the symbolic link points. The
1063 result may be either an absolute or relative pathname; if it is relative, it may
1064 be converted to an absolute pathname using ``os.path.join(os.path.dirname(path),
1065 result)``.
1066
Georg Brandl76e55382008-10-08 16:34:57 +00001067 If the *path* is a string object, the result will also be a string object,
1068 and the call may raise an UnicodeDecodeError. If the *path* is a bytes
1069 object, the result will be a bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +00001070
Georg Brandlc575c902008-09-13 17:46:05 +00001071 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001072
1073
1074.. function:: remove(path)
1075
Georg Brandl7baf6252009-09-01 08:13:16 +00001076 Remove (delete) the file *path*. If *path* is a directory, :exc:`OSError` is
1077 raised; see :func:`rmdir` below to remove a directory. This is identical to
1078 the :func:`unlink` function documented below. On Windows, attempting to
1079 remove a file that is in use causes an exception to be raised; on Unix, the
1080 directory entry is removed but the storage allocated to the file is not made
Benjamin Petersond91203b2010-05-06 23:20:40 +00001081 available until the original file is no longer in use.
1082
1083 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001084
1085
1086.. function:: removedirs(path)
1087
1088 .. index:: single: directory; deleting
1089
Christian Heimesfaf2f632008-01-06 16:59:19 +00001090 Remove directories recursively. Works like :func:`rmdir` except that, if the
Georg Brandl116aa622007-08-15 14:28:22 +00001091 leaf directory is successfully removed, :func:`removedirs` tries to
1092 successively remove every parent directory mentioned in *path* until an error
1093 is raised (which is ignored, because it generally means that a parent directory
1094 is not empty). For example, ``os.removedirs('foo/bar/baz')`` will first remove
1095 the directory ``'foo/bar/baz'``, and then remove ``'foo/bar'`` and ``'foo'`` if
1096 they are empty. Raises :exc:`OSError` if the leaf directory could not be
1097 successfully removed.
1098
Georg Brandl116aa622007-08-15 14:28:22 +00001099
1100.. function:: rename(src, dst)
1101
1102 Rename the file or directory *src* to *dst*. If *dst* is a directory,
1103 :exc:`OSError` will be raised. On Unix, if *dst* exists and is a file, it will
Christian Heimesfaf2f632008-01-06 16:59:19 +00001104 be replaced silently if the user has permission. The operation may fail on some
Georg Brandl116aa622007-08-15 14:28:22 +00001105 Unix flavors if *src* and *dst* are on different filesystems. If successful,
1106 the renaming will be an atomic operation (this is a POSIX requirement). On
1107 Windows, if *dst* already exists, :exc:`OSError` will be raised even if it is a
1108 file; there may be no way to implement an atomic rename when *dst* names an
Benjamin Petersond91203b2010-05-06 23:20:40 +00001109 existing file.
1110
1111 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001112
1113
1114.. function:: renames(old, new)
1115
1116 Recursive directory or file renaming function. Works like :func:`rename`, except
1117 creation of any intermediate directories needed to make the new pathname good is
1118 attempted first. After the rename, directories corresponding to rightmost path
1119 segments of the old name will be pruned away using :func:`removedirs`.
1120
Georg Brandl116aa622007-08-15 14:28:22 +00001121 .. note::
1122
1123 This function can fail with the new directory structure made if you lack
1124 permissions needed to remove the leaf directory or file.
1125
1126
1127.. function:: rmdir(path)
1128
Georg Brandl7baf6252009-09-01 08:13:16 +00001129 Remove (delete) the directory *path*. Only works when the directory is
1130 empty, otherwise, :exc:`OSError` is raised. In order to remove whole
Benjamin Petersond91203b2010-05-06 23:20:40 +00001131 directory trees, :func:`shutil.rmtree` can be used.
1132
1133 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001134
1135
1136.. function:: stat(path)
1137
1138 Perform a :cfunc:`stat` system call on the given path. The return value is an
1139 object whose attributes correspond to the members of the :ctype:`stat`
1140 structure, namely: :attr:`st_mode` (protection bits), :attr:`st_ino` (inode
1141 number), :attr:`st_dev` (device), :attr:`st_nlink` (number of hard links),
Christian Heimesfaf2f632008-01-06 16:59:19 +00001142 :attr:`st_uid` (user id of owner), :attr:`st_gid` (group id of owner),
Georg Brandl116aa622007-08-15 14:28:22 +00001143 :attr:`st_size` (size of file, in bytes), :attr:`st_atime` (time of most recent
1144 access), :attr:`st_mtime` (time of most recent content modification),
1145 :attr:`st_ctime` (platform dependent; time of most recent metadata change on
1146 Unix, or the time of creation on Windows)::
1147
1148 >>> import os
1149 >>> statinfo = os.stat('somefile.txt')
1150 >>> statinfo
Ezio Melotti9ce52612010-01-16 14:52:34 +00001151 (33188, 422511, 769, 1, 1032, 100, 926, 1105022698,1105022732, 1105022732)
Georg Brandl116aa622007-08-15 14:28:22 +00001152 >>> statinfo.st_size
Ezio Melotti9ce52612010-01-16 14:52:34 +00001153 926
Georg Brandl116aa622007-08-15 14:28:22 +00001154 >>>
1155
Georg Brandl116aa622007-08-15 14:28:22 +00001156
1157 On some Unix systems (such as Linux), the following attributes may also be
1158 available: :attr:`st_blocks` (number of blocks allocated for file),
1159 :attr:`st_blksize` (filesystem blocksize), :attr:`st_rdev` (type of device if an
1160 inode device). :attr:`st_flags` (user defined flags for file).
1161
1162 On other Unix systems (such as FreeBSD), the following attributes may be
1163 available (but may be only filled out if root tries to use them): :attr:`st_gen`
1164 (file generation number), :attr:`st_birthtime` (time of file creation).
1165
1166 On Mac OS systems, the following attributes may also be available:
1167 :attr:`st_rsize`, :attr:`st_creator`, :attr:`st_type`.
1168
Georg Brandl116aa622007-08-15 14:28:22 +00001169 .. index:: module: stat
1170
1171 For backward compatibility, the return value of :func:`stat` is also accessible
1172 as a tuple of at least 10 integers giving the most important (and portable)
1173 members of the :ctype:`stat` structure, in the order :attr:`st_mode`,
1174 :attr:`st_ino`, :attr:`st_dev`, :attr:`st_nlink`, :attr:`st_uid`,
1175 :attr:`st_gid`, :attr:`st_size`, :attr:`st_atime`, :attr:`st_mtime`,
1176 :attr:`st_ctime`. More items may be added at the end by some implementations.
1177 The standard module :mod:`stat` defines functions and constants that are useful
1178 for extracting information from a :ctype:`stat` structure. (On Windows, some
1179 items are filled with dummy values.)
1180
1181 .. note::
1182
1183 The exact meaning and resolution of the :attr:`st_atime`, :attr:`st_mtime`, and
1184 :attr:`st_ctime` members depends on the operating system and the file system.
1185 For example, on Windows systems using the FAT or FAT32 file systems,
1186 :attr:`st_mtime` has 2-second resolution, and :attr:`st_atime` has only 1-day
1187 resolution. See your operating system documentation for details.
1188
Georg Brandlc575c902008-09-13 17:46:05 +00001189 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001190
Georg Brandl116aa622007-08-15 14:28:22 +00001191
1192.. function:: stat_float_times([newvalue])
1193
1194 Determine whether :class:`stat_result` represents time stamps as float objects.
1195 If *newvalue* is ``True``, future calls to :func:`stat` return floats, if it is
1196 ``False``, future calls return ints. If *newvalue* is omitted, return the
1197 current setting.
1198
1199 For compatibility with older Python versions, accessing :class:`stat_result` as
1200 a tuple always returns integers.
1201
Georg Brandl55ac8f02007-09-01 13:51:09 +00001202 Python now returns float values by default. Applications which do not work
1203 correctly with floating point time stamps can use this function to restore the
1204 old behaviour.
Georg Brandl116aa622007-08-15 14:28:22 +00001205
1206 The resolution of the timestamps (that is the smallest possible fraction)
1207 depends on the system. Some systems only support second resolution; on these
1208 systems, the fraction will always be zero.
1209
1210 It is recommended that this setting is only changed at program startup time in
1211 the *__main__* module; libraries should never change this setting. If an
1212 application uses a library that works incorrectly if floating point time stamps
1213 are processed, this application should turn the feature off until the library
1214 has been corrected.
1215
1216
1217.. function:: statvfs(path)
1218
1219 Perform a :cfunc:`statvfs` system call on the given path. The return value is
1220 an object whose attributes describe the filesystem on the given path, and
1221 correspond to the members of the :ctype:`statvfs` structure, namely:
1222 :attr:`f_bsize`, :attr:`f_frsize`, :attr:`f_blocks`, :attr:`f_bfree`,
1223 :attr:`f_bavail`, :attr:`f_files`, :attr:`f_ffree`, :attr:`f_favail`,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001224 :attr:`f_flag`, :attr:`f_namemax`.
1225
1226 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001227
Georg Brandl116aa622007-08-15 14:28:22 +00001228
Benjamin Peterson5879d412009-03-30 14:51:56 +00001229.. function:: symlink(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +00001230
Benjamin Petersond91203b2010-05-06 23:20:40 +00001231 Create a symbolic link pointing to *source* named *link_name*.
1232
1233 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001234
1235
Georg Brandl116aa622007-08-15 14:28:22 +00001236.. function:: unlink(path)
1237
Georg Brandl7baf6252009-09-01 08:13:16 +00001238 Remove (delete) the file *path*. This is the same function as
1239 :func:`remove`; the :func:`unlink` name is its traditional Unix
Benjamin Petersond91203b2010-05-06 23:20:40 +00001240 name.
1241
1242 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001243
1244
1245.. function:: utime(path, times)
1246
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001247 Set the access and modified times of the file specified by *path*. If *times*
1248 is ``None``, then the file's access and modified times are set to the current
1249 time. (The effect is similar to running the Unix program :program:`touch` on
1250 the path.) Otherwise, *times* must be a 2-tuple of numbers, of the form
1251 ``(atime, mtime)`` which is used to set the access and modified times,
1252 respectively. Whether a directory can be given for *path* depends on whether
1253 the operating system implements directories as files (for example, Windows
1254 does not). Note that the exact times you set here may not be returned by a
1255 subsequent :func:`stat` call, depending on the resolution with which your
1256 operating system records access and modification times; see :func:`stat`.
Georg Brandl116aa622007-08-15 14:28:22 +00001257
Georg Brandlc575c902008-09-13 17:46:05 +00001258 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001259
1260
1261.. function:: walk(top[, topdown=True [, onerror=None[, followlinks=False]]])
1262
1263 .. index::
1264 single: directory; walking
1265 single: directory; traversal
1266
Christian Heimesfaf2f632008-01-06 16:59:19 +00001267 Generate the file names in a directory tree by walking the tree
1268 either top-down or bottom-up. For each directory in the tree rooted at directory
Georg Brandl116aa622007-08-15 14:28:22 +00001269 *top* (including *top* itself), it yields a 3-tuple ``(dirpath, dirnames,
1270 filenames)``.
1271
1272 *dirpath* is a string, the path to the directory. *dirnames* is a list of the
1273 names of the subdirectories in *dirpath* (excluding ``'.'`` and ``'..'``).
1274 *filenames* is a list of the names of the non-directory files in *dirpath*.
1275 Note that the names in the lists contain no path components. To get a full path
1276 (which begins with *top*) to a file or directory in *dirpath*, do
1277 ``os.path.join(dirpath, name)``.
1278
Christian Heimesfaf2f632008-01-06 16:59:19 +00001279 If optional argument *topdown* is ``True`` or not specified, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001280 directory is generated before the triples for any of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001281 (directories are generated top-down). If *topdown* is ``False``, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001282 directory is generated after the triples for all of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001283 (directories are generated bottom-up).
Georg Brandl116aa622007-08-15 14:28:22 +00001284
Christian Heimesfaf2f632008-01-06 16:59:19 +00001285 When *topdown* is ``True``, the caller can modify the *dirnames* list in-place
Georg Brandl116aa622007-08-15 14:28:22 +00001286 (perhaps using :keyword:`del` or slice assignment), and :func:`walk` will only
1287 recurse into the subdirectories whose names remain in *dirnames*; this can be
1288 used to prune the search, impose a specific order of visiting, or even to inform
1289 :func:`walk` about directories the caller creates or renames before it resumes
Christian Heimesfaf2f632008-01-06 16:59:19 +00001290 :func:`walk` again. Modifying *dirnames* when *topdown* is ``False`` is
Georg Brandl116aa622007-08-15 14:28:22 +00001291 ineffective, because in bottom-up mode the directories in *dirnames* are
1292 generated before *dirpath* itself is generated.
1293
Christian Heimesfaf2f632008-01-06 16:59:19 +00001294 By default errors from the :func:`listdir` call are ignored. If optional
Georg Brandl116aa622007-08-15 14:28:22 +00001295 argument *onerror* is specified, it should be a function; it will be called with
1296 one argument, an :exc:`OSError` instance. It can report the error to continue
1297 with the walk, or raise the exception to abort the walk. Note that the filename
1298 is available as the ``filename`` attribute of the exception object.
1299
1300 By default, :func:`walk` will not walk down into symbolic links that resolve to
Christian Heimesfaf2f632008-01-06 16:59:19 +00001301 directories. Set *followlinks* to ``True`` to visit directories pointed to by
Georg Brandl116aa622007-08-15 14:28:22 +00001302 symlinks, on systems that support them.
1303
Georg Brandl116aa622007-08-15 14:28:22 +00001304 .. note::
1305
Christian Heimesfaf2f632008-01-06 16:59:19 +00001306 Be aware that setting *followlinks* to ``True`` can lead to infinite recursion if a
Georg Brandl116aa622007-08-15 14:28:22 +00001307 link points to a parent directory of itself. :func:`walk` does not keep track of
1308 the directories it visited already.
1309
1310 .. note::
1311
1312 If you pass a relative pathname, don't change the current working directory
1313 between resumptions of :func:`walk`. :func:`walk` never changes the current
1314 directory, and assumes that its caller doesn't either.
1315
1316 This example displays the number of bytes taken by non-directory files in each
1317 directory under the starting directory, except that it doesn't look under any
1318 CVS subdirectory::
1319
1320 import os
1321 from os.path import join, getsize
1322 for root, dirs, files in os.walk('python/Lib/email'):
Georg Brandl6911e3c2007-09-04 07:15:32 +00001323 print(root, "consumes", end=" ")
1324 print(sum(getsize(join(root, name)) for name in files), end=" ")
1325 print("bytes in", len(files), "non-directory files")
Georg Brandl116aa622007-08-15 14:28:22 +00001326 if 'CVS' in dirs:
1327 dirs.remove('CVS') # don't visit CVS directories
1328
Christian Heimesfaf2f632008-01-06 16:59:19 +00001329 In the next example, walking the tree bottom-up is essential: :func:`rmdir`
Georg Brandl116aa622007-08-15 14:28:22 +00001330 doesn't allow deleting a directory before the directory is empty::
1331
Christian Heimesfaf2f632008-01-06 16:59:19 +00001332 # Delete everything reachable from the directory named in "top",
Georg Brandl116aa622007-08-15 14:28:22 +00001333 # assuming there are no symbolic links.
1334 # CAUTION: This is dangerous! For example, if top == '/', it
1335 # could delete all your disk files.
1336 import os
1337 for root, dirs, files in os.walk(top, topdown=False):
1338 for name in files:
1339 os.remove(os.path.join(root, name))
1340 for name in dirs:
1341 os.rmdir(os.path.join(root, name))
1342
Georg Brandl116aa622007-08-15 14:28:22 +00001343
1344.. _os-process:
1345
1346Process Management
1347------------------
1348
1349These functions may be used to create and manage processes.
1350
1351The various :func:`exec\*` functions take a list of arguments for the new
1352program loaded into the process. In each case, the first of these arguments is
1353passed to the new program as its own name rather than as an argument a user may
1354have typed on a command line. For the C programmer, this is the ``argv[0]``
1355passed to a program's :cfunc:`main`. For example, ``os.execv('/bin/echo',
1356['foo', 'bar'])`` will only print ``bar`` on standard output; ``foo`` will seem
1357to be ignored.
1358
1359
1360.. function:: abort()
1361
1362 Generate a :const:`SIGABRT` signal to the current process. On Unix, the default
1363 behavior is to produce a core dump; on Windows, the process immediately returns
1364 an exit code of ``3``. Be aware that programs which use :func:`signal.signal`
1365 to register a handler for :const:`SIGABRT` will behave differently.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001366
Georg Brandlc575c902008-09-13 17:46:05 +00001367 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001368
1369
1370.. function:: execl(path, arg0, arg1, ...)
1371 execle(path, arg0, arg1, ..., env)
1372 execlp(file, arg0, arg1, ...)
1373 execlpe(file, arg0, arg1, ..., env)
1374 execv(path, args)
1375 execve(path, args, env)
1376 execvp(file, args)
1377 execvpe(file, args, env)
1378
1379 These functions all execute a new program, replacing the current process; they
1380 do not return. On Unix, the new executable is loaded into the current process,
Christian Heimesfaf2f632008-01-06 16:59:19 +00001381 and will have the same process id as the caller. Errors will be reported as
Georg Brandl48310cd2009-01-03 21:18:54 +00001382 :exc:`OSError` exceptions.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001383
1384 The current process is replaced immediately. Open file objects and
1385 descriptors are not flushed, so if there may be data buffered
1386 on these open files, you should flush them using
1387 :func:`sys.stdout.flush` or :func:`os.fsync` before calling an
1388 :func:`exec\*` function.
Georg Brandl116aa622007-08-15 14:28:22 +00001389
Christian Heimesfaf2f632008-01-06 16:59:19 +00001390 The "l" and "v" variants of the :func:`exec\*` functions differ in how
1391 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001392 to work with if the number of parameters is fixed when the code is written; the
1393 individual parameters simply become additional parameters to the :func:`execl\*`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001394 functions. The "v" variants are good when the number of parameters is
Georg Brandl116aa622007-08-15 14:28:22 +00001395 variable, with the arguments being passed in a list or tuple as the *args*
1396 parameter. In either case, the arguments to the child process should start with
1397 the name of the command being run, but this is not enforced.
1398
Christian Heimesfaf2f632008-01-06 16:59:19 +00001399 The variants which include a "p" near the end (:func:`execlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001400 :func:`execlpe`, :func:`execvp`, and :func:`execvpe`) will use the
1401 :envvar:`PATH` environment variable to locate the program *file*. When the
1402 environment is being replaced (using one of the :func:`exec\*e` variants,
1403 discussed in the next paragraph), the new environment is used as the source of
1404 the :envvar:`PATH` variable. The other variants, :func:`execl`, :func:`execle`,
1405 :func:`execv`, and :func:`execve`, will not use the :envvar:`PATH` variable to
1406 locate the executable; *path* must contain an appropriate absolute or relative
1407 path.
1408
1409 For :func:`execle`, :func:`execlpe`, :func:`execve`, and :func:`execvpe` (note
Christian Heimesfaf2f632008-01-06 16:59:19 +00001410 that these all end in "e"), the *env* parameter must be a mapping which is
Christian Heimesa342c012008-04-20 21:01:16 +00001411 used to define the environment variables for the new process (these are used
1412 instead of the current process' environment); the functions :func:`execl`,
Georg Brandl116aa622007-08-15 14:28:22 +00001413 :func:`execlp`, :func:`execv`, and :func:`execvp` all cause the new process to
Georg Brandl48310cd2009-01-03 21:18:54 +00001414 inherit the environment of the current process.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001415
1416 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001417
1418
1419.. function:: _exit(n)
1420
1421 Exit to the system with status *n*, without calling cleanup handlers, flushing
Benjamin Petersond91203b2010-05-06 23:20:40 +00001422 stdio buffers, etc.
1423
1424 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001425
1426 .. note::
1427
1428 The standard way to exit is ``sys.exit(n)``. :func:`_exit` should normally only
1429 be used in the child process after a :func:`fork`.
1430
Christian Heimesfaf2f632008-01-06 16:59:19 +00001431The following exit codes are defined and can be used with :func:`_exit`,
Georg Brandl116aa622007-08-15 14:28:22 +00001432although they are not required. These are typically used for system programs
1433written in Python, such as a mail server's external command delivery program.
1434
1435.. note::
1436
1437 Some of these may not be available on all Unix platforms, since there is some
1438 variation. These constants are defined where they are defined by the underlying
1439 platform.
1440
1441
1442.. data:: EX_OK
1443
Benjamin Petersond91203b2010-05-06 23:20:40 +00001444 Exit code that means no error occurred.
1445
1446 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001447
Georg Brandl116aa622007-08-15 14:28:22 +00001448
1449.. data:: EX_USAGE
1450
1451 Exit code that means the command was used incorrectly, such as when the wrong
Benjamin Petersond91203b2010-05-06 23:20:40 +00001452 number of arguments are given.
1453
1454 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001455
Georg Brandl116aa622007-08-15 14:28:22 +00001456
1457.. data:: EX_DATAERR
1458
Benjamin Petersond91203b2010-05-06 23:20:40 +00001459 Exit code that means the input data was incorrect.
1460
1461 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001462
Georg Brandl116aa622007-08-15 14:28:22 +00001463
1464.. data:: EX_NOINPUT
1465
1466 Exit code that means an input file did not exist or was not readable.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001467
Georg Brandlc575c902008-09-13 17:46:05 +00001468 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001469
Georg Brandl116aa622007-08-15 14:28:22 +00001470
1471.. data:: EX_NOUSER
1472
Benjamin Petersond91203b2010-05-06 23:20:40 +00001473 Exit code that means a specified user did not exist.
1474
1475 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001476
Georg Brandl116aa622007-08-15 14:28:22 +00001477
1478.. data:: EX_NOHOST
1479
Benjamin Petersond91203b2010-05-06 23:20:40 +00001480 Exit code that means a specified host did not exist.
1481
1482 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001483
Georg Brandl116aa622007-08-15 14:28:22 +00001484
1485.. data:: EX_UNAVAILABLE
1486
Benjamin Petersond91203b2010-05-06 23:20:40 +00001487 Exit code that means that a required service is unavailable.
1488
1489 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001490
Georg Brandl116aa622007-08-15 14:28:22 +00001491
1492.. data:: EX_SOFTWARE
1493
Benjamin Petersond91203b2010-05-06 23:20:40 +00001494 Exit code that means an internal software error was detected.
1495
1496 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001497
Georg Brandl116aa622007-08-15 14:28:22 +00001498
1499.. data:: EX_OSERR
1500
1501 Exit code that means an operating system error was detected, such as the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001502 inability to fork or create a pipe.
1503
1504 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001505
Georg Brandl116aa622007-08-15 14:28:22 +00001506
1507.. data:: EX_OSFILE
1508
1509 Exit code that means some system file did not exist, could not be opened, or had
Benjamin Petersond91203b2010-05-06 23:20:40 +00001510 some other kind of error.
1511
1512 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001513
Georg Brandl116aa622007-08-15 14:28:22 +00001514
1515.. data:: EX_CANTCREAT
1516
1517 Exit code that means a user specified output file could not be created.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001518
Georg Brandlc575c902008-09-13 17:46:05 +00001519 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001520
Georg Brandl116aa622007-08-15 14:28:22 +00001521
1522.. data:: EX_IOERR
1523
1524 Exit code that means that an error occurred while doing I/O on some file.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001525
Georg Brandlc575c902008-09-13 17:46:05 +00001526 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001527
Georg Brandl116aa622007-08-15 14:28:22 +00001528
1529.. data:: EX_TEMPFAIL
1530
1531 Exit code that means a temporary failure occurred. This indicates something
1532 that may not really be an error, such as a network connection that couldn't be
Benjamin Petersond91203b2010-05-06 23:20:40 +00001533 made during a retryable operation.
1534
1535 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001536
Georg Brandl116aa622007-08-15 14:28:22 +00001537
1538.. data:: EX_PROTOCOL
1539
1540 Exit code that means that a protocol exchange was illegal, invalid, or not
Benjamin Petersond91203b2010-05-06 23:20:40 +00001541 understood.
1542
1543 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001544
Georg Brandl116aa622007-08-15 14:28:22 +00001545
1546.. data:: EX_NOPERM
1547
1548 Exit code that means that there were insufficient permissions to perform the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001549 operation (but not intended for file system problems).
1550
1551 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001552
Georg Brandl116aa622007-08-15 14:28:22 +00001553
1554.. data:: EX_CONFIG
1555
1556 Exit code that means that some kind of configuration error occurred.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001557
Georg Brandlc575c902008-09-13 17:46:05 +00001558 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001559
Georg Brandl116aa622007-08-15 14:28:22 +00001560
1561.. data:: EX_NOTFOUND
1562
Benjamin Petersond91203b2010-05-06 23:20:40 +00001563 Exit code that means something like "an entry was not found".
1564
1565 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001566
Georg Brandl116aa622007-08-15 14:28:22 +00001567
1568.. function:: fork()
1569
Christian Heimesfaf2f632008-01-06 16:59:19 +00001570 Fork a child process. Return ``0`` in the child and the child's process id in the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001571 parent. If an error occurs :exc:`OSError` is raised.
Benjamin Petersonbcd8ac32008-10-10 22:20:52 +00001572
1573 Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have
1574 known issues when using fork() from a thread.
1575
Georg Brandlc575c902008-09-13 17:46:05 +00001576 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001577
1578
1579.. function:: forkpty()
1580
1581 Fork a child process, using a new pseudo-terminal as the child's controlling
1582 terminal. Return a pair of ``(pid, fd)``, where *pid* is ``0`` in the child, the
1583 new child's process id in the parent, and *fd* is the file descriptor of the
1584 master end of the pseudo-terminal. For a more portable approach, use the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001585 :mod:`pty` module. If an error occurs :exc:`OSError` is raised.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001586
Georg Brandlc575c902008-09-13 17:46:05 +00001587 Availability: some flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001588
1589
1590.. function:: kill(pid, sig)
1591
1592 .. index::
1593 single: process; killing
1594 single: process; signalling
1595
1596 Send signal *sig* to the process *pid*. Constants for the specific signals
1597 available on the host platform are defined in the :mod:`signal` module.
Georg Brandlc575c902008-09-13 17:46:05 +00001598 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001599
1600
1601.. function:: killpg(pgid, sig)
1602
1603 .. index::
1604 single: process; killing
1605 single: process; signalling
1606
Benjamin Petersond91203b2010-05-06 23:20:40 +00001607 Send the signal *sig* to the process group *pgid*.
1608
1609 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001610
Georg Brandl116aa622007-08-15 14:28:22 +00001611
1612.. function:: nice(increment)
1613
1614 Add *increment* to the process's "niceness". Return the new niceness.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001615
Georg Brandlc575c902008-09-13 17:46:05 +00001616 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001617
1618
1619.. function:: plock(op)
1620
1621 Lock program segments into memory. The value of *op* (defined in
Benjamin Petersond91203b2010-05-06 23:20:40 +00001622 ``<sys/lock.h>``) determines which segments are locked.
1623
1624 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001625
1626
1627.. function:: popen(...)
1628 :noindex:
1629
1630 Run child processes, returning opened pipes for communications. These functions
1631 are described in section :ref:`os-newstreams`.
1632
1633
1634.. function:: spawnl(mode, path, ...)
1635 spawnle(mode, path, ..., env)
1636 spawnlp(mode, file, ...)
1637 spawnlpe(mode, file, ..., env)
1638 spawnv(mode, path, args)
1639 spawnve(mode, path, args, env)
1640 spawnvp(mode, file, args)
1641 spawnvpe(mode, file, args, env)
1642
1643 Execute the program *path* in a new process.
1644
1645 (Note that the :mod:`subprocess` module provides more powerful facilities for
1646 spawning new processes and retrieving their results; using that module is
Benjamin Peterson87c8d872009-06-11 22:54:11 +00001647 preferable to using these functions. Check especially the
1648 :ref:`subprocess-replacements` section.)
Georg Brandl116aa622007-08-15 14:28:22 +00001649
Christian Heimesfaf2f632008-01-06 16:59:19 +00001650 If *mode* is :const:`P_NOWAIT`, this function returns the process id of the new
Georg Brandl116aa622007-08-15 14:28:22 +00001651 process; if *mode* is :const:`P_WAIT`, returns the process's exit code if it
1652 exits normally, or ``-signal``, where *signal* is the signal that killed the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001653 process. On Windows, the process id will actually be the process handle, so can
Georg Brandl116aa622007-08-15 14:28:22 +00001654 be used with the :func:`waitpid` function.
1655
Christian Heimesfaf2f632008-01-06 16:59:19 +00001656 The "l" and "v" variants of the :func:`spawn\*` functions differ in how
1657 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001658 to work with if the number of parameters is fixed when the code is written; the
1659 individual parameters simply become additional parameters to the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001660 :func:`spawnl\*` functions. The "v" variants are good when the number of
Georg Brandl116aa622007-08-15 14:28:22 +00001661 parameters is variable, with the arguments being passed in a list or tuple as
1662 the *args* parameter. In either case, the arguments to the child process must
1663 start with the name of the command being run.
1664
Christian Heimesfaf2f632008-01-06 16:59:19 +00001665 The variants which include a second "p" near the end (:func:`spawnlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001666 :func:`spawnlpe`, :func:`spawnvp`, and :func:`spawnvpe`) will use the
1667 :envvar:`PATH` environment variable to locate the program *file*. When the
1668 environment is being replaced (using one of the :func:`spawn\*e` variants,
1669 discussed in the next paragraph), the new environment is used as the source of
1670 the :envvar:`PATH` variable. The other variants, :func:`spawnl`,
1671 :func:`spawnle`, :func:`spawnv`, and :func:`spawnve`, will not use the
1672 :envvar:`PATH` variable to locate the executable; *path* must contain an
1673 appropriate absolute or relative path.
1674
1675 For :func:`spawnle`, :func:`spawnlpe`, :func:`spawnve`, and :func:`spawnvpe`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001676 (note that these all end in "e"), the *env* parameter must be a mapping
Christian Heimesa342c012008-04-20 21:01:16 +00001677 which is used to define the environment variables for the new process (they are
1678 used instead of the current process' environment); the functions
Georg Brandl116aa622007-08-15 14:28:22 +00001679 :func:`spawnl`, :func:`spawnlp`, :func:`spawnv`, and :func:`spawnvp` all cause
Benjamin Petersond23f8222009-04-05 19:13:16 +00001680 the new process to inherit the environment of the current process. Note that
1681 keys and values in the *env* dictionary must be strings; invalid keys or
1682 values will cause the function to fail, with a return value of ``127``.
Georg Brandl116aa622007-08-15 14:28:22 +00001683
1684 As an example, the following calls to :func:`spawnlp` and :func:`spawnvpe` are
1685 equivalent::
1686
1687 import os
1688 os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1689
1690 L = ['cp', 'index.html', '/dev/null']
1691 os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1692
1693 Availability: Unix, Windows. :func:`spawnlp`, :func:`spawnlpe`, :func:`spawnvp`
1694 and :func:`spawnvpe` are not available on Windows.
1695
Georg Brandl116aa622007-08-15 14:28:22 +00001696
1697.. data:: P_NOWAIT
1698 P_NOWAITO
1699
1700 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1701 functions. If either of these values is given, the :func:`spawn\*` functions
Christian Heimesfaf2f632008-01-06 16:59:19 +00001702 will return as soon as the new process has been created, with the process id as
Benjamin Petersond91203b2010-05-06 23:20:40 +00001703 the return value.
1704
1705 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001706
Georg Brandl116aa622007-08-15 14:28:22 +00001707
1708.. data:: P_WAIT
1709
1710 Possible value for the *mode* parameter to the :func:`spawn\*` family of
1711 functions. If this is given as *mode*, the :func:`spawn\*` functions will not
1712 return until the new process has run to completion and will return the exit code
1713 of the process the run is successful, or ``-signal`` if a signal kills the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001714 process.
1715
1716 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001717
Georg Brandl116aa622007-08-15 14:28:22 +00001718
1719.. data:: P_DETACH
1720 P_OVERLAY
1721
1722 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1723 functions. These are less portable than those listed above. :const:`P_DETACH`
1724 is similar to :const:`P_NOWAIT`, but the new process is detached from the
1725 console of the calling process. If :const:`P_OVERLAY` is used, the current
1726 process will be replaced; the :func:`spawn\*` function will not return.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001727
Georg Brandl116aa622007-08-15 14:28:22 +00001728 Availability: Windows.
1729
Georg Brandl116aa622007-08-15 14:28:22 +00001730
1731.. function:: startfile(path[, operation])
1732
1733 Start a file with its associated application.
1734
1735 When *operation* is not specified or ``'open'``, this acts like double-clicking
1736 the file in Windows Explorer, or giving the file name as an argument to the
1737 :program:`start` command from the interactive command shell: the file is opened
1738 with whatever application (if any) its extension is associated.
1739
1740 When another *operation* is given, it must be a "command verb" that specifies
1741 what should be done with the file. Common verbs documented by Microsoft are
1742 ``'print'`` and ``'edit'`` (to be used on files) as well as ``'explore'`` and
1743 ``'find'`` (to be used on directories).
1744
1745 :func:`startfile` returns as soon as the associated application is launched.
1746 There is no option to wait for the application to close, and no way to retrieve
1747 the application's exit status. The *path* parameter is relative to the current
1748 directory. If you want to use an absolute path, make sure the first character
1749 is not a slash (``'/'``); the underlying Win32 :cfunc:`ShellExecute` function
1750 doesn't work if it is. Use the :func:`os.path.normpath` function to ensure that
Benjamin Petersond91203b2010-05-06 23:20:40 +00001751 the path is properly encoded for Win32.
1752
1753 Availability: Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001754
Georg Brandl116aa622007-08-15 14:28:22 +00001755
1756.. function:: system(command)
1757
1758 Execute the command (a string) in a subshell. This is implemented by calling
Georg Brandl628e6f92009-10-27 20:24:45 +00001759 the Standard C function :cfunc:`system`, and has the same limitations.
1760 Changes to :data:`sys.stdin`, etc. are not reflected in the environment of the
1761 executed command.
Georg Brandl116aa622007-08-15 14:28:22 +00001762
1763 On Unix, the return value is the exit status of the process encoded in the
1764 format specified for :func:`wait`. Note that POSIX does not specify the meaning
1765 of the return value of the C :cfunc:`system` function, so the return value of
1766 the Python function is system-dependent.
1767
1768 On Windows, the return value is that returned by the system shell after running
1769 *command*, given by the Windows environment variable :envvar:`COMSPEC`: on
1770 :program:`command.com` systems (Windows 95, 98 and ME) this is always ``0``; on
1771 :program:`cmd.exe` systems (Windows NT, 2000 and XP) this is the exit status of
1772 the command run; on systems using a non-native shell, consult your shell
1773 documentation.
1774
Georg Brandl116aa622007-08-15 14:28:22 +00001775 The :mod:`subprocess` module provides more powerful facilities for spawning new
1776 processes and retrieving their results; using that module is preferable to using
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001777 this function. Use the :mod:`subprocess` module. Check especially the
1778 :ref:`subprocess-replacements` section.
Georg Brandl116aa622007-08-15 14:28:22 +00001779
Benjamin Petersond91203b2010-05-06 23:20:40 +00001780 Availability: Unix, Windows.
1781
Georg Brandl116aa622007-08-15 14:28:22 +00001782
1783.. function:: times()
1784
Benjamin Petersond91203b2010-05-06 23:20:40 +00001785 Return a 5-tuple of floating point numbers indicating accumulated (processor
1786 or other) times, in seconds. The items are: user time, system time,
1787 children's user time, children's system time, and elapsed real time since a
1788 fixed point in the past, in that order. See the Unix manual page
1789 :manpage:`times(2)` or the corresponding Windows Platform API documentation.
1790 On Windows, only the first two items are filled, the others are zero.
1791
1792 Availability: Unix, Windows
Georg Brandl116aa622007-08-15 14:28:22 +00001793
1794
1795.. function:: wait()
1796
1797 Wait for completion of a child process, and return a tuple containing its pid
1798 and exit status indication: a 16-bit number, whose low byte is the signal number
1799 that killed the process, and whose high byte is the exit status (if the signal
1800 number is zero); the high bit of the low byte is set if a core file was
Benjamin Petersond91203b2010-05-06 23:20:40 +00001801 produced.
1802
1803 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001804
1805
1806.. function:: waitpid(pid, options)
1807
1808 The details of this function differ on Unix and Windows.
1809
1810 On Unix: Wait for completion of a child process given by process id *pid*, and
1811 return a tuple containing its process id and exit status indication (encoded as
1812 for :func:`wait`). The semantics of the call are affected by the value of the
1813 integer *options*, which should be ``0`` for normal operation.
1814
1815 If *pid* is greater than ``0``, :func:`waitpid` requests status information for
1816 that specific process. If *pid* is ``0``, the request is for the status of any
1817 child in the process group of the current process. If *pid* is ``-1``, the
1818 request pertains to any child of the current process. If *pid* is less than
1819 ``-1``, status is requested for any process in the process group ``-pid`` (the
1820 absolute value of *pid*).
1821
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001822 An :exc:`OSError` is raised with the value of errno when the syscall
1823 returns -1.
1824
Georg Brandl116aa622007-08-15 14:28:22 +00001825 On Windows: Wait for completion of a process given by process handle *pid*, and
1826 return a tuple containing *pid*, and its exit status shifted left by 8 bits
1827 (shifting makes cross-platform use of the function easier). A *pid* less than or
1828 equal to ``0`` has no special meaning on Windows, and raises an exception. The
1829 value of integer *options* has no effect. *pid* can refer to any process whose
1830 id is known, not necessarily a child process. The :func:`spawn` functions called
1831 with :const:`P_NOWAIT` return suitable process handles.
1832
1833
1834.. function:: wait3([options])
1835
1836 Similar to :func:`waitpid`, except no process id argument is given and a
1837 3-element tuple containing the child's process id, exit status indication, and
1838 resource usage information is returned. Refer to :mod:`resource`.\
1839 :func:`getrusage` for details on resource usage information. The option
1840 argument is the same as that provided to :func:`waitpid` and :func:`wait4`.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001841
Georg Brandl116aa622007-08-15 14:28:22 +00001842 Availability: Unix.
1843
Georg Brandl116aa622007-08-15 14:28:22 +00001844
1845.. function:: wait4(pid, options)
1846
1847 Similar to :func:`waitpid`, except a 3-element tuple, containing the child's
1848 process id, exit status indication, and resource usage information is returned.
1849 Refer to :mod:`resource`.\ :func:`getrusage` for details on resource usage
1850 information. The arguments to :func:`wait4` are the same as those provided to
Benjamin Petersond91203b2010-05-06 23:20:40 +00001851 :func:`waitpid`.
1852
1853 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001854
Georg Brandl116aa622007-08-15 14:28:22 +00001855
1856.. data:: WNOHANG
1857
1858 The option for :func:`waitpid` to return immediately if no child process status
1859 is available immediately. The function returns ``(0, 0)`` in this case.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001860
Georg Brandlc575c902008-09-13 17:46:05 +00001861 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001862
1863
1864.. data:: WCONTINUED
1865
1866 This option causes child processes to be reported if they have been continued
Benjamin Petersond91203b2010-05-06 23:20:40 +00001867 from a job control stop since their status was last reported.
1868
1869 Availability: Some Unix systems.
Georg Brandl116aa622007-08-15 14:28:22 +00001870
Georg Brandl116aa622007-08-15 14:28:22 +00001871
1872.. data:: WUNTRACED
1873
1874 This option causes child processes to be reported if they have been stopped but
Benjamin Petersond91203b2010-05-06 23:20:40 +00001875 their current state has not been reported since they were stopped.
1876
1877 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001878
Georg Brandl116aa622007-08-15 14:28:22 +00001879
1880The following functions take a process status code as returned by
1881:func:`system`, :func:`wait`, or :func:`waitpid` as a parameter. They may be
1882used to determine the disposition of a process.
1883
Georg Brandl116aa622007-08-15 14:28:22 +00001884.. function:: WCOREDUMP(status)
1885
Christian Heimesfaf2f632008-01-06 16:59:19 +00001886 Return ``True`` if a core dump was generated for the process, otherwise
Benjamin Petersond91203b2010-05-06 23:20:40 +00001887 return ``False``.
1888
1889 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001890
Georg Brandl116aa622007-08-15 14:28:22 +00001891
1892.. function:: WIFCONTINUED(status)
1893
Christian Heimesfaf2f632008-01-06 16:59:19 +00001894 Return ``True`` if the process has been continued from a job control stop,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001895 otherwise return ``False``.
1896
1897 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001898
Georg Brandl116aa622007-08-15 14:28:22 +00001899
1900.. function:: WIFSTOPPED(status)
1901
Christian Heimesfaf2f632008-01-06 16:59:19 +00001902 Return ``True`` if the process has been stopped, otherwise return
Benjamin Petersond91203b2010-05-06 23:20:40 +00001903 ``False``.
1904
1905 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001906
1907
1908.. function:: WIFSIGNALED(status)
1909
Christian Heimesfaf2f632008-01-06 16:59:19 +00001910 Return ``True`` if the process exited due to a signal, otherwise return
Benjamin Petersond91203b2010-05-06 23:20:40 +00001911 ``False``.
1912
1913 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001914
1915
1916.. function:: WIFEXITED(status)
1917
Christian Heimesfaf2f632008-01-06 16:59:19 +00001918 Return ``True`` if the process exited using the :manpage:`exit(2)` system call,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001919 otherwise return ``False``.
1920
1921 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001922
1923
1924.. function:: WEXITSTATUS(status)
1925
1926 If ``WIFEXITED(status)`` is true, return the integer parameter to the
1927 :manpage:`exit(2)` system call. Otherwise, the return value is meaningless.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001928
Georg Brandlc575c902008-09-13 17:46:05 +00001929 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001930
1931
1932.. function:: WSTOPSIG(status)
1933
Benjamin Petersond91203b2010-05-06 23:20:40 +00001934 Return the signal which caused the process to stop.
1935
1936 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001937
1938
1939.. function:: WTERMSIG(status)
1940
Benjamin Petersond91203b2010-05-06 23:20:40 +00001941 Return the signal which caused the process to exit.
1942
1943 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001944
1945
1946.. _os-path:
1947
1948Miscellaneous System Information
1949--------------------------------
1950
1951
1952.. function:: confstr(name)
1953
1954 Return string-valued system configuration values. *name* specifies the
1955 configuration value to retrieve; it may be a string which is the name of a
1956 defined system value; these names are specified in a number of standards (POSIX,
1957 Unix 95, Unix 98, and others). Some platforms define additional names as well.
1958 The names known to the host operating system are given as the keys of the
1959 ``confstr_names`` dictionary. For configuration variables not included in that
Benjamin Petersond91203b2010-05-06 23:20:40 +00001960 mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +00001961
1962 If the configuration value specified by *name* isn't defined, ``None`` is
1963 returned.
1964
1965 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1966 specific value for *name* is not supported by the host system, even if it is
1967 included in ``confstr_names``, an :exc:`OSError` is raised with
1968 :const:`errno.EINVAL` for the error number.
1969
Benjamin Petersond91203b2010-05-06 23:20:40 +00001970 Availability: Unix
1971
Georg Brandl116aa622007-08-15 14:28:22 +00001972
1973.. data:: confstr_names
1974
1975 Dictionary mapping names accepted by :func:`confstr` to the integer values
1976 defined for those names by the host operating system. This can be used to
Benjamin Petersond91203b2010-05-06 23:20:40 +00001977 determine the set of names known to the system.
1978
1979 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001980
1981
1982.. function:: getloadavg()
1983
Christian Heimesa62da1d2008-01-12 19:39:10 +00001984 Return the number of processes in the system run queue averaged over the last
1985 1, 5, and 15 minutes or raises :exc:`OSError` if the load average was
Benjamin Petersond91203b2010-05-06 23:20:40 +00001986 unobtainable.
1987
1988 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001989
Georg Brandl116aa622007-08-15 14:28:22 +00001990
1991.. function:: sysconf(name)
1992
1993 Return integer-valued system configuration values. If the configuration value
1994 specified by *name* isn't defined, ``-1`` is returned. The comments regarding
1995 the *name* parameter for :func:`confstr` apply here as well; the dictionary that
1996 provides information on the known names is given by ``sysconf_names``.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001997
Georg Brandlc575c902008-09-13 17:46:05 +00001998 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001999
2000
2001.. data:: sysconf_names
2002
2003 Dictionary mapping names accepted by :func:`sysconf` to the integer values
2004 defined for those names by the host operating system. This can be used to
Benjamin Petersond91203b2010-05-06 23:20:40 +00002005 determine the set of names known to the system.
2006
2007 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002008
Christian Heimesfaf2f632008-01-06 16:59:19 +00002009The following data values are used to support path manipulation operations. These
Georg Brandl116aa622007-08-15 14:28:22 +00002010are defined for all platforms.
2011
2012Higher-level operations on pathnames are defined in the :mod:`os.path` module.
2013
2014
2015.. data:: curdir
2016
2017 The constant string used by the operating system to refer to the current
Georg Brandlc575c902008-09-13 17:46:05 +00002018 directory. This is ``'.'`` for Windows and POSIX. Also available via
2019 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002020
2021
2022.. data:: pardir
2023
2024 The constant string used by the operating system to refer to the parent
Georg Brandlc575c902008-09-13 17:46:05 +00002025 directory. This is ``'..'`` for Windows and POSIX. Also available via
2026 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002027
2028
2029.. data:: sep
2030
Georg Brandlc575c902008-09-13 17:46:05 +00002031 The character used by the operating system to separate pathname components.
2032 This is ``'/'`` for POSIX and ``'\\'`` for Windows. Note that knowing this
2033 is not sufficient to be able to parse or concatenate pathnames --- use
Georg Brandl116aa622007-08-15 14:28:22 +00002034 :func:`os.path.split` and :func:`os.path.join` --- but it is occasionally
2035 useful. Also available via :mod:`os.path`.
2036
2037
2038.. data:: altsep
2039
2040 An alternative character used by the operating system to separate pathname
2041 components, or ``None`` if only one separator character exists. This is set to
2042 ``'/'`` on Windows systems where ``sep`` is a backslash. Also available via
2043 :mod:`os.path`.
2044
2045
2046.. data:: extsep
2047
2048 The character which separates the base filename from the extension; for example,
2049 the ``'.'`` in :file:`os.py`. Also available via :mod:`os.path`.
2050
Georg Brandl116aa622007-08-15 14:28:22 +00002051
2052.. data:: pathsep
2053
2054 The character conventionally used by the operating system to separate search
2055 path components (as in :envvar:`PATH`), such as ``':'`` for POSIX or ``';'`` for
2056 Windows. Also available via :mod:`os.path`.
2057
2058
2059.. data:: defpath
2060
2061 The default search path used by :func:`exec\*p\*` and :func:`spawn\*p\*` if the
2062 environment doesn't have a ``'PATH'`` key. Also available via :mod:`os.path`.
2063
2064
2065.. data:: linesep
2066
2067 The string used to separate (or, rather, terminate) lines on the current
Georg Brandlc575c902008-09-13 17:46:05 +00002068 platform. This may be a single character, such as ``'\n'`` for POSIX, or
2069 multiple characters, for example, ``'\r\n'`` for Windows. Do not use
2070 *os.linesep* as a line terminator when writing files opened in text mode (the
2071 default); use a single ``'\n'`` instead, on all platforms.
Georg Brandl116aa622007-08-15 14:28:22 +00002072
2073
2074.. data:: devnull
2075
Georg Brandlc52eeab2010-05-21 22:05:15 +00002076 The file path of the null device. For example: ``'/dev/null'`` for
2077 POSIX, ``'nul'`` for Windows. Also available via :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002078
Georg Brandl116aa622007-08-15 14:28:22 +00002079
2080.. _os-miscfunc:
2081
2082Miscellaneous Functions
2083-----------------------
2084
2085
2086.. function:: urandom(n)
2087
2088 Return a string of *n* random bytes suitable for cryptographic use.
2089
2090 This function returns random bytes from an OS-specific randomness source. The
2091 returned data should be unpredictable enough for cryptographic applications,
2092 though its exact quality depends on the OS implementation. On a UNIX-like
2093 system this will query /dev/urandom, and on Windows it will use CryptGenRandom.
2094 If a randomness source is not found, :exc:`NotImplementedError` will be raised.