blob: cf012ff750e0c08096b3f3e422cbd240b9d1c0bd [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
775 and opening the file to manipulate it.
Georg Brandl116aa622007-08-15 14:28:22 +0000776
777 .. note::
778
779 I/O operations may fail even when :func:`access` indicates that they would
780 succeed, particularly for operations on network filesystems which may have
781 permissions semantics beyond the usual POSIX permission-bit model.
782
783
784.. data:: F_OK
785
786 Value to pass as the *mode* parameter of :func:`access` to test the existence of
787 *path*.
788
789
790.. data:: R_OK
791
792 Value to include in the *mode* parameter of :func:`access` to test the
793 readability of *path*.
794
795
796.. data:: W_OK
797
798 Value to include in the *mode* parameter of :func:`access` to test the
799 writability of *path*.
800
801
802.. data:: X_OK
803
804 Value to include in the *mode* parameter of :func:`access` to determine if
805 *path* can be executed.
806
807
808.. function:: chdir(path)
809
810 .. index:: single: directory; changing
811
Benjamin Petersond91203b2010-05-06 23:20:40 +0000812 Change the current working directory to *path*.
813
814 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000815
816
817.. function:: fchdir(fd)
818
819 Change the current working directory to the directory represented by the file
820 descriptor *fd*. The descriptor must refer to an opened directory, not an open
Benjamin Petersond91203b2010-05-06 23:20:40 +0000821 file.
822
823 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000824
Georg Brandl116aa622007-08-15 14:28:22 +0000825
826.. function:: getcwd()
827
Martin v. Löwis011e8422009-05-05 04:43:17 +0000828 Return a string representing the current working directory.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000829
Martin v. Löwis011e8422009-05-05 04:43:17 +0000830 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000831
Benjamin Petersond91203b2010-05-06 23:20:40 +0000832
Martin v. Löwisa731b992008-10-07 06:36:31 +0000833.. function:: getcwdb()
Georg Brandl116aa622007-08-15 14:28:22 +0000834
Georg Brandl76e55382008-10-08 16:34:57 +0000835 Return a bytestring representing the current working directory.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000836
Georg Brandlc575c902008-09-13 17:46:05 +0000837 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000838
Georg Brandl116aa622007-08-15 14:28:22 +0000839
840.. function:: chflags(path, flags)
841
842 Set the flags of *path* to the numeric *flags*. *flags* may take a combination
843 (bitwise OR) of the following values (as defined in the :mod:`stat` module):
844
845 * ``UF_NODUMP``
846 * ``UF_IMMUTABLE``
847 * ``UF_APPEND``
848 * ``UF_OPAQUE``
849 * ``UF_NOUNLINK``
850 * ``SF_ARCHIVED``
851 * ``SF_IMMUTABLE``
852 * ``SF_APPEND``
853 * ``SF_NOUNLINK``
854 * ``SF_SNAPSHOT``
855
Georg Brandlc575c902008-09-13 17:46:05 +0000856 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000857
Georg Brandl116aa622007-08-15 14:28:22 +0000858
859.. function:: chroot(path)
860
861 Change the root directory of the current process to *path*. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +0000862 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000863
Georg Brandl116aa622007-08-15 14:28:22 +0000864
865.. function:: chmod(path, mode)
866
867 Change the mode of *path* to the numeric *mode*. *mode* may take one of the
Christian Heimesfaf2f632008-01-06 16:59:19 +0000868 following values (as defined in the :mod:`stat` module) or bitwise ORed
Georg Brandl116aa622007-08-15 14:28:22 +0000869 combinations of them:
870
R. David Murrayba426142009-07-21 14:29:59 +0000871 * :data:`stat.S_ISUID`
872 * :data:`stat.S_ISGID`
873 * :data:`stat.S_ENFMT`
874 * :data:`stat.S_ISVTX`
875 * :data:`stat.S_IREAD`
876 * :data:`stat.S_IWRITE`
877 * :data:`stat.S_IEXEC`
878 * :data:`stat.S_IRWXU`
879 * :data:`stat.S_IRUSR`
880 * :data:`stat.S_IWUSR`
881 * :data:`stat.S_IXUSR`
882 * :data:`stat.S_IRWXG`
883 * :data:`stat.S_IRGRP`
884 * :data:`stat.S_IWGRP`
885 * :data:`stat.S_IXGRP`
886 * :data:`stat.S_IRWXO`
887 * :data:`stat.S_IROTH`
888 * :data:`stat.S_IWOTH`
889 * :data:`stat.S_IXOTH`
Georg Brandl116aa622007-08-15 14:28:22 +0000890
Georg Brandlc575c902008-09-13 17:46:05 +0000891 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000892
893 .. note::
894
895 Although Windows supports :func:`chmod`, you can only set the file's read-only
896 flag with it (via the ``stat.S_IWRITE`` and ``stat.S_IREAD``
897 constants or a corresponding integer value). All other bits are
898 ignored.
899
900
901.. function:: chown(path, uid, gid)
902
903 Change the owner and group id of *path* to the numeric *uid* and *gid*. To leave
Benjamin Petersond91203b2010-05-06 23:20:40 +0000904 one of the ids unchanged, set it to -1.
905
906 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000907
908
909.. function:: lchflags(path, flags)
910
911 Set the flags of *path* to the numeric *flags*, like :func:`chflags`, but do not
Benjamin Petersond91203b2010-05-06 23:20:40 +0000912 follow symbolic links.
913
914 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000915
Georg Brandl116aa622007-08-15 14:28:22 +0000916
Christian Heimes93852662007-12-01 12:22:32 +0000917.. function:: lchmod(path, mode)
918
919 Change the mode of *path* to the numeric *mode*. If path is a symlink, this
920 affects the symlink rather than the target. See the docs for :func:`chmod`
Benjamin Petersond91203b2010-05-06 23:20:40 +0000921 for possible values of *mode*.
922
923 Availability: Unix.
Christian Heimes93852662007-12-01 12:22:32 +0000924
Christian Heimes93852662007-12-01 12:22:32 +0000925
Georg Brandl116aa622007-08-15 14:28:22 +0000926.. function:: lchown(path, uid, gid)
927
Christian Heimesfaf2f632008-01-06 16:59:19 +0000928 Change the owner and group id of *path* to the numeric *uid* and *gid*. This
Benjamin Petersond91203b2010-05-06 23:20:40 +0000929 function will not follow symbolic links.
930
931 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000932
Georg Brandl116aa622007-08-15 14:28:22 +0000933
Benjamin Peterson5879d412009-03-30 14:51:56 +0000934.. function:: link(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +0000935
Benjamin Petersond91203b2010-05-06 23:20:40 +0000936 Create a hard link pointing to *source* named *link_name*.
937
938 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000939
940
941.. function:: listdir(path)
942
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000943 Return a list containing the names of the entries in the directory given by
944 *path*. The list is in arbitrary order. It does not include the special
945 entries ``'.'`` and ``'..'`` even if they are present in the directory.
Georg Brandl116aa622007-08-15 14:28:22 +0000946
Martin v. Löwis011e8422009-05-05 04:43:17 +0000947 This function can be called with a bytes or string argument, and returns
948 filenames of the same datatype.
Georg Brandl116aa622007-08-15 14:28:22 +0000949
Benjamin Petersond91203b2010-05-06 23:20:40 +0000950 Availability: Unix, Windows.
951
Georg Brandl116aa622007-08-15 14:28:22 +0000952
953.. function:: lstat(path)
954
R. David Murray43b2f452011-02-11 03:13:19 +0000955 Perform the equivalent of an :c:func:`lstat` system call on the given path.
956 Similar to :func:`~os.stat`, but does not follow symbolic links. On
957 platforms that do not support symbolic links, this is an alias for
958 :func:`~os.stat`.
Georg Brandl116aa622007-08-15 14:28:22 +0000959
960
961.. function:: mkfifo(path[, mode])
962
Georg Brandlf4a41232008-05-26 17:55:52 +0000963 Create a FIFO (a named pipe) named *path* with numeric mode *mode*. The
964 default *mode* is ``0o666`` (octal). The current umask value is first masked
Benjamin Petersond91203b2010-05-06 23:20:40 +0000965 out from the mode.
Georg Brandl116aa622007-08-15 14:28:22 +0000966
967 FIFOs are pipes that can be accessed like regular files. FIFOs exist until they
968 are deleted (for example with :func:`os.unlink`). Generally, FIFOs are used as
969 rendezvous between "client" and "server" type processes: the server opens the
970 FIFO for reading, and the client opens it for writing. Note that :func:`mkfifo`
971 doesn't open the FIFO --- it just creates the rendezvous point.
972
Benjamin Petersond91203b2010-05-06 23:20:40 +0000973 Availability: Unix.
974
Georg Brandl116aa622007-08-15 14:28:22 +0000975
Georg Brandlf4a41232008-05-26 17:55:52 +0000976.. function:: mknod(filename[, mode=0o600, device])
Georg Brandl116aa622007-08-15 14:28:22 +0000977
978 Create a filesystem node (file, device special file or named pipe) named
979 *filename*. *mode* specifies both the permissions to use and the type of node to
980 be created, being combined (bitwise OR) with one of ``stat.S_IFREG``,
981 ``stat.S_IFCHR``, ``stat.S_IFBLK``,
982 and ``stat.S_IFIFO`` (those constants are available in :mod:`stat`).
983 For ``stat.S_IFCHR`` and
984 ``stat.S_IFBLK``, *device* defines the newly created device special file (probably using
985 :func:`os.makedev`), otherwise it is ignored.
986
Georg Brandl116aa622007-08-15 14:28:22 +0000987
988.. function:: major(device)
989
Christian Heimesfaf2f632008-01-06 16:59:19 +0000990 Extract the device major number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000991 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
992
Georg Brandl116aa622007-08-15 14:28:22 +0000993
994.. function:: minor(device)
995
Christian Heimesfaf2f632008-01-06 16:59:19 +0000996 Extract the device minor number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000997 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
998
Georg Brandl116aa622007-08-15 14:28:22 +0000999
1000.. function:: makedev(major, minor)
1001
Christian Heimesfaf2f632008-01-06 16:59:19 +00001002 Compose a raw device number from the major and minor device numbers.
Georg Brandl116aa622007-08-15 14:28:22 +00001003
Georg Brandl116aa622007-08-15 14:28:22 +00001004
1005.. function:: mkdir(path[, mode])
1006
Georg Brandlf4a41232008-05-26 17:55:52 +00001007 Create a directory named *path* with numeric mode *mode*. The default *mode*
1008 is ``0o777`` (octal). On some systems, *mode* is ignored. Where it is used,
Georg Brandlc62efa82010-07-11 10:41:07 +00001009 the current umask value is first masked out. If the directory already
1010 exists, :exc:`OSError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +00001011
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001012 It is also possible to create temporary directories; see the
1013 :mod:`tempfile` module's :func:`tempfile.mkdtemp` function.
1014
Benjamin Petersond91203b2010-05-06 23:20:40 +00001015 Availability: Unix, Windows.
1016
Georg Brandl116aa622007-08-15 14:28:22 +00001017
1018.. function:: makedirs(path[, mode])
1019
1020 .. index::
1021 single: directory; creating
1022 single: UNC paths; and os.makedirs()
1023
1024 Recursive directory creation function. Like :func:`mkdir`, but makes all
Éric Araujod82a47c2010-11-30 17:38:32 +00001025 intermediate-level directories needed to contain the leaf directory. Raises
Georg Brandlf4a41232008-05-26 17:55:52 +00001026 an :exc:`error` exception if the leaf directory already exists or cannot be
1027 created. The default *mode* is ``0o777`` (octal). On some systems, *mode*
1028 is ignored. Where it is used, the current umask value is first masked out.
Georg Brandl116aa622007-08-15 14:28:22 +00001029
1030 .. note::
1031
1032 :func:`makedirs` will become confused if the path elements to create include
Christian Heimesfaf2f632008-01-06 16:59:19 +00001033 :data:`os.pardir`.
Georg Brandl116aa622007-08-15 14:28:22 +00001034
Georg Brandl55ac8f02007-09-01 13:51:09 +00001035 This function handles UNC paths correctly.
Georg Brandl116aa622007-08-15 14:28:22 +00001036
1037
1038.. function:: pathconf(path, name)
1039
1040 Return system configuration information relevant to a named file. *name*
1041 specifies the configuration value to retrieve; it may be a string which is the
1042 name of a defined system value; these names are specified in a number of
1043 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
1044 additional names as well. The names known to the host operating system are
1045 given in the ``pathconf_names`` dictionary. For configuration variables not
1046 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +00001047
1048 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1049 specific value for *name* is not supported by the host system, even if it is
1050 included in ``pathconf_names``, an :exc:`OSError` is raised with
1051 :const:`errno.EINVAL` for the error number.
1052
Benjamin Petersond91203b2010-05-06 23:20:40 +00001053 Availability: Unix.
1054
Georg Brandl116aa622007-08-15 14:28:22 +00001055
1056.. data:: pathconf_names
1057
1058 Dictionary mapping names accepted by :func:`pathconf` and :func:`fpathconf` to
1059 the integer values defined for those names by the host operating system. This
1060 can be used to determine the set of names known to the system. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001061 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001062
1063
1064.. function:: readlink(path)
1065
1066 Return a string representing the path to which the symbolic link points. The
1067 result may be either an absolute or relative pathname; if it is relative, it may
1068 be converted to an absolute pathname using ``os.path.join(os.path.dirname(path),
1069 result)``.
1070
Georg Brandl76e55382008-10-08 16:34:57 +00001071 If the *path* is a string object, the result will also be a string object,
1072 and the call may raise an UnicodeDecodeError. If the *path* is a bytes
1073 object, the result will be a bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +00001074
Georg Brandlc575c902008-09-13 17:46:05 +00001075 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001076
1077
1078.. function:: remove(path)
1079
Georg Brandl7baf6252009-09-01 08:13:16 +00001080 Remove (delete) the file *path*. If *path* is a directory, :exc:`OSError` is
1081 raised; see :func:`rmdir` below to remove a directory. This is identical to
1082 the :func:`unlink` function documented below. On Windows, attempting to
1083 remove a file that is in use causes an exception to be raised; on Unix, the
1084 directory entry is removed but the storage allocated to the file is not made
Benjamin Petersond91203b2010-05-06 23:20:40 +00001085 available until the original file is no longer in use.
1086
1087 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001088
1089
1090.. function:: removedirs(path)
1091
1092 .. index:: single: directory; deleting
1093
Christian Heimesfaf2f632008-01-06 16:59:19 +00001094 Remove directories recursively. Works like :func:`rmdir` except that, if the
Georg Brandl116aa622007-08-15 14:28:22 +00001095 leaf directory is successfully removed, :func:`removedirs` tries to
1096 successively remove every parent directory mentioned in *path* until an error
1097 is raised (which is ignored, because it generally means that a parent directory
1098 is not empty). For example, ``os.removedirs('foo/bar/baz')`` will first remove
1099 the directory ``'foo/bar/baz'``, and then remove ``'foo/bar'`` and ``'foo'`` if
1100 they are empty. Raises :exc:`OSError` if the leaf directory could not be
1101 successfully removed.
1102
Georg Brandl116aa622007-08-15 14:28:22 +00001103
1104.. function:: rename(src, dst)
1105
1106 Rename the file or directory *src* to *dst*. If *dst* is a directory,
1107 :exc:`OSError` will be raised. On Unix, if *dst* exists and is a file, it will
Christian Heimesfaf2f632008-01-06 16:59:19 +00001108 be replaced silently if the user has permission. The operation may fail on some
Georg Brandl116aa622007-08-15 14:28:22 +00001109 Unix flavors if *src* and *dst* are on different filesystems. If successful,
1110 the renaming will be an atomic operation (this is a POSIX requirement). On
1111 Windows, if *dst* already exists, :exc:`OSError` will be raised even if it is a
1112 file; there may be no way to implement an atomic rename when *dst* names an
Benjamin Petersond91203b2010-05-06 23:20:40 +00001113 existing file.
1114
1115 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001116
1117
1118.. function:: renames(old, new)
1119
1120 Recursive directory or file renaming function. Works like :func:`rename`, except
1121 creation of any intermediate directories needed to make the new pathname good is
1122 attempted first. After the rename, directories corresponding to rightmost path
1123 segments of the old name will be pruned away using :func:`removedirs`.
1124
Georg Brandl116aa622007-08-15 14:28:22 +00001125 .. note::
1126
1127 This function can fail with the new directory structure made if you lack
1128 permissions needed to remove the leaf directory or file.
1129
1130
1131.. function:: rmdir(path)
1132
Georg Brandl7baf6252009-09-01 08:13:16 +00001133 Remove (delete) the directory *path*. Only works when the directory is
1134 empty, otherwise, :exc:`OSError` is raised. In order to remove whole
Benjamin Petersond91203b2010-05-06 23:20:40 +00001135 directory trees, :func:`shutil.rmtree` can be used.
1136
1137 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001138
1139
1140.. function:: stat(path)
1141
R. David Murray43b2f452011-02-11 03:13:19 +00001142 Perform the equivalent of a :c:func:`stat` system call on the given path.
1143 (This function follows symlinks; to stat a symlink use :func:`lstat`.)
Georg Brandl116aa622007-08-15 14:28:22 +00001144
R. David Murray43b2f452011-02-11 03:13:19 +00001145 The return value is an object whose attributes correspond to the members
1146 of the :c:type:`stat` structure, namely:
Georg Brandl116aa622007-08-15 14:28:22 +00001147
R. David Murray43b2f452011-02-11 03:13:19 +00001148 * :attr:`st_mode` - protection bits,
1149 * :attr:`st_ino` - inode number,
1150 * :attr:`st_dev` - device,
1151 * :attr:`st_nlink` - number of hard links,
1152 * :attr:`st_uid` - user id of owner,
1153 * :attr:`st_gid` - group id of owner,
1154 * :attr:`st_size` - size of file, in bytes,
1155 * :attr:`st_atime` - time of most recent access,
1156 * :attr:`st_mtime` - time of most recent content modification,
1157 * :attr:`st_ctime` - platform dependent; time of most recent metadata change on
1158 Unix, or the time of creation on Windows)
Georg Brandl116aa622007-08-15 14:28:22 +00001159
1160 On some Unix systems (such as Linux), the following attributes may also be
R. David Murray43b2f452011-02-11 03:13:19 +00001161 available:
1162
1163 * :attr:`st_blocks` - number of blocks allocated for file
1164 * :attr:`st_blksize` - filesystem blocksize
1165 * :attr:`st_rdev` - type of device if an inode device
1166 * :attr:`st_flags` - user defined flags for file
Georg Brandl116aa622007-08-15 14:28:22 +00001167
1168 On other Unix systems (such as FreeBSD), the following attributes may be
R. David Murray43b2f452011-02-11 03:13:19 +00001169 available (but may be only filled out if root tries to use them):
1170
1171 * :attr:`st_gen` - file generation number
1172 * :attr:`st_birthtime` - time of file creation
Georg Brandl116aa622007-08-15 14:28:22 +00001173
1174 On Mac OS systems, the following attributes may also be available:
Georg Brandl116aa622007-08-15 14:28:22 +00001175
R. David Murray43b2f452011-02-11 03:13:19 +00001176 * :attr:`st_rsize`
1177 * :attr:`st_creator`
1178 * :attr:`st_type`
Georg Brandl116aa622007-08-15 14:28:22 +00001179
1180 .. note::
1181
1182 The exact meaning and resolution of the :attr:`st_atime`, :attr:`st_mtime`, and
1183 :attr:`st_ctime` members depends on the operating system and the file system.
1184 For example, on Windows systems using the FAT or FAT32 file systems,
1185 :attr:`st_mtime` has 2-second resolution, and :attr:`st_atime` has only 1-day
1186 resolution. See your operating system documentation for details.
1187
R. David Murray43b2f452011-02-11 03:13:19 +00001188 For backward compatibility, the return value of :func:`~os.stat` is also accessible
1189 as a tuple of at least 10 integers giving the most important (and portable)
1190 members of the :ctype:`stat` structure, in the order :attr:`st_mode`,
1191 :attr:`st_ino`, :attr:`st_dev`, :attr:`st_nlink`, :attr:`st_uid`,
1192 :attr:`st_gid`, :attr:`st_size`, :attr:`st_atime`, :attr:`st_mtime`,
1193 :attr:`st_ctime`. More items may be added at the end by some implementations.
1194
1195 .. index:: module: stat
1196
1197 The standard module :mod:`stat` defines functions and constants that are useful
1198 for extracting information from a :ctype:`stat` structure. (On Windows, some
1199 items are filled with dummy values.)
1200
1201 Example::
1202
1203 >>> import os
1204 >>> statinfo = os.stat('somefile.txt')
1205 >>> statinfo
1206 (33188, 422511, 769, 1, 1032, 100, 926, 1105022698,1105022732, 1105022732)
1207 >>> statinfo.st_size
1208 926
1209
Georg Brandlc575c902008-09-13 17:46:05 +00001210 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001211
Georg Brandl116aa622007-08-15 14:28:22 +00001212
1213.. function:: stat_float_times([newvalue])
1214
1215 Determine whether :class:`stat_result` represents time stamps as float objects.
R. David Murray43b2f452011-02-11 03:13:19 +00001216 If *newvalue* is ``True``, future calls to :func:`~os.stat` return floats, if it is
Georg Brandl116aa622007-08-15 14:28:22 +00001217 ``False``, future calls return ints. If *newvalue* is omitted, return the
1218 current setting.
1219
1220 For compatibility with older Python versions, accessing :class:`stat_result` as
1221 a tuple always returns integers.
1222
Georg Brandl55ac8f02007-09-01 13:51:09 +00001223 Python now returns float values by default. Applications which do not work
1224 correctly with floating point time stamps can use this function to restore the
1225 old behaviour.
Georg Brandl116aa622007-08-15 14:28:22 +00001226
1227 The resolution of the timestamps (that is the smallest possible fraction)
1228 depends on the system. Some systems only support second resolution; on these
1229 systems, the fraction will always be zero.
1230
1231 It is recommended that this setting is only changed at program startup time in
1232 the *__main__* module; libraries should never change this setting. If an
1233 application uses a library that works incorrectly if floating point time stamps
1234 are processed, this application should turn the feature off until the library
1235 has been corrected.
1236
1237
1238.. function:: statvfs(path)
1239
1240 Perform a :cfunc:`statvfs` system call on the given path. The return value is
1241 an object whose attributes describe the filesystem on the given path, and
1242 correspond to the members of the :ctype:`statvfs` structure, namely:
1243 :attr:`f_bsize`, :attr:`f_frsize`, :attr:`f_blocks`, :attr:`f_bfree`,
1244 :attr:`f_bavail`, :attr:`f_files`, :attr:`f_ffree`, :attr:`f_favail`,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001245 :attr:`f_flag`, :attr:`f_namemax`.
1246
1247 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001248
Georg Brandl116aa622007-08-15 14:28:22 +00001249
Benjamin Peterson5879d412009-03-30 14:51:56 +00001250.. function:: symlink(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +00001251
Benjamin Petersond91203b2010-05-06 23:20:40 +00001252 Create a symbolic link pointing to *source* named *link_name*.
1253
1254 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001255
1256
Georg Brandl116aa622007-08-15 14:28:22 +00001257.. function:: unlink(path)
1258
Georg Brandl7baf6252009-09-01 08:13:16 +00001259 Remove (delete) the file *path*. This is the same function as
1260 :func:`remove`; the :func:`unlink` name is its traditional Unix
Benjamin Petersond91203b2010-05-06 23:20:40 +00001261 name.
1262
1263 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001264
1265
1266.. function:: utime(path, times)
1267
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001268 Set the access and modified times of the file specified by *path*. If *times*
1269 is ``None``, then the file's access and modified times are set to the current
1270 time. (The effect is similar to running the Unix program :program:`touch` on
1271 the path.) Otherwise, *times* must be a 2-tuple of numbers, of the form
1272 ``(atime, mtime)`` which is used to set the access and modified times,
1273 respectively. Whether a directory can be given for *path* depends on whether
1274 the operating system implements directories as files (for example, Windows
1275 does not). Note that the exact times you set here may not be returned by a
R. David Murray43b2f452011-02-11 03:13:19 +00001276 subsequent :func:`~os.stat` call, depending on the resolution with which your
1277 operating system records access and modification times; see :func:`~os.stat`.
Georg Brandl116aa622007-08-15 14:28:22 +00001278
Georg Brandlc575c902008-09-13 17:46:05 +00001279 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001280
1281
1282.. function:: walk(top[, topdown=True [, onerror=None[, followlinks=False]]])
1283
1284 .. index::
1285 single: directory; walking
1286 single: directory; traversal
1287
Christian Heimesfaf2f632008-01-06 16:59:19 +00001288 Generate the file names in a directory tree by walking the tree
1289 either top-down or bottom-up. For each directory in the tree rooted at directory
Georg Brandl116aa622007-08-15 14:28:22 +00001290 *top* (including *top* itself), it yields a 3-tuple ``(dirpath, dirnames,
1291 filenames)``.
1292
1293 *dirpath* is a string, the path to the directory. *dirnames* is a list of the
1294 names of the subdirectories in *dirpath* (excluding ``'.'`` and ``'..'``).
1295 *filenames* is a list of the names of the non-directory files in *dirpath*.
1296 Note that the names in the lists contain no path components. To get a full path
1297 (which begins with *top*) to a file or directory in *dirpath*, do
1298 ``os.path.join(dirpath, name)``.
1299
Christian Heimesfaf2f632008-01-06 16:59:19 +00001300 If optional argument *topdown* is ``True`` or not specified, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001301 directory is generated before the triples for any of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001302 (directories are generated top-down). If *topdown* is ``False``, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001303 directory is generated after the triples for all of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001304 (directories are generated bottom-up).
Georg Brandl116aa622007-08-15 14:28:22 +00001305
Christian Heimesfaf2f632008-01-06 16:59:19 +00001306 When *topdown* is ``True``, the caller can modify the *dirnames* list in-place
Georg Brandl116aa622007-08-15 14:28:22 +00001307 (perhaps using :keyword:`del` or slice assignment), and :func:`walk` will only
1308 recurse into the subdirectories whose names remain in *dirnames*; this can be
1309 used to prune the search, impose a specific order of visiting, or even to inform
1310 :func:`walk` about directories the caller creates or renames before it resumes
Christian Heimesfaf2f632008-01-06 16:59:19 +00001311 :func:`walk` again. Modifying *dirnames* when *topdown* is ``False`` is
Georg Brandl116aa622007-08-15 14:28:22 +00001312 ineffective, because in bottom-up mode the directories in *dirnames* are
1313 generated before *dirpath* itself is generated.
1314
Christian Heimesfaf2f632008-01-06 16:59:19 +00001315 By default errors from the :func:`listdir` call are ignored. If optional
Georg Brandl116aa622007-08-15 14:28:22 +00001316 argument *onerror* is specified, it should be a function; it will be called with
1317 one argument, an :exc:`OSError` instance. It can report the error to continue
1318 with the walk, or raise the exception to abort the walk. Note that the filename
1319 is available as the ``filename`` attribute of the exception object.
1320
1321 By default, :func:`walk` will not walk down into symbolic links that resolve to
Christian Heimesfaf2f632008-01-06 16:59:19 +00001322 directories. Set *followlinks* to ``True`` to visit directories pointed to by
Georg Brandl116aa622007-08-15 14:28:22 +00001323 symlinks, on systems that support them.
1324
Georg Brandl116aa622007-08-15 14:28:22 +00001325 .. note::
1326
Christian Heimesfaf2f632008-01-06 16:59:19 +00001327 Be aware that setting *followlinks* to ``True`` can lead to infinite recursion if a
Georg Brandl116aa622007-08-15 14:28:22 +00001328 link points to a parent directory of itself. :func:`walk` does not keep track of
1329 the directories it visited already.
1330
1331 .. note::
1332
1333 If you pass a relative pathname, don't change the current working directory
1334 between resumptions of :func:`walk`. :func:`walk` never changes the current
1335 directory, and assumes that its caller doesn't either.
1336
1337 This example displays the number of bytes taken by non-directory files in each
1338 directory under the starting directory, except that it doesn't look under any
1339 CVS subdirectory::
1340
1341 import os
1342 from os.path import join, getsize
1343 for root, dirs, files in os.walk('python/Lib/email'):
Georg Brandl6911e3c2007-09-04 07:15:32 +00001344 print(root, "consumes", end=" ")
1345 print(sum(getsize(join(root, name)) for name in files), end=" ")
1346 print("bytes in", len(files), "non-directory files")
Georg Brandl116aa622007-08-15 14:28:22 +00001347 if 'CVS' in dirs:
1348 dirs.remove('CVS') # don't visit CVS directories
1349
Christian Heimesfaf2f632008-01-06 16:59:19 +00001350 In the next example, walking the tree bottom-up is essential: :func:`rmdir`
Georg Brandl116aa622007-08-15 14:28:22 +00001351 doesn't allow deleting a directory before the directory is empty::
1352
Christian Heimesfaf2f632008-01-06 16:59:19 +00001353 # Delete everything reachable from the directory named in "top",
Georg Brandl116aa622007-08-15 14:28:22 +00001354 # assuming there are no symbolic links.
1355 # CAUTION: This is dangerous! For example, if top == '/', it
1356 # could delete all your disk files.
1357 import os
1358 for root, dirs, files in os.walk(top, topdown=False):
1359 for name in files:
1360 os.remove(os.path.join(root, name))
1361 for name in dirs:
1362 os.rmdir(os.path.join(root, name))
1363
Georg Brandl116aa622007-08-15 14:28:22 +00001364
1365.. _os-process:
1366
1367Process Management
1368------------------
1369
1370These functions may be used to create and manage processes.
1371
1372The various :func:`exec\*` functions take a list of arguments for the new
1373program loaded into the process. In each case, the first of these arguments is
1374passed to the new program as its own name rather than as an argument a user may
1375have typed on a command line. For the C programmer, this is the ``argv[0]``
1376passed to a program's :cfunc:`main`. For example, ``os.execv('/bin/echo',
1377['foo', 'bar'])`` will only print ``bar`` on standard output; ``foo`` will seem
1378to be ignored.
1379
1380
1381.. function:: abort()
1382
1383 Generate a :const:`SIGABRT` signal to the current process. On Unix, the default
1384 behavior is to produce a core dump; on Windows, the process immediately returns
1385 an exit code of ``3``. Be aware that programs which use :func:`signal.signal`
1386 to register a handler for :const:`SIGABRT` will behave differently.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001387
Georg Brandlc575c902008-09-13 17:46:05 +00001388 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001389
1390
1391.. function:: execl(path, arg0, arg1, ...)
1392 execle(path, arg0, arg1, ..., env)
1393 execlp(file, arg0, arg1, ...)
1394 execlpe(file, arg0, arg1, ..., env)
1395 execv(path, args)
1396 execve(path, args, env)
1397 execvp(file, args)
1398 execvpe(file, args, env)
1399
1400 These functions all execute a new program, replacing the current process; they
1401 do not return. On Unix, the new executable is loaded into the current process,
Christian Heimesfaf2f632008-01-06 16:59:19 +00001402 and will have the same process id as the caller. Errors will be reported as
Georg Brandl48310cd2009-01-03 21:18:54 +00001403 :exc:`OSError` exceptions.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001404
1405 The current process is replaced immediately. Open file objects and
1406 descriptors are not flushed, so if there may be data buffered
1407 on these open files, you should flush them using
1408 :func:`sys.stdout.flush` or :func:`os.fsync` before calling an
1409 :func:`exec\*` function.
Georg Brandl116aa622007-08-15 14:28:22 +00001410
Christian Heimesfaf2f632008-01-06 16:59:19 +00001411 The "l" and "v" variants of the :func:`exec\*` functions differ in how
1412 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001413 to work with if the number of parameters is fixed when the code is written; the
1414 individual parameters simply become additional parameters to the :func:`execl\*`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001415 functions. The "v" variants are good when the number of parameters is
Georg Brandl116aa622007-08-15 14:28:22 +00001416 variable, with the arguments being passed in a list or tuple as the *args*
1417 parameter. In either case, the arguments to the child process should start with
1418 the name of the command being run, but this is not enforced.
1419
Christian Heimesfaf2f632008-01-06 16:59:19 +00001420 The variants which include a "p" near the end (:func:`execlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001421 :func:`execlpe`, :func:`execvp`, and :func:`execvpe`) will use the
1422 :envvar:`PATH` environment variable to locate the program *file*. When the
1423 environment is being replaced (using one of the :func:`exec\*e` variants,
1424 discussed in the next paragraph), the new environment is used as the source of
1425 the :envvar:`PATH` variable. The other variants, :func:`execl`, :func:`execle`,
1426 :func:`execv`, and :func:`execve`, will not use the :envvar:`PATH` variable to
1427 locate the executable; *path* must contain an appropriate absolute or relative
1428 path.
1429
1430 For :func:`execle`, :func:`execlpe`, :func:`execve`, and :func:`execvpe` (note
Christian Heimesfaf2f632008-01-06 16:59:19 +00001431 that these all end in "e"), the *env* parameter must be a mapping which is
Christian Heimesa342c012008-04-20 21:01:16 +00001432 used to define the environment variables for the new process (these are used
1433 instead of the current process' environment); the functions :func:`execl`,
Georg Brandl116aa622007-08-15 14:28:22 +00001434 :func:`execlp`, :func:`execv`, and :func:`execvp` all cause the new process to
Georg Brandl48310cd2009-01-03 21:18:54 +00001435 inherit the environment of the current process.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001436
1437 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001438
1439
1440.. function:: _exit(n)
1441
Georg Brandlab32fec2010-11-26 08:49:15 +00001442 Exit the process with status *n*, without calling cleanup handlers, flushing
Benjamin Petersond91203b2010-05-06 23:20:40 +00001443 stdio buffers, etc.
1444
1445 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001446
1447 .. note::
1448
Georg Brandlab32fec2010-11-26 08:49:15 +00001449 The standard way to exit is ``sys.exit(n)``. :func:`_exit` should
1450 normally only be used in the child process after a :func:`fork`.
Georg Brandl116aa622007-08-15 14:28:22 +00001451
Christian Heimesfaf2f632008-01-06 16:59:19 +00001452The following exit codes are defined and can be used with :func:`_exit`,
Georg Brandl116aa622007-08-15 14:28:22 +00001453although they are not required. These are typically used for system programs
1454written in Python, such as a mail server's external command delivery program.
1455
1456.. note::
1457
1458 Some of these may not be available on all Unix platforms, since there is some
1459 variation. These constants are defined where they are defined by the underlying
1460 platform.
1461
1462
1463.. data:: EX_OK
1464
Benjamin Petersond91203b2010-05-06 23:20:40 +00001465 Exit code that means no error occurred.
1466
1467 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001468
Georg Brandl116aa622007-08-15 14:28:22 +00001469
1470.. data:: EX_USAGE
1471
1472 Exit code that means the command was used incorrectly, such as when the wrong
Benjamin Petersond91203b2010-05-06 23:20:40 +00001473 number of arguments are given.
1474
1475 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001476
Georg Brandl116aa622007-08-15 14:28:22 +00001477
1478.. data:: EX_DATAERR
1479
Benjamin Petersond91203b2010-05-06 23:20:40 +00001480 Exit code that means the input data was incorrect.
1481
1482 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001483
Georg Brandl116aa622007-08-15 14:28:22 +00001484
1485.. data:: EX_NOINPUT
1486
1487 Exit code that means an input file did not exist or was not readable.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001488
Georg Brandlc575c902008-09-13 17:46:05 +00001489 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001490
Georg Brandl116aa622007-08-15 14:28:22 +00001491
1492.. data:: EX_NOUSER
1493
Benjamin Petersond91203b2010-05-06 23:20:40 +00001494 Exit code that means a specified user did not exist.
1495
1496 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001497
Georg Brandl116aa622007-08-15 14:28:22 +00001498
1499.. data:: EX_NOHOST
1500
Benjamin Petersond91203b2010-05-06 23:20:40 +00001501 Exit code that means a specified host did not exist.
1502
1503 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001504
Georg Brandl116aa622007-08-15 14:28:22 +00001505
1506.. data:: EX_UNAVAILABLE
1507
Benjamin Petersond91203b2010-05-06 23:20:40 +00001508 Exit code that means that a required service is unavailable.
1509
1510 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001511
Georg Brandl116aa622007-08-15 14:28:22 +00001512
1513.. data:: EX_SOFTWARE
1514
Benjamin Petersond91203b2010-05-06 23:20:40 +00001515 Exit code that means an internal software error was detected.
1516
1517 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001518
Georg Brandl116aa622007-08-15 14:28:22 +00001519
1520.. data:: EX_OSERR
1521
1522 Exit code that means an operating system error was detected, such as the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001523 inability to fork or create a pipe.
1524
1525 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001526
Georg Brandl116aa622007-08-15 14:28:22 +00001527
1528.. data:: EX_OSFILE
1529
1530 Exit code that means some system file did not exist, could not be opened, or had
Benjamin Petersond91203b2010-05-06 23:20:40 +00001531 some other kind of error.
1532
1533 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001534
Georg Brandl116aa622007-08-15 14:28:22 +00001535
1536.. data:: EX_CANTCREAT
1537
1538 Exit code that means a user specified output file could not be created.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001539
Georg Brandlc575c902008-09-13 17:46:05 +00001540 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001541
Georg Brandl116aa622007-08-15 14:28:22 +00001542
1543.. data:: EX_IOERR
1544
1545 Exit code that means that an error occurred while doing I/O on some file.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001546
Georg Brandlc575c902008-09-13 17:46:05 +00001547 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001548
Georg Brandl116aa622007-08-15 14:28:22 +00001549
1550.. data:: EX_TEMPFAIL
1551
1552 Exit code that means a temporary failure occurred. This indicates something
1553 that may not really be an error, such as a network connection that couldn't be
Benjamin Petersond91203b2010-05-06 23:20:40 +00001554 made during a retryable operation.
1555
1556 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001557
Georg Brandl116aa622007-08-15 14:28:22 +00001558
1559.. data:: EX_PROTOCOL
1560
1561 Exit code that means that a protocol exchange was illegal, invalid, or not
Benjamin Petersond91203b2010-05-06 23:20:40 +00001562 understood.
1563
1564 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001565
Georg Brandl116aa622007-08-15 14:28:22 +00001566
1567.. data:: EX_NOPERM
1568
1569 Exit code that means that there were insufficient permissions to perform the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001570 operation (but not intended for file system problems).
1571
1572 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001573
Georg Brandl116aa622007-08-15 14:28:22 +00001574
1575.. data:: EX_CONFIG
1576
1577 Exit code that means that some kind of configuration error occurred.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001578
Georg Brandlc575c902008-09-13 17:46:05 +00001579 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001580
Georg Brandl116aa622007-08-15 14:28:22 +00001581
1582.. data:: EX_NOTFOUND
1583
Benjamin Petersond91203b2010-05-06 23:20:40 +00001584 Exit code that means something like "an entry was not found".
1585
1586 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001587
Georg Brandl116aa622007-08-15 14:28:22 +00001588
1589.. function:: fork()
1590
Christian Heimesfaf2f632008-01-06 16:59:19 +00001591 Fork a child process. Return ``0`` in the child and the child's process id in the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001592 parent. If an error occurs :exc:`OSError` is raised.
Benjamin Petersonbcd8ac32008-10-10 22:20:52 +00001593
1594 Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have
1595 known issues when using fork() from a thread.
1596
Georg Brandlc575c902008-09-13 17:46:05 +00001597 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001598
1599
1600.. function:: forkpty()
1601
1602 Fork a child process, using a new pseudo-terminal as the child's controlling
1603 terminal. Return a pair of ``(pid, fd)``, where *pid* is ``0`` in the child, the
1604 new child's process id in the parent, and *fd* is the file descriptor of the
1605 master end of the pseudo-terminal. For a more portable approach, use the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001606 :mod:`pty` module. If an error occurs :exc:`OSError` is raised.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001607
Georg Brandlc575c902008-09-13 17:46:05 +00001608 Availability: some flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001609
1610
1611.. function:: kill(pid, sig)
1612
1613 .. index::
1614 single: process; killing
1615 single: process; signalling
1616
1617 Send signal *sig* to the process *pid*. Constants for the specific signals
1618 available on the host platform are defined in the :mod:`signal` module.
Georg Brandlc575c902008-09-13 17:46:05 +00001619 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001620
1621
1622.. function:: killpg(pgid, sig)
1623
1624 .. index::
1625 single: process; killing
1626 single: process; signalling
1627
Benjamin Petersond91203b2010-05-06 23:20:40 +00001628 Send the signal *sig* to the process group *pgid*.
1629
1630 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001631
Georg Brandl116aa622007-08-15 14:28:22 +00001632
1633.. function:: nice(increment)
1634
1635 Add *increment* to the process's "niceness". Return the new niceness.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001636
Georg Brandlc575c902008-09-13 17:46:05 +00001637 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001638
1639
1640.. function:: plock(op)
1641
1642 Lock program segments into memory. The value of *op* (defined in
Benjamin Petersond91203b2010-05-06 23:20:40 +00001643 ``<sys/lock.h>``) determines which segments are locked.
1644
1645 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001646
1647
1648.. function:: popen(...)
1649 :noindex:
1650
1651 Run child processes, returning opened pipes for communications. These functions
1652 are described in section :ref:`os-newstreams`.
1653
1654
1655.. function:: spawnl(mode, path, ...)
1656 spawnle(mode, path, ..., env)
1657 spawnlp(mode, file, ...)
1658 spawnlpe(mode, file, ..., env)
1659 spawnv(mode, path, args)
1660 spawnve(mode, path, args, env)
1661 spawnvp(mode, file, args)
1662 spawnvpe(mode, file, args, env)
1663
1664 Execute the program *path* in a new process.
1665
1666 (Note that the :mod:`subprocess` module provides more powerful facilities for
1667 spawning new processes and retrieving their results; using that module is
Benjamin Peterson87c8d872009-06-11 22:54:11 +00001668 preferable to using these functions. Check especially the
1669 :ref:`subprocess-replacements` section.)
Georg Brandl116aa622007-08-15 14:28:22 +00001670
Christian Heimesfaf2f632008-01-06 16:59:19 +00001671 If *mode* is :const:`P_NOWAIT`, this function returns the process id of the new
Georg Brandl116aa622007-08-15 14:28:22 +00001672 process; if *mode* is :const:`P_WAIT`, returns the process's exit code if it
1673 exits normally, or ``-signal``, where *signal* is the signal that killed the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001674 process. On Windows, the process id will actually be the process handle, so can
Georg Brandl116aa622007-08-15 14:28:22 +00001675 be used with the :func:`waitpid` function.
1676
Christian Heimesfaf2f632008-01-06 16:59:19 +00001677 The "l" and "v" variants of the :func:`spawn\*` functions differ in how
1678 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001679 to work with if the number of parameters is fixed when the code is written; the
1680 individual parameters simply become additional parameters to the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001681 :func:`spawnl\*` functions. The "v" variants are good when the number of
Georg Brandl116aa622007-08-15 14:28:22 +00001682 parameters is variable, with the arguments being passed in a list or tuple as
1683 the *args* parameter. In either case, the arguments to the child process must
1684 start with the name of the command being run.
1685
Christian Heimesfaf2f632008-01-06 16:59:19 +00001686 The variants which include a second "p" near the end (:func:`spawnlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001687 :func:`spawnlpe`, :func:`spawnvp`, and :func:`spawnvpe`) will use the
1688 :envvar:`PATH` environment variable to locate the program *file*. When the
1689 environment is being replaced (using one of the :func:`spawn\*e` variants,
1690 discussed in the next paragraph), the new environment is used as the source of
1691 the :envvar:`PATH` variable. The other variants, :func:`spawnl`,
1692 :func:`spawnle`, :func:`spawnv`, and :func:`spawnve`, will not use the
1693 :envvar:`PATH` variable to locate the executable; *path* must contain an
1694 appropriate absolute or relative path.
1695
1696 For :func:`spawnle`, :func:`spawnlpe`, :func:`spawnve`, and :func:`spawnvpe`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001697 (note that these all end in "e"), the *env* parameter must be a mapping
Christian Heimesa342c012008-04-20 21:01:16 +00001698 which is used to define the environment variables for the new process (they are
1699 used instead of the current process' environment); the functions
Georg Brandl116aa622007-08-15 14:28:22 +00001700 :func:`spawnl`, :func:`spawnlp`, :func:`spawnv`, and :func:`spawnvp` all cause
Benjamin Petersond23f8222009-04-05 19:13:16 +00001701 the new process to inherit the environment of the current process. Note that
1702 keys and values in the *env* dictionary must be strings; invalid keys or
1703 values will cause the function to fail, with a return value of ``127``.
Georg Brandl116aa622007-08-15 14:28:22 +00001704
1705 As an example, the following calls to :func:`spawnlp` and :func:`spawnvpe` are
1706 equivalent::
1707
1708 import os
1709 os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1710
1711 L = ['cp', 'index.html', '/dev/null']
1712 os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1713
1714 Availability: Unix, Windows. :func:`spawnlp`, :func:`spawnlpe`, :func:`spawnvp`
1715 and :func:`spawnvpe` are not available on Windows.
1716
Georg Brandl116aa622007-08-15 14:28:22 +00001717
1718.. data:: P_NOWAIT
1719 P_NOWAITO
1720
1721 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1722 functions. If either of these values is given, the :func:`spawn\*` functions
Christian Heimesfaf2f632008-01-06 16:59:19 +00001723 will return as soon as the new process has been created, with the process id as
Benjamin Petersond91203b2010-05-06 23:20:40 +00001724 the return value.
1725
1726 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001727
Georg Brandl116aa622007-08-15 14:28:22 +00001728
1729.. data:: P_WAIT
1730
1731 Possible value for the *mode* parameter to the :func:`spawn\*` family of
1732 functions. If this is given as *mode*, the :func:`spawn\*` functions will not
1733 return until the new process has run to completion and will return the exit code
1734 of the process the run is successful, or ``-signal`` if a signal kills the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001735 process.
1736
1737 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001738
Georg Brandl116aa622007-08-15 14:28:22 +00001739
1740.. data:: P_DETACH
1741 P_OVERLAY
1742
1743 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1744 functions. These are less portable than those listed above. :const:`P_DETACH`
1745 is similar to :const:`P_NOWAIT`, but the new process is detached from the
1746 console of the calling process. If :const:`P_OVERLAY` is used, the current
1747 process will be replaced; the :func:`spawn\*` function will not return.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001748
Georg Brandl116aa622007-08-15 14:28:22 +00001749 Availability: Windows.
1750
Georg Brandl116aa622007-08-15 14:28:22 +00001751
1752.. function:: startfile(path[, operation])
1753
1754 Start a file with its associated application.
1755
1756 When *operation* is not specified or ``'open'``, this acts like double-clicking
1757 the file in Windows Explorer, or giving the file name as an argument to the
1758 :program:`start` command from the interactive command shell: the file is opened
1759 with whatever application (if any) its extension is associated.
1760
1761 When another *operation* is given, it must be a "command verb" that specifies
1762 what should be done with the file. Common verbs documented by Microsoft are
1763 ``'print'`` and ``'edit'`` (to be used on files) as well as ``'explore'`` and
1764 ``'find'`` (to be used on directories).
1765
1766 :func:`startfile` returns as soon as the associated application is launched.
1767 There is no option to wait for the application to close, and no way to retrieve
1768 the application's exit status. The *path* parameter is relative to the current
1769 directory. If you want to use an absolute path, make sure the first character
1770 is not a slash (``'/'``); the underlying Win32 :cfunc:`ShellExecute` function
1771 doesn't work if it is. Use the :func:`os.path.normpath` function to ensure that
Benjamin Petersond91203b2010-05-06 23:20:40 +00001772 the path is properly encoded for Win32.
1773
1774 Availability: Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001775
Georg Brandl116aa622007-08-15 14:28:22 +00001776
1777.. function:: system(command)
1778
1779 Execute the command (a string) in a subshell. This is implemented by calling
Georg Brandl628e6f92009-10-27 20:24:45 +00001780 the Standard C function :cfunc:`system`, and has the same limitations.
Georg Brandlaba97962010-11-26 08:37:46 +00001781 Changes to :data:`sys.stdin`, etc. are not reflected in the environment of
1782 the executed command. If *command* generates any output, it will be sent to
1783 the interpreter standard output stream.
Georg Brandl116aa622007-08-15 14:28:22 +00001784
1785 On Unix, the return value is the exit status of the process encoded in the
Georg Brandlaba97962010-11-26 08:37:46 +00001786 format specified for :func:`wait`. Note that POSIX does not specify the
1787 meaning of the return value of the C :cfunc:`system` function, so the return
1788 value of the Python function is system-dependent.
Georg Brandl116aa622007-08-15 14:28:22 +00001789
Georg Brandlaba97962010-11-26 08:37:46 +00001790 On Windows, the return value is that returned by the system shell after
1791 running *command*. The shell is given by the Windows environment variable
1792 :envvar:`COMSPEC`: it is usually :program:`cmd.exe`, which returns the exit
1793 status of the command run; on systems using a non-native shell, consult your
1794 shell documentation.
Georg Brandl116aa622007-08-15 14:28:22 +00001795
Georg Brandlaba97962010-11-26 08:37:46 +00001796 The :mod:`subprocess` module provides more powerful facilities for spawning
1797 new processes and retrieving their results; using that module is preferable
1798 to using this function. See the :ref:`subprocess-replacements` section in
1799 the :mod:`subprocess` documentation for some helpful recipes.
Georg Brandl116aa622007-08-15 14:28:22 +00001800
Benjamin Petersond91203b2010-05-06 23:20:40 +00001801 Availability: Unix, Windows.
1802
Georg Brandl116aa622007-08-15 14:28:22 +00001803
1804.. function:: times()
1805
Benjamin Petersond91203b2010-05-06 23:20:40 +00001806 Return a 5-tuple of floating point numbers indicating accumulated (processor
1807 or other) times, in seconds. The items are: user time, system time,
1808 children's user time, children's system time, and elapsed real time since a
1809 fixed point in the past, in that order. See the Unix manual page
1810 :manpage:`times(2)` or the corresponding Windows Platform API documentation.
1811 On Windows, only the first two items are filled, the others are zero.
1812
1813 Availability: Unix, Windows
Georg Brandl116aa622007-08-15 14:28:22 +00001814
1815
1816.. function:: wait()
1817
1818 Wait for completion of a child process, and return a tuple containing its pid
1819 and exit status indication: a 16-bit number, whose low byte is the signal number
1820 that killed the process, and whose high byte is the exit status (if the signal
1821 number is zero); the high bit of the low byte is set if a core file was
Benjamin Petersond91203b2010-05-06 23:20:40 +00001822 produced.
1823
1824 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001825
1826
1827.. function:: waitpid(pid, options)
1828
1829 The details of this function differ on Unix and Windows.
1830
1831 On Unix: Wait for completion of a child process given by process id *pid*, and
1832 return a tuple containing its process id and exit status indication (encoded as
1833 for :func:`wait`). The semantics of the call are affected by the value of the
1834 integer *options*, which should be ``0`` for normal operation.
1835
1836 If *pid* is greater than ``0``, :func:`waitpid` requests status information for
1837 that specific process. If *pid* is ``0``, the request is for the status of any
1838 child in the process group of the current process. If *pid* is ``-1``, the
1839 request pertains to any child of the current process. If *pid* is less than
1840 ``-1``, status is requested for any process in the process group ``-pid`` (the
1841 absolute value of *pid*).
1842
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001843 An :exc:`OSError` is raised with the value of errno when the syscall
1844 returns -1.
1845
Georg Brandl116aa622007-08-15 14:28:22 +00001846 On Windows: Wait for completion of a process given by process handle *pid*, and
1847 return a tuple containing *pid*, and its exit status shifted left by 8 bits
1848 (shifting makes cross-platform use of the function easier). A *pid* less than or
1849 equal to ``0`` has no special meaning on Windows, and raises an exception. The
1850 value of integer *options* has no effect. *pid* can refer to any process whose
1851 id is known, not necessarily a child process. The :func:`spawn` functions called
1852 with :const:`P_NOWAIT` return suitable process handles.
1853
1854
1855.. function:: wait3([options])
1856
1857 Similar to :func:`waitpid`, except no process id argument is given and a
1858 3-element tuple containing the child's process id, exit status indication, and
1859 resource usage information is returned. Refer to :mod:`resource`.\
1860 :func:`getrusage` for details on resource usage information. The option
1861 argument is the same as that provided to :func:`waitpid` and :func:`wait4`.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001862
Georg Brandl116aa622007-08-15 14:28:22 +00001863 Availability: Unix.
1864
Georg Brandl116aa622007-08-15 14:28:22 +00001865
1866.. function:: wait4(pid, options)
1867
1868 Similar to :func:`waitpid`, except a 3-element tuple, containing the child's
1869 process id, exit status indication, and resource usage information is returned.
1870 Refer to :mod:`resource`.\ :func:`getrusage` for details on resource usage
1871 information. The arguments to :func:`wait4` are the same as those provided to
Benjamin Petersond91203b2010-05-06 23:20:40 +00001872 :func:`waitpid`.
1873
1874 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001875
Georg Brandl116aa622007-08-15 14:28:22 +00001876
1877.. data:: WNOHANG
1878
1879 The option for :func:`waitpid` to return immediately if no child process status
1880 is available immediately. The function returns ``(0, 0)`` in this case.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001881
Georg Brandlc575c902008-09-13 17:46:05 +00001882 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001883
1884
1885.. data:: WCONTINUED
1886
1887 This option causes child processes to be reported if they have been continued
Benjamin Petersond91203b2010-05-06 23:20:40 +00001888 from a job control stop since their status was last reported.
1889
1890 Availability: Some Unix systems.
Georg Brandl116aa622007-08-15 14:28:22 +00001891
Georg Brandl116aa622007-08-15 14:28:22 +00001892
1893.. data:: WUNTRACED
1894
1895 This option causes child processes to be reported if they have been stopped but
Benjamin Petersond91203b2010-05-06 23:20:40 +00001896 their current state has not been reported since they were stopped.
1897
1898 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001899
Georg Brandl116aa622007-08-15 14:28:22 +00001900
1901The following functions take a process status code as returned by
1902:func:`system`, :func:`wait`, or :func:`waitpid` as a parameter. They may be
1903used to determine the disposition of a process.
1904
Georg Brandl116aa622007-08-15 14:28:22 +00001905.. function:: WCOREDUMP(status)
1906
Christian Heimesfaf2f632008-01-06 16:59:19 +00001907 Return ``True`` if a core dump was generated for the process, otherwise
Benjamin Petersond91203b2010-05-06 23:20:40 +00001908 return ``False``.
1909
1910 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001911
Georg Brandl116aa622007-08-15 14:28:22 +00001912
1913.. function:: WIFCONTINUED(status)
1914
Christian Heimesfaf2f632008-01-06 16:59:19 +00001915 Return ``True`` if the process has been continued from a job control stop,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001916 otherwise return ``False``.
1917
1918 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001919
Georg Brandl116aa622007-08-15 14:28:22 +00001920
1921.. function:: WIFSTOPPED(status)
1922
Christian Heimesfaf2f632008-01-06 16:59:19 +00001923 Return ``True`` if the process has been stopped, otherwise return
Benjamin Petersond91203b2010-05-06 23:20:40 +00001924 ``False``.
1925
1926 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001927
1928
1929.. function:: WIFSIGNALED(status)
1930
Christian Heimesfaf2f632008-01-06 16:59:19 +00001931 Return ``True`` if the process exited due to a signal, otherwise return
Benjamin Petersond91203b2010-05-06 23:20:40 +00001932 ``False``.
1933
1934 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001935
1936
1937.. function:: WIFEXITED(status)
1938
Christian Heimesfaf2f632008-01-06 16:59:19 +00001939 Return ``True`` if the process exited using the :manpage:`exit(2)` system call,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001940 otherwise return ``False``.
1941
1942 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001943
1944
1945.. function:: WEXITSTATUS(status)
1946
1947 If ``WIFEXITED(status)`` is true, return the integer parameter to the
1948 :manpage:`exit(2)` system call. Otherwise, the return value is meaningless.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001949
Georg Brandlc575c902008-09-13 17:46:05 +00001950 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001951
1952
1953.. function:: WSTOPSIG(status)
1954
Benjamin Petersond91203b2010-05-06 23:20:40 +00001955 Return the signal which caused the process to stop.
1956
1957 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001958
1959
1960.. function:: WTERMSIG(status)
1961
Benjamin Petersond91203b2010-05-06 23:20:40 +00001962 Return the signal which caused the process to exit.
1963
1964 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001965
1966
1967.. _os-path:
1968
1969Miscellaneous System Information
1970--------------------------------
1971
1972
1973.. function:: confstr(name)
1974
1975 Return string-valued system configuration values. *name* specifies the
1976 configuration value to retrieve; it may be a string which is the name of a
1977 defined system value; these names are specified in a number of standards (POSIX,
1978 Unix 95, Unix 98, and others). Some platforms define additional names as well.
1979 The names known to the host operating system are given as the keys of the
1980 ``confstr_names`` dictionary. For configuration variables not included in that
Benjamin Petersond91203b2010-05-06 23:20:40 +00001981 mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +00001982
1983 If the configuration value specified by *name* isn't defined, ``None`` is
1984 returned.
1985
1986 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1987 specific value for *name* is not supported by the host system, even if it is
1988 included in ``confstr_names``, an :exc:`OSError` is raised with
1989 :const:`errno.EINVAL` for the error number.
1990
Benjamin Petersond91203b2010-05-06 23:20:40 +00001991 Availability: Unix
1992
Georg Brandl116aa622007-08-15 14:28:22 +00001993
1994.. data:: confstr_names
1995
1996 Dictionary mapping names accepted by :func:`confstr` to the integer values
1997 defined for those names by the host operating system. This can be used to
Benjamin Petersond91203b2010-05-06 23:20:40 +00001998 determine the set of names known to the system.
1999
2000 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002001
2002
2003.. function:: getloadavg()
2004
Christian Heimesa62da1d2008-01-12 19:39:10 +00002005 Return the number of processes in the system run queue averaged over the last
2006 1, 5, and 15 minutes or raises :exc:`OSError` if the load average was
Benjamin Petersond91203b2010-05-06 23:20:40 +00002007 unobtainable.
2008
2009 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002010
Georg Brandl116aa622007-08-15 14:28:22 +00002011
2012.. function:: sysconf(name)
2013
2014 Return integer-valued system configuration values. If the configuration value
2015 specified by *name* isn't defined, ``-1`` is returned. The comments regarding
2016 the *name* parameter for :func:`confstr` apply here as well; the dictionary that
2017 provides information on the known names is given by ``sysconf_names``.
Benjamin Petersond91203b2010-05-06 23:20:40 +00002018
Georg Brandlc575c902008-09-13 17:46:05 +00002019 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002020
2021
2022.. data:: sysconf_names
2023
2024 Dictionary mapping names accepted by :func:`sysconf` to the integer values
2025 defined for those names by the host operating system. This can be used to
Benjamin Petersond91203b2010-05-06 23:20:40 +00002026 determine the set of names known to the system.
2027
2028 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002029
Christian Heimesfaf2f632008-01-06 16:59:19 +00002030The following data values are used to support path manipulation operations. These
Georg Brandl116aa622007-08-15 14:28:22 +00002031are defined for all platforms.
2032
2033Higher-level operations on pathnames are defined in the :mod:`os.path` module.
2034
2035
2036.. data:: curdir
2037
2038 The constant string used by the operating system to refer to the current
Georg Brandlc575c902008-09-13 17:46:05 +00002039 directory. This is ``'.'`` for Windows and POSIX. Also available via
2040 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002041
2042
2043.. data:: pardir
2044
2045 The constant string used by the operating system to refer to the parent
Georg Brandlc575c902008-09-13 17:46:05 +00002046 directory. This is ``'..'`` for Windows and POSIX. Also available via
2047 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002048
2049
2050.. data:: sep
2051
Georg Brandlc575c902008-09-13 17:46:05 +00002052 The character used by the operating system to separate pathname components.
2053 This is ``'/'`` for POSIX and ``'\\'`` for Windows. Note that knowing this
2054 is not sufficient to be able to parse or concatenate pathnames --- use
Georg Brandl116aa622007-08-15 14:28:22 +00002055 :func:`os.path.split` and :func:`os.path.join` --- but it is occasionally
2056 useful. Also available via :mod:`os.path`.
2057
2058
2059.. data:: altsep
2060
2061 An alternative character used by the operating system to separate pathname
2062 components, or ``None`` if only one separator character exists. This is set to
2063 ``'/'`` on Windows systems where ``sep`` is a backslash. Also available via
2064 :mod:`os.path`.
2065
2066
2067.. data:: extsep
2068
2069 The character which separates the base filename from the extension; for example,
2070 the ``'.'`` in :file:`os.py`. Also available via :mod:`os.path`.
2071
Georg Brandl116aa622007-08-15 14:28:22 +00002072
2073.. data:: pathsep
2074
2075 The character conventionally used by the operating system to separate search
2076 path components (as in :envvar:`PATH`), such as ``':'`` for POSIX or ``';'`` for
2077 Windows. Also available via :mod:`os.path`.
2078
2079
2080.. data:: defpath
2081
2082 The default search path used by :func:`exec\*p\*` and :func:`spawn\*p\*` if the
2083 environment doesn't have a ``'PATH'`` key. Also available via :mod:`os.path`.
2084
2085
2086.. data:: linesep
2087
2088 The string used to separate (or, rather, terminate) lines on the current
Georg Brandlc575c902008-09-13 17:46:05 +00002089 platform. This may be a single character, such as ``'\n'`` for POSIX, or
2090 multiple characters, for example, ``'\r\n'`` for Windows. Do not use
2091 *os.linesep* as a line terminator when writing files opened in text mode (the
2092 default); use a single ``'\n'`` instead, on all platforms.
Georg Brandl116aa622007-08-15 14:28:22 +00002093
2094
2095.. data:: devnull
2096
Georg Brandlc52eeab2010-05-21 22:05:15 +00002097 The file path of the null device. For example: ``'/dev/null'`` for
2098 POSIX, ``'nul'`` for Windows. Also available via :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002099
Georg Brandl116aa622007-08-15 14:28:22 +00002100
2101.. _os-miscfunc:
2102
2103Miscellaneous Functions
2104-----------------------
2105
2106
2107.. function:: urandom(n)
2108
2109 Return a string of *n* random bytes suitable for cryptographic use.
2110
2111 This function returns random bytes from an OS-specific randomness source. The
2112 returned data should be unpredictable enough for cryptographic applications,
2113 though its exact quality depends on the OS implementation. On a UNIX-like
2114 system this will query /dev/urandom, and on Windows it will use CryptGenRandom.
2115 If a randomness source is not found, :exc:`NotImplementedError` will be raised.