blob: e6bafced8f2220a3d7aae213891f2486b510f84f [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
Georg Brandl23b4f922010-10-06 08:43:56 +000070In Python, file names, command line arguments, and environment variables are
71represented using the string type. On some systems, decoding these strings to
72and from bytes is necessary before passing them to the operating system. Python
73uses the file system encoding to perform this conversion (see
74:func:`sys.getfilesystemencoding`).
Martin v. Löwis011e8422009-05-05 04:43:17 +000075
76.. versionchanged:: 3.1
Georg Brandl23b4f922010-10-06 08:43:56 +000077 On some systems, conversion using the file system encoding may fail. In this
78 case, Python uses the ``surrogateescape`` encoding error handler, which means
79 that undecodable bytes are replaced by a Unicode character U+DCxx on
80 decoding, and these are again translated to the original byte on encoding.
Martin v. Löwis011e8422009-05-05 04:43:17 +000081
82
Georg Brandl23b4f922010-10-06 08:43:56 +000083The file system encoding must guarantee to successfully decode all bytes
84below 128. If the file system encoding fails to provide this guarantee, API
85functions may raise UnicodeErrors.
Martin v. Löwis011e8422009-05-05 04:43:17 +000086
87
Georg Brandl116aa622007-08-15 14:28:22 +000088.. _os-procinfo:
89
90Process Parameters
91------------------
92
93These functions and data items provide information and operate on the current
94process and user.
95
96
97.. data:: environ
98
99 A mapping object representing the string environment. For example,
100 ``environ['HOME']`` is the pathname of your home directory (on some platforms),
101 and is equivalent to ``getenv("HOME")`` in C.
102
103 This mapping is captured the first time the :mod:`os` module is imported,
104 typically during Python startup as part of processing :file:`site.py`. Changes
105 to the environment made after this time are not reflected in ``os.environ``,
106 except for changes made by modifying ``os.environ`` directly.
107
108 If the platform supports the :func:`putenv` function, this mapping may be used
109 to modify the environment as well as query the environment. :func:`putenv` will
110 be called automatically when the mapping is modified.
111
112 .. note::
113
114 Calling :func:`putenv` directly does not change ``os.environ``, so it's better
115 to modify ``os.environ``.
116
117 .. note::
118
Georg Brandlc575c902008-09-13 17:46:05 +0000119 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
120 cause memory leaks. Refer to the system documentation for
121 :cfunc:`putenv`.
Georg Brandl116aa622007-08-15 14:28:22 +0000122
123 If :func:`putenv` is not provided, a modified copy of this mapping may be
124 passed to the appropriate process-creation functions to cause child processes
125 to use a modified environment.
126
Georg Brandl9afde1c2007-11-01 20:32:30 +0000127 If the platform supports the :func:`unsetenv` function, you can delete items in
Georg Brandl116aa622007-08-15 14:28:22 +0000128 this mapping to unset environment variables. :func:`unsetenv` will be called
Georg Brandl9afde1c2007-11-01 20:32:30 +0000129 automatically when an item is deleted from ``os.environ``, and when
130 one of the :meth:`pop` or :meth:`clear` methods is called.
131
Georg Brandl116aa622007-08-15 14:28:22 +0000132
133.. function:: chdir(path)
134 fchdir(fd)
135 getcwd()
136 :noindex:
137
138 These functions are described in :ref:`os-file-dir`.
139
140
141.. function:: ctermid()
142
143 Return the filename corresponding to the controlling terminal of the process.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000144
Georg Brandl116aa622007-08-15 14:28:22 +0000145 Availability: Unix.
146
147
148.. function:: getegid()
149
150 Return the effective group id of the current process. This corresponds to the
Benjamin Petersond91203b2010-05-06 23:20:40 +0000151 "set id" bit on the file being executed in the current process.
152
153 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000154
155
156.. function:: geteuid()
157
158 .. index:: single: user; effective id
159
Benjamin Petersond91203b2010-05-06 23:20:40 +0000160 Return the current process's effective user id.
161
162 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000163
164
165.. function:: getgid()
166
167 .. index:: single: process; group
168
Benjamin Petersond91203b2010-05-06 23:20:40 +0000169 Return the real group id of the current process.
170
171 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000172
173
174.. function:: getgroups()
175
176 Return list of supplemental group ids associated with the current process.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000177
Georg Brandl116aa622007-08-15 14:28:22 +0000178 Availability: Unix.
179
180
181.. function:: getlogin()
182
183 Return the name of the user logged in on the controlling terminal of the
184 process. For most purposes, it is more useful to use the environment variable
185 :envvar:`LOGNAME` to find out who the user is, or
186 ``pwd.getpwuid(os.getuid())[0]`` to get the login name of the currently
Benjamin Petersond91203b2010-05-06 23:20:40 +0000187 effective user id.
188
189 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000190
191
192.. function:: getpgid(pid)
193
194 Return the process group id of the process with process id *pid*. If *pid* is 0,
Benjamin Petersond91203b2010-05-06 23:20:40 +0000195 the process group id of the current process is returned.
Georg Brandl116aa622007-08-15 14:28:22 +0000196
Benjamin Petersond91203b2010-05-06 23:20:40 +0000197 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000198
199.. function:: getpgrp()
200
201 .. index:: single: process; group
202
Benjamin Petersond91203b2010-05-06 23:20:40 +0000203 Return the id of the current process group.
204
205 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000206
207
208.. function:: getpid()
209
210 .. index:: single: process; id
211
Benjamin Petersond91203b2010-05-06 23:20:40 +0000212 Return the current process id.
213
214 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000215
216
217.. function:: getppid()
218
219 .. index:: single: process; id of parent
220
Benjamin Petersond91203b2010-05-06 23:20:40 +0000221 Return the parent's process id.
222
223 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000224
225
226.. function:: getuid()
227
228 .. index:: single: user; id
229
Benjamin Petersond91203b2010-05-06 23:20:40 +0000230 Return the current process's user id.
231
232 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000233
234
235.. function:: getenv(varname[, value])
236
237 Return the value of the environment variable *varname* if it exists, or *value*
Benjamin Petersond91203b2010-05-06 23:20:40 +0000238 if it doesn't. *value* defaults to ``None``.
239
240 Availability: most flavors of Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000241
242
243.. function:: putenv(varname, value)
244
245 .. index:: single: environment variables; setting
246
247 Set the environment variable named *varname* to the string *value*. Such
248 changes to the environment affect subprocesses started with :func:`os.system`,
Benjamin Petersond91203b2010-05-06 23:20:40 +0000249 :func:`popen` or :func:`fork` and :func:`execv`.
250
251 Availability: most flavors of Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000252
253 .. note::
254
Georg Brandlc575c902008-09-13 17:46:05 +0000255 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
256 cause memory leaks. Refer to the system documentation for putenv.
Georg Brandl116aa622007-08-15 14:28:22 +0000257
258 When :func:`putenv` is supported, assignments to items in ``os.environ`` are
259 automatically translated into corresponding calls to :func:`putenv`; however,
260 calls to :func:`putenv` don't update ``os.environ``, so it is actually
261 preferable to assign to items of ``os.environ``.
262
263
264.. function:: setegid(egid)
265
Benjamin Petersond91203b2010-05-06 23:20:40 +0000266 Set the current process's effective group id.
267
268 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000269
270
271.. function:: seteuid(euid)
272
Benjamin Petersond91203b2010-05-06 23:20:40 +0000273 Set the current process's effective user id.
274
275 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000276
277
278.. function:: setgid(gid)
279
Benjamin Petersond91203b2010-05-06 23:20:40 +0000280 Set the current process' group id.
281
282 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000283
284
285.. function:: setgroups(groups)
286
287 Set the list of supplemental group ids associated with the current process to
288 *groups*. *groups* must be a sequence, and each element must be an integer
Christian Heimesfaf2f632008-01-06 16:59:19 +0000289 identifying a group. This operation is typically available only to the superuser.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000290
Georg Brandl116aa622007-08-15 14:28:22 +0000291 Availability: Unix.
292
Georg Brandl116aa622007-08-15 14:28:22 +0000293
294.. function:: setpgrp()
295
Christian Heimesfaf2f632008-01-06 16:59:19 +0000296 Call the system call :cfunc:`setpgrp` or :cfunc:`setpgrp(0, 0)` depending on
Georg Brandl116aa622007-08-15 14:28:22 +0000297 which version is implemented (if any). See the Unix manual for the semantics.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000298
Georg Brandl116aa622007-08-15 14:28:22 +0000299 Availability: Unix.
300
301
302.. function:: setpgid(pid, pgrp)
303
Christian Heimesfaf2f632008-01-06 16:59:19 +0000304 Call the system call :cfunc:`setpgid` to set the process group id of the
Georg Brandl116aa622007-08-15 14:28:22 +0000305 process with id *pid* to the process group with id *pgrp*. See the Unix manual
Benjamin Petersond91203b2010-05-06 23:20:40 +0000306 for the semantics.
307
308 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000309
310
311.. function:: setreuid(ruid, euid)
312
Benjamin Petersond91203b2010-05-06 23:20:40 +0000313 Set the current process's real and effective user ids.
314
315 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000316
317
318.. function:: setregid(rgid, egid)
319
Benjamin Petersond91203b2010-05-06 23:20:40 +0000320 Set the current process's real and effective group ids.
321
322 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000323
324
325.. function:: getsid(pid)
326
Christian Heimesfaf2f632008-01-06 16:59:19 +0000327 Call the system call :cfunc:`getsid`. See the Unix manual for the semantics.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000328
Georg Brandl116aa622007-08-15 14:28:22 +0000329 Availability: Unix.
330
Georg Brandl116aa622007-08-15 14:28:22 +0000331
332.. function:: setsid()
333
Christian Heimesfaf2f632008-01-06 16:59:19 +0000334 Call the system call :cfunc:`setsid`. See the Unix manual for the semantics.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000335
Georg Brandl116aa622007-08-15 14:28:22 +0000336 Availability: Unix.
337
338
339.. function:: setuid(uid)
340
341 .. index:: single: user; id, setting
342
Benjamin Petersond91203b2010-05-06 23:20:40 +0000343 Set the current process's user id.
344
345 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000346
Georg Brandl116aa622007-08-15 14:28:22 +0000347
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000348.. placed in this section since it relates to errno.... a little weak
Georg Brandl116aa622007-08-15 14:28:22 +0000349.. function:: strerror(code)
350
351 Return the error message corresponding to the error code in *code*.
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000352 On platforms where :cfunc:`strerror` returns ``NULL`` when given an unknown
Benjamin Petersond91203b2010-05-06 23:20:40 +0000353 error number, :exc:`ValueError` is raised.
354
355 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000356
357
358.. function:: umask(mask)
359
Benjamin Petersond91203b2010-05-06 23:20:40 +0000360 Set the current numeric umask and return the previous umask.
361
362 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000363
364
365.. function:: uname()
366
367 .. index::
368 single: gethostname() (in module socket)
369 single: gethostbyaddr() (in module socket)
370
371 Return a 5-tuple containing information identifying the current operating
372 system. The tuple contains 5 strings: ``(sysname, nodename, release, version,
373 machine)``. Some systems truncate the nodename to 8 characters or to the
374 leading component; a better way to get the hostname is
375 :func:`socket.gethostname` or even
Benjamin Petersond91203b2010-05-06 23:20:40 +0000376 ``socket.gethostbyaddr(socket.gethostname())``.
377
378 Availability: recent flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000379
380
381.. function:: unsetenv(varname)
382
383 .. index:: single: environment variables; deleting
384
385 Unset (delete) the environment variable named *varname*. Such changes to the
386 environment affect subprocesses started with :func:`os.system`, :func:`popen` or
Benjamin Petersond91203b2010-05-06 23:20:40 +0000387 :func:`fork` and :func:`execv`.
Georg Brandl116aa622007-08-15 14:28:22 +0000388
389 When :func:`unsetenv` is supported, deletion of items in ``os.environ`` is
390 automatically translated into a corresponding call to :func:`unsetenv`; however,
391 calls to :func:`unsetenv` don't update ``os.environ``, so it is actually
392 preferable to delete items of ``os.environ``.
393
Benjamin Petersond91203b2010-05-06 23:20:40 +0000394 Availability: most flavors of Unix, Windows.
395
Georg Brandl116aa622007-08-15 14:28:22 +0000396
397.. _os-newstreams:
398
399File Object Creation
400--------------------
401
Antoine Pitrou25d535e2010-09-15 11:25:11 +0000402These functions create new :term:`file objects <file object>`. (See also :func:`open`.)
Georg Brandl116aa622007-08-15 14:28:22 +0000403
404
405.. function:: fdopen(fd[, mode[, bufsize]])
406
407 .. index:: single: I/O control; buffering
408
409 Return an open file object connected to the file descriptor *fd*. The *mode*
410 and *bufsize* arguments have the same meaning as the corresponding arguments to
Benjamin Petersond91203b2010-05-06 23:20:40 +0000411 the built-in :func:`open` function.
Georg Brandl116aa622007-08-15 14:28:22 +0000412
Georg Brandl55ac8f02007-09-01 13:51:09 +0000413 When specified, the *mode* argument must start with one of the letters
414 ``'r'``, ``'w'``, or ``'a'``, otherwise a :exc:`ValueError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000415
Georg Brandl55ac8f02007-09-01 13:51:09 +0000416 On Unix, when the *mode* argument starts with ``'a'``, the *O_APPEND* flag is
417 set on the file descriptor (which the :cfunc:`fdopen` implementation already
418 does on most platforms).
Georg Brandl116aa622007-08-15 14:28:22 +0000419
Benjamin Petersond91203b2010-05-06 23:20:40 +0000420 Availability: Unix, Windows.
421
Georg Brandl116aa622007-08-15 14:28:22 +0000422
Georg Brandl116aa622007-08-15 14:28:22 +0000423.. _os-fd-ops:
424
425File Descriptor Operations
426--------------------------
427
428These functions operate on I/O streams referenced using file descriptors.
429
430File descriptors are small integers corresponding to a file that has been opened
431by the current process. For example, standard input is usually file descriptor
4320, standard output is 1, and standard error is 2. Further files opened by a
433process will then be assigned 3, 4, 5, and so forth. The name "file descriptor"
434is slightly deceptive; on Unix platforms, sockets and pipes are also referenced
435by file descriptors.
436
Antoine Pitrou25d535e2010-09-15 11:25:11 +0000437The :meth:`~file.fileno` method can be used to obtain the file descriptor
438associated with a :term:`file object` when required. Note that using the file
439descriptor directly will bypass the file object methods, ignoring aspects such
440as internal buffering of data.
Georg Brandl116aa622007-08-15 14:28:22 +0000441
442.. function:: close(fd)
443
Benjamin Petersond91203b2010-05-06 23:20:40 +0000444 Close file descriptor *fd*.
445
446 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000447
448 .. note::
449
450 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000451 descriptor as returned by :func:`os.open` or :func:`pipe`. To close a "file
Georg Brandl116aa622007-08-15 14:28:22 +0000452 object" returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000453 :func:`fdopen`, use its :meth:`~file.close` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000454
455
Christian Heimesfdab48e2008-01-20 09:06:41 +0000456.. function:: closerange(fd_low, fd_high)
457
458 Close all file descriptors from *fd_low* (inclusive) to *fd_high* (exclusive),
Benjamin Petersond91203b2010-05-06 23:20:40 +0000459 ignoring errors. Equivalent to::
Christian Heimesfdab48e2008-01-20 09:06:41 +0000460
Georg Brandl7baf6252009-09-01 08:13:16 +0000461 for fd in range(fd_low, fd_high):
Christian Heimesfdab48e2008-01-20 09:06:41 +0000462 try:
463 os.close(fd)
464 except OSError:
465 pass
466
Benjamin Petersond91203b2010-05-06 23:20:40 +0000467 Availability: Unix, Windows.
468
Christian Heimesfdab48e2008-01-20 09:06:41 +0000469
Georg Brandl81f11302007-12-21 08:45:42 +0000470.. function:: device_encoding(fd)
471
472 Return a string describing the encoding of the device associated with *fd*
473 if it is connected to a terminal; else return :const:`None`.
474
475
Georg Brandl116aa622007-08-15 14:28:22 +0000476.. function:: dup(fd)
477
Benjamin Petersond91203b2010-05-06 23:20:40 +0000478 Return a duplicate of file descriptor *fd*.
479
480 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000481
482
483.. function:: dup2(fd, fd2)
484
485 Duplicate file descriptor *fd* to *fd2*, closing the latter first if necessary.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000486
Georg Brandlc575c902008-09-13 17:46:05 +0000487 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000488
489
Christian Heimes4e30a842007-11-30 22:12:06 +0000490.. function:: fchmod(fd, mode)
491
492 Change the mode of the file given by *fd* to the numeric *mode*. See the docs
Benjamin Petersond91203b2010-05-06 23:20:40 +0000493 for :func:`chmod` for possible values of *mode*.
494
495 Availability: Unix.
Christian Heimes4e30a842007-11-30 22:12:06 +0000496
497
498.. function:: fchown(fd, uid, gid)
499
500 Change the owner and group id of the file given by *fd* to the numeric *uid*
501 and *gid*. To leave one of the ids unchanged, set it to -1.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000502
Christian Heimes4e30a842007-11-30 22:12:06 +0000503 Availability: Unix.
504
505
Georg Brandl116aa622007-08-15 14:28:22 +0000506.. function:: fdatasync(fd)
507
508 Force write of file with filedescriptor *fd* to disk. Does not force update of
Benjamin Petersond91203b2010-05-06 23:20:40 +0000509 metadata.
510
511 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000512
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000513 .. note::
514 This function is not available on MacOS.
515
Georg Brandl116aa622007-08-15 14:28:22 +0000516
517.. function:: fpathconf(fd, name)
518
519 Return system configuration information relevant to an open file. *name*
520 specifies the configuration value to retrieve; it may be a string which is the
521 name of a defined system value; these names are specified in a number of
522 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
523 additional names as well. The names known to the host operating system are
524 given in the ``pathconf_names`` dictionary. For configuration variables not
525 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +0000526
527 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
528 specific value for *name* is not supported by the host system, even if it is
529 included in ``pathconf_names``, an :exc:`OSError` is raised with
530 :const:`errno.EINVAL` for the error number.
531
Benjamin Petersond91203b2010-05-06 23:20:40 +0000532 Availability: Unix.
533
Georg Brandl116aa622007-08-15 14:28:22 +0000534
535.. function:: fstat(fd)
536
R. David Murray43b2f452011-02-11 03:13:19 +0000537 Return status for file descriptor *fd*, like :func:`~os.stat`.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000538
539 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000540
541
542.. function:: fstatvfs(fd)
543
544 Return information about the filesystem containing the file associated with file
Benjamin Petersond91203b2010-05-06 23:20:40 +0000545 descriptor *fd*, like :func:`statvfs`.
546
547 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000548
549
550.. function:: fsync(fd)
551
552 Force write of file with filedescriptor *fd* to disk. On Unix, this calls the
553 native :cfunc:`fsync` function; on Windows, the MS :cfunc:`_commit` function.
554
Antoine Pitrou25d535e2010-09-15 11:25:11 +0000555 If you're starting with a buffered Python :term:`file object` *f*, first do
556 ``f.flush()``, and then do ``os.fsync(f.fileno())``, to ensure that all internal
557 buffers associated with *f* are written to disk.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000558
559 Availability: Unix, and Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000560
561
562.. function:: ftruncate(fd, length)
563
564 Truncate the file corresponding to file descriptor *fd*, so that it is at most
Benjamin Petersond91203b2010-05-06 23:20:40 +0000565 *length* bytes in size.
566
567 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000568
569
570.. function:: isatty(fd)
571
572 Return ``True`` if the file descriptor *fd* is open and connected to a
Benjamin Petersond91203b2010-05-06 23:20:40 +0000573 tty(-like) device, else ``False``.
574
575 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000576
577
578.. function:: lseek(fd, pos, how)
579
Christian Heimesfaf2f632008-01-06 16:59:19 +0000580 Set the current position of file descriptor *fd* to position *pos*, modified
581 by *how*: :const:`SEEK_SET` or ``0`` to set the position relative to the
582 beginning of the file; :const:`SEEK_CUR` or ``1`` to set it relative to the
583 current position; :const:`os.SEEK_END` or ``2`` to set it relative to the end of
Benjamin Petersond91203b2010-05-06 23:20:40 +0000584 the file.
585
586 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000587
588
Georg Brandl6c8583f2010-05-19 21:22:58 +0000589.. data:: SEEK_SET
590 SEEK_CUR
591 SEEK_END
592
593 Parameters to the :func:`lseek` function. Their values are 0, 1, and 2,
594 respectively. Availability: Windows, Unix.
595
596
Georg Brandl116aa622007-08-15 14:28:22 +0000597.. function:: open(file, flags[, mode])
598
Georg Brandlf4a41232008-05-26 17:55:52 +0000599 Open the file *file* and set various flags according to *flags* and possibly
600 its mode according to *mode*. The default *mode* is ``0o777`` (octal), and
601 the current umask value is first masked out. Return the file descriptor for
Benjamin Petersond91203b2010-05-06 23:20:40 +0000602 the newly opened file.
Georg Brandl116aa622007-08-15 14:28:22 +0000603
604 For a description of the flag and mode values, see the C run-time documentation;
605 flag constants (like :const:`O_RDONLY` and :const:`O_WRONLY`) are defined in
Georg Brandl6c8583f2010-05-19 21:22:58 +0000606 this module too (see :ref:`open-constants`). In particular, on Windows adding
607 :const:`O_BINARY` is needed to open files in binary mode.
Georg Brandl116aa622007-08-15 14:28:22 +0000608
Benjamin Petersond91203b2010-05-06 23:20:40 +0000609 Availability: Unix, Windows.
610
Georg Brandl116aa622007-08-15 14:28:22 +0000611 .. note::
612
Georg Brandlc5605df2009-08-13 08:26:44 +0000613 This function is intended for low-level I/O. For normal usage, use the
Antoine Pitrou25d535e2010-09-15 11:25:11 +0000614 built-in function :func:`open`, which returns a :term:`file object` with
Benjamin Petersond91203b2010-05-06 23:20:40 +0000615 :meth:`~file.read` and :meth:`~file.wprite` methods (and many more). To
Antoine Pitrou25d535e2010-09-15 11:25:11 +0000616 wrap a file descriptor in a file object, use :func:`fdopen`.
Georg Brandl116aa622007-08-15 14:28:22 +0000617
618
619.. function:: openpty()
620
621 .. index:: module: pty
622
623 Open a new pseudo-terminal pair. Return a pair of file descriptors ``(master,
624 slave)`` for the pty and the tty, respectively. For a (slightly) more portable
Benjamin Petersond91203b2010-05-06 23:20:40 +0000625 approach, use the :mod:`pty` module.
626
627 Availability: some flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000628
629
630.. function:: pipe()
631
632 Create a pipe. Return a pair of file descriptors ``(r, w)`` usable for reading
Benjamin Petersond91203b2010-05-06 23:20:40 +0000633 and writing, respectively.
634
635 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000636
637
638.. function:: read(fd, n)
639
Georg Brandlc5605df2009-08-13 08:26:44 +0000640 Read at most *n* bytes from file descriptor *fd*. Return a bytestring containing the
Georg Brandl116aa622007-08-15 14:28:22 +0000641 bytes read. If the end of the file referred to by *fd* has been reached, an
Benjamin Petersond91203b2010-05-06 23:20:40 +0000642 empty bytes object is returned.
643
644 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000645
646 .. note::
647
648 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000649 descriptor as returned by :func:`os.open` or :func:`pipe`. To read a "file object"
Georg Brandl116aa622007-08-15 14:28:22 +0000650 returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000651 :func:`fdopen`, or :data:`sys.stdin`, use its :meth:`~file.read` or
652 :meth:`~file.readline` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000653
654
655.. function:: tcgetpgrp(fd)
656
657 Return the process group associated with the terminal given by *fd* (an open
Benjamin Petersond91203b2010-05-06 23:20:40 +0000658 file descriptor as returned by :func:`os.open`).
659
660 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000661
662
663.. function:: tcsetpgrp(fd, pg)
664
665 Set the process group associated with the terminal given by *fd* (an open file
Benjamin Petersond91203b2010-05-06 23:20:40 +0000666 descriptor as returned by :func:`os.open`) to *pg*.
667
668 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000669
670
671.. function:: ttyname(fd)
672
673 Return a string which specifies the terminal device associated with
Georg Brandl9afde1c2007-11-01 20:32:30 +0000674 file descriptor *fd*. If *fd* is not associated with a terminal device, an
Benjamin Petersond91203b2010-05-06 23:20:40 +0000675 exception is raised.
676
677 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000678
679
680.. function:: write(fd, str)
681
Georg Brandlc5605df2009-08-13 08:26:44 +0000682 Write the bytestring in *str* to file descriptor *fd*. Return the number of
Benjamin Petersond91203b2010-05-06 23:20:40 +0000683 bytes actually written.
684
685 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000686
687 .. note::
688
689 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000690 descriptor as returned by :func:`os.open` or :func:`pipe`. To write a "file
Georg Brandl116aa622007-08-15 14:28:22 +0000691 object" returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000692 :func:`fdopen`, or :data:`sys.stdout` or :data:`sys.stderr`, use its
693 :meth:`~file.write` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000694
Georg Brandl6c8583f2010-05-19 21:22:58 +0000695
696.. _open-constants:
697
698``open()`` flag constants
699~~~~~~~~~~~~~~~~~~~~~~~~~
700
Georg Brandlaf265f42008-12-07 15:06:20 +0000701The following constants are options for the *flags* parameter to the
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000702:func:`~os.open` function. They can be combined using the bitwise OR operator
Georg Brandlaf265f42008-12-07 15:06:20 +0000703``|``. Some of them are not available on all platforms. For descriptions of
704their availability and use, consult the :manpage:`open(2)` manual page on Unix
Doug Hellmann14214262009-09-21 12:16:43 +0000705or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000706
707
708.. data:: O_RDONLY
709 O_WRONLY
710 O_RDWR
711 O_APPEND
712 O_CREAT
713 O_EXCL
714 O_TRUNC
715
Georg Brandlaf265f42008-12-07 15:06:20 +0000716 These constants are available on Unix and Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000717
718
719.. data:: O_DSYNC
720 O_RSYNC
721 O_SYNC
722 O_NDELAY
723 O_NONBLOCK
724 O_NOCTTY
725 O_SHLOCK
726 O_EXLOCK
727
Georg Brandlaf265f42008-12-07 15:06:20 +0000728 These constants are only available on Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000729
730
731.. data:: O_BINARY
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000732 O_NOINHERIT
Georg Brandl116aa622007-08-15 14:28:22 +0000733 O_SHORT_LIVED
734 O_TEMPORARY
735 O_RANDOM
736 O_SEQUENTIAL
737 O_TEXT
738
Georg Brandlaf265f42008-12-07 15:06:20 +0000739 These constants are only available on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000740
741
Alexandre Vassalottibee32532008-05-16 18:15:12 +0000742.. data:: O_ASYNC
743 O_DIRECT
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000744 O_DIRECTORY
745 O_NOFOLLOW
746 O_NOATIME
747
Georg Brandlaf265f42008-12-07 15:06:20 +0000748 These constants are GNU extensions and not present if they are not defined by
749 the C library.
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000750
751
Georg Brandl116aa622007-08-15 14:28:22 +0000752.. _os-file-dir:
753
754Files and Directories
755---------------------
756
Georg Brandl116aa622007-08-15 14:28:22 +0000757.. function:: access(path, mode)
758
759 Use the real uid/gid to test for access to *path*. Note that most operations
760 will use the effective uid/gid, therefore this routine can be used in a
761 suid/sgid environment to test if the invoking user has the specified access to
762 *path*. *mode* should be :const:`F_OK` to test the existence of *path*, or it
763 can be the inclusive OR of one or more of :const:`R_OK`, :const:`W_OK`, and
764 :const:`X_OK` to test permissions. Return :const:`True` if access is allowed,
765 :const:`False` if not. See the Unix man page :manpage:`access(2)` for more
Benjamin Petersond91203b2010-05-06 23:20:40 +0000766 information.
767
768 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000769
770 .. note::
771
Georg Brandlc5605df2009-08-13 08:26:44 +0000772 Using :func:`access` to check if a user is authorized to e.g. open a file
773 before actually doing so using :func:`open` creates a security hole,
774 because the user might exploit the short time interval between checking
Benjamin Peterson249b5082011-05-20 11:41:13 -0500775 and opening the file to manipulate it. It's preferable to use :term:`EAFP`
776 techniques. For example::
777
778 if os.access("myfile", os.R_OK):
779 with open("myfile") as fp:
780 return fp.read()
781 return "some default data"
782
783 is better written as::
784
785 try:
786 fp = open("myfile")
787 except OSError as e:
788 if e.errno == errno.EACCESS:
789 return "some default data"
790 # Not a permission error.
791 raise
792 else:
793 with fp:
794 return fp.read()
Georg Brandl116aa622007-08-15 14:28:22 +0000795
796 .. note::
797
798 I/O operations may fail even when :func:`access` indicates that they would
799 succeed, particularly for operations on network filesystems which may have
800 permissions semantics beyond the usual POSIX permission-bit model.
801
802
803.. data:: F_OK
804
805 Value to pass as the *mode* parameter of :func:`access` to test the existence of
806 *path*.
807
808
809.. data:: R_OK
810
811 Value to include in the *mode* parameter of :func:`access` to test the
812 readability of *path*.
813
814
815.. data:: W_OK
816
817 Value to include in the *mode* parameter of :func:`access` to test the
818 writability of *path*.
819
820
821.. data:: X_OK
822
823 Value to include in the *mode* parameter of :func:`access` to determine if
824 *path* can be executed.
825
826
827.. function:: chdir(path)
828
829 .. index:: single: directory; changing
830
Benjamin Petersond91203b2010-05-06 23:20:40 +0000831 Change the current working directory to *path*.
832
833 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000834
835
836.. function:: fchdir(fd)
837
838 Change the current working directory to the directory represented by the file
839 descriptor *fd*. The descriptor must refer to an opened directory, not an open
Benjamin Petersond91203b2010-05-06 23:20:40 +0000840 file.
841
842 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000843
Georg Brandl116aa622007-08-15 14:28:22 +0000844
845.. function:: getcwd()
846
Martin v. Löwis011e8422009-05-05 04:43:17 +0000847 Return a string representing the current working directory.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000848
Martin v. Löwis011e8422009-05-05 04:43:17 +0000849 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000850
Benjamin Petersond91203b2010-05-06 23:20:40 +0000851
Martin v. Löwisa731b992008-10-07 06:36:31 +0000852.. function:: getcwdb()
Georg Brandl116aa622007-08-15 14:28:22 +0000853
Georg Brandl76e55382008-10-08 16:34:57 +0000854 Return a bytestring representing the current working directory.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000855
Georg Brandlc575c902008-09-13 17:46:05 +0000856 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000857
Georg Brandl116aa622007-08-15 14:28:22 +0000858
859.. function:: chflags(path, flags)
860
861 Set the flags of *path* to the numeric *flags*. *flags* may take a combination
862 (bitwise OR) of the following values (as defined in the :mod:`stat` module):
863
R David Murray30178062011-03-10 17:18:33 -0500864 * :data:`stat.UF_NODUMP`
865 * :data:`stat.UF_IMMUTABLE`
866 * :data:`stat.UF_APPEND`
867 * :data:`stat.UF_OPAQUE`
868 * :data:`stat.UF_NOUNLINK`
869 * :data:`stat.SF_ARCHIVED`
870 * :data:`stat.SF_IMMUTABLE`
871 * :data:`stat.SF_APPEND`
872 * :data:`stat.SF_NOUNLINK`
873 * :data:`stat.SF_SNAPSHOT`
Georg Brandl116aa622007-08-15 14:28:22 +0000874
Georg Brandlc575c902008-09-13 17:46:05 +0000875 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000876
Georg Brandl116aa622007-08-15 14:28:22 +0000877
878.. function:: chroot(path)
879
880 Change the root directory of the current process to *path*. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +0000881 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000882
Georg Brandl116aa622007-08-15 14:28:22 +0000883
884.. function:: chmod(path, mode)
885
886 Change the mode of *path* to the numeric *mode*. *mode* may take one of the
Christian Heimesfaf2f632008-01-06 16:59:19 +0000887 following values (as defined in the :mod:`stat` module) or bitwise ORed
Georg Brandl116aa622007-08-15 14:28:22 +0000888 combinations of them:
889
R. David Murrayba426142009-07-21 14:29:59 +0000890 * :data:`stat.S_ISUID`
891 * :data:`stat.S_ISGID`
892 * :data:`stat.S_ENFMT`
893 * :data:`stat.S_ISVTX`
894 * :data:`stat.S_IREAD`
895 * :data:`stat.S_IWRITE`
896 * :data:`stat.S_IEXEC`
897 * :data:`stat.S_IRWXU`
898 * :data:`stat.S_IRUSR`
899 * :data:`stat.S_IWUSR`
900 * :data:`stat.S_IXUSR`
901 * :data:`stat.S_IRWXG`
902 * :data:`stat.S_IRGRP`
903 * :data:`stat.S_IWGRP`
904 * :data:`stat.S_IXGRP`
905 * :data:`stat.S_IRWXO`
906 * :data:`stat.S_IROTH`
907 * :data:`stat.S_IWOTH`
908 * :data:`stat.S_IXOTH`
Georg Brandl116aa622007-08-15 14:28:22 +0000909
Georg Brandlc575c902008-09-13 17:46:05 +0000910 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000911
912 .. note::
913
914 Although Windows supports :func:`chmod`, you can only set the file's read-only
915 flag with it (via the ``stat.S_IWRITE`` and ``stat.S_IREAD``
916 constants or a corresponding integer value). All other bits are
917 ignored.
918
919
920.. function:: chown(path, uid, gid)
921
922 Change the owner and group id of *path* to the numeric *uid* and *gid*. To leave
Benjamin Petersond91203b2010-05-06 23:20:40 +0000923 one of the ids unchanged, set it to -1.
924
925 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000926
927
928.. function:: lchflags(path, flags)
929
930 Set the flags of *path* to the numeric *flags*, like :func:`chflags`, but do not
Benjamin Petersond91203b2010-05-06 23:20:40 +0000931 follow symbolic links.
932
933 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000934
Georg Brandl116aa622007-08-15 14:28:22 +0000935
Christian Heimes93852662007-12-01 12:22:32 +0000936.. function:: lchmod(path, mode)
937
938 Change the mode of *path* to the numeric *mode*. If path is a symlink, this
939 affects the symlink rather than the target. See the docs for :func:`chmod`
Benjamin Petersond91203b2010-05-06 23:20:40 +0000940 for possible values of *mode*.
941
942 Availability: Unix.
Christian Heimes93852662007-12-01 12:22:32 +0000943
Christian Heimes93852662007-12-01 12:22:32 +0000944
Georg Brandl116aa622007-08-15 14:28:22 +0000945.. function:: lchown(path, uid, gid)
946
Christian Heimesfaf2f632008-01-06 16:59:19 +0000947 Change the owner and group id of *path* to the numeric *uid* and *gid*. This
Benjamin Petersond91203b2010-05-06 23:20:40 +0000948 function will not follow symbolic links.
949
950 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000951
Georg Brandl116aa622007-08-15 14:28:22 +0000952
Benjamin Peterson5879d412009-03-30 14:51:56 +0000953.. function:: link(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +0000954
Benjamin Petersond91203b2010-05-06 23:20:40 +0000955 Create a hard link pointing to *source* named *link_name*.
956
957 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000958
959
960.. function:: listdir(path)
961
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000962 Return a list containing the names of the entries in the directory given by
963 *path*. The list is in arbitrary order. It does not include the special
964 entries ``'.'`` and ``'..'`` even if they are present in the directory.
Georg Brandl116aa622007-08-15 14:28:22 +0000965
Martin v. Löwis011e8422009-05-05 04:43:17 +0000966 This function can be called with a bytes or string argument, and returns
967 filenames of the same datatype.
Georg Brandl116aa622007-08-15 14:28:22 +0000968
Benjamin Petersond91203b2010-05-06 23:20:40 +0000969 Availability: Unix, Windows.
970
Georg Brandl116aa622007-08-15 14:28:22 +0000971
972.. function:: lstat(path)
973
Georg Brandlc60e3062011-02-12 07:32:02 +0000974 Perform the equivalent of an :cfunc:`lstat` system call on the given path.
R. David Murray43b2f452011-02-11 03:13:19 +0000975 Similar to :func:`~os.stat`, but does not follow symbolic links. On
976 platforms that do not support symbolic links, this is an alias for
977 :func:`~os.stat`.
Georg Brandl116aa622007-08-15 14:28:22 +0000978
979
980.. function:: mkfifo(path[, mode])
981
Georg Brandlf4a41232008-05-26 17:55:52 +0000982 Create a FIFO (a named pipe) named *path* with numeric mode *mode*. The
983 default *mode* is ``0o666`` (octal). The current umask value is first masked
Benjamin Petersond91203b2010-05-06 23:20:40 +0000984 out from the mode.
Georg Brandl116aa622007-08-15 14:28:22 +0000985
986 FIFOs are pipes that can be accessed like regular files. FIFOs exist until they
987 are deleted (for example with :func:`os.unlink`). Generally, FIFOs are used as
988 rendezvous between "client" and "server" type processes: the server opens the
989 FIFO for reading, and the client opens it for writing. Note that :func:`mkfifo`
990 doesn't open the FIFO --- it just creates the rendezvous point.
991
Benjamin Petersond91203b2010-05-06 23:20:40 +0000992 Availability: Unix.
993
Georg Brandl116aa622007-08-15 14:28:22 +0000994
Georg Brandlf4a41232008-05-26 17:55:52 +0000995.. function:: mknod(filename[, mode=0o600, device])
Georg Brandl116aa622007-08-15 14:28:22 +0000996
997 Create a filesystem node (file, device special file or named pipe) named
998 *filename*. *mode* specifies both the permissions to use and the type of node to
999 be created, being combined (bitwise OR) with one of ``stat.S_IFREG``,
1000 ``stat.S_IFCHR``, ``stat.S_IFBLK``,
1001 and ``stat.S_IFIFO`` (those constants are available in :mod:`stat`).
1002 For ``stat.S_IFCHR`` and
1003 ``stat.S_IFBLK``, *device* defines the newly created device special file (probably using
1004 :func:`os.makedev`), otherwise it is ignored.
1005
Georg Brandl116aa622007-08-15 14:28:22 +00001006
1007.. function:: major(device)
1008
Christian Heimesfaf2f632008-01-06 16:59:19 +00001009 Extract the device major number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +00001010 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
1011
Georg Brandl116aa622007-08-15 14:28:22 +00001012
1013.. function:: minor(device)
1014
Christian Heimesfaf2f632008-01-06 16:59:19 +00001015 Extract the device minor number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +00001016 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
1017
Georg Brandl116aa622007-08-15 14:28:22 +00001018
1019.. function:: makedev(major, minor)
1020
Christian Heimesfaf2f632008-01-06 16:59:19 +00001021 Compose a raw device number from the major and minor device numbers.
Georg Brandl116aa622007-08-15 14:28:22 +00001022
Georg Brandl116aa622007-08-15 14:28:22 +00001023
1024.. function:: mkdir(path[, mode])
1025
Georg Brandlf4a41232008-05-26 17:55:52 +00001026 Create a directory named *path* with numeric mode *mode*. The default *mode*
1027 is ``0o777`` (octal). On some systems, *mode* is ignored. Where it is used,
Georg Brandlc62efa82010-07-11 10:41:07 +00001028 the current umask value is first masked out. If the directory already
1029 exists, :exc:`OSError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +00001030
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001031 It is also possible to create temporary directories; see the
1032 :mod:`tempfile` module's :func:`tempfile.mkdtemp` function.
1033
Benjamin Petersond91203b2010-05-06 23:20:40 +00001034 Availability: Unix, Windows.
1035
Georg Brandl116aa622007-08-15 14:28:22 +00001036
Georg Brandlf6c8fd62011-02-25 09:48:21 +00001037.. function:: makedirs(path, mode=0o777)
Georg Brandl116aa622007-08-15 14:28:22 +00001038
1039 .. index::
1040 single: directory; creating
1041 single: UNC paths; and os.makedirs()
1042
1043 Recursive directory creation function. Like :func:`mkdir`, but makes all
Éric Araujod82a47c2010-11-30 17:38:32 +00001044 intermediate-level directories needed to contain the leaf directory. Raises
Georg Brandlf4a41232008-05-26 17:55:52 +00001045 an :exc:`error` exception if the leaf directory already exists or cannot be
1046 created. The default *mode* is ``0o777`` (octal). On some systems, *mode*
1047 is ignored. Where it is used, the current umask value is first masked out.
Georg Brandl116aa622007-08-15 14:28:22 +00001048
1049 .. note::
1050
Georg Brandlf6c8fd62011-02-25 09:48:21 +00001051 :func:`makedirs` will become confused if the path elements to create
1052 include :data:`pardir`.
Georg Brandl116aa622007-08-15 14:28:22 +00001053
Georg Brandl55ac8f02007-09-01 13:51:09 +00001054 This function handles UNC paths correctly.
Georg Brandl116aa622007-08-15 14:28:22 +00001055
1056
1057.. function:: pathconf(path, name)
1058
1059 Return system configuration information relevant to a named file. *name*
1060 specifies the configuration value to retrieve; it may be a string which is the
1061 name of a defined system value; these names are specified in a number of
1062 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
1063 additional names as well. The names known to the host operating system are
1064 given in the ``pathconf_names`` dictionary. For configuration variables not
1065 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +00001066
1067 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1068 specific value for *name* is not supported by the host system, even if it is
1069 included in ``pathconf_names``, an :exc:`OSError` is raised with
1070 :const:`errno.EINVAL` for the error number.
1071
Benjamin Petersond91203b2010-05-06 23:20:40 +00001072 Availability: Unix.
1073
Georg Brandl116aa622007-08-15 14:28:22 +00001074
1075.. data:: pathconf_names
1076
1077 Dictionary mapping names accepted by :func:`pathconf` and :func:`fpathconf` to
1078 the integer values defined for those names by the host operating system. This
1079 can be used to determine the set of names known to the system. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001080 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001081
1082
1083.. function:: readlink(path)
1084
1085 Return a string representing the path to which the symbolic link points. The
1086 result may be either an absolute or relative pathname; if it is relative, it may
1087 be converted to an absolute pathname using ``os.path.join(os.path.dirname(path),
1088 result)``.
1089
Georg Brandl76e55382008-10-08 16:34:57 +00001090 If the *path* is a string object, the result will also be a string object,
1091 and the call may raise an UnicodeDecodeError. If the *path* is a bytes
1092 object, the result will be a bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +00001093
Georg Brandlc575c902008-09-13 17:46:05 +00001094 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001095
1096
1097.. function:: remove(path)
1098
Georg Brandl7baf6252009-09-01 08:13:16 +00001099 Remove (delete) the file *path*. If *path* is a directory, :exc:`OSError` is
1100 raised; see :func:`rmdir` below to remove a directory. This is identical to
1101 the :func:`unlink` function documented below. On Windows, attempting to
1102 remove a file that is in use causes an exception to be raised; on Unix, the
1103 directory entry is removed but the storage allocated to the file is not made
Benjamin Petersond91203b2010-05-06 23:20:40 +00001104 available until the original file is no longer in use.
1105
1106 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001107
1108
1109.. function:: removedirs(path)
1110
1111 .. index:: single: directory; deleting
1112
Christian Heimesfaf2f632008-01-06 16:59:19 +00001113 Remove directories recursively. Works like :func:`rmdir` except that, if the
Georg Brandl116aa622007-08-15 14:28:22 +00001114 leaf directory is successfully removed, :func:`removedirs` tries to
1115 successively remove every parent directory mentioned in *path* until an error
1116 is raised (which is ignored, because it generally means that a parent directory
1117 is not empty). For example, ``os.removedirs('foo/bar/baz')`` will first remove
1118 the directory ``'foo/bar/baz'``, and then remove ``'foo/bar'`` and ``'foo'`` if
1119 they are empty. Raises :exc:`OSError` if the leaf directory could not be
1120 successfully removed.
1121
Georg Brandl116aa622007-08-15 14:28:22 +00001122
1123.. function:: rename(src, dst)
1124
1125 Rename the file or directory *src* to *dst*. If *dst* is a directory,
1126 :exc:`OSError` will be raised. On Unix, if *dst* exists and is a file, it will
Christian Heimesfaf2f632008-01-06 16:59:19 +00001127 be replaced silently if the user has permission. The operation may fail on some
Georg Brandl116aa622007-08-15 14:28:22 +00001128 Unix flavors if *src* and *dst* are on different filesystems. If successful,
1129 the renaming will be an atomic operation (this is a POSIX requirement). On
1130 Windows, if *dst* already exists, :exc:`OSError` will be raised even if it is a
1131 file; there may be no way to implement an atomic rename when *dst* names an
Benjamin Petersond91203b2010-05-06 23:20:40 +00001132 existing file.
1133
1134 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001135
1136
1137.. function:: renames(old, new)
1138
1139 Recursive directory or file renaming function. Works like :func:`rename`, except
1140 creation of any intermediate directories needed to make the new pathname good is
1141 attempted first. After the rename, directories corresponding to rightmost path
1142 segments of the old name will be pruned away using :func:`removedirs`.
1143
Georg Brandl116aa622007-08-15 14:28:22 +00001144 .. note::
1145
1146 This function can fail with the new directory structure made if you lack
1147 permissions needed to remove the leaf directory or file.
1148
1149
1150.. function:: rmdir(path)
1151
Georg Brandl7baf6252009-09-01 08:13:16 +00001152 Remove (delete) the directory *path*. Only works when the directory is
1153 empty, otherwise, :exc:`OSError` is raised. In order to remove whole
Benjamin Petersond91203b2010-05-06 23:20:40 +00001154 directory trees, :func:`shutil.rmtree` can be used.
1155
1156 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001157
1158
1159.. function:: stat(path)
1160
Georg Brandlc60e3062011-02-12 07:32:02 +00001161 Perform the equivalent of a :cfunc:`stat` system call on the given path.
R. David Murray43b2f452011-02-11 03:13:19 +00001162 (This function follows symlinks; to stat a symlink use :func:`lstat`.)
Georg Brandl116aa622007-08-15 14:28:22 +00001163
R. David Murray43b2f452011-02-11 03:13:19 +00001164 The return value is an object whose attributes correspond to the members
Georg Brandlc60e3062011-02-12 07:32:02 +00001165 of the :ctype:`stat` structure, namely:
Georg Brandl116aa622007-08-15 14:28:22 +00001166
R. David Murray43b2f452011-02-11 03:13:19 +00001167 * :attr:`st_mode` - protection bits,
1168 * :attr:`st_ino` - inode number,
1169 * :attr:`st_dev` - device,
1170 * :attr:`st_nlink` - number of hard links,
1171 * :attr:`st_uid` - user id of owner,
1172 * :attr:`st_gid` - group id of owner,
1173 * :attr:`st_size` - size of file, in bytes,
1174 * :attr:`st_atime` - time of most recent access,
1175 * :attr:`st_mtime` - time of most recent content modification,
1176 * :attr:`st_ctime` - platform dependent; time of most recent metadata change on
1177 Unix, or the time of creation on Windows)
Georg Brandl116aa622007-08-15 14:28:22 +00001178
1179 On some Unix systems (such as Linux), the following attributes may also be
R. David Murray43b2f452011-02-11 03:13:19 +00001180 available:
1181
1182 * :attr:`st_blocks` - number of blocks allocated for file
1183 * :attr:`st_blksize` - filesystem blocksize
1184 * :attr:`st_rdev` - type of device if an inode device
1185 * :attr:`st_flags` - user defined flags for file
Georg Brandl116aa622007-08-15 14:28:22 +00001186
1187 On other Unix systems (such as FreeBSD), the following attributes may be
R. David Murray43b2f452011-02-11 03:13:19 +00001188 available (but may be only filled out if root tries to use them):
1189
1190 * :attr:`st_gen` - file generation number
1191 * :attr:`st_birthtime` - time of file creation
Georg Brandl116aa622007-08-15 14:28:22 +00001192
1193 On Mac OS systems, the following attributes may also be available:
Georg Brandl116aa622007-08-15 14:28:22 +00001194
R. David Murray43b2f452011-02-11 03:13:19 +00001195 * :attr:`st_rsize`
1196 * :attr:`st_creator`
1197 * :attr:`st_type`
Georg Brandl116aa622007-08-15 14:28:22 +00001198
1199 .. note::
1200
1201 The exact meaning and resolution of the :attr:`st_atime`, :attr:`st_mtime`, and
1202 :attr:`st_ctime` members depends on the operating system and the file system.
1203 For example, on Windows systems using the FAT or FAT32 file systems,
1204 :attr:`st_mtime` has 2-second resolution, and :attr:`st_atime` has only 1-day
1205 resolution. See your operating system documentation for details.
1206
R. David Murray43b2f452011-02-11 03:13:19 +00001207 For backward compatibility, the return value of :func:`~os.stat` is also accessible
1208 as a tuple of at least 10 integers giving the most important (and portable)
1209 members of the :ctype:`stat` structure, in the order :attr:`st_mode`,
1210 :attr:`st_ino`, :attr:`st_dev`, :attr:`st_nlink`, :attr:`st_uid`,
1211 :attr:`st_gid`, :attr:`st_size`, :attr:`st_atime`, :attr:`st_mtime`,
1212 :attr:`st_ctime`. More items may be added at the end by some implementations.
1213
1214 .. index:: module: stat
1215
1216 The standard module :mod:`stat` defines functions and constants that are useful
1217 for extracting information from a :ctype:`stat` structure. (On Windows, some
1218 items are filled with dummy values.)
1219
1220 Example::
1221
1222 >>> import os
1223 >>> statinfo = os.stat('somefile.txt')
1224 >>> statinfo
1225 (33188, 422511, 769, 1, 1032, 100, 926, 1105022698,1105022732, 1105022732)
1226 >>> statinfo.st_size
1227 926
1228
Georg Brandlc575c902008-09-13 17:46:05 +00001229 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001230
Georg Brandl116aa622007-08-15 14:28:22 +00001231
1232.. function:: stat_float_times([newvalue])
1233
1234 Determine whether :class:`stat_result` represents time stamps as float objects.
R. David Murray43b2f452011-02-11 03:13:19 +00001235 If *newvalue* is ``True``, future calls to :func:`~os.stat` return floats, if it is
Georg Brandl116aa622007-08-15 14:28:22 +00001236 ``False``, future calls return ints. If *newvalue* is omitted, return the
1237 current setting.
1238
1239 For compatibility with older Python versions, accessing :class:`stat_result` as
1240 a tuple always returns integers.
1241
Georg Brandl55ac8f02007-09-01 13:51:09 +00001242 Python now returns float values by default. Applications which do not work
1243 correctly with floating point time stamps can use this function to restore the
1244 old behaviour.
Georg Brandl116aa622007-08-15 14:28:22 +00001245
1246 The resolution of the timestamps (that is the smallest possible fraction)
1247 depends on the system. Some systems only support second resolution; on these
1248 systems, the fraction will always be zero.
1249
1250 It is recommended that this setting is only changed at program startup time in
1251 the *__main__* module; libraries should never change this setting. If an
1252 application uses a library that works incorrectly if floating point time stamps
1253 are processed, this application should turn the feature off until the library
1254 has been corrected.
1255
1256
1257.. function:: statvfs(path)
1258
1259 Perform a :cfunc:`statvfs` system call on the given path. The return value is
1260 an object whose attributes describe the filesystem on the given path, and
1261 correspond to the members of the :ctype:`statvfs` structure, namely:
1262 :attr:`f_bsize`, :attr:`f_frsize`, :attr:`f_blocks`, :attr:`f_bfree`,
1263 :attr:`f_bavail`, :attr:`f_files`, :attr:`f_ffree`, :attr:`f_favail`,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001264 :attr:`f_flag`, :attr:`f_namemax`.
1265
1266 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001267
Georg Brandl116aa622007-08-15 14:28:22 +00001268
Benjamin Peterson5879d412009-03-30 14:51:56 +00001269.. function:: symlink(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +00001270
Benjamin Petersond91203b2010-05-06 23:20:40 +00001271 Create a symbolic link pointing to *source* named *link_name*.
1272
1273 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001274
1275
Georg Brandl116aa622007-08-15 14:28:22 +00001276.. function:: unlink(path)
1277
Georg Brandl7baf6252009-09-01 08:13:16 +00001278 Remove (delete) the file *path*. This is the same function as
1279 :func:`remove`; the :func:`unlink` name is its traditional Unix
Benjamin Petersond91203b2010-05-06 23:20:40 +00001280 name.
1281
1282 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001283
1284
1285.. function:: utime(path, times)
1286
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001287 Set the access and modified times of the file specified by *path*. If *times*
1288 is ``None``, then the file's access and modified times are set to the current
1289 time. (The effect is similar to running the Unix program :program:`touch` on
1290 the path.) Otherwise, *times* must be a 2-tuple of numbers, of the form
1291 ``(atime, mtime)`` which is used to set the access and modified times,
1292 respectively. Whether a directory can be given for *path* depends on whether
1293 the operating system implements directories as files (for example, Windows
1294 does not). Note that the exact times you set here may not be returned by a
R. David Murray43b2f452011-02-11 03:13:19 +00001295 subsequent :func:`~os.stat` call, depending on the resolution with which your
1296 operating system records access and modification times; see :func:`~os.stat`.
Georg Brandl116aa622007-08-15 14:28:22 +00001297
Georg Brandlc575c902008-09-13 17:46:05 +00001298 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001299
1300
1301.. function:: walk(top[, topdown=True [, onerror=None[, followlinks=False]]])
1302
1303 .. index::
1304 single: directory; walking
1305 single: directory; traversal
1306
Christian Heimesfaf2f632008-01-06 16:59:19 +00001307 Generate the file names in a directory tree by walking the tree
1308 either top-down or bottom-up. For each directory in the tree rooted at directory
Georg Brandl116aa622007-08-15 14:28:22 +00001309 *top* (including *top* itself), it yields a 3-tuple ``(dirpath, dirnames,
1310 filenames)``.
1311
1312 *dirpath* is a string, the path to the directory. *dirnames* is a list of the
1313 names of the subdirectories in *dirpath* (excluding ``'.'`` and ``'..'``).
1314 *filenames* is a list of the names of the non-directory files in *dirpath*.
1315 Note that the names in the lists contain no path components. To get a full path
1316 (which begins with *top*) to a file or directory in *dirpath*, do
1317 ``os.path.join(dirpath, name)``.
1318
Christian Heimesfaf2f632008-01-06 16:59:19 +00001319 If optional argument *topdown* is ``True`` or not specified, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001320 directory is generated before the triples for any of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001321 (directories are generated top-down). If *topdown* is ``False``, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001322 directory is generated after the triples for all of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001323 (directories are generated bottom-up).
Georg Brandl116aa622007-08-15 14:28:22 +00001324
Christian Heimesfaf2f632008-01-06 16:59:19 +00001325 When *topdown* is ``True``, the caller can modify the *dirnames* list in-place
Georg Brandl116aa622007-08-15 14:28:22 +00001326 (perhaps using :keyword:`del` or slice assignment), and :func:`walk` will only
1327 recurse into the subdirectories whose names remain in *dirnames*; this can be
1328 used to prune the search, impose a specific order of visiting, or even to inform
1329 :func:`walk` about directories the caller creates or renames before it resumes
Christian Heimesfaf2f632008-01-06 16:59:19 +00001330 :func:`walk` again. Modifying *dirnames* when *topdown* is ``False`` is
Georg Brandl116aa622007-08-15 14:28:22 +00001331 ineffective, because in bottom-up mode the directories in *dirnames* are
1332 generated before *dirpath* itself is generated.
1333
Christian Heimesfaf2f632008-01-06 16:59:19 +00001334 By default errors from the :func:`listdir` call are ignored. If optional
Georg Brandl116aa622007-08-15 14:28:22 +00001335 argument *onerror* is specified, it should be a function; it will be called with
1336 one argument, an :exc:`OSError` instance. It can report the error to continue
1337 with the walk, or raise the exception to abort the walk. Note that the filename
1338 is available as the ``filename`` attribute of the exception object.
1339
1340 By default, :func:`walk` will not walk down into symbolic links that resolve to
Christian Heimesfaf2f632008-01-06 16:59:19 +00001341 directories. Set *followlinks* to ``True`` to visit directories pointed to by
Georg Brandl116aa622007-08-15 14:28:22 +00001342 symlinks, on systems that support them.
1343
Georg Brandl116aa622007-08-15 14:28:22 +00001344 .. note::
1345
Christian Heimesfaf2f632008-01-06 16:59:19 +00001346 Be aware that setting *followlinks* to ``True`` can lead to infinite recursion if a
Georg Brandl116aa622007-08-15 14:28:22 +00001347 link points to a parent directory of itself. :func:`walk` does not keep track of
1348 the directories it visited already.
1349
1350 .. note::
1351
1352 If you pass a relative pathname, don't change the current working directory
1353 between resumptions of :func:`walk`. :func:`walk` never changes the current
1354 directory, and assumes that its caller doesn't either.
1355
1356 This example displays the number of bytes taken by non-directory files in each
1357 directory under the starting directory, except that it doesn't look under any
1358 CVS subdirectory::
1359
1360 import os
1361 from os.path import join, getsize
1362 for root, dirs, files in os.walk('python/Lib/email'):
Georg Brandl6911e3c2007-09-04 07:15:32 +00001363 print(root, "consumes", end=" ")
1364 print(sum(getsize(join(root, name)) for name in files), end=" ")
1365 print("bytes in", len(files), "non-directory files")
Georg Brandl116aa622007-08-15 14:28:22 +00001366 if 'CVS' in dirs:
1367 dirs.remove('CVS') # don't visit CVS directories
1368
Christian Heimesfaf2f632008-01-06 16:59:19 +00001369 In the next example, walking the tree bottom-up is essential: :func:`rmdir`
Georg Brandl116aa622007-08-15 14:28:22 +00001370 doesn't allow deleting a directory before the directory is empty::
1371
Christian Heimesfaf2f632008-01-06 16:59:19 +00001372 # Delete everything reachable from the directory named in "top",
Georg Brandl116aa622007-08-15 14:28:22 +00001373 # assuming there are no symbolic links.
1374 # CAUTION: This is dangerous! For example, if top == '/', it
1375 # could delete all your disk files.
1376 import os
1377 for root, dirs, files in os.walk(top, topdown=False):
1378 for name in files:
1379 os.remove(os.path.join(root, name))
1380 for name in dirs:
1381 os.rmdir(os.path.join(root, name))
1382
Georg Brandl116aa622007-08-15 14:28:22 +00001383
1384.. _os-process:
1385
1386Process Management
1387------------------
1388
1389These functions may be used to create and manage processes.
1390
1391The various :func:`exec\*` functions take a list of arguments for the new
1392program loaded into the process. In each case, the first of these arguments is
1393passed to the new program as its own name rather than as an argument a user may
1394have typed on a command line. For the C programmer, this is the ``argv[0]``
1395passed to a program's :cfunc:`main`. For example, ``os.execv('/bin/echo',
1396['foo', 'bar'])`` will only print ``bar`` on standard output; ``foo`` will seem
1397to be ignored.
1398
1399
1400.. function:: abort()
1401
1402 Generate a :const:`SIGABRT` signal to the current process. On Unix, the default
1403 behavior is to produce a core dump; on Windows, the process immediately returns
1404 an exit code of ``3``. Be aware that programs which use :func:`signal.signal`
1405 to register a handler for :const:`SIGABRT` will behave differently.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001406
Georg Brandlc575c902008-09-13 17:46:05 +00001407 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001408
1409
1410.. function:: execl(path, arg0, arg1, ...)
1411 execle(path, arg0, arg1, ..., env)
1412 execlp(file, arg0, arg1, ...)
1413 execlpe(file, arg0, arg1, ..., env)
1414 execv(path, args)
1415 execve(path, args, env)
1416 execvp(file, args)
1417 execvpe(file, args, env)
1418
1419 These functions all execute a new program, replacing the current process; they
1420 do not return. On Unix, the new executable is loaded into the current process,
Christian Heimesfaf2f632008-01-06 16:59:19 +00001421 and will have the same process id as the caller. Errors will be reported as
Georg Brandl48310cd2009-01-03 21:18:54 +00001422 :exc:`OSError` exceptions.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001423
1424 The current process is replaced immediately. Open file objects and
1425 descriptors are not flushed, so if there may be data buffered
1426 on these open files, you should flush them using
1427 :func:`sys.stdout.flush` or :func:`os.fsync` before calling an
1428 :func:`exec\*` function.
Georg Brandl116aa622007-08-15 14:28:22 +00001429
Christian Heimesfaf2f632008-01-06 16:59:19 +00001430 The "l" and "v" variants of the :func:`exec\*` functions differ in how
1431 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001432 to work with if the number of parameters is fixed when the code is written; the
1433 individual parameters simply become additional parameters to the :func:`execl\*`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001434 functions. The "v" variants are good when the number of parameters is
Georg Brandl116aa622007-08-15 14:28:22 +00001435 variable, with the arguments being passed in a list or tuple as the *args*
1436 parameter. In either case, the arguments to the child process should start with
1437 the name of the command being run, but this is not enforced.
1438
Christian Heimesfaf2f632008-01-06 16:59:19 +00001439 The variants which include a "p" near the end (:func:`execlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001440 :func:`execlpe`, :func:`execvp`, and :func:`execvpe`) will use the
1441 :envvar:`PATH` environment variable to locate the program *file*. When the
1442 environment is being replaced (using one of the :func:`exec\*e` variants,
1443 discussed in the next paragraph), the new environment is used as the source of
1444 the :envvar:`PATH` variable. The other variants, :func:`execl`, :func:`execle`,
1445 :func:`execv`, and :func:`execve`, will not use the :envvar:`PATH` variable to
1446 locate the executable; *path* must contain an appropriate absolute or relative
1447 path.
1448
1449 For :func:`execle`, :func:`execlpe`, :func:`execve`, and :func:`execvpe` (note
Christian Heimesfaf2f632008-01-06 16:59:19 +00001450 that these all end in "e"), the *env* parameter must be a mapping which is
Christian Heimesa342c012008-04-20 21:01:16 +00001451 used to define the environment variables for the new process (these are used
1452 instead of the current process' environment); the functions :func:`execl`,
Georg Brandl116aa622007-08-15 14:28:22 +00001453 :func:`execlp`, :func:`execv`, and :func:`execvp` all cause the new process to
Georg Brandl48310cd2009-01-03 21:18:54 +00001454 inherit the environment of the current process.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001455
1456 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001457
1458
1459.. function:: _exit(n)
1460
Georg Brandlab32fec2010-11-26 08:49:15 +00001461 Exit the process with status *n*, without calling cleanup handlers, flushing
Benjamin Petersond91203b2010-05-06 23:20:40 +00001462 stdio buffers, etc.
1463
1464 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001465
1466 .. note::
1467
Georg Brandlab32fec2010-11-26 08:49:15 +00001468 The standard way to exit is ``sys.exit(n)``. :func:`_exit` should
1469 normally only be used in the child process after a :func:`fork`.
Georg Brandl116aa622007-08-15 14:28:22 +00001470
Christian Heimesfaf2f632008-01-06 16:59:19 +00001471The following exit codes are defined and can be used with :func:`_exit`,
Georg Brandl116aa622007-08-15 14:28:22 +00001472although they are not required. These are typically used for system programs
1473written in Python, such as a mail server's external command delivery program.
1474
1475.. note::
1476
1477 Some of these may not be available on all Unix platforms, since there is some
1478 variation. These constants are defined where they are defined by the underlying
1479 platform.
1480
1481
1482.. data:: EX_OK
1483
Benjamin Petersond91203b2010-05-06 23:20:40 +00001484 Exit code that means no error occurred.
1485
1486 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001487
Georg Brandl116aa622007-08-15 14:28:22 +00001488
1489.. data:: EX_USAGE
1490
1491 Exit code that means the command was used incorrectly, such as when the wrong
Benjamin Petersond91203b2010-05-06 23:20:40 +00001492 number of arguments are given.
1493
1494 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001495
Georg Brandl116aa622007-08-15 14:28:22 +00001496
1497.. data:: EX_DATAERR
1498
Benjamin Petersond91203b2010-05-06 23:20:40 +00001499 Exit code that means the input data was incorrect.
1500
1501 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001502
Georg Brandl116aa622007-08-15 14:28:22 +00001503
1504.. data:: EX_NOINPUT
1505
1506 Exit code that means an input file did not exist or was not readable.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001507
Georg Brandlc575c902008-09-13 17:46:05 +00001508 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001509
Georg Brandl116aa622007-08-15 14:28:22 +00001510
1511.. data:: EX_NOUSER
1512
Benjamin Petersond91203b2010-05-06 23:20:40 +00001513 Exit code that means a specified user did not exist.
1514
1515 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001516
Georg Brandl116aa622007-08-15 14:28:22 +00001517
1518.. data:: EX_NOHOST
1519
Benjamin Petersond91203b2010-05-06 23:20:40 +00001520 Exit code that means a specified host did not exist.
1521
1522 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001523
Georg Brandl116aa622007-08-15 14:28:22 +00001524
1525.. data:: EX_UNAVAILABLE
1526
Benjamin Petersond91203b2010-05-06 23:20:40 +00001527 Exit code that means that a required service is unavailable.
1528
1529 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001530
Georg Brandl116aa622007-08-15 14:28:22 +00001531
1532.. data:: EX_SOFTWARE
1533
Benjamin Petersond91203b2010-05-06 23:20:40 +00001534 Exit code that means an internal software error was detected.
1535
1536 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001537
Georg Brandl116aa622007-08-15 14:28:22 +00001538
1539.. data:: EX_OSERR
1540
1541 Exit code that means an operating system error was detected, such as the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001542 inability to fork or create a pipe.
1543
1544 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001545
Georg Brandl116aa622007-08-15 14:28:22 +00001546
1547.. data:: EX_OSFILE
1548
1549 Exit code that means some system file did not exist, could not be opened, or had
Benjamin Petersond91203b2010-05-06 23:20:40 +00001550 some other kind of error.
1551
1552 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001553
Georg Brandl116aa622007-08-15 14:28:22 +00001554
1555.. data:: EX_CANTCREAT
1556
1557 Exit code that means a user specified output file could not be created.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001558
Georg Brandlc575c902008-09-13 17:46:05 +00001559 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001560
Georg Brandl116aa622007-08-15 14:28:22 +00001561
1562.. data:: EX_IOERR
1563
1564 Exit code that means that an error occurred while doing I/O on some file.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001565
Georg Brandlc575c902008-09-13 17:46:05 +00001566 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001567
Georg Brandl116aa622007-08-15 14:28:22 +00001568
1569.. data:: EX_TEMPFAIL
1570
1571 Exit code that means a temporary failure occurred. This indicates something
1572 that may not really be an error, such as a network connection that couldn't be
Benjamin Petersond91203b2010-05-06 23:20:40 +00001573 made during a retryable operation.
1574
1575 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001576
Georg Brandl116aa622007-08-15 14:28:22 +00001577
1578.. data:: EX_PROTOCOL
1579
1580 Exit code that means that a protocol exchange was illegal, invalid, or not
Benjamin Petersond91203b2010-05-06 23:20:40 +00001581 understood.
1582
1583 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001584
Georg Brandl116aa622007-08-15 14:28:22 +00001585
1586.. data:: EX_NOPERM
1587
1588 Exit code that means that there were insufficient permissions to perform the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001589 operation (but not intended for file system problems).
1590
1591 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001592
Georg Brandl116aa622007-08-15 14:28:22 +00001593
1594.. data:: EX_CONFIG
1595
1596 Exit code that means that some kind of configuration error occurred.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001597
Georg Brandlc575c902008-09-13 17:46:05 +00001598 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001599
Georg Brandl116aa622007-08-15 14:28:22 +00001600
1601.. data:: EX_NOTFOUND
1602
Benjamin Petersond91203b2010-05-06 23:20:40 +00001603 Exit code that means something like "an entry was not found".
1604
1605 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001606
Georg Brandl116aa622007-08-15 14:28:22 +00001607
1608.. function:: fork()
1609
Christian Heimesfaf2f632008-01-06 16:59:19 +00001610 Fork a child process. Return ``0`` in the child and the child's process id in the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001611 parent. If an error occurs :exc:`OSError` is raised.
Benjamin Petersonbcd8ac32008-10-10 22:20:52 +00001612
1613 Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have
1614 known issues when using fork() from a thread.
1615
Georg Brandlc575c902008-09-13 17:46:05 +00001616 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001617
1618
1619.. function:: forkpty()
1620
1621 Fork a child process, using a new pseudo-terminal as the child's controlling
1622 terminal. Return a pair of ``(pid, fd)``, where *pid* is ``0`` in the child, the
1623 new child's process id in the parent, and *fd* is the file descriptor of the
1624 master end of the pseudo-terminal. For a more portable approach, use the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001625 :mod:`pty` module. If an error occurs :exc:`OSError` is raised.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001626
Georg Brandlc575c902008-09-13 17:46:05 +00001627 Availability: some flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001628
1629
1630.. function:: kill(pid, sig)
1631
1632 .. index::
1633 single: process; killing
1634 single: process; signalling
1635
1636 Send signal *sig* to the process *pid*. Constants for the specific signals
1637 available on the host platform are defined in the :mod:`signal` module.
Georg Brandlc575c902008-09-13 17:46:05 +00001638 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001639
1640
1641.. function:: killpg(pgid, sig)
1642
1643 .. index::
1644 single: process; killing
1645 single: process; signalling
1646
Benjamin Petersond91203b2010-05-06 23:20:40 +00001647 Send the signal *sig* to the process group *pgid*.
1648
1649 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001650
Georg Brandl116aa622007-08-15 14:28:22 +00001651
1652.. function:: nice(increment)
1653
1654 Add *increment* to the process's "niceness". Return the new niceness.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001655
Georg Brandlc575c902008-09-13 17:46:05 +00001656 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001657
1658
1659.. function:: plock(op)
1660
1661 Lock program segments into memory. The value of *op* (defined in
Benjamin Petersond91203b2010-05-06 23:20:40 +00001662 ``<sys/lock.h>``) determines which segments are locked.
1663
1664 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001665
1666
1667.. function:: popen(...)
1668 :noindex:
1669
1670 Run child processes, returning opened pipes for communications. These functions
1671 are described in section :ref:`os-newstreams`.
1672
1673
1674.. function:: spawnl(mode, path, ...)
1675 spawnle(mode, path, ..., env)
1676 spawnlp(mode, file, ...)
1677 spawnlpe(mode, file, ..., env)
1678 spawnv(mode, path, args)
1679 spawnve(mode, path, args, env)
1680 spawnvp(mode, file, args)
1681 spawnvpe(mode, file, args, env)
1682
1683 Execute the program *path* in a new process.
1684
1685 (Note that the :mod:`subprocess` module provides more powerful facilities for
1686 spawning new processes and retrieving their results; using that module is
Benjamin Peterson87c8d872009-06-11 22:54:11 +00001687 preferable to using these functions. Check especially the
1688 :ref:`subprocess-replacements` section.)
Georg Brandl116aa622007-08-15 14:28:22 +00001689
Christian Heimesfaf2f632008-01-06 16:59:19 +00001690 If *mode* is :const:`P_NOWAIT`, this function returns the process id of the new
Georg Brandl116aa622007-08-15 14:28:22 +00001691 process; if *mode* is :const:`P_WAIT`, returns the process's exit code if it
1692 exits normally, or ``-signal``, where *signal* is the signal that killed the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001693 process. On Windows, the process id will actually be the process handle, so can
Georg Brandl116aa622007-08-15 14:28:22 +00001694 be used with the :func:`waitpid` function.
1695
Christian Heimesfaf2f632008-01-06 16:59:19 +00001696 The "l" and "v" variants of the :func:`spawn\*` functions differ in how
1697 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001698 to work with if the number of parameters is fixed when the code is written; the
1699 individual parameters simply become additional parameters to the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001700 :func:`spawnl\*` functions. The "v" variants are good when the number of
Georg Brandl116aa622007-08-15 14:28:22 +00001701 parameters is variable, with the arguments being passed in a list or tuple as
1702 the *args* parameter. In either case, the arguments to the child process must
1703 start with the name of the command being run.
1704
Christian Heimesfaf2f632008-01-06 16:59:19 +00001705 The variants which include a second "p" near the end (:func:`spawnlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001706 :func:`spawnlpe`, :func:`spawnvp`, and :func:`spawnvpe`) will use the
1707 :envvar:`PATH` environment variable to locate the program *file*. When the
1708 environment is being replaced (using one of the :func:`spawn\*e` variants,
1709 discussed in the next paragraph), the new environment is used as the source of
1710 the :envvar:`PATH` variable. The other variants, :func:`spawnl`,
1711 :func:`spawnle`, :func:`spawnv`, and :func:`spawnve`, will not use the
1712 :envvar:`PATH` variable to locate the executable; *path* must contain an
1713 appropriate absolute or relative path.
1714
1715 For :func:`spawnle`, :func:`spawnlpe`, :func:`spawnve`, and :func:`spawnvpe`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001716 (note that these all end in "e"), the *env* parameter must be a mapping
Christian Heimesa342c012008-04-20 21:01:16 +00001717 which is used to define the environment variables for the new process (they are
1718 used instead of the current process' environment); the functions
Georg Brandl116aa622007-08-15 14:28:22 +00001719 :func:`spawnl`, :func:`spawnlp`, :func:`spawnv`, and :func:`spawnvp` all cause
Benjamin Petersond23f8222009-04-05 19:13:16 +00001720 the new process to inherit the environment of the current process. Note that
1721 keys and values in the *env* dictionary must be strings; invalid keys or
1722 values will cause the function to fail, with a return value of ``127``.
Georg Brandl116aa622007-08-15 14:28:22 +00001723
1724 As an example, the following calls to :func:`spawnlp` and :func:`spawnvpe` are
1725 equivalent::
1726
1727 import os
1728 os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1729
1730 L = ['cp', 'index.html', '/dev/null']
1731 os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1732
1733 Availability: Unix, Windows. :func:`spawnlp`, :func:`spawnlpe`, :func:`spawnvp`
1734 and :func:`spawnvpe` are not available on Windows.
1735
Georg Brandl116aa622007-08-15 14:28:22 +00001736
1737.. data:: P_NOWAIT
1738 P_NOWAITO
1739
1740 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1741 functions. If either of these values is given, the :func:`spawn\*` functions
Christian Heimesfaf2f632008-01-06 16:59:19 +00001742 will return as soon as the new process has been created, with the process id as
Benjamin Petersond91203b2010-05-06 23:20:40 +00001743 the return value.
1744
1745 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001746
Georg Brandl116aa622007-08-15 14:28:22 +00001747
1748.. data:: P_WAIT
1749
1750 Possible value for the *mode* parameter to the :func:`spawn\*` family of
1751 functions. If this is given as *mode*, the :func:`spawn\*` functions will not
1752 return until the new process has run to completion and will return the exit code
1753 of the process the run is successful, or ``-signal`` if a signal kills the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001754 process.
1755
1756 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001757
Georg Brandl116aa622007-08-15 14:28:22 +00001758
1759.. data:: P_DETACH
1760 P_OVERLAY
1761
1762 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1763 functions. These are less portable than those listed above. :const:`P_DETACH`
1764 is similar to :const:`P_NOWAIT`, but the new process is detached from the
1765 console of the calling process. If :const:`P_OVERLAY` is used, the current
1766 process will be replaced; the :func:`spawn\*` function will not return.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001767
Georg Brandl116aa622007-08-15 14:28:22 +00001768 Availability: Windows.
1769
Georg Brandl116aa622007-08-15 14:28:22 +00001770
1771.. function:: startfile(path[, operation])
1772
1773 Start a file with its associated application.
1774
1775 When *operation* is not specified or ``'open'``, this acts like double-clicking
1776 the file in Windows Explorer, or giving the file name as an argument to the
1777 :program:`start` command from the interactive command shell: the file is opened
1778 with whatever application (if any) its extension is associated.
1779
1780 When another *operation* is given, it must be a "command verb" that specifies
1781 what should be done with the file. Common verbs documented by Microsoft are
1782 ``'print'`` and ``'edit'`` (to be used on files) as well as ``'explore'`` and
1783 ``'find'`` (to be used on directories).
1784
1785 :func:`startfile` returns as soon as the associated application is launched.
1786 There is no option to wait for the application to close, and no way to retrieve
1787 the application's exit status. The *path* parameter is relative to the current
1788 directory. If you want to use an absolute path, make sure the first character
1789 is not a slash (``'/'``); the underlying Win32 :cfunc:`ShellExecute` function
1790 doesn't work if it is. Use the :func:`os.path.normpath` function to ensure that
Benjamin Petersond91203b2010-05-06 23:20:40 +00001791 the path is properly encoded for Win32.
1792
1793 Availability: Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001794
Georg Brandl116aa622007-08-15 14:28:22 +00001795
1796.. function:: system(command)
1797
1798 Execute the command (a string) in a subshell. This is implemented by calling
Georg Brandl628e6f92009-10-27 20:24:45 +00001799 the Standard C function :cfunc:`system`, and has the same limitations.
Georg Brandlaba97962010-11-26 08:37:46 +00001800 Changes to :data:`sys.stdin`, etc. are not reflected in the environment of
1801 the executed command. If *command* generates any output, it will be sent to
1802 the interpreter standard output stream.
Georg Brandl116aa622007-08-15 14:28:22 +00001803
1804 On Unix, the return value is the exit status of the process encoded in the
Georg Brandlaba97962010-11-26 08:37:46 +00001805 format specified for :func:`wait`. Note that POSIX does not specify the
1806 meaning of the return value of the C :cfunc:`system` function, so the return
1807 value of the Python function is system-dependent.
Georg Brandl116aa622007-08-15 14:28:22 +00001808
Georg Brandlaba97962010-11-26 08:37:46 +00001809 On Windows, the return value is that returned by the system shell after
1810 running *command*. The shell is given by the Windows environment variable
1811 :envvar:`COMSPEC`: it is usually :program:`cmd.exe`, which returns the exit
1812 status of the command run; on systems using a non-native shell, consult your
1813 shell documentation.
Georg Brandl116aa622007-08-15 14:28:22 +00001814
Georg Brandlaba97962010-11-26 08:37:46 +00001815 The :mod:`subprocess` module provides more powerful facilities for spawning
1816 new processes and retrieving their results; using that module is preferable
1817 to using this function. See the :ref:`subprocess-replacements` section in
1818 the :mod:`subprocess` documentation for some helpful recipes.
Georg Brandl116aa622007-08-15 14:28:22 +00001819
Benjamin Petersond91203b2010-05-06 23:20:40 +00001820 Availability: Unix, Windows.
1821
Georg Brandl116aa622007-08-15 14:28:22 +00001822
1823.. function:: times()
1824
Benjamin Petersond91203b2010-05-06 23:20:40 +00001825 Return a 5-tuple of floating point numbers indicating accumulated (processor
1826 or other) times, in seconds. The items are: user time, system time,
1827 children's user time, children's system time, and elapsed real time since a
1828 fixed point in the past, in that order. See the Unix manual page
1829 :manpage:`times(2)` or the corresponding Windows Platform API documentation.
1830 On Windows, only the first two items are filled, the others are zero.
1831
1832 Availability: Unix, Windows
Georg Brandl116aa622007-08-15 14:28:22 +00001833
1834
1835.. function:: wait()
1836
1837 Wait for completion of a child process, and return a tuple containing its pid
1838 and exit status indication: a 16-bit number, whose low byte is the signal number
1839 that killed the process, and whose high byte is the exit status (if the signal
1840 number is zero); the high bit of the low byte is set if a core file was
Benjamin Petersond91203b2010-05-06 23:20:40 +00001841 produced.
1842
1843 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001844
1845
1846.. function:: waitpid(pid, options)
1847
1848 The details of this function differ on Unix and Windows.
1849
1850 On Unix: Wait for completion of a child process given by process id *pid*, and
1851 return a tuple containing its process id and exit status indication (encoded as
1852 for :func:`wait`). The semantics of the call are affected by the value of the
1853 integer *options*, which should be ``0`` for normal operation.
1854
1855 If *pid* is greater than ``0``, :func:`waitpid` requests status information for
1856 that specific process. If *pid* is ``0``, the request is for the status of any
1857 child in the process group of the current process. If *pid* is ``-1``, the
1858 request pertains to any child of the current process. If *pid* is less than
1859 ``-1``, status is requested for any process in the process group ``-pid`` (the
1860 absolute value of *pid*).
1861
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001862 An :exc:`OSError` is raised with the value of errno when the syscall
1863 returns -1.
1864
Georg Brandl116aa622007-08-15 14:28:22 +00001865 On Windows: Wait for completion of a process given by process handle *pid*, and
1866 return a tuple containing *pid*, and its exit status shifted left by 8 bits
1867 (shifting makes cross-platform use of the function easier). A *pid* less than or
1868 equal to ``0`` has no special meaning on Windows, and raises an exception. The
1869 value of integer *options* has no effect. *pid* can refer to any process whose
1870 id is known, not necessarily a child process. The :func:`spawn` functions called
1871 with :const:`P_NOWAIT` return suitable process handles.
1872
1873
1874.. function:: wait3([options])
1875
1876 Similar to :func:`waitpid`, except no process id argument is given and a
1877 3-element tuple containing the child's process id, exit status indication, and
1878 resource usage information is returned. Refer to :mod:`resource`.\
1879 :func:`getrusage` for details on resource usage information. The option
1880 argument is the same as that provided to :func:`waitpid` and :func:`wait4`.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001881
Georg Brandl116aa622007-08-15 14:28:22 +00001882 Availability: Unix.
1883
Georg Brandl116aa622007-08-15 14:28:22 +00001884
1885.. function:: wait4(pid, options)
1886
1887 Similar to :func:`waitpid`, except a 3-element tuple, containing the child's
1888 process id, exit status indication, and resource usage information is returned.
1889 Refer to :mod:`resource`.\ :func:`getrusage` for details on resource usage
1890 information. The arguments to :func:`wait4` are the same as those provided to
Benjamin Petersond91203b2010-05-06 23:20:40 +00001891 :func:`waitpid`.
1892
1893 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001894
Georg Brandl116aa622007-08-15 14:28:22 +00001895
1896.. data:: WNOHANG
1897
1898 The option for :func:`waitpid` to return immediately if no child process status
1899 is available immediately. The function returns ``(0, 0)`` in this case.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001900
Georg Brandlc575c902008-09-13 17:46:05 +00001901 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001902
1903
1904.. data:: WCONTINUED
1905
1906 This option causes child processes to be reported if they have been continued
Benjamin Petersond91203b2010-05-06 23:20:40 +00001907 from a job control stop since their status was last reported.
1908
1909 Availability: Some Unix systems.
Georg Brandl116aa622007-08-15 14:28:22 +00001910
Georg Brandl116aa622007-08-15 14:28:22 +00001911
1912.. data:: WUNTRACED
1913
1914 This option causes child processes to be reported if they have been stopped but
Benjamin Petersond91203b2010-05-06 23:20:40 +00001915 their current state has not been reported since they were stopped.
1916
1917 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001918
Georg Brandl116aa622007-08-15 14:28:22 +00001919
1920The following functions take a process status code as returned by
1921:func:`system`, :func:`wait`, or :func:`waitpid` as a parameter. They may be
1922used to determine the disposition of a process.
1923
Georg Brandl116aa622007-08-15 14:28:22 +00001924.. function:: WCOREDUMP(status)
1925
Christian Heimesfaf2f632008-01-06 16:59:19 +00001926 Return ``True`` if a core dump was generated for the process, otherwise
Benjamin Petersond91203b2010-05-06 23:20:40 +00001927 return ``False``.
1928
1929 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001930
Georg Brandl116aa622007-08-15 14:28:22 +00001931
1932.. function:: WIFCONTINUED(status)
1933
Christian Heimesfaf2f632008-01-06 16:59:19 +00001934 Return ``True`` if the process has been continued from a job control stop,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001935 otherwise return ``False``.
1936
1937 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001938
Georg Brandl116aa622007-08-15 14:28:22 +00001939
1940.. function:: WIFSTOPPED(status)
1941
Christian Heimesfaf2f632008-01-06 16:59:19 +00001942 Return ``True`` if the process has been stopped, otherwise return
Benjamin Petersond91203b2010-05-06 23:20:40 +00001943 ``False``.
1944
1945 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001946
1947
1948.. function:: WIFSIGNALED(status)
1949
Christian Heimesfaf2f632008-01-06 16:59:19 +00001950 Return ``True`` if the process exited due to a signal, otherwise return
Benjamin Petersond91203b2010-05-06 23:20:40 +00001951 ``False``.
1952
1953 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001954
1955
1956.. function:: WIFEXITED(status)
1957
Christian Heimesfaf2f632008-01-06 16:59:19 +00001958 Return ``True`` if the process exited using the :manpage:`exit(2)` system call,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001959 otherwise return ``False``.
1960
1961 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001962
1963
1964.. function:: WEXITSTATUS(status)
1965
1966 If ``WIFEXITED(status)`` is true, return the integer parameter to the
1967 :manpage:`exit(2)` system call. Otherwise, the return value is meaningless.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001968
Georg Brandlc575c902008-09-13 17:46:05 +00001969 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001970
1971
1972.. function:: WSTOPSIG(status)
1973
Benjamin Petersond91203b2010-05-06 23:20:40 +00001974 Return the signal which caused the process to stop.
1975
1976 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001977
1978
1979.. function:: WTERMSIG(status)
1980
Benjamin Petersond91203b2010-05-06 23:20:40 +00001981 Return the signal which caused the process to exit.
1982
1983 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001984
1985
1986.. _os-path:
1987
1988Miscellaneous System Information
1989--------------------------------
1990
1991
1992.. function:: confstr(name)
1993
1994 Return string-valued system configuration values. *name* specifies the
1995 configuration value to retrieve; it may be a string which is the name of a
1996 defined system value; these names are specified in a number of standards (POSIX,
1997 Unix 95, Unix 98, and others). Some platforms define additional names as well.
1998 The names known to the host operating system are given as the keys of the
1999 ``confstr_names`` dictionary. For configuration variables not included in that
Benjamin Petersond91203b2010-05-06 23:20:40 +00002000 mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +00002001
2002 If the configuration value specified by *name* isn't defined, ``None`` is
2003 returned.
2004
2005 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
2006 specific value for *name* is not supported by the host system, even if it is
2007 included in ``confstr_names``, an :exc:`OSError` is raised with
2008 :const:`errno.EINVAL` for the error number.
2009
Benjamin Petersond91203b2010-05-06 23:20:40 +00002010 Availability: Unix
2011
Georg Brandl116aa622007-08-15 14:28:22 +00002012
2013.. data:: confstr_names
2014
2015 Dictionary mapping names accepted by :func:`confstr` to the integer values
2016 defined for those names by the host operating system. This can be used to
Benjamin Petersond91203b2010-05-06 23:20:40 +00002017 determine the set of names known to the system.
2018
2019 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002020
2021
2022.. function:: getloadavg()
2023
Christian Heimesa62da1d2008-01-12 19:39:10 +00002024 Return the number of processes in the system run queue averaged over the last
2025 1, 5, and 15 minutes or raises :exc:`OSError` if the load average was
Benjamin Petersond91203b2010-05-06 23:20:40 +00002026 unobtainable.
2027
2028 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002029
Georg Brandl116aa622007-08-15 14:28:22 +00002030
2031.. function:: sysconf(name)
2032
2033 Return integer-valued system configuration values. If the configuration value
2034 specified by *name* isn't defined, ``-1`` is returned. The comments regarding
2035 the *name* parameter for :func:`confstr` apply here as well; the dictionary that
2036 provides information on the known names is given by ``sysconf_names``.
Benjamin Petersond91203b2010-05-06 23:20:40 +00002037
Georg Brandlc575c902008-09-13 17:46:05 +00002038 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002039
2040
2041.. data:: sysconf_names
2042
2043 Dictionary mapping names accepted by :func:`sysconf` to the integer values
2044 defined for those names by the host operating system. This can be used to
Benjamin Petersond91203b2010-05-06 23:20:40 +00002045 determine the set of names known to the system.
2046
2047 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002048
Christian Heimesfaf2f632008-01-06 16:59:19 +00002049The following data values are used to support path manipulation operations. These
Georg Brandl116aa622007-08-15 14:28:22 +00002050are defined for all platforms.
2051
2052Higher-level operations on pathnames are defined in the :mod:`os.path` module.
2053
2054
2055.. data:: curdir
2056
2057 The constant string used by the operating system to refer to the current
Georg Brandlc575c902008-09-13 17:46:05 +00002058 directory. This is ``'.'`` for Windows and POSIX. Also available via
2059 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002060
2061
2062.. data:: pardir
2063
2064 The constant string used by the operating system to refer to the parent
Georg Brandlc575c902008-09-13 17:46:05 +00002065 directory. This is ``'..'`` for Windows and POSIX. Also available via
2066 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002067
2068
2069.. data:: sep
2070
Georg Brandlc575c902008-09-13 17:46:05 +00002071 The character used by the operating system to separate pathname components.
2072 This is ``'/'`` for POSIX and ``'\\'`` for Windows. Note that knowing this
2073 is not sufficient to be able to parse or concatenate pathnames --- use
Georg Brandl116aa622007-08-15 14:28:22 +00002074 :func:`os.path.split` and :func:`os.path.join` --- but it is occasionally
2075 useful. Also available via :mod:`os.path`.
2076
2077
2078.. data:: altsep
2079
2080 An alternative character used by the operating system to separate pathname
2081 components, or ``None`` if only one separator character exists. This is set to
2082 ``'/'`` on Windows systems where ``sep`` is a backslash. Also available via
2083 :mod:`os.path`.
2084
2085
2086.. data:: extsep
2087
2088 The character which separates the base filename from the extension; for example,
2089 the ``'.'`` in :file:`os.py`. Also available via :mod:`os.path`.
2090
Georg Brandl116aa622007-08-15 14:28:22 +00002091
2092.. data:: pathsep
2093
2094 The character conventionally used by the operating system to separate search
2095 path components (as in :envvar:`PATH`), such as ``':'`` for POSIX or ``';'`` for
2096 Windows. Also available via :mod:`os.path`.
2097
2098
2099.. data:: defpath
2100
2101 The default search path used by :func:`exec\*p\*` and :func:`spawn\*p\*` if the
2102 environment doesn't have a ``'PATH'`` key. Also available via :mod:`os.path`.
2103
2104
2105.. data:: linesep
2106
2107 The string used to separate (or, rather, terminate) lines on the current
Georg Brandlc575c902008-09-13 17:46:05 +00002108 platform. This may be a single character, such as ``'\n'`` for POSIX, or
2109 multiple characters, for example, ``'\r\n'`` for Windows. Do not use
2110 *os.linesep* as a line terminator when writing files opened in text mode (the
2111 default); use a single ``'\n'`` instead, on all platforms.
Georg Brandl116aa622007-08-15 14:28:22 +00002112
2113
2114.. data:: devnull
2115
Georg Brandlc52eeab2010-05-21 22:05:15 +00002116 The file path of the null device. For example: ``'/dev/null'`` for
2117 POSIX, ``'nul'`` for Windows. Also available via :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002118
Georg Brandl116aa622007-08-15 14:28:22 +00002119
2120.. _os-miscfunc:
2121
2122Miscellaneous Functions
2123-----------------------
2124
2125
2126.. function:: urandom(n)
2127
2128 Return a string of *n* random bytes suitable for cryptographic use.
2129
2130 This function returns random bytes from an OS-specific randomness source. The
2131 returned data should be unpredictable enough for cryptographic applications,
2132 though its exact quality depends on the OS implementation. On a UNIX-like
2133 system this will query /dev/urandom, and on Windows it will use CryptGenRandom.
2134 If a randomness source is not found, :exc:`NotImplementedError` will be raised.