blob: d5aa6fe3431a3f6970f60e681c72169ff7d4af09 [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
Benjamin Petersond91203b2010-05-06 23:20:40 +0000537 Return status for file descriptor *fd*, like :func:`stat`.
538
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
Guido van Rossum2cc30da2007-11-02 23:46:40 +0000955 Like :func:`stat`, but do not follow symbolic links. This is an alias for
956 :func:`stat` on platforms that do not support symbolic links, such as
957 Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000958
959
960.. function:: mkfifo(path[, mode])
961
Georg Brandlf4a41232008-05-26 17:55:52 +0000962 Create a FIFO (a named pipe) named *path* with numeric mode *mode*. The
963 default *mode* is ``0o666`` (octal). The current umask value is first masked
Benjamin Petersond91203b2010-05-06 23:20:40 +0000964 out from the mode.
Georg Brandl116aa622007-08-15 14:28:22 +0000965
966 FIFOs are pipes that can be accessed like regular files. FIFOs exist until they
967 are deleted (for example with :func:`os.unlink`). Generally, FIFOs are used as
968 rendezvous between "client" and "server" type processes: the server opens the
969 FIFO for reading, and the client opens it for writing. Note that :func:`mkfifo`
970 doesn't open the FIFO --- it just creates the rendezvous point.
971
Benjamin Petersond91203b2010-05-06 23:20:40 +0000972 Availability: Unix.
973
Georg Brandl116aa622007-08-15 14:28:22 +0000974
Georg Brandlf4a41232008-05-26 17:55:52 +0000975.. function:: mknod(filename[, mode=0o600, device])
Georg Brandl116aa622007-08-15 14:28:22 +0000976
977 Create a filesystem node (file, device special file or named pipe) named
978 *filename*. *mode* specifies both the permissions to use and the type of node to
979 be created, being combined (bitwise OR) with one of ``stat.S_IFREG``,
980 ``stat.S_IFCHR``, ``stat.S_IFBLK``,
981 and ``stat.S_IFIFO`` (those constants are available in :mod:`stat`).
982 For ``stat.S_IFCHR`` and
983 ``stat.S_IFBLK``, *device* defines the newly created device special file (probably using
984 :func:`os.makedev`), otherwise it is ignored.
985
Georg Brandl116aa622007-08-15 14:28:22 +0000986
987.. function:: major(device)
988
Christian Heimesfaf2f632008-01-06 16:59:19 +0000989 Extract the device major number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000990 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
991
Georg Brandl116aa622007-08-15 14:28:22 +0000992
993.. function:: minor(device)
994
Christian Heimesfaf2f632008-01-06 16:59:19 +0000995 Extract the device minor number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000996 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
997
Georg Brandl116aa622007-08-15 14:28:22 +0000998
999.. function:: makedev(major, minor)
1000
Christian Heimesfaf2f632008-01-06 16:59:19 +00001001 Compose a raw device number from the major and minor device numbers.
Georg Brandl116aa622007-08-15 14:28:22 +00001002
Georg Brandl116aa622007-08-15 14:28:22 +00001003
1004.. function:: mkdir(path[, mode])
1005
Georg Brandlf4a41232008-05-26 17:55:52 +00001006 Create a directory named *path* with numeric mode *mode*. The default *mode*
1007 is ``0o777`` (octal). On some systems, *mode* is ignored. Where it is used,
Georg Brandlc62efa82010-07-11 10:41:07 +00001008 the current umask value is first masked out. If the directory already
1009 exists, :exc:`OSError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +00001010
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001011 It is also possible to create temporary directories; see the
1012 :mod:`tempfile` module's :func:`tempfile.mkdtemp` function.
1013
Benjamin Petersond91203b2010-05-06 23:20:40 +00001014 Availability: Unix, Windows.
1015
Georg Brandl116aa622007-08-15 14:28:22 +00001016
1017.. function:: makedirs(path[, mode])
1018
1019 .. index::
1020 single: directory; creating
1021 single: UNC paths; and os.makedirs()
1022
1023 Recursive directory creation function. Like :func:`mkdir`, but makes all
Éric Araujod82a47c2010-11-30 17:38:32 +00001024 intermediate-level directories needed to contain the leaf directory. Raises
Georg Brandlf4a41232008-05-26 17:55:52 +00001025 an :exc:`error` exception if the leaf directory already exists or cannot be
1026 created. The default *mode* is ``0o777`` (octal). On some systems, *mode*
1027 is ignored. Where it is used, the current umask value is first masked out.
Georg Brandl116aa622007-08-15 14:28:22 +00001028
1029 .. note::
1030
1031 :func:`makedirs` will become confused if the path elements to create include
Christian Heimesfaf2f632008-01-06 16:59:19 +00001032 :data:`os.pardir`.
Georg Brandl116aa622007-08-15 14:28:22 +00001033
Georg Brandl55ac8f02007-09-01 13:51:09 +00001034 This function handles UNC paths correctly.
Georg Brandl116aa622007-08-15 14:28:22 +00001035
1036
1037.. function:: pathconf(path, name)
1038
1039 Return system configuration information relevant to a named file. *name*
1040 specifies the configuration value to retrieve; it may be a string which is the
1041 name of a defined system value; these names are specified in a number of
1042 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
1043 additional names as well. The names known to the host operating system are
1044 given in the ``pathconf_names`` dictionary. For configuration variables not
1045 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +00001046
1047 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1048 specific value for *name* is not supported by the host system, even if it is
1049 included in ``pathconf_names``, an :exc:`OSError` is raised with
1050 :const:`errno.EINVAL` for the error number.
1051
Benjamin Petersond91203b2010-05-06 23:20:40 +00001052 Availability: Unix.
1053
Georg Brandl116aa622007-08-15 14:28:22 +00001054
1055.. data:: pathconf_names
1056
1057 Dictionary mapping names accepted by :func:`pathconf` and :func:`fpathconf` to
1058 the integer values defined for those names by the host operating system. This
1059 can be used to determine the set of names known to the system. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001060 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001061
1062
1063.. function:: readlink(path)
1064
1065 Return a string representing the path to which the symbolic link points. The
1066 result may be either an absolute or relative pathname; if it is relative, it may
1067 be converted to an absolute pathname using ``os.path.join(os.path.dirname(path),
1068 result)``.
1069
Georg Brandl76e55382008-10-08 16:34:57 +00001070 If the *path* is a string object, the result will also be a string object,
1071 and the call may raise an UnicodeDecodeError. If the *path* is a bytes
1072 object, the result will be a bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +00001073
Georg Brandlc575c902008-09-13 17:46:05 +00001074 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001075
1076
1077.. function:: remove(path)
1078
Georg Brandl7baf6252009-09-01 08:13:16 +00001079 Remove (delete) the file *path*. If *path* is a directory, :exc:`OSError` is
1080 raised; see :func:`rmdir` below to remove a directory. This is identical to
1081 the :func:`unlink` function documented below. On Windows, attempting to
1082 remove a file that is in use causes an exception to be raised; on Unix, the
1083 directory entry is removed but the storage allocated to the file is not made
Benjamin Petersond91203b2010-05-06 23:20:40 +00001084 available until the original file is no longer in use.
1085
1086 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001087
1088
1089.. function:: removedirs(path)
1090
1091 .. index:: single: directory; deleting
1092
Christian Heimesfaf2f632008-01-06 16:59:19 +00001093 Remove directories recursively. Works like :func:`rmdir` except that, if the
Georg Brandl116aa622007-08-15 14:28:22 +00001094 leaf directory is successfully removed, :func:`removedirs` tries to
1095 successively remove every parent directory mentioned in *path* until an error
1096 is raised (which is ignored, because it generally means that a parent directory
1097 is not empty). For example, ``os.removedirs('foo/bar/baz')`` will first remove
1098 the directory ``'foo/bar/baz'``, and then remove ``'foo/bar'`` and ``'foo'`` if
1099 they are empty. Raises :exc:`OSError` if the leaf directory could not be
1100 successfully removed.
1101
Georg Brandl116aa622007-08-15 14:28:22 +00001102
1103.. function:: rename(src, dst)
1104
1105 Rename the file or directory *src* to *dst*. If *dst* is a directory,
1106 :exc:`OSError` will be raised. On Unix, if *dst* exists and is a file, it will
Christian Heimesfaf2f632008-01-06 16:59:19 +00001107 be replaced silently if the user has permission. The operation may fail on some
Georg Brandl116aa622007-08-15 14:28:22 +00001108 Unix flavors if *src* and *dst* are on different filesystems. If successful,
1109 the renaming will be an atomic operation (this is a POSIX requirement). On
1110 Windows, if *dst* already exists, :exc:`OSError` will be raised even if it is a
1111 file; there may be no way to implement an atomic rename when *dst* names an
Benjamin Petersond91203b2010-05-06 23:20:40 +00001112 existing file.
1113
1114 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001115
1116
1117.. function:: renames(old, new)
1118
1119 Recursive directory or file renaming function. Works like :func:`rename`, except
1120 creation of any intermediate directories needed to make the new pathname good is
1121 attempted first. After the rename, directories corresponding to rightmost path
1122 segments of the old name will be pruned away using :func:`removedirs`.
1123
Georg Brandl116aa622007-08-15 14:28:22 +00001124 .. note::
1125
1126 This function can fail with the new directory structure made if you lack
1127 permissions needed to remove the leaf directory or file.
1128
1129
1130.. function:: rmdir(path)
1131
Georg Brandl7baf6252009-09-01 08:13:16 +00001132 Remove (delete) the directory *path*. Only works when the directory is
1133 empty, otherwise, :exc:`OSError` is raised. In order to remove whole
Benjamin Petersond91203b2010-05-06 23:20:40 +00001134 directory trees, :func:`shutil.rmtree` can be used.
1135
1136 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001137
1138
1139.. function:: stat(path)
1140
1141 Perform a :cfunc:`stat` system call on the given path. The return value is an
1142 object whose attributes correspond to the members of the :ctype:`stat`
1143 structure, namely: :attr:`st_mode` (protection bits), :attr:`st_ino` (inode
1144 number), :attr:`st_dev` (device), :attr:`st_nlink` (number of hard links),
Christian Heimesfaf2f632008-01-06 16:59:19 +00001145 :attr:`st_uid` (user id of owner), :attr:`st_gid` (group id of owner),
Georg Brandl116aa622007-08-15 14:28:22 +00001146 :attr:`st_size` (size of file, in bytes), :attr:`st_atime` (time of most recent
1147 access), :attr:`st_mtime` (time of most recent content modification),
1148 :attr:`st_ctime` (platform dependent; time of most recent metadata change on
1149 Unix, or the time of creation on Windows)::
1150
1151 >>> import os
1152 >>> statinfo = os.stat('somefile.txt')
1153 >>> statinfo
Ezio Melotti9ce52612010-01-16 14:52:34 +00001154 (33188, 422511, 769, 1, 1032, 100, 926, 1105022698,1105022732, 1105022732)
Georg Brandl116aa622007-08-15 14:28:22 +00001155 >>> statinfo.st_size
Ezio Melotti9ce52612010-01-16 14:52:34 +00001156 926
Georg Brandl116aa622007-08-15 14:28:22 +00001157 >>>
1158
Georg Brandl116aa622007-08-15 14:28:22 +00001159
1160 On some Unix systems (such as Linux), the following attributes may also be
1161 available: :attr:`st_blocks` (number of blocks allocated for file),
1162 :attr:`st_blksize` (filesystem blocksize), :attr:`st_rdev` (type of device if an
1163 inode device). :attr:`st_flags` (user defined flags for file).
1164
1165 On other Unix systems (such as FreeBSD), the following attributes may be
1166 available (but may be only filled out if root tries to use them): :attr:`st_gen`
1167 (file generation number), :attr:`st_birthtime` (time of file creation).
1168
1169 On Mac OS systems, the following attributes may also be available:
1170 :attr:`st_rsize`, :attr:`st_creator`, :attr:`st_type`.
1171
Georg Brandl116aa622007-08-15 14:28:22 +00001172 .. index:: module: stat
1173
1174 For backward compatibility, the return value of :func:`stat` is also accessible
1175 as a tuple of at least 10 integers giving the most important (and portable)
1176 members of the :ctype:`stat` structure, in the order :attr:`st_mode`,
1177 :attr:`st_ino`, :attr:`st_dev`, :attr:`st_nlink`, :attr:`st_uid`,
1178 :attr:`st_gid`, :attr:`st_size`, :attr:`st_atime`, :attr:`st_mtime`,
1179 :attr:`st_ctime`. More items may be added at the end by some implementations.
1180 The standard module :mod:`stat` defines functions and constants that are useful
1181 for extracting information from a :ctype:`stat` structure. (On Windows, some
1182 items are filled with dummy values.)
1183
1184 .. note::
1185
1186 The exact meaning and resolution of the :attr:`st_atime`, :attr:`st_mtime`, and
1187 :attr:`st_ctime` members depends on the operating system and the file system.
1188 For example, on Windows systems using the FAT or FAT32 file systems,
1189 :attr:`st_mtime` has 2-second resolution, and :attr:`st_atime` has only 1-day
1190 resolution. See your operating system documentation for details.
1191
Georg Brandlc575c902008-09-13 17:46:05 +00001192 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001193
Georg Brandl116aa622007-08-15 14:28:22 +00001194
1195.. function:: stat_float_times([newvalue])
1196
1197 Determine whether :class:`stat_result` represents time stamps as float objects.
1198 If *newvalue* is ``True``, future calls to :func:`stat` return floats, if it is
1199 ``False``, future calls return ints. If *newvalue* is omitted, return the
1200 current setting.
1201
1202 For compatibility with older Python versions, accessing :class:`stat_result` as
1203 a tuple always returns integers.
1204
Georg Brandl55ac8f02007-09-01 13:51:09 +00001205 Python now returns float values by default. Applications which do not work
1206 correctly with floating point time stamps can use this function to restore the
1207 old behaviour.
Georg Brandl116aa622007-08-15 14:28:22 +00001208
1209 The resolution of the timestamps (that is the smallest possible fraction)
1210 depends on the system. Some systems only support second resolution; on these
1211 systems, the fraction will always be zero.
1212
1213 It is recommended that this setting is only changed at program startup time in
1214 the *__main__* module; libraries should never change this setting. If an
1215 application uses a library that works incorrectly if floating point time stamps
1216 are processed, this application should turn the feature off until the library
1217 has been corrected.
1218
1219
1220.. function:: statvfs(path)
1221
1222 Perform a :cfunc:`statvfs` system call on the given path. The return value is
1223 an object whose attributes describe the filesystem on the given path, and
1224 correspond to the members of the :ctype:`statvfs` structure, namely:
1225 :attr:`f_bsize`, :attr:`f_frsize`, :attr:`f_blocks`, :attr:`f_bfree`,
1226 :attr:`f_bavail`, :attr:`f_files`, :attr:`f_ffree`, :attr:`f_favail`,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001227 :attr:`f_flag`, :attr:`f_namemax`.
1228
1229 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001230
Georg Brandl116aa622007-08-15 14:28:22 +00001231
Benjamin Peterson5879d412009-03-30 14:51:56 +00001232.. function:: symlink(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +00001233
Benjamin Petersond91203b2010-05-06 23:20:40 +00001234 Create a symbolic link pointing to *source* named *link_name*.
1235
1236 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001237
1238
Georg Brandl116aa622007-08-15 14:28:22 +00001239.. function:: unlink(path)
1240
Georg Brandl7baf6252009-09-01 08:13:16 +00001241 Remove (delete) the file *path*. This is the same function as
1242 :func:`remove`; the :func:`unlink` name is its traditional Unix
Benjamin Petersond91203b2010-05-06 23:20:40 +00001243 name.
1244
1245 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001246
1247
1248.. function:: utime(path, times)
1249
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001250 Set the access and modified times of the file specified by *path*. If *times*
1251 is ``None``, then the file's access and modified times are set to the current
1252 time. (The effect is similar to running the Unix program :program:`touch` on
1253 the path.) Otherwise, *times* must be a 2-tuple of numbers, of the form
1254 ``(atime, mtime)`` which is used to set the access and modified times,
1255 respectively. Whether a directory can be given for *path* depends on whether
1256 the operating system implements directories as files (for example, Windows
1257 does not). Note that the exact times you set here may not be returned by a
1258 subsequent :func:`stat` call, depending on the resolution with which your
1259 operating system records access and modification times; see :func:`stat`.
Georg Brandl116aa622007-08-15 14:28:22 +00001260
Georg Brandlc575c902008-09-13 17:46:05 +00001261 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001262
1263
1264.. function:: walk(top[, topdown=True [, onerror=None[, followlinks=False]]])
1265
1266 .. index::
1267 single: directory; walking
1268 single: directory; traversal
1269
Christian Heimesfaf2f632008-01-06 16:59:19 +00001270 Generate the file names in a directory tree by walking the tree
1271 either top-down or bottom-up. For each directory in the tree rooted at directory
Georg Brandl116aa622007-08-15 14:28:22 +00001272 *top* (including *top* itself), it yields a 3-tuple ``(dirpath, dirnames,
1273 filenames)``.
1274
1275 *dirpath* is a string, the path to the directory. *dirnames* is a list of the
1276 names of the subdirectories in *dirpath* (excluding ``'.'`` and ``'..'``).
1277 *filenames* is a list of the names of the non-directory files in *dirpath*.
1278 Note that the names in the lists contain no path components. To get a full path
1279 (which begins with *top*) to a file or directory in *dirpath*, do
1280 ``os.path.join(dirpath, name)``.
1281
Christian Heimesfaf2f632008-01-06 16:59:19 +00001282 If optional argument *topdown* is ``True`` or not specified, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001283 directory is generated before the triples for any of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001284 (directories are generated top-down). If *topdown* is ``False``, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001285 directory is generated after the triples for all of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001286 (directories are generated bottom-up).
Georg Brandl116aa622007-08-15 14:28:22 +00001287
Christian Heimesfaf2f632008-01-06 16:59:19 +00001288 When *topdown* is ``True``, the caller can modify the *dirnames* list in-place
Georg Brandl116aa622007-08-15 14:28:22 +00001289 (perhaps using :keyword:`del` or slice assignment), and :func:`walk` will only
1290 recurse into the subdirectories whose names remain in *dirnames*; this can be
1291 used to prune the search, impose a specific order of visiting, or even to inform
1292 :func:`walk` about directories the caller creates or renames before it resumes
Christian Heimesfaf2f632008-01-06 16:59:19 +00001293 :func:`walk` again. Modifying *dirnames* when *topdown* is ``False`` is
Georg Brandl116aa622007-08-15 14:28:22 +00001294 ineffective, because in bottom-up mode the directories in *dirnames* are
1295 generated before *dirpath* itself is generated.
1296
Christian Heimesfaf2f632008-01-06 16:59:19 +00001297 By default errors from the :func:`listdir` call are ignored. If optional
Georg Brandl116aa622007-08-15 14:28:22 +00001298 argument *onerror* is specified, it should be a function; it will be called with
1299 one argument, an :exc:`OSError` instance. It can report the error to continue
1300 with the walk, or raise the exception to abort the walk. Note that the filename
1301 is available as the ``filename`` attribute of the exception object.
1302
1303 By default, :func:`walk` will not walk down into symbolic links that resolve to
Christian Heimesfaf2f632008-01-06 16:59:19 +00001304 directories. Set *followlinks* to ``True`` to visit directories pointed to by
Georg Brandl116aa622007-08-15 14:28:22 +00001305 symlinks, on systems that support them.
1306
Georg Brandl116aa622007-08-15 14:28:22 +00001307 .. note::
1308
Christian Heimesfaf2f632008-01-06 16:59:19 +00001309 Be aware that setting *followlinks* to ``True`` can lead to infinite recursion if a
Georg Brandl116aa622007-08-15 14:28:22 +00001310 link points to a parent directory of itself. :func:`walk` does not keep track of
1311 the directories it visited already.
1312
1313 .. note::
1314
1315 If you pass a relative pathname, don't change the current working directory
1316 between resumptions of :func:`walk`. :func:`walk` never changes the current
1317 directory, and assumes that its caller doesn't either.
1318
1319 This example displays the number of bytes taken by non-directory files in each
1320 directory under the starting directory, except that it doesn't look under any
1321 CVS subdirectory::
1322
1323 import os
1324 from os.path import join, getsize
1325 for root, dirs, files in os.walk('python/Lib/email'):
Georg Brandl6911e3c2007-09-04 07:15:32 +00001326 print(root, "consumes", end=" ")
1327 print(sum(getsize(join(root, name)) for name in files), end=" ")
1328 print("bytes in", len(files), "non-directory files")
Georg Brandl116aa622007-08-15 14:28:22 +00001329 if 'CVS' in dirs:
1330 dirs.remove('CVS') # don't visit CVS directories
1331
Christian Heimesfaf2f632008-01-06 16:59:19 +00001332 In the next example, walking the tree bottom-up is essential: :func:`rmdir`
Georg Brandl116aa622007-08-15 14:28:22 +00001333 doesn't allow deleting a directory before the directory is empty::
1334
Christian Heimesfaf2f632008-01-06 16:59:19 +00001335 # Delete everything reachable from the directory named in "top",
Georg Brandl116aa622007-08-15 14:28:22 +00001336 # assuming there are no symbolic links.
1337 # CAUTION: This is dangerous! For example, if top == '/', it
1338 # could delete all your disk files.
1339 import os
1340 for root, dirs, files in os.walk(top, topdown=False):
1341 for name in files:
1342 os.remove(os.path.join(root, name))
1343 for name in dirs:
1344 os.rmdir(os.path.join(root, name))
1345
Georg Brandl116aa622007-08-15 14:28:22 +00001346
1347.. _os-process:
1348
1349Process Management
1350------------------
1351
1352These functions may be used to create and manage processes.
1353
1354The various :func:`exec\*` functions take a list of arguments for the new
1355program loaded into the process. In each case, the first of these arguments is
1356passed to the new program as its own name rather than as an argument a user may
1357have typed on a command line. For the C programmer, this is the ``argv[0]``
1358passed to a program's :cfunc:`main`. For example, ``os.execv('/bin/echo',
1359['foo', 'bar'])`` will only print ``bar`` on standard output; ``foo`` will seem
1360to be ignored.
1361
1362
1363.. function:: abort()
1364
1365 Generate a :const:`SIGABRT` signal to the current process. On Unix, the default
1366 behavior is to produce a core dump; on Windows, the process immediately returns
1367 an exit code of ``3``. Be aware that programs which use :func:`signal.signal`
1368 to register a handler for :const:`SIGABRT` will behave differently.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001369
Georg Brandlc575c902008-09-13 17:46:05 +00001370 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001371
1372
1373.. function:: execl(path, arg0, arg1, ...)
1374 execle(path, arg0, arg1, ..., env)
1375 execlp(file, arg0, arg1, ...)
1376 execlpe(file, arg0, arg1, ..., env)
1377 execv(path, args)
1378 execve(path, args, env)
1379 execvp(file, args)
1380 execvpe(file, args, env)
1381
1382 These functions all execute a new program, replacing the current process; they
1383 do not return. On Unix, the new executable is loaded into the current process,
Christian Heimesfaf2f632008-01-06 16:59:19 +00001384 and will have the same process id as the caller. Errors will be reported as
Georg Brandl48310cd2009-01-03 21:18:54 +00001385 :exc:`OSError` exceptions.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001386
1387 The current process is replaced immediately. Open file objects and
1388 descriptors are not flushed, so if there may be data buffered
1389 on these open files, you should flush them using
1390 :func:`sys.stdout.flush` or :func:`os.fsync` before calling an
1391 :func:`exec\*` function.
Georg Brandl116aa622007-08-15 14:28:22 +00001392
Christian Heimesfaf2f632008-01-06 16:59:19 +00001393 The "l" and "v" variants of the :func:`exec\*` functions differ in how
1394 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001395 to work with if the number of parameters is fixed when the code is written; the
1396 individual parameters simply become additional parameters to the :func:`execl\*`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001397 functions. The "v" variants are good when the number of parameters is
Georg Brandl116aa622007-08-15 14:28:22 +00001398 variable, with the arguments being passed in a list or tuple as the *args*
1399 parameter. In either case, the arguments to the child process should start with
1400 the name of the command being run, but this is not enforced.
1401
Christian Heimesfaf2f632008-01-06 16:59:19 +00001402 The variants which include a "p" near the end (:func:`execlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001403 :func:`execlpe`, :func:`execvp`, and :func:`execvpe`) will use the
1404 :envvar:`PATH` environment variable to locate the program *file*. When the
1405 environment is being replaced (using one of the :func:`exec\*e` variants,
1406 discussed in the next paragraph), the new environment is used as the source of
1407 the :envvar:`PATH` variable. The other variants, :func:`execl`, :func:`execle`,
1408 :func:`execv`, and :func:`execve`, will not use the :envvar:`PATH` variable to
1409 locate the executable; *path* must contain an appropriate absolute or relative
1410 path.
1411
1412 For :func:`execle`, :func:`execlpe`, :func:`execve`, and :func:`execvpe` (note
Christian Heimesfaf2f632008-01-06 16:59:19 +00001413 that these all end in "e"), the *env* parameter must be a mapping which is
Christian Heimesa342c012008-04-20 21:01:16 +00001414 used to define the environment variables for the new process (these are used
1415 instead of the current process' environment); the functions :func:`execl`,
Georg Brandl116aa622007-08-15 14:28:22 +00001416 :func:`execlp`, :func:`execv`, and :func:`execvp` all cause the new process to
Georg Brandl48310cd2009-01-03 21:18:54 +00001417 inherit the environment of the current process.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001418
1419 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001420
1421
1422.. function:: _exit(n)
1423
Georg Brandlab32fec2010-11-26 08:49:15 +00001424 Exit the process with status *n*, without calling cleanup handlers, flushing
Benjamin Petersond91203b2010-05-06 23:20:40 +00001425 stdio buffers, etc.
1426
1427 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001428
1429 .. note::
1430
Georg Brandlab32fec2010-11-26 08:49:15 +00001431 The standard way to exit is ``sys.exit(n)``. :func:`_exit` should
1432 normally only be used in the child process after a :func:`fork`.
Georg Brandl116aa622007-08-15 14:28:22 +00001433
Christian Heimesfaf2f632008-01-06 16:59:19 +00001434The following exit codes are defined and can be used with :func:`_exit`,
Georg Brandl116aa622007-08-15 14:28:22 +00001435although they are not required. These are typically used for system programs
1436written in Python, such as a mail server's external command delivery program.
1437
1438.. note::
1439
1440 Some of these may not be available on all Unix platforms, since there is some
1441 variation. These constants are defined where they are defined by the underlying
1442 platform.
1443
1444
1445.. data:: EX_OK
1446
Benjamin Petersond91203b2010-05-06 23:20:40 +00001447 Exit code that means no error occurred.
1448
1449 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001450
Georg Brandl116aa622007-08-15 14:28:22 +00001451
1452.. data:: EX_USAGE
1453
1454 Exit code that means the command was used incorrectly, such as when the wrong
Benjamin Petersond91203b2010-05-06 23:20:40 +00001455 number of arguments are given.
1456
1457 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001458
Georg Brandl116aa622007-08-15 14:28:22 +00001459
1460.. data:: EX_DATAERR
1461
Benjamin Petersond91203b2010-05-06 23:20:40 +00001462 Exit code that means the input data was incorrect.
1463
1464 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001465
Georg Brandl116aa622007-08-15 14:28:22 +00001466
1467.. data:: EX_NOINPUT
1468
1469 Exit code that means an input file did not exist or was not readable.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001470
Georg Brandlc575c902008-09-13 17:46:05 +00001471 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001472
Georg Brandl116aa622007-08-15 14:28:22 +00001473
1474.. data:: EX_NOUSER
1475
Benjamin Petersond91203b2010-05-06 23:20:40 +00001476 Exit code that means a specified user did not exist.
1477
1478 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001479
Georg Brandl116aa622007-08-15 14:28:22 +00001480
1481.. data:: EX_NOHOST
1482
Benjamin Petersond91203b2010-05-06 23:20:40 +00001483 Exit code that means a specified host did not exist.
1484
1485 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001486
Georg Brandl116aa622007-08-15 14:28:22 +00001487
1488.. data:: EX_UNAVAILABLE
1489
Benjamin Petersond91203b2010-05-06 23:20:40 +00001490 Exit code that means that a required service is unavailable.
1491
1492 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001493
Georg Brandl116aa622007-08-15 14:28:22 +00001494
1495.. data:: EX_SOFTWARE
1496
Benjamin Petersond91203b2010-05-06 23:20:40 +00001497 Exit code that means an internal software error was detected.
1498
1499 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001500
Georg Brandl116aa622007-08-15 14:28:22 +00001501
1502.. data:: EX_OSERR
1503
1504 Exit code that means an operating system error was detected, such as the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001505 inability to fork or create a pipe.
1506
1507 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001508
Georg Brandl116aa622007-08-15 14:28:22 +00001509
1510.. data:: EX_OSFILE
1511
1512 Exit code that means some system file did not exist, could not be opened, or had
Benjamin Petersond91203b2010-05-06 23:20:40 +00001513 some other kind of error.
1514
1515 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001516
Georg Brandl116aa622007-08-15 14:28:22 +00001517
1518.. data:: EX_CANTCREAT
1519
1520 Exit code that means a user specified output file could not be created.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001521
Georg Brandlc575c902008-09-13 17:46:05 +00001522 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001523
Georg Brandl116aa622007-08-15 14:28:22 +00001524
1525.. data:: EX_IOERR
1526
1527 Exit code that means that an error occurred while doing I/O on some file.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001528
Georg Brandlc575c902008-09-13 17:46:05 +00001529 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001530
Georg Brandl116aa622007-08-15 14:28:22 +00001531
1532.. data:: EX_TEMPFAIL
1533
1534 Exit code that means a temporary failure occurred. This indicates something
1535 that may not really be an error, such as a network connection that couldn't be
Benjamin Petersond91203b2010-05-06 23:20:40 +00001536 made during a retryable operation.
1537
1538 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001539
Georg Brandl116aa622007-08-15 14:28:22 +00001540
1541.. data:: EX_PROTOCOL
1542
1543 Exit code that means that a protocol exchange was illegal, invalid, or not
Benjamin Petersond91203b2010-05-06 23:20:40 +00001544 understood.
1545
1546 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001547
Georg Brandl116aa622007-08-15 14:28:22 +00001548
1549.. data:: EX_NOPERM
1550
1551 Exit code that means that there were insufficient permissions to perform the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001552 operation (but not intended for file system problems).
1553
1554 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001555
Georg Brandl116aa622007-08-15 14:28:22 +00001556
1557.. data:: EX_CONFIG
1558
1559 Exit code that means that some kind of configuration error occurred.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001560
Georg Brandlc575c902008-09-13 17:46:05 +00001561 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001562
Georg Brandl116aa622007-08-15 14:28:22 +00001563
1564.. data:: EX_NOTFOUND
1565
Benjamin Petersond91203b2010-05-06 23:20:40 +00001566 Exit code that means something like "an entry was not found".
1567
1568 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001569
Georg Brandl116aa622007-08-15 14:28:22 +00001570
1571.. function:: fork()
1572
Christian Heimesfaf2f632008-01-06 16:59:19 +00001573 Fork a child process. Return ``0`` in the child and the child's process id in the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001574 parent. If an error occurs :exc:`OSError` is raised.
Benjamin Petersonbcd8ac32008-10-10 22:20:52 +00001575
1576 Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have
1577 known issues when using fork() from a thread.
1578
Georg Brandlc575c902008-09-13 17:46:05 +00001579 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001580
1581
1582.. function:: forkpty()
1583
1584 Fork a child process, using a new pseudo-terminal as the child's controlling
1585 terminal. Return a pair of ``(pid, fd)``, where *pid* is ``0`` in the child, the
1586 new child's process id in the parent, and *fd* is the file descriptor of the
1587 master end of the pseudo-terminal. For a more portable approach, use the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001588 :mod:`pty` module. If an error occurs :exc:`OSError` is raised.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001589
Georg Brandlc575c902008-09-13 17:46:05 +00001590 Availability: some flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001591
1592
1593.. function:: kill(pid, sig)
1594
1595 .. index::
1596 single: process; killing
1597 single: process; signalling
1598
1599 Send signal *sig* to the process *pid*. Constants for the specific signals
1600 available on the host platform are defined in the :mod:`signal` module.
Georg Brandlc575c902008-09-13 17:46:05 +00001601 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001602
1603
1604.. function:: killpg(pgid, sig)
1605
1606 .. index::
1607 single: process; killing
1608 single: process; signalling
1609
Benjamin Petersond91203b2010-05-06 23:20:40 +00001610 Send the signal *sig* to the process group *pgid*.
1611
1612 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001613
Georg Brandl116aa622007-08-15 14:28:22 +00001614
1615.. function:: nice(increment)
1616
1617 Add *increment* to the process's "niceness". Return the new niceness.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001618
Georg Brandlc575c902008-09-13 17:46:05 +00001619 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001620
1621
1622.. function:: plock(op)
1623
1624 Lock program segments into memory. The value of *op* (defined in
Benjamin Petersond91203b2010-05-06 23:20:40 +00001625 ``<sys/lock.h>``) determines which segments are locked.
1626
1627 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001628
1629
1630.. function:: popen(...)
1631 :noindex:
1632
1633 Run child processes, returning opened pipes for communications. These functions
1634 are described in section :ref:`os-newstreams`.
1635
1636
1637.. function:: spawnl(mode, path, ...)
1638 spawnle(mode, path, ..., env)
1639 spawnlp(mode, file, ...)
1640 spawnlpe(mode, file, ..., env)
1641 spawnv(mode, path, args)
1642 spawnve(mode, path, args, env)
1643 spawnvp(mode, file, args)
1644 spawnvpe(mode, file, args, env)
1645
1646 Execute the program *path* in a new process.
1647
1648 (Note that the :mod:`subprocess` module provides more powerful facilities for
1649 spawning new processes and retrieving their results; using that module is
Benjamin Peterson87c8d872009-06-11 22:54:11 +00001650 preferable to using these functions. Check especially the
1651 :ref:`subprocess-replacements` section.)
Georg Brandl116aa622007-08-15 14:28:22 +00001652
Christian Heimesfaf2f632008-01-06 16:59:19 +00001653 If *mode* is :const:`P_NOWAIT`, this function returns the process id of the new
Georg Brandl116aa622007-08-15 14:28:22 +00001654 process; if *mode* is :const:`P_WAIT`, returns the process's exit code if it
1655 exits normally, or ``-signal``, where *signal* is the signal that killed the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001656 process. On Windows, the process id will actually be the process handle, so can
Georg Brandl116aa622007-08-15 14:28:22 +00001657 be used with the :func:`waitpid` function.
1658
Christian Heimesfaf2f632008-01-06 16:59:19 +00001659 The "l" and "v" variants of the :func:`spawn\*` functions differ in how
1660 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001661 to work with if the number of parameters is fixed when the code is written; the
1662 individual parameters simply become additional parameters to the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001663 :func:`spawnl\*` functions. The "v" variants are good when the number of
Georg Brandl116aa622007-08-15 14:28:22 +00001664 parameters is variable, with the arguments being passed in a list or tuple as
1665 the *args* parameter. In either case, the arguments to the child process must
1666 start with the name of the command being run.
1667
Christian Heimesfaf2f632008-01-06 16:59:19 +00001668 The variants which include a second "p" near the end (:func:`spawnlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001669 :func:`spawnlpe`, :func:`spawnvp`, and :func:`spawnvpe`) will use the
1670 :envvar:`PATH` environment variable to locate the program *file*. When the
1671 environment is being replaced (using one of the :func:`spawn\*e` variants,
1672 discussed in the next paragraph), the new environment is used as the source of
1673 the :envvar:`PATH` variable. The other variants, :func:`spawnl`,
1674 :func:`spawnle`, :func:`spawnv`, and :func:`spawnve`, will not use the
1675 :envvar:`PATH` variable to locate the executable; *path* must contain an
1676 appropriate absolute or relative path.
1677
1678 For :func:`spawnle`, :func:`spawnlpe`, :func:`spawnve`, and :func:`spawnvpe`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001679 (note that these all end in "e"), the *env* parameter must be a mapping
Christian Heimesa342c012008-04-20 21:01:16 +00001680 which is used to define the environment variables for the new process (they are
1681 used instead of the current process' environment); the functions
Georg Brandl116aa622007-08-15 14:28:22 +00001682 :func:`spawnl`, :func:`spawnlp`, :func:`spawnv`, and :func:`spawnvp` all cause
Benjamin Petersond23f8222009-04-05 19:13:16 +00001683 the new process to inherit the environment of the current process. Note that
1684 keys and values in the *env* dictionary must be strings; invalid keys or
1685 values will cause the function to fail, with a return value of ``127``.
Georg Brandl116aa622007-08-15 14:28:22 +00001686
1687 As an example, the following calls to :func:`spawnlp` and :func:`spawnvpe` are
1688 equivalent::
1689
1690 import os
1691 os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1692
1693 L = ['cp', 'index.html', '/dev/null']
1694 os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1695
1696 Availability: Unix, Windows. :func:`spawnlp`, :func:`spawnlpe`, :func:`spawnvp`
1697 and :func:`spawnvpe` are not available on Windows.
1698
Georg Brandl116aa622007-08-15 14:28:22 +00001699
1700.. data:: P_NOWAIT
1701 P_NOWAITO
1702
1703 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1704 functions. If either of these values is given, the :func:`spawn\*` functions
Christian Heimesfaf2f632008-01-06 16:59:19 +00001705 will return as soon as the new process has been created, with the process id as
Benjamin Petersond91203b2010-05-06 23:20:40 +00001706 the return value.
1707
1708 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001709
Georg Brandl116aa622007-08-15 14:28:22 +00001710
1711.. data:: P_WAIT
1712
1713 Possible value for the *mode* parameter to the :func:`spawn\*` family of
1714 functions. If this is given as *mode*, the :func:`spawn\*` functions will not
1715 return until the new process has run to completion and will return the exit code
1716 of the process the run is successful, or ``-signal`` if a signal kills the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001717 process.
1718
1719 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001720
Georg Brandl116aa622007-08-15 14:28:22 +00001721
1722.. data:: P_DETACH
1723 P_OVERLAY
1724
1725 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1726 functions. These are less portable than those listed above. :const:`P_DETACH`
1727 is similar to :const:`P_NOWAIT`, but the new process is detached from the
1728 console of the calling process. If :const:`P_OVERLAY` is used, the current
1729 process will be replaced; the :func:`spawn\*` function will not return.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001730
Georg Brandl116aa622007-08-15 14:28:22 +00001731 Availability: Windows.
1732
Georg Brandl116aa622007-08-15 14:28:22 +00001733
1734.. function:: startfile(path[, operation])
1735
1736 Start a file with its associated application.
1737
1738 When *operation* is not specified or ``'open'``, this acts like double-clicking
1739 the file in Windows Explorer, or giving the file name as an argument to the
1740 :program:`start` command from the interactive command shell: the file is opened
1741 with whatever application (if any) its extension is associated.
1742
1743 When another *operation* is given, it must be a "command verb" that specifies
1744 what should be done with the file. Common verbs documented by Microsoft are
1745 ``'print'`` and ``'edit'`` (to be used on files) as well as ``'explore'`` and
1746 ``'find'`` (to be used on directories).
1747
1748 :func:`startfile` returns as soon as the associated application is launched.
1749 There is no option to wait for the application to close, and no way to retrieve
1750 the application's exit status. The *path* parameter is relative to the current
1751 directory. If you want to use an absolute path, make sure the first character
1752 is not a slash (``'/'``); the underlying Win32 :cfunc:`ShellExecute` function
1753 doesn't work if it is. Use the :func:`os.path.normpath` function to ensure that
Benjamin Petersond91203b2010-05-06 23:20:40 +00001754 the path is properly encoded for Win32.
1755
1756 Availability: Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001757
Georg Brandl116aa622007-08-15 14:28:22 +00001758
1759.. function:: system(command)
1760
1761 Execute the command (a string) in a subshell. This is implemented by calling
Georg Brandl628e6f92009-10-27 20:24:45 +00001762 the Standard C function :cfunc:`system`, and has the same limitations.
Georg Brandlaba97962010-11-26 08:37:46 +00001763 Changes to :data:`sys.stdin`, etc. are not reflected in the environment of
1764 the executed command. If *command* generates any output, it will be sent to
1765 the interpreter standard output stream.
Georg Brandl116aa622007-08-15 14:28:22 +00001766
1767 On Unix, the return value is the exit status of the process encoded in the
Georg Brandlaba97962010-11-26 08:37:46 +00001768 format specified for :func:`wait`. Note that POSIX does not specify the
1769 meaning of the return value of the C :cfunc:`system` function, so the return
1770 value of the Python function is system-dependent.
Georg Brandl116aa622007-08-15 14:28:22 +00001771
Georg Brandlaba97962010-11-26 08:37:46 +00001772 On Windows, the return value is that returned by the system shell after
1773 running *command*. The shell is given by the Windows environment variable
1774 :envvar:`COMSPEC`: it is usually :program:`cmd.exe`, which returns the exit
1775 status of the command run; on systems using a non-native shell, consult your
1776 shell documentation.
Georg Brandl116aa622007-08-15 14:28:22 +00001777
Georg Brandlaba97962010-11-26 08:37:46 +00001778 The :mod:`subprocess` module provides more powerful facilities for spawning
1779 new processes and retrieving their results; using that module is preferable
1780 to using this function. See the :ref:`subprocess-replacements` section in
1781 the :mod:`subprocess` documentation for some helpful recipes.
Georg Brandl116aa622007-08-15 14:28:22 +00001782
Benjamin Petersond91203b2010-05-06 23:20:40 +00001783 Availability: Unix, Windows.
1784
Georg Brandl116aa622007-08-15 14:28:22 +00001785
1786.. function:: times()
1787
Benjamin Petersond91203b2010-05-06 23:20:40 +00001788 Return a 5-tuple of floating point numbers indicating accumulated (processor
1789 or other) times, in seconds. The items are: user time, system time,
1790 children's user time, children's system time, and elapsed real time since a
1791 fixed point in the past, in that order. See the Unix manual page
1792 :manpage:`times(2)` or the corresponding Windows Platform API documentation.
1793 On Windows, only the first two items are filled, the others are zero.
1794
1795 Availability: Unix, Windows
Georg Brandl116aa622007-08-15 14:28:22 +00001796
1797
1798.. function:: wait()
1799
1800 Wait for completion of a child process, and return a tuple containing its pid
1801 and exit status indication: a 16-bit number, whose low byte is the signal number
1802 that killed the process, and whose high byte is the exit status (if the signal
1803 number is zero); the high bit of the low byte is set if a core file was
Benjamin Petersond91203b2010-05-06 23:20:40 +00001804 produced.
1805
1806 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001807
1808
1809.. function:: waitpid(pid, options)
1810
1811 The details of this function differ on Unix and Windows.
1812
1813 On Unix: Wait for completion of a child process given by process id *pid*, and
1814 return a tuple containing its process id and exit status indication (encoded as
1815 for :func:`wait`). The semantics of the call are affected by the value of the
1816 integer *options*, which should be ``0`` for normal operation.
1817
1818 If *pid* is greater than ``0``, :func:`waitpid` requests status information for
1819 that specific process. If *pid* is ``0``, the request is for the status of any
1820 child in the process group of the current process. If *pid* is ``-1``, the
1821 request pertains to any child of the current process. If *pid* is less than
1822 ``-1``, status is requested for any process in the process group ``-pid`` (the
1823 absolute value of *pid*).
1824
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001825 An :exc:`OSError` is raised with the value of errno when the syscall
1826 returns -1.
1827
Georg Brandl116aa622007-08-15 14:28:22 +00001828 On Windows: Wait for completion of a process given by process handle *pid*, and
1829 return a tuple containing *pid*, and its exit status shifted left by 8 bits
1830 (shifting makes cross-platform use of the function easier). A *pid* less than or
1831 equal to ``0`` has no special meaning on Windows, and raises an exception. The
1832 value of integer *options* has no effect. *pid* can refer to any process whose
1833 id is known, not necessarily a child process. The :func:`spawn` functions called
1834 with :const:`P_NOWAIT` return suitable process handles.
1835
1836
1837.. function:: wait3([options])
1838
1839 Similar to :func:`waitpid`, except no process id argument is given and a
1840 3-element tuple containing the child's process id, exit status indication, and
1841 resource usage information is returned. Refer to :mod:`resource`.\
1842 :func:`getrusage` for details on resource usage information. The option
1843 argument is the same as that provided to :func:`waitpid` and :func:`wait4`.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001844
Georg Brandl116aa622007-08-15 14:28:22 +00001845 Availability: Unix.
1846
Georg Brandl116aa622007-08-15 14:28:22 +00001847
1848.. function:: wait4(pid, options)
1849
1850 Similar to :func:`waitpid`, except a 3-element tuple, containing the child's
1851 process id, exit status indication, and resource usage information is returned.
1852 Refer to :mod:`resource`.\ :func:`getrusage` for details on resource usage
1853 information. The arguments to :func:`wait4` are the same as those provided to
Benjamin Petersond91203b2010-05-06 23:20:40 +00001854 :func:`waitpid`.
1855
1856 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001857
Georg Brandl116aa622007-08-15 14:28:22 +00001858
1859.. data:: WNOHANG
1860
1861 The option for :func:`waitpid` to return immediately if no child process status
1862 is available immediately. The function returns ``(0, 0)`` in this case.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001863
Georg Brandlc575c902008-09-13 17:46:05 +00001864 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001865
1866
1867.. data:: WCONTINUED
1868
1869 This option causes child processes to be reported if they have been continued
Benjamin Petersond91203b2010-05-06 23:20:40 +00001870 from a job control stop since their status was last reported.
1871
1872 Availability: Some Unix systems.
Georg Brandl116aa622007-08-15 14:28:22 +00001873
Georg Brandl116aa622007-08-15 14:28:22 +00001874
1875.. data:: WUNTRACED
1876
1877 This option causes child processes to be reported if they have been stopped but
Benjamin Petersond91203b2010-05-06 23:20:40 +00001878 their current state has not been reported since they were stopped.
1879
1880 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001881
Georg Brandl116aa622007-08-15 14:28:22 +00001882
1883The following functions take a process status code as returned by
1884:func:`system`, :func:`wait`, or :func:`waitpid` as a parameter. They may be
1885used to determine the disposition of a process.
1886
Georg Brandl116aa622007-08-15 14:28:22 +00001887.. function:: WCOREDUMP(status)
1888
Christian Heimesfaf2f632008-01-06 16:59:19 +00001889 Return ``True`` if a core dump was generated for the process, otherwise
Benjamin Petersond91203b2010-05-06 23:20:40 +00001890 return ``False``.
1891
1892 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001893
Georg Brandl116aa622007-08-15 14:28:22 +00001894
1895.. function:: WIFCONTINUED(status)
1896
Christian Heimesfaf2f632008-01-06 16:59:19 +00001897 Return ``True`` if the process has been continued from a job control stop,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001898 otherwise return ``False``.
1899
1900 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001901
Georg Brandl116aa622007-08-15 14:28:22 +00001902
1903.. function:: WIFSTOPPED(status)
1904
Christian Heimesfaf2f632008-01-06 16:59:19 +00001905 Return ``True`` if the process has been stopped, otherwise return
Benjamin Petersond91203b2010-05-06 23:20:40 +00001906 ``False``.
1907
1908 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001909
1910
1911.. function:: WIFSIGNALED(status)
1912
Christian Heimesfaf2f632008-01-06 16:59:19 +00001913 Return ``True`` if the process exited due to a signal, otherwise return
Benjamin Petersond91203b2010-05-06 23:20:40 +00001914 ``False``.
1915
1916 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001917
1918
1919.. function:: WIFEXITED(status)
1920
Christian Heimesfaf2f632008-01-06 16:59:19 +00001921 Return ``True`` if the process exited using the :manpage:`exit(2)` system call,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001922 otherwise return ``False``.
1923
1924 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001925
1926
1927.. function:: WEXITSTATUS(status)
1928
1929 If ``WIFEXITED(status)`` is true, return the integer parameter to the
1930 :manpage:`exit(2)` system call. Otherwise, the return value is meaningless.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001931
Georg Brandlc575c902008-09-13 17:46:05 +00001932 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001933
1934
1935.. function:: WSTOPSIG(status)
1936
Benjamin Petersond91203b2010-05-06 23:20:40 +00001937 Return the signal which caused the process to stop.
1938
1939 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001940
1941
1942.. function:: WTERMSIG(status)
1943
Benjamin Petersond91203b2010-05-06 23:20:40 +00001944 Return the signal which caused the process to exit.
1945
1946 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001947
1948
1949.. _os-path:
1950
1951Miscellaneous System Information
1952--------------------------------
1953
1954
1955.. function:: confstr(name)
1956
1957 Return string-valued system configuration values. *name* specifies the
1958 configuration value to retrieve; it may be a string which is the name of a
1959 defined system value; these names are specified in a number of standards (POSIX,
1960 Unix 95, Unix 98, and others). Some platforms define additional names as well.
1961 The names known to the host operating system are given as the keys of the
1962 ``confstr_names`` dictionary. For configuration variables not included in that
Benjamin Petersond91203b2010-05-06 23:20:40 +00001963 mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +00001964
1965 If the configuration value specified by *name* isn't defined, ``None`` is
1966 returned.
1967
1968 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1969 specific value for *name* is not supported by the host system, even if it is
1970 included in ``confstr_names``, an :exc:`OSError` is raised with
1971 :const:`errno.EINVAL` for the error number.
1972
Benjamin Petersond91203b2010-05-06 23:20:40 +00001973 Availability: Unix
1974
Georg Brandl116aa622007-08-15 14:28:22 +00001975
1976.. data:: confstr_names
1977
1978 Dictionary mapping names accepted by :func:`confstr` to the integer values
1979 defined for those names by the host operating system. This can be used to
Benjamin Petersond91203b2010-05-06 23:20:40 +00001980 determine the set of names known to the system.
1981
1982 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001983
1984
1985.. function:: getloadavg()
1986
Christian Heimesa62da1d2008-01-12 19:39:10 +00001987 Return the number of processes in the system run queue averaged over the last
1988 1, 5, and 15 minutes or raises :exc:`OSError` if the load average was
Benjamin Petersond91203b2010-05-06 23:20:40 +00001989 unobtainable.
1990
1991 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001992
Georg Brandl116aa622007-08-15 14:28:22 +00001993
1994.. function:: sysconf(name)
1995
1996 Return integer-valued system configuration values. If the configuration value
1997 specified by *name* isn't defined, ``-1`` is returned. The comments regarding
1998 the *name* parameter for :func:`confstr` apply here as well; the dictionary that
1999 provides information on the known names is given by ``sysconf_names``.
Benjamin Petersond91203b2010-05-06 23:20:40 +00002000
Georg Brandlc575c902008-09-13 17:46:05 +00002001 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002002
2003
2004.. data:: sysconf_names
2005
2006 Dictionary mapping names accepted by :func:`sysconf` to the integer values
2007 defined for those names by the host operating system. This can be used to
Benjamin Petersond91203b2010-05-06 23:20:40 +00002008 determine the set of names known to the system.
2009
2010 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002011
Christian Heimesfaf2f632008-01-06 16:59:19 +00002012The following data values are used to support path manipulation operations. These
Georg Brandl116aa622007-08-15 14:28:22 +00002013are defined for all platforms.
2014
2015Higher-level operations on pathnames are defined in the :mod:`os.path` module.
2016
2017
2018.. data:: curdir
2019
2020 The constant string used by the operating system to refer to the current
Georg Brandlc575c902008-09-13 17:46:05 +00002021 directory. This is ``'.'`` for Windows and POSIX. Also available via
2022 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002023
2024
2025.. data:: pardir
2026
2027 The constant string used by the operating system to refer to the parent
Georg Brandlc575c902008-09-13 17:46:05 +00002028 directory. This is ``'..'`` for Windows and POSIX. Also available via
2029 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002030
2031
2032.. data:: sep
2033
Georg Brandlc575c902008-09-13 17:46:05 +00002034 The character used by the operating system to separate pathname components.
2035 This is ``'/'`` for POSIX and ``'\\'`` for Windows. Note that knowing this
2036 is not sufficient to be able to parse or concatenate pathnames --- use
Georg Brandl116aa622007-08-15 14:28:22 +00002037 :func:`os.path.split` and :func:`os.path.join` --- but it is occasionally
2038 useful. Also available via :mod:`os.path`.
2039
2040
2041.. data:: altsep
2042
2043 An alternative character used by the operating system to separate pathname
2044 components, or ``None`` if only one separator character exists. This is set to
2045 ``'/'`` on Windows systems where ``sep`` is a backslash. Also available via
2046 :mod:`os.path`.
2047
2048
2049.. data:: extsep
2050
2051 The character which separates the base filename from the extension; for example,
2052 the ``'.'`` in :file:`os.py`. Also available via :mod:`os.path`.
2053
Georg Brandl116aa622007-08-15 14:28:22 +00002054
2055.. data:: pathsep
2056
2057 The character conventionally used by the operating system to separate search
2058 path components (as in :envvar:`PATH`), such as ``':'`` for POSIX or ``';'`` for
2059 Windows. Also available via :mod:`os.path`.
2060
2061
2062.. data:: defpath
2063
2064 The default search path used by :func:`exec\*p\*` and :func:`spawn\*p\*` if the
2065 environment doesn't have a ``'PATH'`` key. Also available via :mod:`os.path`.
2066
2067
2068.. data:: linesep
2069
2070 The string used to separate (or, rather, terminate) lines on the current
Georg Brandlc575c902008-09-13 17:46:05 +00002071 platform. This may be a single character, such as ``'\n'`` for POSIX, or
2072 multiple characters, for example, ``'\r\n'`` for Windows. Do not use
2073 *os.linesep* as a line terminator when writing files opened in text mode (the
2074 default); use a single ``'\n'`` instead, on all platforms.
Georg Brandl116aa622007-08-15 14:28:22 +00002075
2076
2077.. data:: devnull
2078
Georg Brandlc52eeab2010-05-21 22:05:15 +00002079 The file path of the null device. For example: ``'/dev/null'`` for
2080 POSIX, ``'nul'`` for Windows. Also available via :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002081
Georg Brandl116aa622007-08-15 14:28:22 +00002082
2083.. _os-miscfunc:
2084
2085Miscellaneous Functions
2086-----------------------
2087
2088
2089.. function:: urandom(n)
2090
2091 Return a string of *n* random bytes suitable for cryptographic use.
2092
2093 This function returns random bytes from an OS-specific randomness source. The
2094 returned data should be unpredictable enough for cryptographic applications,
2095 though its exact quality depends on the OS implementation. On a UNIX-like
2096 system this will query /dev/urandom, and on Windows it will use CryptGenRandom.
2097 If a randomness source is not found, :exc:`NotImplementedError` will be raised.