blob: 9635d8aee2cd51e89b22ee925e0b6d9d7c2b6eba [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 Set the current process's real and effective user ids.
315
316 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000317
318
319.. function:: setregid(rgid, egid)
320
Benjamin Petersond91203b2010-05-06 23:20:40 +0000321 Set the current process's real and effective group ids.
322
323 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000324
325
326.. function:: getsid(pid)
327
Christian Heimesfaf2f632008-01-06 16:59:19 +0000328 Call the system call :cfunc:`getsid`. See the Unix manual for the semantics.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000329
Georg Brandl116aa622007-08-15 14:28:22 +0000330 Availability: Unix.
331
Georg Brandl116aa622007-08-15 14:28:22 +0000332
333.. function:: setsid()
334
Christian Heimesfaf2f632008-01-06 16:59:19 +0000335 Call the system call :cfunc:`setsid`. See the Unix manual for the semantics.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000336
Georg Brandl116aa622007-08-15 14:28:22 +0000337 Availability: Unix.
338
339
340.. function:: setuid(uid)
341
342 .. index:: single: user; id, setting
343
Benjamin Petersond91203b2010-05-06 23:20:40 +0000344 Set the current process's user id.
345
346 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000347
Georg Brandl116aa622007-08-15 14:28:22 +0000348
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000349.. placed in this section since it relates to errno.... a little weak
Georg Brandl116aa622007-08-15 14:28:22 +0000350.. function:: strerror(code)
351
352 Return the error message corresponding to the error code in *code*.
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000353 On platforms where :cfunc:`strerror` returns ``NULL`` when given an unknown
Benjamin Petersond91203b2010-05-06 23:20:40 +0000354 error number, :exc:`ValueError` is raised.
355
356 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000357
358
359.. function:: umask(mask)
360
Benjamin Petersond91203b2010-05-06 23:20:40 +0000361 Set the current numeric umask and return the previous umask.
362
363 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000364
365
366.. function:: uname()
367
368 .. index::
369 single: gethostname() (in module socket)
370 single: gethostbyaddr() (in module socket)
371
372 Return a 5-tuple containing information identifying the current operating
373 system. The tuple contains 5 strings: ``(sysname, nodename, release, version,
374 machine)``. Some systems truncate the nodename to 8 characters or to the
375 leading component; a better way to get the hostname is
376 :func:`socket.gethostname` or even
Benjamin Petersond91203b2010-05-06 23:20:40 +0000377 ``socket.gethostbyaddr(socket.gethostname())``.
378
379 Availability: recent flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000380
381
382.. function:: unsetenv(varname)
383
384 .. index:: single: environment variables; deleting
385
386 Unset (delete) the environment variable named *varname*. Such changes to the
387 environment affect subprocesses started with :func:`os.system`, :func:`popen` or
Benjamin Petersond91203b2010-05-06 23:20:40 +0000388 :func:`fork` and :func:`execv`.
Georg Brandl116aa622007-08-15 14:28:22 +0000389
390 When :func:`unsetenv` is supported, deletion of items in ``os.environ`` is
391 automatically translated into a corresponding call to :func:`unsetenv`; however,
392 calls to :func:`unsetenv` don't update ``os.environ``, so it is actually
393 preferable to delete items of ``os.environ``.
394
Benjamin Petersond91203b2010-05-06 23:20:40 +0000395 Availability: most flavors of Unix, Windows.
396
Georg Brandl116aa622007-08-15 14:28:22 +0000397
398.. _os-newstreams:
399
400File Object Creation
401--------------------
402
Antoine Pitrou25d535e2010-09-15 11:25:11 +0000403These functions create new :term:`file objects <file object>`. (See also :func:`open`.)
Georg Brandl116aa622007-08-15 14:28:22 +0000404
405
406.. function:: fdopen(fd[, mode[, bufsize]])
407
408 .. index:: single: I/O control; buffering
409
410 Return an open file object connected to the file descriptor *fd*. The *mode*
411 and *bufsize* arguments have the same meaning as the corresponding arguments to
Benjamin Petersond91203b2010-05-06 23:20:40 +0000412 the built-in :func:`open` function.
Georg Brandl116aa622007-08-15 14:28:22 +0000413
Georg Brandl55ac8f02007-09-01 13:51:09 +0000414 When specified, the *mode* argument must start with one of the letters
415 ``'r'``, ``'w'``, or ``'a'``, otherwise a :exc:`ValueError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000416
Georg Brandl55ac8f02007-09-01 13:51:09 +0000417 On Unix, when the *mode* argument starts with ``'a'``, the *O_APPEND* flag is
418 set on the file descriptor (which the :cfunc:`fdopen` implementation already
419 does on most platforms).
Georg Brandl116aa622007-08-15 14:28:22 +0000420
Benjamin Petersond91203b2010-05-06 23:20:40 +0000421 Availability: Unix, Windows.
422
Georg Brandl116aa622007-08-15 14:28:22 +0000423
Georg Brandl116aa622007-08-15 14:28:22 +0000424.. _os-fd-ops:
425
426File Descriptor Operations
427--------------------------
428
429These functions operate on I/O streams referenced using file descriptors.
430
431File descriptors are small integers corresponding to a file that has been opened
432by the current process. For example, standard input is usually file descriptor
4330, standard output is 1, and standard error is 2. Further files opened by a
434process will then be assigned 3, 4, 5, and so forth. The name "file descriptor"
435is slightly deceptive; on Unix platforms, sockets and pipes are also referenced
436by file descriptors.
437
Antoine Pitrou25d535e2010-09-15 11:25:11 +0000438The :meth:`~file.fileno` method can be used to obtain the file descriptor
439associated with a :term:`file object` when required. Note that using the file
440descriptor directly will bypass the file object methods, ignoring aspects such
441as internal buffering of data.
Georg Brandl116aa622007-08-15 14:28:22 +0000442
443.. function:: close(fd)
444
Benjamin Petersond91203b2010-05-06 23:20:40 +0000445 Close file descriptor *fd*.
446
447 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000448
449 .. note::
450
451 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000452 descriptor as returned by :func:`os.open` or :func:`pipe`. To close a "file
Georg Brandl116aa622007-08-15 14:28:22 +0000453 object" returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000454 :func:`fdopen`, use its :meth:`~file.close` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000455
456
Christian Heimesfdab48e2008-01-20 09:06:41 +0000457.. function:: closerange(fd_low, fd_high)
458
459 Close all file descriptors from *fd_low* (inclusive) to *fd_high* (exclusive),
Benjamin Petersond91203b2010-05-06 23:20:40 +0000460 ignoring errors. Equivalent to::
Christian Heimesfdab48e2008-01-20 09:06:41 +0000461
Georg Brandl7baf6252009-09-01 08:13:16 +0000462 for fd in range(fd_low, fd_high):
Christian Heimesfdab48e2008-01-20 09:06:41 +0000463 try:
464 os.close(fd)
465 except OSError:
466 pass
467
Benjamin Petersond91203b2010-05-06 23:20:40 +0000468 Availability: Unix, Windows.
469
Christian Heimesfdab48e2008-01-20 09:06:41 +0000470
Georg Brandl81f11302007-12-21 08:45:42 +0000471.. function:: device_encoding(fd)
472
473 Return a string describing the encoding of the device associated with *fd*
474 if it is connected to a terminal; else return :const:`None`.
475
476
Georg Brandl116aa622007-08-15 14:28:22 +0000477.. function:: dup(fd)
478
Benjamin Petersond91203b2010-05-06 23:20:40 +0000479 Return a duplicate of file descriptor *fd*.
480
481 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000482
483
484.. function:: dup2(fd, fd2)
485
486 Duplicate file descriptor *fd* to *fd2*, closing the latter first if necessary.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000487
Georg Brandlc575c902008-09-13 17:46:05 +0000488 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000489
490
Christian Heimes4e30a842007-11-30 22:12:06 +0000491.. function:: fchmod(fd, mode)
492
493 Change the mode of the file given by *fd* to the numeric *mode*. See the docs
Benjamin Petersond91203b2010-05-06 23:20:40 +0000494 for :func:`chmod` for possible values of *mode*.
495
496 Availability: Unix.
Christian Heimes4e30a842007-11-30 22:12:06 +0000497
498
499.. function:: fchown(fd, uid, gid)
500
501 Change the owner and group id of the file given by *fd* to the numeric *uid*
502 and *gid*. To leave one of the ids unchanged, set it to -1.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000503
Christian Heimes4e30a842007-11-30 22:12:06 +0000504 Availability: Unix.
505
506
Georg Brandl116aa622007-08-15 14:28:22 +0000507.. function:: fdatasync(fd)
508
509 Force write of file with filedescriptor *fd* to disk. Does not force update of
Benjamin Petersond91203b2010-05-06 23:20:40 +0000510 metadata.
511
512 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000513
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000514 .. note::
515 This function is not available on MacOS.
516
Georg Brandl116aa622007-08-15 14:28:22 +0000517
518.. function:: fpathconf(fd, name)
519
520 Return system configuration information relevant to an open file. *name*
521 specifies the configuration value to retrieve; it may be a string which is the
522 name of a defined system value; these names are specified in a number of
523 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
524 additional names as well. The names known to the host operating system are
525 given in the ``pathconf_names`` dictionary. For configuration variables not
526 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +0000527
528 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
529 specific value for *name* is not supported by the host system, even if it is
530 included in ``pathconf_names``, an :exc:`OSError` is raised with
531 :const:`errno.EINVAL` for the error number.
532
Benjamin Petersond91203b2010-05-06 23:20:40 +0000533 Availability: Unix.
534
Georg Brandl116aa622007-08-15 14:28:22 +0000535
536.. function:: fstat(fd)
537
Benjamin Petersond91203b2010-05-06 23:20:40 +0000538 Return status for file descriptor *fd*, like :func:`stat`.
539
540 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000541
542
543.. function:: fstatvfs(fd)
544
545 Return information about the filesystem containing the file associated with file
Benjamin Petersond91203b2010-05-06 23:20:40 +0000546 descriptor *fd*, like :func:`statvfs`.
547
548 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000549
550
551.. function:: fsync(fd)
552
553 Force write of file with filedescriptor *fd* to disk. On Unix, this calls the
554 native :cfunc:`fsync` function; on Windows, the MS :cfunc:`_commit` function.
555
Antoine Pitrou25d535e2010-09-15 11:25:11 +0000556 If you're starting with a buffered Python :term:`file object` *f*, first do
557 ``f.flush()``, and then do ``os.fsync(f.fileno())``, to ensure that all internal
558 buffers associated with *f* are written to disk.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000559
560 Availability: Unix, and Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000561
562
563.. function:: ftruncate(fd, length)
564
565 Truncate the file corresponding to file descriptor *fd*, so that it is at most
Benjamin Petersond91203b2010-05-06 23:20:40 +0000566 *length* bytes in size.
567
568 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000569
570
571.. function:: isatty(fd)
572
573 Return ``True`` if the file descriptor *fd* is open and connected to a
Benjamin Petersond91203b2010-05-06 23:20:40 +0000574 tty(-like) device, else ``False``.
575
576 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000577
578
579.. function:: lseek(fd, pos, how)
580
Christian Heimesfaf2f632008-01-06 16:59:19 +0000581 Set the current position of file descriptor *fd* to position *pos*, modified
582 by *how*: :const:`SEEK_SET` or ``0`` to set the position relative to the
583 beginning of the file; :const:`SEEK_CUR` or ``1`` to set it relative to the
584 current position; :const:`os.SEEK_END` or ``2`` to set it relative to the end of
Benjamin Petersond91203b2010-05-06 23:20:40 +0000585 the file.
586
587 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000588
589
Georg Brandl6c8583f2010-05-19 21:22:58 +0000590.. data:: SEEK_SET
591 SEEK_CUR
592 SEEK_END
593
594 Parameters to the :func:`lseek` function. Their values are 0, 1, and 2,
595 respectively. Availability: Windows, Unix.
596
597
Georg Brandl116aa622007-08-15 14:28:22 +0000598.. function:: open(file, flags[, mode])
599
Georg Brandlf4a41232008-05-26 17:55:52 +0000600 Open the file *file* and set various flags according to *flags* and possibly
601 its mode according to *mode*. The default *mode* is ``0o777`` (octal), and
602 the current umask value is first masked out. Return the file descriptor for
Benjamin Petersond91203b2010-05-06 23:20:40 +0000603 the newly opened file.
Georg Brandl116aa622007-08-15 14:28:22 +0000604
605 For a description of the flag and mode values, see the C run-time documentation;
606 flag constants (like :const:`O_RDONLY` and :const:`O_WRONLY`) are defined in
Georg Brandl6c8583f2010-05-19 21:22:58 +0000607 this module too (see :ref:`open-constants`). In particular, on Windows adding
608 :const:`O_BINARY` is needed to open files in binary mode.
Georg Brandl116aa622007-08-15 14:28:22 +0000609
Benjamin Petersond91203b2010-05-06 23:20:40 +0000610 Availability: Unix, Windows.
611
Georg Brandl116aa622007-08-15 14:28:22 +0000612 .. note::
613
Georg Brandlc5605df2009-08-13 08:26:44 +0000614 This function is intended for low-level I/O. For normal usage, use the
Antoine Pitrou25d535e2010-09-15 11:25:11 +0000615 built-in function :func:`open`, which returns a :term:`file object` with
Benjamin Petersond91203b2010-05-06 23:20:40 +0000616 :meth:`~file.read` and :meth:`~file.wprite` methods (and many more). To
Antoine Pitrou25d535e2010-09-15 11:25:11 +0000617 wrap a file descriptor in a file object, use :func:`fdopen`.
Georg Brandl116aa622007-08-15 14:28:22 +0000618
619
620.. function:: openpty()
621
622 .. index:: module: pty
623
624 Open a new pseudo-terminal pair. Return a pair of file descriptors ``(master,
625 slave)`` for the pty and the tty, respectively. For a (slightly) more portable
Benjamin Petersond91203b2010-05-06 23:20:40 +0000626 approach, use the :mod:`pty` module.
627
628 Availability: some flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000629
630
631.. function:: pipe()
632
633 Create a pipe. Return a pair of file descriptors ``(r, w)`` usable for reading
Benjamin Petersond91203b2010-05-06 23:20:40 +0000634 and writing, respectively.
635
636 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000637
638
639.. function:: read(fd, n)
640
Georg Brandlc5605df2009-08-13 08:26:44 +0000641 Read at most *n* bytes from file descriptor *fd*. Return a bytestring containing the
Georg Brandl116aa622007-08-15 14:28:22 +0000642 bytes read. If the end of the file referred to by *fd* has been reached, an
Benjamin Petersond91203b2010-05-06 23:20:40 +0000643 empty bytes object is returned.
644
645 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000646
647 .. note::
648
649 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000650 descriptor as returned by :func:`os.open` or :func:`pipe`. To read a "file object"
Georg Brandl116aa622007-08-15 14:28:22 +0000651 returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000652 :func:`fdopen`, or :data:`sys.stdin`, use its :meth:`~file.read` or
653 :meth:`~file.readline` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000654
655
656.. function:: tcgetpgrp(fd)
657
658 Return the process group associated with the terminal given by *fd* (an open
Benjamin Petersond91203b2010-05-06 23:20:40 +0000659 file descriptor as returned by :func:`os.open`).
660
661 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000662
663
664.. function:: tcsetpgrp(fd, pg)
665
666 Set the process group associated with the terminal given by *fd* (an open file
Benjamin Petersond91203b2010-05-06 23:20:40 +0000667 descriptor as returned by :func:`os.open`) to *pg*.
668
669 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000670
671
672.. function:: ttyname(fd)
673
674 Return a string which specifies the terminal device associated with
Georg Brandl9afde1c2007-11-01 20:32:30 +0000675 file descriptor *fd*. If *fd* is not associated with a terminal device, an
Benjamin Petersond91203b2010-05-06 23:20:40 +0000676 exception is raised.
677
678 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000679
680
681.. function:: write(fd, str)
682
Georg Brandlc5605df2009-08-13 08:26:44 +0000683 Write the bytestring in *str* to file descriptor *fd*. Return the number of
Benjamin Petersond91203b2010-05-06 23:20:40 +0000684 bytes actually written.
685
686 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000687
688 .. note::
689
690 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000691 descriptor as returned by :func:`os.open` or :func:`pipe`. To write a "file
Georg Brandl116aa622007-08-15 14:28:22 +0000692 object" returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000693 :func:`fdopen`, or :data:`sys.stdout` or :data:`sys.stderr`, use its
694 :meth:`~file.write` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000695
Georg Brandl6c8583f2010-05-19 21:22:58 +0000696
697.. _open-constants:
698
699``open()`` flag constants
700~~~~~~~~~~~~~~~~~~~~~~~~~
701
Georg Brandlaf265f42008-12-07 15:06:20 +0000702The following constants are options for the *flags* parameter to the
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000703:func:`~os.open` function. They can be combined using the bitwise OR operator
Georg Brandlaf265f42008-12-07 15:06:20 +0000704``|``. Some of them are not available on all platforms. For descriptions of
705their availability and use, consult the :manpage:`open(2)` manual page on Unix
Doug Hellmann14214262009-09-21 12:16:43 +0000706or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000707
708
709.. data:: O_RDONLY
710 O_WRONLY
711 O_RDWR
712 O_APPEND
713 O_CREAT
714 O_EXCL
715 O_TRUNC
716
Georg Brandlaf265f42008-12-07 15:06:20 +0000717 These constants are available on Unix and Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000718
719
720.. data:: O_DSYNC
721 O_RSYNC
722 O_SYNC
723 O_NDELAY
724 O_NONBLOCK
725 O_NOCTTY
726 O_SHLOCK
727 O_EXLOCK
728
Georg Brandlaf265f42008-12-07 15:06:20 +0000729 These constants are only available on Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000730
731
732.. data:: O_BINARY
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000733 O_NOINHERIT
Georg Brandl116aa622007-08-15 14:28:22 +0000734 O_SHORT_LIVED
735 O_TEMPORARY
736 O_RANDOM
737 O_SEQUENTIAL
738 O_TEXT
739
Georg Brandlaf265f42008-12-07 15:06:20 +0000740 These constants are only available on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000741
742
Alexandre Vassalottibee32532008-05-16 18:15:12 +0000743.. data:: O_ASYNC
744 O_DIRECT
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000745 O_DIRECTORY
746 O_NOFOLLOW
747 O_NOATIME
748
Georg Brandlaf265f42008-12-07 15:06:20 +0000749 These constants are GNU extensions and not present if they are not defined by
750 the C library.
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000751
752
Georg Brandl116aa622007-08-15 14:28:22 +0000753.. _os-file-dir:
754
755Files and Directories
756---------------------
757
Georg Brandl116aa622007-08-15 14:28:22 +0000758.. function:: access(path, mode)
759
760 Use the real uid/gid to test for access to *path*. Note that most operations
761 will use the effective uid/gid, therefore this routine can be used in a
762 suid/sgid environment to test if the invoking user has the specified access to
763 *path*. *mode* should be :const:`F_OK` to test the existence of *path*, or it
764 can be the inclusive OR of one or more of :const:`R_OK`, :const:`W_OK`, and
765 :const:`X_OK` to test permissions. Return :const:`True` if access is allowed,
766 :const:`False` if not. See the Unix man page :manpage:`access(2)` for more
Benjamin Petersond91203b2010-05-06 23:20:40 +0000767 information.
768
769 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000770
771 .. note::
772
Georg Brandlc5605df2009-08-13 08:26:44 +0000773 Using :func:`access` to check if a user is authorized to e.g. open a file
774 before actually doing so using :func:`open` creates a security hole,
775 because the user might exploit the short time interval between checking
776 and opening the file to manipulate it.
Georg Brandl116aa622007-08-15 14:28:22 +0000777
778 .. note::
779
780 I/O operations may fail even when :func:`access` indicates that they would
781 succeed, particularly for operations on network filesystems which may have
782 permissions semantics beyond the usual POSIX permission-bit model.
783
784
785.. data:: F_OK
786
787 Value to pass as the *mode* parameter of :func:`access` to test the existence of
788 *path*.
789
790
791.. data:: R_OK
792
793 Value to include in the *mode* parameter of :func:`access` to test the
794 readability of *path*.
795
796
797.. data:: W_OK
798
799 Value to include in the *mode* parameter of :func:`access` to test the
800 writability of *path*.
801
802
803.. data:: X_OK
804
805 Value to include in the *mode* parameter of :func:`access` to determine if
806 *path* can be executed.
807
808
809.. function:: chdir(path)
810
811 .. index:: single: directory; changing
812
Benjamin Petersond91203b2010-05-06 23:20:40 +0000813 Change the current working directory to *path*.
814
815 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000816
817
818.. function:: fchdir(fd)
819
820 Change the current working directory to the directory represented by the file
821 descriptor *fd*. The descriptor must refer to an opened directory, not an open
Benjamin Petersond91203b2010-05-06 23:20:40 +0000822 file.
823
824 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000825
Georg Brandl116aa622007-08-15 14:28:22 +0000826
827.. function:: getcwd()
828
Martin v. Löwis011e8422009-05-05 04:43:17 +0000829 Return a string representing the current working directory.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000830
Martin v. Löwis011e8422009-05-05 04:43:17 +0000831 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000832
Benjamin Petersond91203b2010-05-06 23:20:40 +0000833
Martin v. Löwisa731b992008-10-07 06:36:31 +0000834.. function:: getcwdb()
Georg Brandl116aa622007-08-15 14:28:22 +0000835
Georg Brandl76e55382008-10-08 16:34:57 +0000836 Return a bytestring representing the current working directory.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000837
Georg Brandlc575c902008-09-13 17:46:05 +0000838 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000839
Georg Brandl116aa622007-08-15 14:28:22 +0000840
841.. function:: chflags(path, flags)
842
843 Set the flags of *path* to the numeric *flags*. *flags* may take a combination
844 (bitwise OR) of the following values (as defined in the :mod:`stat` module):
845
846 * ``UF_NODUMP``
847 * ``UF_IMMUTABLE``
848 * ``UF_APPEND``
849 * ``UF_OPAQUE``
850 * ``UF_NOUNLINK``
851 * ``SF_ARCHIVED``
852 * ``SF_IMMUTABLE``
853 * ``SF_APPEND``
854 * ``SF_NOUNLINK``
855 * ``SF_SNAPSHOT``
856
Georg Brandlc575c902008-09-13 17:46:05 +0000857 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000858
Georg Brandl116aa622007-08-15 14:28:22 +0000859
860.. function:: chroot(path)
861
862 Change the root directory of the current process to *path*. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +0000863 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000864
Georg Brandl116aa622007-08-15 14:28:22 +0000865
866.. function:: chmod(path, mode)
867
868 Change the mode of *path* to the numeric *mode*. *mode* may take one of the
Christian Heimesfaf2f632008-01-06 16:59:19 +0000869 following values (as defined in the :mod:`stat` module) or bitwise ORed
Georg Brandl116aa622007-08-15 14:28:22 +0000870 combinations of them:
871
R. David Murrayba426142009-07-21 14:29:59 +0000872 * :data:`stat.S_ISUID`
873 * :data:`stat.S_ISGID`
874 * :data:`stat.S_ENFMT`
875 * :data:`stat.S_ISVTX`
876 * :data:`stat.S_IREAD`
877 * :data:`stat.S_IWRITE`
878 * :data:`stat.S_IEXEC`
879 * :data:`stat.S_IRWXU`
880 * :data:`stat.S_IRUSR`
881 * :data:`stat.S_IWUSR`
882 * :data:`stat.S_IXUSR`
883 * :data:`stat.S_IRWXG`
884 * :data:`stat.S_IRGRP`
885 * :data:`stat.S_IWGRP`
886 * :data:`stat.S_IXGRP`
887 * :data:`stat.S_IRWXO`
888 * :data:`stat.S_IROTH`
889 * :data:`stat.S_IWOTH`
890 * :data:`stat.S_IXOTH`
Georg Brandl116aa622007-08-15 14:28:22 +0000891
Georg Brandlc575c902008-09-13 17:46:05 +0000892 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000893
894 .. note::
895
896 Although Windows supports :func:`chmod`, you can only set the file's read-only
897 flag with it (via the ``stat.S_IWRITE`` and ``stat.S_IREAD``
898 constants or a corresponding integer value). All other bits are
899 ignored.
900
901
902.. function:: chown(path, uid, gid)
903
904 Change the owner and group id of *path* to the numeric *uid* and *gid*. To leave
Benjamin Petersond91203b2010-05-06 23:20:40 +0000905 one of the ids unchanged, set it to -1.
906
907 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000908
909
910.. function:: lchflags(path, flags)
911
912 Set the flags of *path* to the numeric *flags*, like :func:`chflags`, but do not
Benjamin Petersond91203b2010-05-06 23:20:40 +0000913 follow symbolic links.
914
915 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000916
Georg Brandl116aa622007-08-15 14:28:22 +0000917
Christian Heimes93852662007-12-01 12:22:32 +0000918.. function:: lchmod(path, mode)
919
920 Change the mode of *path* to the numeric *mode*. If path is a symlink, this
921 affects the symlink rather than the target. See the docs for :func:`chmod`
Benjamin Petersond91203b2010-05-06 23:20:40 +0000922 for possible values of *mode*.
923
924 Availability: Unix.
Christian Heimes93852662007-12-01 12:22:32 +0000925
Christian Heimes93852662007-12-01 12:22:32 +0000926
Georg Brandl116aa622007-08-15 14:28:22 +0000927.. function:: lchown(path, uid, gid)
928
Christian Heimesfaf2f632008-01-06 16:59:19 +0000929 Change the owner and group id of *path* to the numeric *uid* and *gid*. This
Benjamin Petersond91203b2010-05-06 23:20:40 +0000930 function will not follow symbolic links.
931
932 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000933
Georg Brandl116aa622007-08-15 14:28:22 +0000934
Benjamin Peterson5879d412009-03-30 14:51:56 +0000935.. function:: link(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +0000936
Benjamin Petersond91203b2010-05-06 23:20:40 +0000937 Create a hard link pointing to *source* named *link_name*.
938
939 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000940
941
942.. function:: listdir(path)
943
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000944 Return a list containing the names of the entries in the directory given by
945 *path*. The list is in arbitrary order. It does not include the special
946 entries ``'.'`` and ``'..'`` even if they are present in the directory.
Georg Brandl116aa622007-08-15 14:28:22 +0000947
Martin v. Löwis011e8422009-05-05 04:43:17 +0000948 This function can be called with a bytes or string argument, and returns
949 filenames of the same datatype.
Georg Brandl116aa622007-08-15 14:28:22 +0000950
Benjamin Petersond91203b2010-05-06 23:20:40 +0000951 Availability: Unix, Windows.
952
Georg Brandl116aa622007-08-15 14:28:22 +0000953
954.. function:: lstat(path)
955
Guido van Rossum2cc30da2007-11-02 23:46:40 +0000956 Like :func:`stat`, but do not follow symbolic links. This is an alias for
957 :func:`stat` on platforms that do not support symbolic links, such as
958 Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000959
960
961.. function:: mkfifo(path[, mode])
962
Georg Brandlf4a41232008-05-26 17:55:52 +0000963 Create a FIFO (a named pipe) named *path* with numeric mode *mode*. The
964 default *mode* is ``0o666`` (octal). The current umask value is first masked
Benjamin Petersond91203b2010-05-06 23:20:40 +0000965 out from the mode.
Georg Brandl116aa622007-08-15 14:28:22 +0000966
967 FIFOs are pipes that can be accessed like regular files. FIFOs exist until they
968 are deleted (for example with :func:`os.unlink`). Generally, FIFOs are used as
969 rendezvous between "client" and "server" type processes: the server opens the
970 FIFO for reading, and the client opens it for writing. Note that :func:`mkfifo`
971 doesn't open the FIFO --- it just creates the rendezvous point.
972
Benjamin Petersond91203b2010-05-06 23:20:40 +0000973 Availability: Unix.
974
Georg Brandl116aa622007-08-15 14:28:22 +0000975
Georg Brandlf4a41232008-05-26 17:55:52 +0000976.. function:: mknod(filename[, mode=0o600, device])
Georg Brandl116aa622007-08-15 14:28:22 +0000977
978 Create a filesystem node (file, device special file or named pipe) named
979 *filename*. *mode* specifies both the permissions to use and the type of node to
980 be created, being combined (bitwise OR) with one of ``stat.S_IFREG``,
981 ``stat.S_IFCHR``, ``stat.S_IFBLK``,
982 and ``stat.S_IFIFO`` (those constants are available in :mod:`stat`).
983 For ``stat.S_IFCHR`` and
984 ``stat.S_IFBLK``, *device* defines the newly created device special file (probably using
985 :func:`os.makedev`), otherwise it is ignored.
986
Georg Brandl116aa622007-08-15 14:28:22 +0000987
988.. function:: major(device)
989
Christian Heimesfaf2f632008-01-06 16:59:19 +0000990 Extract the device major number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000991 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
992
Georg Brandl116aa622007-08-15 14:28:22 +0000993
994.. function:: minor(device)
995
Christian Heimesfaf2f632008-01-06 16:59:19 +0000996 Extract the device minor number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000997 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
998
Georg Brandl116aa622007-08-15 14:28:22 +0000999
1000.. function:: makedev(major, minor)
1001
Christian Heimesfaf2f632008-01-06 16:59:19 +00001002 Compose a raw device number from the major and minor device numbers.
Georg Brandl116aa622007-08-15 14:28:22 +00001003
Georg Brandl116aa622007-08-15 14:28:22 +00001004
1005.. function:: mkdir(path[, mode])
1006
Georg Brandlf4a41232008-05-26 17:55:52 +00001007 Create a directory named *path* with numeric mode *mode*. The default *mode*
1008 is ``0o777`` (octal). On some systems, *mode* is ignored. Where it is used,
Georg Brandlc62efa82010-07-11 10:41:07 +00001009 the current umask value is first masked out. If the directory already
1010 exists, :exc:`OSError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +00001011
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001012 It is also possible to create temporary directories; see the
1013 :mod:`tempfile` module's :func:`tempfile.mkdtemp` function.
1014
Benjamin Petersond91203b2010-05-06 23:20:40 +00001015 Availability: Unix, Windows.
1016
Georg Brandl116aa622007-08-15 14:28:22 +00001017
1018.. function:: makedirs(path[, mode])
1019
1020 .. index::
1021 single: directory; creating
1022 single: UNC paths; and os.makedirs()
1023
1024 Recursive directory creation function. Like :func:`mkdir`, but makes all
Georg Brandlf4a41232008-05-26 17:55:52 +00001025 intermediate-level directories needed to contain the leaf directory. Throws
1026 an :exc:`error` exception if the leaf directory already exists or cannot be
1027 created. The default *mode* is ``0o777`` (octal). On some systems, *mode*
1028 is ignored. Where it is used, the current umask value is first masked out.
Georg Brandl116aa622007-08-15 14:28:22 +00001029
1030 .. note::
1031
1032 :func:`makedirs` will become confused if the path elements to create include
Christian Heimesfaf2f632008-01-06 16:59:19 +00001033 :data:`os.pardir`.
Georg Brandl116aa622007-08-15 14:28:22 +00001034
Georg Brandl55ac8f02007-09-01 13:51:09 +00001035 This function handles UNC paths correctly.
Georg Brandl116aa622007-08-15 14:28:22 +00001036
1037
1038.. function:: pathconf(path, name)
1039
1040 Return system configuration information relevant to a named file. *name*
1041 specifies the configuration value to retrieve; it may be a string which is the
1042 name of a defined system value; these names are specified in a number of
1043 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
1044 additional names as well. The names known to the host operating system are
1045 given in the ``pathconf_names`` dictionary. For configuration variables not
1046 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +00001047
1048 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1049 specific value for *name* is not supported by the host system, even if it is
1050 included in ``pathconf_names``, an :exc:`OSError` is raised with
1051 :const:`errno.EINVAL` for the error number.
1052
Benjamin Petersond91203b2010-05-06 23:20:40 +00001053 Availability: Unix.
1054
Georg Brandl116aa622007-08-15 14:28:22 +00001055
1056.. data:: pathconf_names
1057
1058 Dictionary mapping names accepted by :func:`pathconf` and :func:`fpathconf` to
1059 the integer values defined for those names by the host operating system. This
1060 can be used to determine the set of names known to the system. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001061 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001062
1063
1064.. function:: readlink(path)
1065
1066 Return a string representing the path to which the symbolic link points. The
1067 result may be either an absolute or relative pathname; if it is relative, it may
1068 be converted to an absolute pathname using ``os.path.join(os.path.dirname(path),
1069 result)``.
1070
Georg Brandl76e55382008-10-08 16:34:57 +00001071 If the *path* is a string object, the result will also be a string object,
1072 and the call may raise an UnicodeDecodeError. If the *path* is a bytes
1073 object, the result will be a bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +00001074
Georg Brandlc575c902008-09-13 17:46:05 +00001075 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001076
1077
1078.. function:: remove(path)
1079
Georg Brandl7baf6252009-09-01 08:13:16 +00001080 Remove (delete) the file *path*. If *path* is a directory, :exc:`OSError` is
1081 raised; see :func:`rmdir` below to remove a directory. This is identical to
1082 the :func:`unlink` function documented below. On Windows, attempting to
1083 remove a file that is in use causes an exception to be raised; on Unix, the
1084 directory entry is removed but the storage allocated to the file is not made
Benjamin Petersond91203b2010-05-06 23:20:40 +00001085 available until the original file is no longer in use.
1086
1087 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001088
1089
1090.. function:: removedirs(path)
1091
1092 .. index:: single: directory; deleting
1093
Christian Heimesfaf2f632008-01-06 16:59:19 +00001094 Remove directories recursively. Works like :func:`rmdir` except that, if the
Georg Brandl116aa622007-08-15 14:28:22 +00001095 leaf directory is successfully removed, :func:`removedirs` tries to
1096 successively remove every parent directory mentioned in *path* until an error
1097 is raised (which is ignored, because it generally means that a parent directory
1098 is not empty). For example, ``os.removedirs('foo/bar/baz')`` will first remove
1099 the directory ``'foo/bar/baz'``, and then remove ``'foo/bar'`` and ``'foo'`` if
1100 they are empty. Raises :exc:`OSError` if the leaf directory could not be
1101 successfully removed.
1102
Georg Brandl116aa622007-08-15 14:28:22 +00001103
1104.. function:: rename(src, dst)
1105
1106 Rename the file or directory *src* to *dst*. If *dst* is a directory,
1107 :exc:`OSError` will be raised. On Unix, if *dst* exists and is a file, it will
Christian Heimesfaf2f632008-01-06 16:59:19 +00001108 be replaced silently if the user has permission. The operation may fail on some
Georg Brandl116aa622007-08-15 14:28:22 +00001109 Unix flavors if *src* and *dst* are on different filesystems. If successful,
1110 the renaming will be an atomic operation (this is a POSIX requirement). On
1111 Windows, if *dst* already exists, :exc:`OSError` will be raised even if it is a
1112 file; there may be no way to implement an atomic rename when *dst* names an
Benjamin Petersond91203b2010-05-06 23:20:40 +00001113 existing file.
1114
1115 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001116
1117
1118.. function:: renames(old, new)
1119
1120 Recursive directory or file renaming function. Works like :func:`rename`, except
1121 creation of any intermediate directories needed to make the new pathname good is
1122 attempted first. After the rename, directories corresponding to rightmost path
1123 segments of the old name will be pruned away using :func:`removedirs`.
1124
Georg Brandl116aa622007-08-15 14:28:22 +00001125 .. note::
1126
1127 This function can fail with the new directory structure made if you lack
1128 permissions needed to remove the leaf directory or file.
1129
1130
1131.. function:: rmdir(path)
1132
Georg Brandl7baf6252009-09-01 08:13:16 +00001133 Remove (delete) the directory *path*. Only works when the directory is
1134 empty, otherwise, :exc:`OSError` is raised. In order to remove whole
Benjamin Petersond91203b2010-05-06 23:20:40 +00001135 directory trees, :func:`shutil.rmtree` can be used.
1136
1137 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001138
1139
1140.. function:: stat(path)
1141
1142 Perform a :cfunc:`stat` system call on the given path. The return value is an
1143 object whose attributes correspond to the members of the :ctype:`stat`
1144 structure, namely: :attr:`st_mode` (protection bits), :attr:`st_ino` (inode
1145 number), :attr:`st_dev` (device), :attr:`st_nlink` (number of hard links),
Christian Heimesfaf2f632008-01-06 16:59:19 +00001146 :attr:`st_uid` (user id of owner), :attr:`st_gid` (group id of owner),
Georg Brandl116aa622007-08-15 14:28:22 +00001147 :attr:`st_size` (size of file, in bytes), :attr:`st_atime` (time of most recent
1148 access), :attr:`st_mtime` (time of most recent content modification),
1149 :attr:`st_ctime` (platform dependent; time of most recent metadata change on
1150 Unix, or the time of creation on Windows)::
1151
1152 >>> import os
1153 >>> statinfo = os.stat('somefile.txt')
1154 >>> statinfo
Ezio Melotti9ce52612010-01-16 14:52:34 +00001155 (33188, 422511, 769, 1, 1032, 100, 926, 1105022698,1105022732, 1105022732)
Georg Brandl116aa622007-08-15 14:28:22 +00001156 >>> statinfo.st_size
Ezio Melotti9ce52612010-01-16 14:52:34 +00001157 926
Georg Brandl116aa622007-08-15 14:28:22 +00001158 >>>
1159
Georg Brandl116aa622007-08-15 14:28:22 +00001160
1161 On some Unix systems (such as Linux), the following attributes may also be
1162 available: :attr:`st_blocks` (number of blocks allocated for file),
1163 :attr:`st_blksize` (filesystem blocksize), :attr:`st_rdev` (type of device if an
1164 inode device). :attr:`st_flags` (user defined flags for file).
1165
1166 On other Unix systems (such as FreeBSD), the following attributes may be
1167 available (but may be only filled out if root tries to use them): :attr:`st_gen`
1168 (file generation number), :attr:`st_birthtime` (time of file creation).
1169
1170 On Mac OS systems, the following attributes may also be available:
1171 :attr:`st_rsize`, :attr:`st_creator`, :attr:`st_type`.
1172
Georg Brandl116aa622007-08-15 14:28:22 +00001173 .. index:: module: stat
1174
1175 For backward compatibility, the return value of :func:`stat` is also accessible
1176 as a tuple of at least 10 integers giving the most important (and portable)
1177 members of the :ctype:`stat` structure, in the order :attr:`st_mode`,
1178 :attr:`st_ino`, :attr:`st_dev`, :attr:`st_nlink`, :attr:`st_uid`,
1179 :attr:`st_gid`, :attr:`st_size`, :attr:`st_atime`, :attr:`st_mtime`,
1180 :attr:`st_ctime`. More items may be added at the end by some implementations.
1181 The standard module :mod:`stat` defines functions and constants that are useful
1182 for extracting information from a :ctype:`stat` structure. (On Windows, some
1183 items are filled with dummy values.)
1184
1185 .. note::
1186
1187 The exact meaning and resolution of the :attr:`st_atime`, :attr:`st_mtime`, and
1188 :attr:`st_ctime` members depends on the operating system and the file system.
1189 For example, on Windows systems using the FAT or FAT32 file systems,
1190 :attr:`st_mtime` has 2-second resolution, and :attr:`st_atime` has only 1-day
1191 resolution. See your operating system documentation for details.
1192
Georg Brandlc575c902008-09-13 17:46:05 +00001193 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001194
Georg Brandl116aa622007-08-15 14:28:22 +00001195
1196.. function:: stat_float_times([newvalue])
1197
1198 Determine whether :class:`stat_result` represents time stamps as float objects.
1199 If *newvalue* is ``True``, future calls to :func:`stat` return floats, if it is
1200 ``False``, future calls return ints. If *newvalue* is omitted, return the
1201 current setting.
1202
1203 For compatibility with older Python versions, accessing :class:`stat_result` as
1204 a tuple always returns integers.
1205
Georg Brandl55ac8f02007-09-01 13:51:09 +00001206 Python now returns float values by default. Applications which do not work
1207 correctly with floating point time stamps can use this function to restore the
1208 old behaviour.
Georg Brandl116aa622007-08-15 14:28:22 +00001209
1210 The resolution of the timestamps (that is the smallest possible fraction)
1211 depends on the system. Some systems only support second resolution; on these
1212 systems, the fraction will always be zero.
1213
1214 It is recommended that this setting is only changed at program startup time in
1215 the *__main__* module; libraries should never change this setting. If an
1216 application uses a library that works incorrectly if floating point time stamps
1217 are processed, this application should turn the feature off until the library
1218 has been corrected.
1219
1220
1221.. function:: statvfs(path)
1222
1223 Perform a :cfunc:`statvfs` system call on the given path. The return value is
1224 an object whose attributes describe the filesystem on the given path, and
1225 correspond to the members of the :ctype:`statvfs` structure, namely:
1226 :attr:`f_bsize`, :attr:`f_frsize`, :attr:`f_blocks`, :attr:`f_bfree`,
1227 :attr:`f_bavail`, :attr:`f_files`, :attr:`f_ffree`, :attr:`f_favail`,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001228 :attr:`f_flag`, :attr:`f_namemax`.
1229
1230 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001231
Georg Brandl116aa622007-08-15 14:28:22 +00001232
Benjamin Peterson5879d412009-03-30 14:51:56 +00001233.. function:: symlink(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +00001234
Benjamin Petersond91203b2010-05-06 23:20:40 +00001235 Create a symbolic link pointing to *source* named *link_name*.
1236
1237 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001238
1239
Georg Brandl116aa622007-08-15 14:28:22 +00001240.. function:: unlink(path)
1241
Georg Brandl7baf6252009-09-01 08:13:16 +00001242 Remove (delete) the file *path*. This is the same function as
1243 :func:`remove`; the :func:`unlink` name is its traditional Unix
Benjamin Petersond91203b2010-05-06 23:20:40 +00001244 name.
1245
1246 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001247
1248
1249.. function:: utime(path, times)
1250
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001251 Set the access and modified times of the file specified by *path*. If *times*
1252 is ``None``, then the file's access and modified times are set to the current
1253 time. (The effect is similar to running the Unix program :program:`touch` on
1254 the path.) Otherwise, *times* must be a 2-tuple of numbers, of the form
1255 ``(atime, mtime)`` which is used to set the access and modified times,
1256 respectively. Whether a directory can be given for *path* depends on whether
1257 the operating system implements directories as files (for example, Windows
1258 does not). Note that the exact times you set here may not be returned by a
1259 subsequent :func:`stat` call, depending on the resolution with which your
1260 operating system records access and modification times; see :func:`stat`.
Georg Brandl116aa622007-08-15 14:28:22 +00001261
Georg Brandlc575c902008-09-13 17:46:05 +00001262 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001263
1264
1265.. function:: walk(top[, topdown=True [, onerror=None[, followlinks=False]]])
1266
1267 .. index::
1268 single: directory; walking
1269 single: directory; traversal
1270
Christian Heimesfaf2f632008-01-06 16:59:19 +00001271 Generate the file names in a directory tree by walking the tree
1272 either top-down or bottom-up. For each directory in the tree rooted at directory
Georg Brandl116aa622007-08-15 14:28:22 +00001273 *top* (including *top* itself), it yields a 3-tuple ``(dirpath, dirnames,
1274 filenames)``.
1275
1276 *dirpath* is a string, the path to the directory. *dirnames* is a list of the
1277 names of the subdirectories in *dirpath* (excluding ``'.'`` and ``'..'``).
1278 *filenames* is a list of the names of the non-directory files in *dirpath*.
1279 Note that the names in the lists contain no path components. To get a full path
1280 (which begins with *top*) to a file or directory in *dirpath*, do
1281 ``os.path.join(dirpath, name)``.
1282
Christian Heimesfaf2f632008-01-06 16:59:19 +00001283 If optional argument *topdown* is ``True`` or not specified, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001284 directory is generated before the triples for any of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001285 (directories are generated top-down). If *topdown* is ``False``, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001286 directory is generated after the triples for all of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001287 (directories are generated bottom-up).
Georg Brandl116aa622007-08-15 14:28:22 +00001288
Christian Heimesfaf2f632008-01-06 16:59:19 +00001289 When *topdown* is ``True``, the caller can modify the *dirnames* list in-place
Georg Brandl116aa622007-08-15 14:28:22 +00001290 (perhaps using :keyword:`del` or slice assignment), and :func:`walk` will only
1291 recurse into the subdirectories whose names remain in *dirnames*; this can be
1292 used to prune the search, impose a specific order of visiting, or even to inform
1293 :func:`walk` about directories the caller creates or renames before it resumes
Christian Heimesfaf2f632008-01-06 16:59:19 +00001294 :func:`walk` again. Modifying *dirnames* when *topdown* is ``False`` is
Georg Brandl116aa622007-08-15 14:28:22 +00001295 ineffective, because in bottom-up mode the directories in *dirnames* are
1296 generated before *dirpath* itself is generated.
1297
Christian Heimesfaf2f632008-01-06 16:59:19 +00001298 By default errors from the :func:`listdir` call are ignored. If optional
Georg Brandl116aa622007-08-15 14:28:22 +00001299 argument *onerror* is specified, it should be a function; it will be called with
1300 one argument, an :exc:`OSError` instance. It can report the error to continue
1301 with the walk, or raise the exception to abort the walk. Note that the filename
1302 is available as the ``filename`` attribute of the exception object.
1303
1304 By default, :func:`walk` will not walk down into symbolic links that resolve to
Christian Heimesfaf2f632008-01-06 16:59:19 +00001305 directories. Set *followlinks* to ``True`` to visit directories pointed to by
Georg Brandl116aa622007-08-15 14:28:22 +00001306 symlinks, on systems that support them.
1307
Georg Brandl116aa622007-08-15 14:28:22 +00001308 .. note::
1309
Christian Heimesfaf2f632008-01-06 16:59:19 +00001310 Be aware that setting *followlinks* to ``True`` can lead to infinite recursion if a
Georg Brandl116aa622007-08-15 14:28:22 +00001311 link points to a parent directory of itself. :func:`walk` does not keep track of
1312 the directories it visited already.
1313
1314 .. note::
1315
1316 If you pass a relative pathname, don't change the current working directory
1317 between resumptions of :func:`walk`. :func:`walk` never changes the current
1318 directory, and assumes that its caller doesn't either.
1319
1320 This example displays the number of bytes taken by non-directory files in each
1321 directory under the starting directory, except that it doesn't look under any
1322 CVS subdirectory::
1323
1324 import os
1325 from os.path import join, getsize
1326 for root, dirs, files in os.walk('python/Lib/email'):
Georg Brandl6911e3c2007-09-04 07:15:32 +00001327 print(root, "consumes", end=" ")
1328 print(sum(getsize(join(root, name)) for name in files), end=" ")
1329 print("bytes in", len(files), "non-directory files")
Georg Brandl116aa622007-08-15 14:28:22 +00001330 if 'CVS' in dirs:
1331 dirs.remove('CVS') # don't visit CVS directories
1332
Christian Heimesfaf2f632008-01-06 16:59:19 +00001333 In the next example, walking the tree bottom-up is essential: :func:`rmdir`
Georg Brandl116aa622007-08-15 14:28:22 +00001334 doesn't allow deleting a directory before the directory is empty::
1335
Christian Heimesfaf2f632008-01-06 16:59:19 +00001336 # Delete everything reachable from the directory named in "top",
Georg Brandl116aa622007-08-15 14:28:22 +00001337 # assuming there are no symbolic links.
1338 # CAUTION: This is dangerous! For example, if top == '/', it
1339 # could delete all your disk files.
1340 import os
1341 for root, dirs, files in os.walk(top, topdown=False):
1342 for name in files:
1343 os.remove(os.path.join(root, name))
1344 for name in dirs:
1345 os.rmdir(os.path.join(root, name))
1346
Georg Brandl116aa622007-08-15 14:28:22 +00001347
1348.. _os-process:
1349
1350Process Management
1351------------------
1352
1353These functions may be used to create and manage processes.
1354
1355The various :func:`exec\*` functions take a list of arguments for the new
1356program loaded into the process. In each case, the first of these arguments is
1357passed to the new program as its own name rather than as an argument a user may
1358have typed on a command line. For the C programmer, this is the ``argv[0]``
1359passed to a program's :cfunc:`main`. For example, ``os.execv('/bin/echo',
1360['foo', 'bar'])`` will only print ``bar`` on standard output; ``foo`` will seem
1361to be ignored.
1362
1363
1364.. function:: abort()
1365
1366 Generate a :const:`SIGABRT` signal to the current process. On Unix, the default
1367 behavior is to produce a core dump; on Windows, the process immediately returns
1368 an exit code of ``3``. Be aware that programs which use :func:`signal.signal`
1369 to register a handler for :const:`SIGABRT` will behave differently.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001370
Georg Brandlc575c902008-09-13 17:46:05 +00001371 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001372
1373
1374.. function:: execl(path, arg0, arg1, ...)
1375 execle(path, arg0, arg1, ..., env)
1376 execlp(file, arg0, arg1, ...)
1377 execlpe(file, arg0, arg1, ..., env)
1378 execv(path, args)
1379 execve(path, args, env)
1380 execvp(file, args)
1381 execvpe(file, args, env)
1382
1383 These functions all execute a new program, replacing the current process; they
1384 do not return. On Unix, the new executable is loaded into the current process,
Christian Heimesfaf2f632008-01-06 16:59:19 +00001385 and will have the same process id as the caller. Errors will be reported as
Georg Brandl48310cd2009-01-03 21:18:54 +00001386 :exc:`OSError` exceptions.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001387
1388 The current process is replaced immediately. Open file objects and
1389 descriptors are not flushed, so if there may be data buffered
1390 on these open files, you should flush them using
1391 :func:`sys.stdout.flush` or :func:`os.fsync` before calling an
1392 :func:`exec\*` function.
Georg Brandl116aa622007-08-15 14:28:22 +00001393
Christian Heimesfaf2f632008-01-06 16:59:19 +00001394 The "l" and "v" variants of the :func:`exec\*` functions differ in how
1395 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001396 to work with if the number of parameters is fixed when the code is written; the
1397 individual parameters simply become additional parameters to the :func:`execl\*`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001398 functions. The "v" variants are good when the number of parameters is
Georg Brandl116aa622007-08-15 14:28:22 +00001399 variable, with the arguments being passed in a list or tuple as the *args*
1400 parameter. In either case, the arguments to the child process should start with
1401 the name of the command being run, but this is not enforced.
1402
Christian Heimesfaf2f632008-01-06 16:59:19 +00001403 The variants which include a "p" near the end (:func:`execlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001404 :func:`execlpe`, :func:`execvp`, and :func:`execvpe`) will use the
1405 :envvar:`PATH` environment variable to locate the program *file*. When the
1406 environment is being replaced (using one of the :func:`exec\*e` variants,
1407 discussed in the next paragraph), the new environment is used as the source of
1408 the :envvar:`PATH` variable. The other variants, :func:`execl`, :func:`execle`,
1409 :func:`execv`, and :func:`execve`, will not use the :envvar:`PATH` variable to
1410 locate the executable; *path* must contain an appropriate absolute or relative
1411 path.
1412
1413 For :func:`execle`, :func:`execlpe`, :func:`execve`, and :func:`execvpe` (note
Christian Heimesfaf2f632008-01-06 16:59:19 +00001414 that these all end in "e"), the *env* parameter must be a mapping which is
Christian Heimesa342c012008-04-20 21:01:16 +00001415 used to define the environment variables for the new process (these are used
1416 instead of the current process' environment); the functions :func:`execl`,
Georg Brandl116aa622007-08-15 14:28:22 +00001417 :func:`execlp`, :func:`execv`, and :func:`execvp` all cause the new process to
Georg Brandl48310cd2009-01-03 21:18:54 +00001418 inherit the environment of the current process.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001419
1420 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001421
1422
1423.. function:: _exit(n)
1424
1425 Exit to the system with status *n*, without calling cleanup handlers, flushing
Benjamin Petersond91203b2010-05-06 23:20:40 +00001426 stdio buffers, etc.
1427
1428 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001429
1430 .. note::
1431
1432 The standard way to exit is ``sys.exit(n)``. :func:`_exit` should normally only
1433 be used in the child process after a :func:`fork`.
1434
Christian Heimesfaf2f632008-01-06 16:59:19 +00001435The following exit codes are defined and can be used with :func:`_exit`,
Georg Brandl116aa622007-08-15 14:28:22 +00001436although they are not required. These are typically used for system programs
1437written in Python, such as a mail server's external command delivery program.
1438
1439.. note::
1440
1441 Some of these may not be available on all Unix platforms, since there is some
1442 variation. These constants are defined where they are defined by the underlying
1443 platform.
1444
1445
1446.. data:: EX_OK
1447
Benjamin Petersond91203b2010-05-06 23:20:40 +00001448 Exit code that means no error occurred.
1449
1450 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001451
Georg Brandl116aa622007-08-15 14:28:22 +00001452
1453.. data:: EX_USAGE
1454
1455 Exit code that means the command was used incorrectly, such as when the wrong
Benjamin Petersond91203b2010-05-06 23:20:40 +00001456 number of arguments are given.
1457
1458 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001459
Georg Brandl116aa622007-08-15 14:28:22 +00001460
1461.. data:: EX_DATAERR
1462
Benjamin Petersond91203b2010-05-06 23:20:40 +00001463 Exit code that means the input data was incorrect.
1464
1465 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001466
Georg Brandl116aa622007-08-15 14:28:22 +00001467
1468.. data:: EX_NOINPUT
1469
1470 Exit code that means an input file did not exist or was not readable.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001471
Georg Brandlc575c902008-09-13 17:46:05 +00001472 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001473
Georg Brandl116aa622007-08-15 14:28:22 +00001474
1475.. data:: EX_NOUSER
1476
Benjamin Petersond91203b2010-05-06 23:20:40 +00001477 Exit code that means a specified user did not exist.
1478
1479 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001480
Georg Brandl116aa622007-08-15 14:28:22 +00001481
1482.. data:: EX_NOHOST
1483
Benjamin Petersond91203b2010-05-06 23:20:40 +00001484 Exit code that means a specified host did not exist.
1485
1486 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001487
Georg Brandl116aa622007-08-15 14:28:22 +00001488
1489.. data:: EX_UNAVAILABLE
1490
Benjamin Petersond91203b2010-05-06 23:20:40 +00001491 Exit code that means that a required service is unavailable.
1492
1493 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001494
Georg Brandl116aa622007-08-15 14:28:22 +00001495
1496.. data:: EX_SOFTWARE
1497
Benjamin Petersond91203b2010-05-06 23:20:40 +00001498 Exit code that means an internal software error was detected.
1499
1500 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001501
Georg Brandl116aa622007-08-15 14:28:22 +00001502
1503.. data:: EX_OSERR
1504
1505 Exit code that means an operating system error was detected, such as the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001506 inability to fork or create a pipe.
1507
1508 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001509
Georg Brandl116aa622007-08-15 14:28:22 +00001510
1511.. data:: EX_OSFILE
1512
1513 Exit code that means some system file did not exist, could not be opened, or had
Benjamin Petersond91203b2010-05-06 23:20:40 +00001514 some other kind of error.
1515
1516 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001517
Georg Brandl116aa622007-08-15 14:28:22 +00001518
1519.. data:: EX_CANTCREAT
1520
1521 Exit code that means a user specified output file could not be created.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001522
Georg Brandlc575c902008-09-13 17:46:05 +00001523 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001524
Georg Brandl116aa622007-08-15 14:28:22 +00001525
1526.. data:: EX_IOERR
1527
1528 Exit code that means that an error occurred while doing I/O on some file.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001529
Georg Brandlc575c902008-09-13 17:46:05 +00001530 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001531
Georg Brandl116aa622007-08-15 14:28:22 +00001532
1533.. data:: EX_TEMPFAIL
1534
1535 Exit code that means a temporary failure occurred. This indicates something
1536 that may not really be an error, such as a network connection that couldn't be
Benjamin Petersond91203b2010-05-06 23:20:40 +00001537 made during a retryable operation.
1538
1539 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001540
Georg Brandl116aa622007-08-15 14:28:22 +00001541
1542.. data:: EX_PROTOCOL
1543
1544 Exit code that means that a protocol exchange was illegal, invalid, or not
Benjamin Petersond91203b2010-05-06 23:20:40 +00001545 understood.
1546
1547 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001548
Georg Brandl116aa622007-08-15 14:28:22 +00001549
1550.. data:: EX_NOPERM
1551
1552 Exit code that means that there were insufficient permissions to perform the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001553 operation (but not intended for file system problems).
1554
1555 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001556
Georg Brandl116aa622007-08-15 14:28:22 +00001557
1558.. data:: EX_CONFIG
1559
1560 Exit code that means that some kind of configuration error occurred.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001561
Georg Brandlc575c902008-09-13 17:46:05 +00001562 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001563
Georg Brandl116aa622007-08-15 14:28:22 +00001564
1565.. data:: EX_NOTFOUND
1566
Benjamin Petersond91203b2010-05-06 23:20:40 +00001567 Exit code that means something like "an entry was not found".
1568
1569 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001570
Georg Brandl116aa622007-08-15 14:28:22 +00001571
1572.. function:: fork()
1573
Christian Heimesfaf2f632008-01-06 16:59:19 +00001574 Fork a child process. Return ``0`` in the child and the child's process id in the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001575 parent. If an error occurs :exc:`OSError` is raised.
Benjamin Petersonbcd8ac32008-10-10 22:20:52 +00001576
1577 Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have
1578 known issues when using fork() from a thread.
1579
Georg Brandlc575c902008-09-13 17:46:05 +00001580 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001581
1582
1583.. function:: forkpty()
1584
1585 Fork a child process, using a new pseudo-terminal as the child's controlling
1586 terminal. Return a pair of ``(pid, fd)``, where *pid* is ``0`` in the child, the
1587 new child's process id in the parent, and *fd* is the file descriptor of the
1588 master end of the pseudo-terminal. For a more portable approach, use the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001589 :mod:`pty` module. If an error occurs :exc:`OSError` is raised.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001590
Georg Brandlc575c902008-09-13 17:46:05 +00001591 Availability: some flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001592
1593
1594.. function:: kill(pid, sig)
1595
1596 .. index::
1597 single: process; killing
1598 single: process; signalling
1599
1600 Send signal *sig* to the process *pid*. Constants for the specific signals
1601 available on the host platform are defined in the :mod:`signal` module.
Georg Brandlc575c902008-09-13 17:46:05 +00001602 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001603
1604
1605.. function:: killpg(pgid, sig)
1606
1607 .. index::
1608 single: process; killing
1609 single: process; signalling
1610
Benjamin Petersond91203b2010-05-06 23:20:40 +00001611 Send the signal *sig* to the process group *pgid*.
1612
1613 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001614
Georg Brandl116aa622007-08-15 14:28:22 +00001615
1616.. function:: nice(increment)
1617
1618 Add *increment* to the process's "niceness". Return the new niceness.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001619
Georg Brandlc575c902008-09-13 17:46:05 +00001620 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001621
1622
1623.. function:: plock(op)
1624
1625 Lock program segments into memory. The value of *op* (defined in
Benjamin Petersond91203b2010-05-06 23:20:40 +00001626 ``<sys/lock.h>``) determines which segments are locked.
1627
1628 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001629
1630
1631.. function:: popen(...)
1632 :noindex:
1633
1634 Run child processes, returning opened pipes for communications. These functions
1635 are described in section :ref:`os-newstreams`.
1636
1637
1638.. function:: spawnl(mode, path, ...)
1639 spawnle(mode, path, ..., env)
1640 spawnlp(mode, file, ...)
1641 spawnlpe(mode, file, ..., env)
1642 spawnv(mode, path, args)
1643 spawnve(mode, path, args, env)
1644 spawnvp(mode, file, args)
1645 spawnvpe(mode, file, args, env)
1646
1647 Execute the program *path* in a new process.
1648
1649 (Note that the :mod:`subprocess` module provides more powerful facilities for
1650 spawning new processes and retrieving their results; using that module is
Benjamin Peterson87c8d872009-06-11 22:54:11 +00001651 preferable to using these functions. Check especially the
1652 :ref:`subprocess-replacements` section.)
Georg Brandl116aa622007-08-15 14:28:22 +00001653
Christian Heimesfaf2f632008-01-06 16:59:19 +00001654 If *mode* is :const:`P_NOWAIT`, this function returns the process id of the new
Georg Brandl116aa622007-08-15 14:28:22 +00001655 process; if *mode* is :const:`P_WAIT`, returns the process's exit code if it
1656 exits normally, or ``-signal``, where *signal* is the signal that killed the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001657 process. On Windows, the process id will actually be the process handle, so can
Georg Brandl116aa622007-08-15 14:28:22 +00001658 be used with the :func:`waitpid` function.
1659
Christian Heimesfaf2f632008-01-06 16:59:19 +00001660 The "l" and "v" variants of the :func:`spawn\*` functions differ in how
1661 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001662 to work with if the number of parameters is fixed when the code is written; the
1663 individual parameters simply become additional parameters to the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001664 :func:`spawnl\*` functions. The "v" variants are good when the number of
Georg Brandl116aa622007-08-15 14:28:22 +00001665 parameters is variable, with the arguments being passed in a list or tuple as
1666 the *args* parameter. In either case, the arguments to the child process must
1667 start with the name of the command being run.
1668
Christian Heimesfaf2f632008-01-06 16:59:19 +00001669 The variants which include a second "p" near the end (:func:`spawnlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001670 :func:`spawnlpe`, :func:`spawnvp`, and :func:`spawnvpe`) will use the
1671 :envvar:`PATH` environment variable to locate the program *file*. When the
1672 environment is being replaced (using one of the :func:`spawn\*e` variants,
1673 discussed in the next paragraph), the new environment is used as the source of
1674 the :envvar:`PATH` variable. The other variants, :func:`spawnl`,
1675 :func:`spawnle`, :func:`spawnv`, and :func:`spawnve`, will not use the
1676 :envvar:`PATH` variable to locate the executable; *path* must contain an
1677 appropriate absolute or relative path.
1678
1679 For :func:`spawnle`, :func:`spawnlpe`, :func:`spawnve`, and :func:`spawnvpe`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001680 (note that these all end in "e"), the *env* parameter must be a mapping
Christian Heimesa342c012008-04-20 21:01:16 +00001681 which is used to define the environment variables for the new process (they are
1682 used instead of the current process' environment); the functions
Georg Brandl116aa622007-08-15 14:28:22 +00001683 :func:`spawnl`, :func:`spawnlp`, :func:`spawnv`, and :func:`spawnvp` all cause
Benjamin Petersond23f8222009-04-05 19:13:16 +00001684 the new process to inherit the environment of the current process. Note that
1685 keys and values in the *env* dictionary must be strings; invalid keys or
1686 values will cause the function to fail, with a return value of ``127``.
Georg Brandl116aa622007-08-15 14:28:22 +00001687
1688 As an example, the following calls to :func:`spawnlp` and :func:`spawnvpe` are
1689 equivalent::
1690
1691 import os
1692 os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1693
1694 L = ['cp', 'index.html', '/dev/null']
1695 os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1696
1697 Availability: Unix, Windows. :func:`spawnlp`, :func:`spawnlpe`, :func:`spawnvp`
1698 and :func:`spawnvpe` are not available on Windows.
1699
Georg Brandl116aa622007-08-15 14:28:22 +00001700
1701.. data:: P_NOWAIT
1702 P_NOWAITO
1703
1704 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1705 functions. If either of these values is given, the :func:`spawn\*` functions
Christian Heimesfaf2f632008-01-06 16:59:19 +00001706 will return as soon as the new process has been created, with the process id as
Benjamin Petersond91203b2010-05-06 23:20:40 +00001707 the return value.
1708
1709 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001710
Georg Brandl116aa622007-08-15 14:28:22 +00001711
1712.. data:: P_WAIT
1713
1714 Possible value for the *mode* parameter to the :func:`spawn\*` family of
1715 functions. If this is given as *mode*, the :func:`spawn\*` functions will not
1716 return until the new process has run to completion and will return the exit code
1717 of the process the run is successful, or ``-signal`` if a signal kills the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001718 process.
1719
1720 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001721
Georg Brandl116aa622007-08-15 14:28:22 +00001722
1723.. data:: P_DETACH
1724 P_OVERLAY
1725
1726 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1727 functions. These are less portable than those listed above. :const:`P_DETACH`
1728 is similar to :const:`P_NOWAIT`, but the new process is detached from the
1729 console of the calling process. If :const:`P_OVERLAY` is used, the current
1730 process will be replaced; the :func:`spawn\*` function will not return.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001731
Georg Brandl116aa622007-08-15 14:28:22 +00001732 Availability: Windows.
1733
Georg Brandl116aa622007-08-15 14:28:22 +00001734
1735.. function:: startfile(path[, operation])
1736
1737 Start a file with its associated application.
1738
1739 When *operation* is not specified or ``'open'``, this acts like double-clicking
1740 the file in Windows Explorer, or giving the file name as an argument to the
1741 :program:`start` command from the interactive command shell: the file is opened
1742 with whatever application (if any) its extension is associated.
1743
1744 When another *operation* is given, it must be a "command verb" that specifies
1745 what should be done with the file. Common verbs documented by Microsoft are
1746 ``'print'`` and ``'edit'`` (to be used on files) as well as ``'explore'`` and
1747 ``'find'`` (to be used on directories).
1748
1749 :func:`startfile` returns as soon as the associated application is launched.
1750 There is no option to wait for the application to close, and no way to retrieve
1751 the application's exit status. The *path* parameter is relative to the current
1752 directory. If you want to use an absolute path, make sure the first character
1753 is not a slash (``'/'``); the underlying Win32 :cfunc:`ShellExecute` function
1754 doesn't work if it is. Use the :func:`os.path.normpath` function to ensure that
Benjamin Petersond91203b2010-05-06 23:20:40 +00001755 the path is properly encoded for Win32.
1756
1757 Availability: Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001758
Georg Brandl116aa622007-08-15 14:28:22 +00001759
1760.. function:: system(command)
1761
1762 Execute the command (a string) in a subshell. This is implemented by calling
Georg Brandl628e6f92009-10-27 20:24:45 +00001763 the Standard C function :cfunc:`system`, and has the same limitations.
1764 Changes to :data:`sys.stdin`, etc. are not reflected in the environment of the
1765 executed command.
Georg Brandl116aa622007-08-15 14:28:22 +00001766
1767 On Unix, the return value is the exit status of the process encoded in the
1768 format specified for :func:`wait`. Note that POSIX does not specify the meaning
1769 of the return value of the C :cfunc:`system` function, so the return value of
1770 the Python function is system-dependent.
1771
1772 On Windows, the return value is that returned by the system shell after running
1773 *command*, given by the Windows environment variable :envvar:`COMSPEC`: on
1774 :program:`command.com` systems (Windows 95, 98 and ME) this is always ``0``; on
1775 :program:`cmd.exe` systems (Windows NT, 2000 and XP) this is the exit status of
1776 the command run; on systems using a non-native shell, consult your shell
1777 documentation.
1778
Georg Brandl116aa622007-08-15 14:28:22 +00001779 The :mod:`subprocess` module provides more powerful facilities for spawning new
1780 processes and retrieving their results; using that module is preferable to using
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001781 this function. Use the :mod:`subprocess` module. Check especially the
1782 :ref:`subprocess-replacements` section.
Georg Brandl116aa622007-08-15 14:28:22 +00001783
Benjamin Petersond91203b2010-05-06 23:20:40 +00001784 Availability: Unix, Windows.
1785
Georg Brandl116aa622007-08-15 14:28:22 +00001786
1787.. function:: times()
1788
Benjamin Petersond91203b2010-05-06 23:20:40 +00001789 Return a 5-tuple of floating point numbers indicating accumulated (processor
1790 or other) times, in seconds. The items are: user time, system time,
1791 children's user time, children's system time, and elapsed real time since a
1792 fixed point in the past, in that order. See the Unix manual page
1793 :manpage:`times(2)` or the corresponding Windows Platform API documentation.
1794 On Windows, only the first two items are filled, the others are zero.
1795
1796 Availability: Unix, Windows
Georg Brandl116aa622007-08-15 14:28:22 +00001797
1798
1799.. function:: wait()
1800
1801 Wait for completion of a child process, and return a tuple containing its pid
1802 and exit status indication: a 16-bit number, whose low byte is the signal number
1803 that killed the process, and whose high byte is the exit status (if the signal
1804 number is zero); the high bit of the low byte is set if a core file was
Benjamin Petersond91203b2010-05-06 23:20:40 +00001805 produced.
1806
1807 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001808
1809
1810.. function:: waitpid(pid, options)
1811
1812 The details of this function differ on Unix and Windows.
1813
1814 On Unix: Wait for completion of a child process given by process id *pid*, and
1815 return a tuple containing its process id and exit status indication (encoded as
1816 for :func:`wait`). The semantics of the call are affected by the value of the
1817 integer *options*, which should be ``0`` for normal operation.
1818
1819 If *pid* is greater than ``0``, :func:`waitpid` requests status information for
1820 that specific process. If *pid* is ``0``, the request is for the status of any
1821 child in the process group of the current process. If *pid* is ``-1``, the
1822 request pertains to any child of the current process. If *pid* is less than
1823 ``-1``, status is requested for any process in the process group ``-pid`` (the
1824 absolute value of *pid*).
1825
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001826 An :exc:`OSError` is raised with the value of errno when the syscall
1827 returns -1.
1828
Georg Brandl116aa622007-08-15 14:28:22 +00001829 On Windows: Wait for completion of a process given by process handle *pid*, and
1830 return a tuple containing *pid*, and its exit status shifted left by 8 bits
1831 (shifting makes cross-platform use of the function easier). A *pid* less than or
1832 equal to ``0`` has no special meaning on Windows, and raises an exception. The
1833 value of integer *options* has no effect. *pid* can refer to any process whose
1834 id is known, not necessarily a child process. The :func:`spawn` functions called
1835 with :const:`P_NOWAIT` return suitable process handles.
1836
1837
1838.. function:: wait3([options])
1839
1840 Similar to :func:`waitpid`, except no process id argument is given and a
1841 3-element tuple containing the child's process id, exit status indication, and
1842 resource usage information is returned. Refer to :mod:`resource`.\
1843 :func:`getrusage` for details on resource usage information. The option
1844 argument is the same as that provided to :func:`waitpid` and :func:`wait4`.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001845
Georg Brandl116aa622007-08-15 14:28:22 +00001846 Availability: Unix.
1847
Georg Brandl116aa622007-08-15 14:28:22 +00001848
1849.. function:: wait4(pid, options)
1850
1851 Similar to :func:`waitpid`, except a 3-element tuple, containing the child's
1852 process id, exit status indication, and resource usage information is returned.
1853 Refer to :mod:`resource`.\ :func:`getrusage` for details on resource usage
1854 information. The arguments to :func:`wait4` are the same as those provided to
Benjamin Petersond91203b2010-05-06 23:20:40 +00001855 :func:`waitpid`.
1856
1857 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001858
Georg Brandl116aa622007-08-15 14:28:22 +00001859
1860.. data:: WNOHANG
1861
1862 The option for :func:`waitpid` to return immediately if no child process status
1863 is available immediately. The function returns ``(0, 0)`` in this case.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001864
Georg Brandlc575c902008-09-13 17:46:05 +00001865 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001866
1867
1868.. data:: WCONTINUED
1869
1870 This option causes child processes to be reported if they have been continued
Benjamin Petersond91203b2010-05-06 23:20:40 +00001871 from a job control stop since their status was last reported.
1872
1873 Availability: Some Unix systems.
Georg Brandl116aa622007-08-15 14:28:22 +00001874
Georg Brandl116aa622007-08-15 14:28:22 +00001875
1876.. data:: WUNTRACED
1877
1878 This option causes child processes to be reported if they have been stopped but
Benjamin Petersond91203b2010-05-06 23:20:40 +00001879 their current state has not been reported since they were stopped.
1880
1881 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001882
Georg Brandl116aa622007-08-15 14:28:22 +00001883
1884The following functions take a process status code as returned by
1885:func:`system`, :func:`wait`, or :func:`waitpid` as a parameter. They may be
1886used to determine the disposition of a process.
1887
Georg Brandl116aa622007-08-15 14:28:22 +00001888.. function:: WCOREDUMP(status)
1889
Christian Heimesfaf2f632008-01-06 16:59:19 +00001890 Return ``True`` if a core dump was generated for the process, otherwise
Benjamin Petersond91203b2010-05-06 23:20:40 +00001891 return ``False``.
1892
1893 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001894
Georg Brandl116aa622007-08-15 14:28:22 +00001895
1896.. function:: WIFCONTINUED(status)
1897
Christian Heimesfaf2f632008-01-06 16:59:19 +00001898 Return ``True`` if the process has been continued from a job control stop,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001899 otherwise return ``False``.
1900
1901 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001902
Georg Brandl116aa622007-08-15 14:28:22 +00001903
1904.. function:: WIFSTOPPED(status)
1905
Christian Heimesfaf2f632008-01-06 16:59:19 +00001906 Return ``True`` if the process has been stopped, otherwise return
Benjamin Petersond91203b2010-05-06 23:20:40 +00001907 ``False``.
1908
1909 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001910
1911
1912.. function:: WIFSIGNALED(status)
1913
Christian Heimesfaf2f632008-01-06 16:59:19 +00001914 Return ``True`` if the process exited due to a signal, otherwise return
Benjamin Petersond91203b2010-05-06 23:20:40 +00001915 ``False``.
1916
1917 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001918
1919
1920.. function:: WIFEXITED(status)
1921
Christian Heimesfaf2f632008-01-06 16:59:19 +00001922 Return ``True`` if the process exited using the :manpage:`exit(2)` system call,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001923 otherwise return ``False``.
1924
1925 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001926
1927
1928.. function:: WEXITSTATUS(status)
1929
1930 If ``WIFEXITED(status)`` is true, return the integer parameter to the
1931 :manpage:`exit(2)` system call. Otherwise, the return value is meaningless.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001932
Georg Brandlc575c902008-09-13 17:46:05 +00001933 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001934
1935
1936.. function:: WSTOPSIG(status)
1937
Benjamin Petersond91203b2010-05-06 23:20:40 +00001938 Return the signal which caused the process to stop.
1939
1940 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001941
1942
1943.. function:: WTERMSIG(status)
1944
Benjamin Petersond91203b2010-05-06 23:20:40 +00001945 Return the signal which caused the process to exit.
1946
1947 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001948
1949
1950.. _os-path:
1951
1952Miscellaneous System Information
1953--------------------------------
1954
1955
1956.. function:: confstr(name)
1957
1958 Return string-valued system configuration values. *name* specifies the
1959 configuration value to retrieve; it may be a string which is the name of a
1960 defined system value; these names are specified in a number of standards (POSIX,
1961 Unix 95, Unix 98, and others). Some platforms define additional names as well.
1962 The names known to the host operating system are given as the keys of the
1963 ``confstr_names`` dictionary. For configuration variables not included in that
Benjamin Petersond91203b2010-05-06 23:20:40 +00001964 mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +00001965
1966 If the configuration value specified by *name* isn't defined, ``None`` is
1967 returned.
1968
1969 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1970 specific value for *name* is not supported by the host system, even if it is
1971 included in ``confstr_names``, an :exc:`OSError` is raised with
1972 :const:`errno.EINVAL` for the error number.
1973
Benjamin Petersond91203b2010-05-06 23:20:40 +00001974 Availability: Unix
1975
Georg Brandl116aa622007-08-15 14:28:22 +00001976
1977.. data:: confstr_names
1978
1979 Dictionary mapping names accepted by :func:`confstr` to the integer values
1980 defined for those names by the host operating system. This can be used to
Benjamin Petersond91203b2010-05-06 23:20:40 +00001981 determine the set of names known to the system.
1982
1983 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001984
1985
1986.. function:: getloadavg()
1987
Christian Heimesa62da1d2008-01-12 19:39:10 +00001988 Return the number of processes in the system run queue averaged over the last
1989 1, 5, and 15 minutes or raises :exc:`OSError` if the load average was
Benjamin Petersond91203b2010-05-06 23:20:40 +00001990 unobtainable.
1991
1992 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001993
Georg Brandl116aa622007-08-15 14:28:22 +00001994
1995.. function:: sysconf(name)
1996
1997 Return integer-valued system configuration values. If the configuration value
1998 specified by *name* isn't defined, ``-1`` is returned. The comments regarding
1999 the *name* parameter for :func:`confstr` apply here as well; the dictionary that
2000 provides information on the known names is given by ``sysconf_names``.
Benjamin Petersond91203b2010-05-06 23:20:40 +00002001
Georg Brandlc575c902008-09-13 17:46:05 +00002002 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002003
2004
2005.. data:: sysconf_names
2006
2007 Dictionary mapping names accepted by :func:`sysconf` to the integer values
2008 defined for those names by the host operating system. This can be used to
Benjamin Petersond91203b2010-05-06 23:20:40 +00002009 determine the set of names known to the system.
2010
2011 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002012
Christian Heimesfaf2f632008-01-06 16:59:19 +00002013The following data values are used to support path manipulation operations. These
Georg Brandl116aa622007-08-15 14:28:22 +00002014are defined for all platforms.
2015
2016Higher-level operations on pathnames are defined in the :mod:`os.path` module.
2017
2018
2019.. data:: curdir
2020
2021 The constant string used by the operating system to refer to the current
Georg Brandlc575c902008-09-13 17:46:05 +00002022 directory. This is ``'.'`` for Windows and POSIX. Also available via
2023 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002024
2025
2026.. data:: pardir
2027
2028 The constant string used by the operating system to refer to the parent
Georg Brandlc575c902008-09-13 17:46:05 +00002029 directory. This is ``'..'`` for Windows and POSIX. Also available via
2030 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002031
2032
2033.. data:: sep
2034
Georg Brandlc575c902008-09-13 17:46:05 +00002035 The character used by the operating system to separate pathname components.
2036 This is ``'/'`` for POSIX and ``'\\'`` for Windows. Note that knowing this
2037 is not sufficient to be able to parse or concatenate pathnames --- use
Georg Brandl116aa622007-08-15 14:28:22 +00002038 :func:`os.path.split` and :func:`os.path.join` --- but it is occasionally
2039 useful. Also available via :mod:`os.path`.
2040
2041
2042.. data:: altsep
2043
2044 An alternative character used by the operating system to separate pathname
2045 components, or ``None`` if only one separator character exists. This is set to
2046 ``'/'`` on Windows systems where ``sep`` is a backslash. Also available via
2047 :mod:`os.path`.
2048
2049
2050.. data:: extsep
2051
2052 The character which separates the base filename from the extension; for example,
2053 the ``'.'`` in :file:`os.py`. Also available via :mod:`os.path`.
2054
Georg Brandl116aa622007-08-15 14:28:22 +00002055
2056.. data:: pathsep
2057
2058 The character conventionally used by the operating system to separate search
2059 path components (as in :envvar:`PATH`), such as ``':'`` for POSIX or ``';'`` for
2060 Windows. Also available via :mod:`os.path`.
2061
2062
2063.. data:: defpath
2064
2065 The default search path used by :func:`exec\*p\*` and :func:`spawn\*p\*` if the
2066 environment doesn't have a ``'PATH'`` key. Also available via :mod:`os.path`.
2067
2068
2069.. data:: linesep
2070
2071 The string used to separate (or, rather, terminate) lines on the current
Georg Brandlc575c902008-09-13 17:46:05 +00002072 platform. This may be a single character, such as ``'\n'`` for POSIX, or
2073 multiple characters, for example, ``'\r\n'`` for Windows. Do not use
2074 *os.linesep* as a line terminator when writing files opened in text mode (the
2075 default); use a single ``'\n'`` instead, on all platforms.
Georg Brandl116aa622007-08-15 14:28:22 +00002076
2077
2078.. data:: devnull
2079
Georg Brandlc52eeab2010-05-21 22:05:15 +00002080 The file path of the null device. For example: ``'/dev/null'`` for
2081 POSIX, ``'nul'`` for Windows. Also available via :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002082
Georg Brandl116aa622007-08-15 14:28:22 +00002083
2084.. _os-miscfunc:
2085
2086Miscellaneous Functions
2087-----------------------
2088
2089
2090.. function:: urandom(n)
2091
2092 Return a string of *n* random bytes suitable for cryptographic use.
2093
2094 This function returns random bytes from an OS-specific randomness source. The
2095 returned data should be unpredictable enough for cryptographic applications,
2096 though its exact quality depends on the OS implementation. On a UNIX-like
2097 system this will query /dev/urandom, and on Windows it will use CryptGenRandom.
2098 If a randomness source is not found, :exc:`NotImplementedError` will be raised.