blob: 2fb8cd64a20331362e4a2c36a558302fcaeabe9a [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`os` --- Miscellaneous operating system interfaces
2=======================================================
3
4.. module:: os
5 :synopsis: Miscellaneous operating system interfaces.
6
7
Christian Heimesa62da1d2008-01-12 19:39:10 +00008This module provides a portable way of using operating system dependent
9functionality. If you just want to read or write a file see :func:`open`, if
10you want to manipulate paths, see the :mod:`os.path` module, and if you want to
11read all the lines in all the files on the command line see the :mod:`fileinput`
12module. For creating temporary files and directories see the :mod:`tempfile`
13module, and for high-level file and directory handling see the :mod:`shutil`
14module.
Georg Brandl116aa622007-08-15 14:28:22 +000015
Benjamin Peterson68dbebc2009-12-31 03:30:26 +000016Notes on the availability of these functions:
Georg Brandl116aa622007-08-15 14:28:22 +000017
Benjamin Peterson68dbebc2009-12-31 03:30:26 +000018* The design of all built-in operating system dependent modules of Python is
19 such that as long as the same functionality is available, it uses the same
20 interface; for example, the function ``os.stat(path)`` returns stat
21 information about *path* in the same format (which happens to have originated
22 with the POSIX interface).
Georg Brandl116aa622007-08-15 14:28:22 +000023
Benjamin Peterson68dbebc2009-12-31 03:30:26 +000024* Extensions peculiar to a particular operating system are also available
25 through the :mod:`os` module, but using them is of course a threat to
26 portability.
Georg Brandl116aa622007-08-15 14:28:22 +000027
Benjamin Peterson68dbebc2009-12-31 03:30:26 +000028* All functions accepting path or file names accept both bytes and string
29 objects, and result in an object of the same type, if a path or file name is
30 returned.
Georg Brandl76e55382008-10-08 16:34:57 +000031
32.. note::
33
Georg Brandlc575c902008-09-13 17:46:05 +000034 If not separately noted, all functions that claim "Availability: Unix" are
35 supported on Mac OS X, which builds on a Unix core.
36
Benjamin Peterson68dbebc2009-12-31 03:30:26 +000037* An "Availability: Unix" note means that this function is commonly found on
38 Unix systems. It does not make any claims about its existence on a specific
39 operating system.
40
41* If not separately noted, all functions that claim "Availability: Unix" are
42 supported on Mac OS X, which builds on a Unix core.
43
Benjamin Petersond91203b2010-05-06 23:20:40 +000044.. Availability notes get their own line and occur at the end of the function
45.. documentation.
46
Georg Brandlc575c902008-09-13 17:46:05 +000047.. note::
48
Christian Heimesa62da1d2008-01-12 19:39:10 +000049 All functions in this module raise :exc:`OSError` in the case of invalid or
50 inaccessible file names and paths, or other arguments that have the correct
51 type, but are not accepted by the operating system.
Georg Brandl116aa622007-08-15 14:28:22 +000052
Georg Brandl116aa622007-08-15 14:28:22 +000053.. exception:: error
54
Christian Heimesa62da1d2008-01-12 19:39:10 +000055 An alias for the built-in :exc:`OSError` exception.
Georg Brandl116aa622007-08-15 14:28:22 +000056
57
58.. data:: name
59
Benjamin Peterson68dbebc2009-12-31 03:30:26 +000060 The name of the operating system dependent module imported. The following
61 names have currently been registered: ``'posix'``, ``'nt'``, ``'mac'``,
62 ``'os2'``, ``'ce'``, ``'java'``.
Georg Brandl116aa622007-08-15 14:28:22 +000063
64
Martin v. Löwis011e8422009-05-05 04:43:17 +000065.. _os-filenames:
66
67File Names, Command Line Arguments, and Environment Variables
68-------------------------------------------------------------
69
70In Python, file names, command line arguments, and environment
71variables are represented using the string type. On some systems,
72decoding these strings to and from bytes is necessary before passing
73them to the operating system. Python uses the file system encoding to
74perform this conversion (see :func:`sys.getfilesystemencoding`).
75
76.. versionchanged:: 3.1
77 On some systems, conversion using the file system encoding may
Martin v. Löwis43c57782009-05-10 08:15:24 +000078 fail. In this case, Python uses the ``surrogateescape`` encoding
79 error handler, which means that undecodable bytes are replaced by a
Martin v. Löwis011e8422009-05-05 04:43:17 +000080 Unicode character U+DCxx on decoding, and these are again
81 translated to the original byte on encoding.
82
83
84The file system encoding must guarantee to successfully decode all
85bytes below 128. If the file system encoding fails to provide this
86guarantee, API functions may raise UnicodeErrors.
87
88
Georg Brandl116aa622007-08-15 14:28:22 +000089.. _os-procinfo:
90
91Process Parameters
92------------------
93
94These functions and data items provide information and operate on the current
95process and user.
96
97
98.. data:: environ
99
100 A mapping object representing the string environment. For example,
101 ``environ['HOME']`` is the pathname of your home directory (on some platforms),
102 and is equivalent to ``getenv("HOME")`` in C.
103
104 This mapping is captured the first time the :mod:`os` module is imported,
105 typically during Python startup as part of processing :file:`site.py`. Changes
106 to the environment made after this time are not reflected in ``os.environ``,
107 except for changes made by modifying ``os.environ`` directly.
108
109 If the platform supports the :func:`putenv` function, this mapping may be used
110 to modify the environment as well as query the environment. :func:`putenv` will
111 be called automatically when the mapping is modified.
112
113 .. note::
114
115 Calling :func:`putenv` directly does not change ``os.environ``, so it's better
116 to modify ``os.environ``.
117
118 .. note::
119
Georg Brandlc575c902008-09-13 17:46:05 +0000120 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
121 cause memory leaks. Refer to the system documentation for
122 :cfunc:`putenv`.
Georg Brandl116aa622007-08-15 14:28:22 +0000123
124 If :func:`putenv` is not provided, a modified copy of this mapping may be
125 passed to the appropriate process-creation functions to cause child processes
126 to use a modified environment.
127
Georg Brandl9afde1c2007-11-01 20:32:30 +0000128 If the platform supports the :func:`unsetenv` function, you can delete items in
Georg Brandl116aa622007-08-15 14:28:22 +0000129 this mapping to unset environment variables. :func:`unsetenv` will be called
Georg Brandl9afde1c2007-11-01 20:32:30 +0000130 automatically when an item is deleted from ``os.environ``, and when
131 one of the :meth:`pop` or :meth:`clear` methods is called.
132
Georg Brandl116aa622007-08-15 14:28:22 +0000133
134.. function:: chdir(path)
135 fchdir(fd)
136 getcwd()
137 :noindex:
138
139 These functions are described in :ref:`os-file-dir`.
140
141
142.. function:: ctermid()
143
144 Return the filename corresponding to the controlling terminal of the process.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000145
Georg Brandl116aa622007-08-15 14:28:22 +0000146 Availability: Unix.
147
148
149.. function:: getegid()
150
151 Return the effective group id of the current process. This corresponds to the
Benjamin Petersond91203b2010-05-06 23:20:40 +0000152 "set id" bit on the file being executed in the current process.
153
154 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000155
156
157.. function:: geteuid()
158
159 .. index:: single: user; effective id
160
Benjamin Petersond91203b2010-05-06 23:20:40 +0000161 Return the current process's effective user id.
162
163 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000164
165
166.. function:: getgid()
167
168 .. index:: single: process; group
169
Benjamin Petersond91203b2010-05-06 23:20:40 +0000170 Return the real group id of the current process.
171
172 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000173
174
175.. function:: getgroups()
176
177 Return list of supplemental group ids associated with the current process.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000178
Georg Brandl116aa622007-08-15 14:28:22 +0000179 Availability: Unix.
180
181
182.. function:: getlogin()
183
184 Return the name of the user logged in on the controlling terminal of the
185 process. For most purposes, it is more useful to use the environment variable
186 :envvar:`LOGNAME` to find out who the user is, or
187 ``pwd.getpwuid(os.getuid())[0]`` to get the login name of the currently
Benjamin Petersond91203b2010-05-06 23:20:40 +0000188 effective user id.
189
190 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000191
192
193.. function:: getpgid(pid)
194
195 Return the process group id of the process with process id *pid*. If *pid* is 0,
Benjamin Petersond91203b2010-05-06 23:20:40 +0000196 the process group id of the current process is returned.
Georg Brandl116aa622007-08-15 14:28:22 +0000197
Benjamin Petersond91203b2010-05-06 23:20:40 +0000198 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000199
200.. function:: getpgrp()
201
202 .. index:: single: process; group
203
Benjamin Petersond91203b2010-05-06 23:20:40 +0000204 Return the id of the current process group.
205
206 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000207
208
209.. function:: getpid()
210
211 .. index:: single: process; id
212
Benjamin Petersond91203b2010-05-06 23:20:40 +0000213 Return the current process id.
214
215 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000216
217
218.. function:: getppid()
219
220 .. index:: single: process; id of parent
221
Benjamin Petersond91203b2010-05-06 23:20:40 +0000222 Return the parent's process id.
223
224 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000225
226
227.. function:: getuid()
228
229 .. index:: single: user; id
230
Benjamin Petersond91203b2010-05-06 23:20:40 +0000231 Return the current process's user id.
232
233 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000234
235
236.. function:: getenv(varname[, value])
237
238 Return the value of the environment variable *varname* if it exists, or *value*
Benjamin Petersond91203b2010-05-06 23:20:40 +0000239 if it doesn't. *value* defaults to ``None``.
240
241 Availability: most flavors of Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000242
243
244.. function:: putenv(varname, value)
245
246 .. index:: single: environment variables; setting
247
248 Set the environment variable named *varname* to the string *value*. Such
249 changes to the environment affect subprocesses started with :func:`os.system`,
Benjamin Petersond91203b2010-05-06 23:20:40 +0000250 :func:`popen` or :func:`fork` and :func:`execv`.
251
252 Availability: most flavors of Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000253
254 .. note::
255
Georg Brandlc575c902008-09-13 17:46:05 +0000256 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
257 cause memory leaks. Refer to the system documentation for putenv.
Georg Brandl116aa622007-08-15 14:28:22 +0000258
259 When :func:`putenv` is supported, assignments to items in ``os.environ`` are
260 automatically translated into corresponding calls to :func:`putenv`; however,
261 calls to :func:`putenv` don't update ``os.environ``, so it is actually
262 preferable to assign to items of ``os.environ``.
263
264
265.. function:: setegid(egid)
266
Benjamin Petersond91203b2010-05-06 23:20:40 +0000267 Set the current process's effective group id.
268
269 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000270
271
272.. function:: seteuid(euid)
273
Benjamin Petersond91203b2010-05-06 23:20:40 +0000274 Set the current process's effective user id.
275
276 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000277
278
279.. function:: setgid(gid)
280
Benjamin Petersond91203b2010-05-06 23:20:40 +0000281 Set the current process' group id.
282
283 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000284
285
286.. function:: setgroups(groups)
287
288 Set the list of supplemental group ids associated with the current process to
289 *groups*. *groups* must be a sequence, and each element must be an integer
Christian Heimesfaf2f632008-01-06 16:59:19 +0000290 identifying a group. This operation is typically available only to the superuser.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000291
Georg Brandl116aa622007-08-15 14:28:22 +0000292 Availability: Unix.
293
Georg Brandl116aa622007-08-15 14:28:22 +0000294
295.. function:: setpgrp()
296
Christian Heimesfaf2f632008-01-06 16:59:19 +0000297 Call the system call :cfunc:`setpgrp` or :cfunc:`setpgrp(0, 0)` depending on
Georg Brandl116aa622007-08-15 14:28:22 +0000298 which version is implemented (if any). See the Unix manual for the semantics.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000299
Georg Brandl116aa622007-08-15 14:28:22 +0000300 Availability: Unix.
301
302
303.. function:: setpgid(pid, pgrp)
304
Christian Heimesfaf2f632008-01-06 16:59:19 +0000305 Call the system call :cfunc:`setpgid` to set the process group id of the
Georg Brandl116aa622007-08-15 14:28:22 +0000306 process with id *pid* to the process group with id *pgrp*. See the Unix manual
Benjamin Petersond91203b2010-05-06 23:20:40 +0000307 for the semantics.
308
309 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000310
311
312.. function:: setreuid(ruid, euid)
313
Benjamin Petersond91203b2010-05-06 23:20:40 +0000314
315 Set the current process's real and effective user ids.
316
317 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000318
319
320.. function:: setregid(rgid, egid)
321
Benjamin Petersond91203b2010-05-06 23:20:40 +0000322 Set the current process's real and effective group ids.
323
324 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000325
326
327.. function:: getsid(pid)
328
Christian Heimesfaf2f632008-01-06 16:59:19 +0000329 Call the system call :cfunc:`getsid`. See the Unix manual for the semantics.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000330
Georg Brandl116aa622007-08-15 14:28:22 +0000331 Availability: Unix.
332
Georg Brandl116aa622007-08-15 14:28:22 +0000333
334.. function:: setsid()
335
Christian Heimesfaf2f632008-01-06 16:59:19 +0000336 Call the system call :cfunc:`setsid`. See the Unix manual for the semantics.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000337
Georg Brandl116aa622007-08-15 14:28:22 +0000338 Availability: Unix.
339
340
341.. function:: setuid(uid)
342
343 .. index:: single: user; id, setting
344
Benjamin Petersond91203b2010-05-06 23:20:40 +0000345 Set the current process's user id.
346
347 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000348
Georg Brandl116aa622007-08-15 14:28:22 +0000349
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000350.. placed in this section since it relates to errno.... a little weak
Georg Brandl116aa622007-08-15 14:28:22 +0000351.. function:: strerror(code)
352
353 Return the error message corresponding to the error code in *code*.
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000354 On platforms where :cfunc:`strerror` returns ``NULL`` when given an unknown
Benjamin Petersond91203b2010-05-06 23:20:40 +0000355 error number, :exc:`ValueError` is raised.
356
357 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000358
359
360.. function:: umask(mask)
361
Benjamin Petersond91203b2010-05-06 23:20:40 +0000362 Set the current numeric umask and return the previous umask.
363
364 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000365
366
367.. function:: uname()
368
369 .. index::
370 single: gethostname() (in module socket)
371 single: gethostbyaddr() (in module socket)
372
373 Return a 5-tuple containing information identifying the current operating
374 system. The tuple contains 5 strings: ``(sysname, nodename, release, version,
375 machine)``. Some systems truncate the nodename to 8 characters or to the
376 leading component; a better way to get the hostname is
377 :func:`socket.gethostname` or even
Benjamin Petersond91203b2010-05-06 23:20:40 +0000378 ``socket.gethostbyaddr(socket.gethostname())``.
379
380 Availability: recent flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000381
382
383.. function:: unsetenv(varname)
384
385 .. index:: single: environment variables; deleting
386
387 Unset (delete) the environment variable named *varname*. Such changes to the
388 environment affect subprocesses started with :func:`os.system`, :func:`popen` or
Benjamin Petersond91203b2010-05-06 23:20:40 +0000389 :func:`fork` and :func:`execv`.
Georg Brandl116aa622007-08-15 14:28:22 +0000390
391 When :func:`unsetenv` is supported, deletion of items in ``os.environ`` is
392 automatically translated into a corresponding call to :func:`unsetenv`; however,
393 calls to :func:`unsetenv` don't update ``os.environ``, so it is actually
394 preferable to delete items of ``os.environ``.
395
Benjamin Petersond91203b2010-05-06 23:20:40 +0000396 Availability: most flavors of Unix, Windows.
397
Georg Brandl116aa622007-08-15 14:28:22 +0000398
399.. _os-newstreams:
400
401File Object Creation
402--------------------
403
Antoine Pitrou25d535e2010-09-15 11:25:11 +0000404These functions create new :term:`file objects <file object>`. (See also :func:`open`.)
Georg Brandl116aa622007-08-15 14:28:22 +0000405
406
407.. function:: fdopen(fd[, mode[, bufsize]])
408
409 .. index:: single: I/O control; buffering
410
411 Return an open file object connected to the file descriptor *fd*. The *mode*
412 and *bufsize* arguments have the same meaning as the corresponding arguments to
Benjamin Petersond91203b2010-05-06 23:20:40 +0000413 the built-in :func:`open` function.
Georg Brandl116aa622007-08-15 14:28:22 +0000414
Georg Brandl55ac8f02007-09-01 13:51:09 +0000415 When specified, the *mode* argument must start with one of the letters
416 ``'r'``, ``'w'``, or ``'a'``, otherwise a :exc:`ValueError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000417
Georg Brandl55ac8f02007-09-01 13:51:09 +0000418 On Unix, when the *mode* argument starts with ``'a'``, the *O_APPEND* flag is
419 set on the file descriptor (which the :cfunc:`fdopen` implementation already
420 does on most platforms).
Georg Brandl116aa622007-08-15 14:28:22 +0000421
Benjamin Petersond91203b2010-05-06 23:20:40 +0000422 Availability: Unix, Windows.
423
Georg Brandl116aa622007-08-15 14:28:22 +0000424
Georg Brandl116aa622007-08-15 14:28:22 +0000425.. _os-fd-ops:
426
427File Descriptor Operations
428--------------------------
429
430These functions operate on I/O streams referenced using file descriptors.
431
432File descriptors are small integers corresponding to a file that has been opened
433by the current process. For example, standard input is usually file descriptor
4340, standard output is 1, and standard error is 2. Further files opened by a
435process will then be assigned 3, 4, 5, and so forth. The name "file descriptor"
436is slightly deceptive; on Unix platforms, sockets and pipes are also referenced
437by file descriptors.
438
Antoine Pitrou25d535e2010-09-15 11:25:11 +0000439The :meth:`~file.fileno` method can be used to obtain the file descriptor
440associated with a :term:`file object` when required. Note that using the file
441descriptor directly will bypass the file object methods, ignoring aspects such
442as internal buffering of data.
Georg Brandl116aa622007-08-15 14:28:22 +0000443
444.. function:: close(fd)
445
Benjamin Petersond91203b2010-05-06 23:20:40 +0000446 Close file descriptor *fd*.
447
448 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000449
450 .. note::
451
452 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000453 descriptor as returned by :func:`os.open` or :func:`pipe`. To close a "file
Georg Brandl116aa622007-08-15 14:28:22 +0000454 object" returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000455 :func:`fdopen`, use its :meth:`~file.close` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000456
457
Christian Heimesfdab48e2008-01-20 09:06:41 +0000458.. function:: closerange(fd_low, fd_high)
459
460 Close all file descriptors from *fd_low* (inclusive) to *fd_high* (exclusive),
Benjamin Petersond91203b2010-05-06 23:20:40 +0000461 ignoring errors. Equivalent to::
Christian Heimesfdab48e2008-01-20 09:06:41 +0000462
Georg Brandl7baf6252009-09-01 08:13:16 +0000463 for fd in range(fd_low, fd_high):
Christian Heimesfdab48e2008-01-20 09:06:41 +0000464 try:
465 os.close(fd)
466 except OSError:
467 pass
468
Benjamin Petersond91203b2010-05-06 23:20:40 +0000469 Availability: Unix, Windows.
470
Christian Heimesfdab48e2008-01-20 09:06:41 +0000471
Georg Brandl81f11302007-12-21 08:45:42 +0000472.. function:: device_encoding(fd)
473
474 Return a string describing the encoding of the device associated with *fd*
475 if it is connected to a terminal; else return :const:`None`.
476
477
Georg Brandl116aa622007-08-15 14:28:22 +0000478.. function:: dup(fd)
479
Benjamin Petersond91203b2010-05-06 23:20:40 +0000480 Return a duplicate of file descriptor *fd*.
481
482 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000483
484
485.. function:: dup2(fd, fd2)
486
487 Duplicate file descriptor *fd* to *fd2*, closing the latter first if necessary.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000488
Georg Brandlc575c902008-09-13 17:46:05 +0000489 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000490
491
Christian Heimes4e30a842007-11-30 22:12:06 +0000492.. function:: fchmod(fd, mode)
493
494 Change the mode of the file given by *fd* to the numeric *mode*. See the docs
Benjamin Petersond91203b2010-05-06 23:20:40 +0000495 for :func:`chmod` for possible values of *mode*.
496
497 Availability: Unix.
Christian Heimes4e30a842007-11-30 22:12:06 +0000498
499
500.. function:: fchown(fd, uid, gid)
501
502 Change the owner and group id of the file given by *fd* to the numeric *uid*
503 and *gid*. To leave one of the ids unchanged, set it to -1.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000504
Christian Heimes4e30a842007-11-30 22:12:06 +0000505 Availability: Unix.
506
507
Georg Brandl116aa622007-08-15 14:28:22 +0000508.. function:: fdatasync(fd)
509
510 Force write of file with filedescriptor *fd* to disk. Does not force update of
Benjamin Petersond91203b2010-05-06 23:20:40 +0000511 metadata.
512
513 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000514
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000515 .. note::
516 This function is not available on MacOS.
517
Georg Brandl116aa622007-08-15 14:28:22 +0000518
519.. function:: fpathconf(fd, name)
520
521 Return system configuration information relevant to an open file. *name*
522 specifies the configuration value to retrieve; it may be a string which is the
523 name of a defined system value; these names are specified in a number of
524 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
525 additional names as well. The names known to the host operating system are
526 given in the ``pathconf_names`` dictionary. For configuration variables not
527 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +0000528
529 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
530 specific value for *name* is not supported by the host system, even if it is
531 included in ``pathconf_names``, an :exc:`OSError` is raised with
532 :const:`errno.EINVAL` for the error number.
533
Benjamin Petersond91203b2010-05-06 23:20:40 +0000534 Availability: Unix.
535
Georg Brandl116aa622007-08-15 14:28:22 +0000536
537.. function:: fstat(fd)
538
Benjamin Petersond91203b2010-05-06 23:20:40 +0000539 Return status for file descriptor *fd*, like :func:`stat`.
540
541 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000542
543
544.. function:: fstatvfs(fd)
545
546 Return information about the filesystem containing the file associated with file
Benjamin Petersond91203b2010-05-06 23:20:40 +0000547 descriptor *fd*, like :func:`statvfs`.
548
549 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000550
551
552.. function:: fsync(fd)
553
554 Force write of file with filedescriptor *fd* to disk. On Unix, this calls the
555 native :cfunc:`fsync` function; on Windows, the MS :cfunc:`_commit` function.
556
Antoine Pitrou25d535e2010-09-15 11:25:11 +0000557 If you're starting with a buffered Python :term:`file object` *f*, first do
558 ``f.flush()``, and then do ``os.fsync(f.fileno())``, to ensure that all internal
559 buffers associated with *f* are written to disk.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000560
561 Availability: Unix, and Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000562
563
564.. function:: ftruncate(fd, length)
565
566 Truncate the file corresponding to file descriptor *fd*, so that it is at most
Benjamin Petersond91203b2010-05-06 23:20:40 +0000567 *length* bytes in size.
568
569 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000570
571
572.. function:: isatty(fd)
573
574 Return ``True`` if the file descriptor *fd* is open and connected to a
Benjamin Petersond91203b2010-05-06 23:20:40 +0000575 tty(-like) device, else ``False``.
576
577 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000578
579
580.. function:: lseek(fd, pos, how)
581
Christian Heimesfaf2f632008-01-06 16:59:19 +0000582 Set the current position of file descriptor *fd* to position *pos*, modified
583 by *how*: :const:`SEEK_SET` or ``0`` to set the position relative to the
584 beginning of the file; :const:`SEEK_CUR` or ``1`` to set it relative to the
585 current position; :const:`os.SEEK_END` or ``2`` to set it relative to the end of
Benjamin Petersond91203b2010-05-06 23:20:40 +0000586 the file.
587
588 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000589
590
Georg Brandl6c8583f2010-05-19 21:22:58 +0000591.. data:: SEEK_SET
592 SEEK_CUR
593 SEEK_END
594
595 Parameters to the :func:`lseek` function. Their values are 0, 1, and 2,
596 respectively. Availability: Windows, Unix.
597
598
Georg Brandl116aa622007-08-15 14:28:22 +0000599.. function:: open(file, flags[, mode])
600
Georg Brandlf4a41232008-05-26 17:55:52 +0000601 Open the file *file* and set various flags according to *flags* and possibly
602 its mode according to *mode*. The default *mode* is ``0o777`` (octal), and
603 the current umask value is first masked out. Return the file descriptor for
Benjamin Petersond91203b2010-05-06 23:20:40 +0000604 the newly opened file.
Georg Brandl116aa622007-08-15 14:28:22 +0000605
606 For a description of the flag and mode values, see the C run-time documentation;
607 flag constants (like :const:`O_RDONLY` and :const:`O_WRONLY`) are defined in
Georg Brandl6c8583f2010-05-19 21:22:58 +0000608 this module too (see :ref:`open-constants`). In particular, on Windows adding
609 :const:`O_BINARY` is needed to open files in binary mode.
Georg Brandl116aa622007-08-15 14:28:22 +0000610
Benjamin Petersond91203b2010-05-06 23:20:40 +0000611 Availability: Unix, Windows.
612
Georg Brandl116aa622007-08-15 14:28:22 +0000613 .. note::
614
Georg Brandlc5605df2009-08-13 08:26:44 +0000615 This function is intended for low-level I/O. For normal usage, use the
Antoine Pitrou25d535e2010-09-15 11:25:11 +0000616 built-in function :func:`open`, which returns a :term:`file object` with
Benjamin Petersond91203b2010-05-06 23:20:40 +0000617 :meth:`~file.read` and :meth:`~file.wprite` methods (and many more). To
Antoine Pitrou25d535e2010-09-15 11:25:11 +0000618 wrap a file descriptor in a file object, use :func:`fdopen`.
Georg Brandl116aa622007-08-15 14:28:22 +0000619
620
621.. function:: openpty()
622
623 .. index:: module: pty
624
625 Open a new pseudo-terminal pair. Return a pair of file descriptors ``(master,
626 slave)`` for the pty and the tty, respectively. For a (slightly) more portable
Benjamin Petersond91203b2010-05-06 23:20:40 +0000627 approach, use the :mod:`pty` module.
628
629 Availability: some flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000630
631
632.. function:: pipe()
633
634 Create a pipe. Return a pair of file descriptors ``(r, w)`` usable for reading
Benjamin Petersond91203b2010-05-06 23:20:40 +0000635 and writing, respectively.
636
637 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000638
639
640.. function:: read(fd, n)
641
Georg Brandlc5605df2009-08-13 08:26:44 +0000642 Read at most *n* bytes from file descriptor *fd*. Return a bytestring containing the
Georg Brandl116aa622007-08-15 14:28:22 +0000643 bytes read. If the end of the file referred to by *fd* has been reached, an
Benjamin Petersond91203b2010-05-06 23:20:40 +0000644 empty bytes object is returned.
645
646 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000647
648 .. note::
649
650 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000651 descriptor as returned by :func:`os.open` or :func:`pipe`. To read a "file object"
Georg Brandl116aa622007-08-15 14:28:22 +0000652 returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000653 :func:`fdopen`, or :data:`sys.stdin`, use its :meth:`~file.read` or
654 :meth:`~file.readline` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000655
656
657.. function:: tcgetpgrp(fd)
658
659 Return the process group associated with the terminal given by *fd* (an open
Benjamin Petersond91203b2010-05-06 23:20:40 +0000660 file descriptor as returned by :func:`os.open`).
661
662 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000663
664
665.. function:: tcsetpgrp(fd, pg)
666
667 Set the process group associated with the terminal given by *fd* (an open file
Benjamin Petersond91203b2010-05-06 23:20:40 +0000668 descriptor as returned by :func:`os.open`) to *pg*.
669
670 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000671
672
673.. function:: ttyname(fd)
674
675 Return a string which specifies the terminal device associated with
Georg Brandl9afde1c2007-11-01 20:32:30 +0000676 file descriptor *fd*. If *fd* is not associated with a terminal device, an
Benjamin Petersond91203b2010-05-06 23:20:40 +0000677 exception is raised.
678
679 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000680
681
682.. function:: write(fd, str)
683
Georg Brandlc5605df2009-08-13 08:26:44 +0000684 Write the bytestring in *str* to file descriptor *fd*. Return the number of
Benjamin Petersond91203b2010-05-06 23:20:40 +0000685 bytes actually written.
686
687 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000688
689 .. note::
690
691 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000692 descriptor as returned by :func:`os.open` or :func:`pipe`. To write a "file
Georg Brandl116aa622007-08-15 14:28:22 +0000693 object" returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000694 :func:`fdopen`, or :data:`sys.stdout` or :data:`sys.stderr`, use its
695 :meth:`~file.write` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000696
Georg Brandl6c8583f2010-05-19 21:22:58 +0000697
698.. _open-constants:
699
700``open()`` flag constants
701~~~~~~~~~~~~~~~~~~~~~~~~~
702
Georg Brandlaf265f42008-12-07 15:06:20 +0000703The following constants are options for the *flags* parameter to the
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000704:func:`~os.open` function. They can be combined using the bitwise OR operator
Georg Brandlaf265f42008-12-07 15:06:20 +0000705``|``. Some of them are not available on all platforms. For descriptions of
706their availability and use, consult the :manpage:`open(2)` manual page on Unix
Doug Hellmann14214262009-09-21 12:16:43 +0000707or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000708
709
710.. data:: O_RDONLY
711 O_WRONLY
712 O_RDWR
713 O_APPEND
714 O_CREAT
715 O_EXCL
716 O_TRUNC
717
Georg Brandlaf265f42008-12-07 15:06:20 +0000718 These constants are available on Unix and Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000719
720
721.. data:: O_DSYNC
722 O_RSYNC
723 O_SYNC
724 O_NDELAY
725 O_NONBLOCK
726 O_NOCTTY
727 O_SHLOCK
728 O_EXLOCK
729
Georg Brandlaf265f42008-12-07 15:06:20 +0000730 These constants are only available on Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000731
732
733.. data:: O_BINARY
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000734 O_NOINHERIT
Georg Brandl116aa622007-08-15 14:28:22 +0000735 O_SHORT_LIVED
736 O_TEMPORARY
737 O_RANDOM
738 O_SEQUENTIAL
739 O_TEXT
740
Georg Brandlaf265f42008-12-07 15:06:20 +0000741 These constants are only available on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000742
743
Alexandre Vassalottibee32532008-05-16 18:15:12 +0000744.. data:: O_ASYNC
745 O_DIRECT
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000746 O_DIRECTORY
747 O_NOFOLLOW
748 O_NOATIME
749
Georg Brandlaf265f42008-12-07 15:06:20 +0000750 These constants are GNU extensions and not present if they are not defined by
751 the C library.
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000752
753
Georg Brandl116aa622007-08-15 14:28:22 +0000754.. _os-file-dir:
755
756Files and Directories
757---------------------
758
Georg Brandl116aa622007-08-15 14:28:22 +0000759.. function:: access(path, mode)
760
761 Use the real uid/gid to test for access to *path*. Note that most operations
762 will use the effective uid/gid, therefore this routine can be used in a
763 suid/sgid environment to test if the invoking user has the specified access to
764 *path*. *mode* should be :const:`F_OK` to test the existence of *path*, or it
765 can be the inclusive OR of one or more of :const:`R_OK`, :const:`W_OK`, and
766 :const:`X_OK` to test permissions. Return :const:`True` if access is allowed,
767 :const:`False` if not. See the Unix man page :manpage:`access(2)` for more
Benjamin Petersond91203b2010-05-06 23:20:40 +0000768 information.
769
770 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000771
772 .. note::
773
Georg Brandlc5605df2009-08-13 08:26:44 +0000774 Using :func:`access` to check if a user is authorized to e.g. open a file
775 before actually doing so using :func:`open` creates a security hole,
776 because the user might exploit the short time interval between checking
777 and opening the file to manipulate it.
Georg Brandl116aa622007-08-15 14:28:22 +0000778
779 .. note::
780
781 I/O operations may fail even when :func:`access` indicates that they would
782 succeed, particularly for operations on network filesystems which may have
783 permissions semantics beyond the usual POSIX permission-bit model.
784
785
786.. data:: F_OK
787
788 Value to pass as the *mode* parameter of :func:`access` to test the existence of
789 *path*.
790
791
792.. data:: R_OK
793
794 Value to include in the *mode* parameter of :func:`access` to test the
795 readability of *path*.
796
797
798.. data:: W_OK
799
800 Value to include in the *mode* parameter of :func:`access` to test the
801 writability of *path*.
802
803
804.. data:: X_OK
805
806 Value to include in the *mode* parameter of :func:`access` to determine if
807 *path* can be executed.
808
809
810.. function:: chdir(path)
811
812 .. index:: single: directory; changing
813
Benjamin Petersond91203b2010-05-06 23:20:40 +0000814 Change the current working directory to *path*.
815
816 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000817
818
819.. function:: fchdir(fd)
820
821 Change the current working directory to the directory represented by the file
822 descriptor *fd*. The descriptor must refer to an opened directory, not an open
Benjamin Petersond91203b2010-05-06 23:20:40 +0000823 file.
824
825 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000826
Georg Brandl116aa622007-08-15 14:28:22 +0000827
828.. function:: getcwd()
829
Martin v. Löwis011e8422009-05-05 04:43:17 +0000830 Return a string representing the current working directory.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000831
Martin v. Löwis011e8422009-05-05 04:43:17 +0000832 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000833
Benjamin Petersond91203b2010-05-06 23:20:40 +0000834
Martin v. Löwisa731b992008-10-07 06:36:31 +0000835.. function:: getcwdb()
Georg Brandl116aa622007-08-15 14:28:22 +0000836
Georg Brandl76e55382008-10-08 16:34:57 +0000837 Return a bytestring representing the current working directory.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000838
Georg Brandlc575c902008-09-13 17:46:05 +0000839 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000840
Georg Brandl116aa622007-08-15 14:28:22 +0000841
842.. function:: chflags(path, flags)
843
844 Set the flags of *path* to the numeric *flags*. *flags* may take a combination
845 (bitwise OR) of the following values (as defined in the :mod:`stat` module):
846
847 * ``UF_NODUMP``
848 * ``UF_IMMUTABLE``
849 * ``UF_APPEND``
850 * ``UF_OPAQUE``
851 * ``UF_NOUNLINK``
852 * ``SF_ARCHIVED``
853 * ``SF_IMMUTABLE``
854 * ``SF_APPEND``
855 * ``SF_NOUNLINK``
856 * ``SF_SNAPSHOT``
857
Georg Brandlc575c902008-09-13 17:46:05 +0000858 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000859
Georg Brandl116aa622007-08-15 14:28:22 +0000860
861.. function:: chroot(path)
862
863 Change the root directory of the current process to *path*. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +0000864 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000865
Georg Brandl116aa622007-08-15 14:28:22 +0000866
867.. function:: chmod(path, mode)
868
869 Change the mode of *path* to the numeric *mode*. *mode* may take one of the
Christian Heimesfaf2f632008-01-06 16:59:19 +0000870 following values (as defined in the :mod:`stat` module) or bitwise ORed
Georg Brandl116aa622007-08-15 14:28:22 +0000871 combinations of them:
872
R. David Murrayba426142009-07-21 14:29:59 +0000873 * :data:`stat.S_ISUID`
874 * :data:`stat.S_ISGID`
875 * :data:`stat.S_ENFMT`
876 * :data:`stat.S_ISVTX`
877 * :data:`stat.S_IREAD`
878 * :data:`stat.S_IWRITE`
879 * :data:`stat.S_IEXEC`
880 * :data:`stat.S_IRWXU`
881 * :data:`stat.S_IRUSR`
882 * :data:`stat.S_IWUSR`
883 * :data:`stat.S_IXUSR`
884 * :data:`stat.S_IRWXG`
885 * :data:`stat.S_IRGRP`
886 * :data:`stat.S_IWGRP`
887 * :data:`stat.S_IXGRP`
888 * :data:`stat.S_IRWXO`
889 * :data:`stat.S_IROTH`
890 * :data:`stat.S_IWOTH`
891 * :data:`stat.S_IXOTH`
Georg Brandl116aa622007-08-15 14:28:22 +0000892
Georg Brandlc575c902008-09-13 17:46:05 +0000893 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000894
895 .. note::
896
897 Although Windows supports :func:`chmod`, you can only set the file's read-only
898 flag with it (via the ``stat.S_IWRITE`` and ``stat.S_IREAD``
899 constants or a corresponding integer value). All other bits are
900 ignored.
901
902
903.. function:: chown(path, uid, gid)
904
905 Change the owner and group id of *path* to the numeric *uid* and *gid*. To leave
Benjamin Petersond91203b2010-05-06 23:20:40 +0000906 one of the ids unchanged, set it to -1.
907
908 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000909
910
911.. function:: lchflags(path, flags)
912
913 Set the flags of *path* to the numeric *flags*, like :func:`chflags`, but do not
Benjamin Petersond91203b2010-05-06 23:20:40 +0000914 follow symbolic links.
915
916 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000917
Georg Brandl116aa622007-08-15 14:28:22 +0000918
Christian Heimes93852662007-12-01 12:22:32 +0000919.. function:: lchmod(path, mode)
920
921 Change the mode of *path* to the numeric *mode*. If path is a symlink, this
922 affects the symlink rather than the target. See the docs for :func:`chmod`
Benjamin Petersond91203b2010-05-06 23:20:40 +0000923 for possible values of *mode*.
924
925 Availability: Unix.
Christian Heimes93852662007-12-01 12:22:32 +0000926
Christian Heimes93852662007-12-01 12:22:32 +0000927
Georg Brandl116aa622007-08-15 14:28:22 +0000928.. function:: lchown(path, uid, gid)
929
Christian Heimesfaf2f632008-01-06 16:59:19 +0000930 Change the owner and group id of *path* to the numeric *uid* and *gid*. This
Benjamin Petersond91203b2010-05-06 23:20:40 +0000931 function will not follow symbolic links.
932
933 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000934
Georg Brandl116aa622007-08-15 14:28:22 +0000935
Benjamin Peterson5879d412009-03-30 14:51:56 +0000936.. function:: link(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +0000937
Benjamin Petersond91203b2010-05-06 23:20:40 +0000938 Create a hard link pointing to *source* named *link_name*.
939
940 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000941
942
943.. function:: listdir(path)
944
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000945 Return a list containing the names of the entries in the directory given by
946 *path*. The list is in arbitrary order. It does not include the special
947 entries ``'.'`` and ``'..'`` even if they are present in the directory.
Georg Brandl116aa622007-08-15 14:28:22 +0000948
Martin v. Löwis011e8422009-05-05 04:43:17 +0000949 This function can be called with a bytes or string argument, and returns
950 filenames of the same datatype.
Georg Brandl116aa622007-08-15 14:28:22 +0000951
Benjamin Petersond91203b2010-05-06 23:20:40 +0000952 Availability: Unix, Windows.
953
Georg Brandl116aa622007-08-15 14:28:22 +0000954
955.. function:: lstat(path)
956
Guido van Rossum2cc30da2007-11-02 23:46:40 +0000957 Like :func:`stat`, but do not follow symbolic links. This is an alias for
958 :func:`stat` on platforms that do not support symbolic links, such as
959 Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000960
961
962.. function:: mkfifo(path[, mode])
963
Georg Brandlf4a41232008-05-26 17:55:52 +0000964 Create a FIFO (a named pipe) named *path* with numeric mode *mode*. The
965 default *mode* is ``0o666`` (octal). The current umask value is first masked
Benjamin Petersond91203b2010-05-06 23:20:40 +0000966 out from the mode.
Georg Brandl116aa622007-08-15 14:28:22 +0000967
968 FIFOs are pipes that can be accessed like regular files. FIFOs exist until they
969 are deleted (for example with :func:`os.unlink`). Generally, FIFOs are used as
970 rendezvous between "client" and "server" type processes: the server opens the
971 FIFO for reading, and the client opens it for writing. Note that :func:`mkfifo`
972 doesn't open the FIFO --- it just creates the rendezvous point.
973
Benjamin Petersond91203b2010-05-06 23:20:40 +0000974 Availability: Unix.
975
Georg Brandl116aa622007-08-15 14:28:22 +0000976
Georg Brandlf4a41232008-05-26 17:55:52 +0000977.. function:: mknod(filename[, mode=0o600, device])
Georg Brandl116aa622007-08-15 14:28:22 +0000978
979 Create a filesystem node (file, device special file or named pipe) named
980 *filename*. *mode* specifies both the permissions to use and the type of node to
981 be created, being combined (bitwise OR) with one of ``stat.S_IFREG``,
982 ``stat.S_IFCHR``, ``stat.S_IFBLK``,
983 and ``stat.S_IFIFO`` (those constants are available in :mod:`stat`).
984 For ``stat.S_IFCHR`` and
985 ``stat.S_IFBLK``, *device* defines the newly created device special file (probably using
986 :func:`os.makedev`), otherwise it is ignored.
987
Georg Brandl116aa622007-08-15 14:28:22 +0000988
989.. function:: major(device)
990
Christian Heimesfaf2f632008-01-06 16:59:19 +0000991 Extract the device major number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000992 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
993
Georg Brandl116aa622007-08-15 14:28:22 +0000994
995.. function:: minor(device)
996
Christian Heimesfaf2f632008-01-06 16:59:19 +0000997 Extract the device minor number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000998 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
999
Georg Brandl116aa622007-08-15 14:28:22 +00001000
1001.. function:: makedev(major, minor)
1002
Christian Heimesfaf2f632008-01-06 16:59:19 +00001003 Compose a raw device number from the major and minor device numbers.
Georg Brandl116aa622007-08-15 14:28:22 +00001004
Georg Brandl116aa622007-08-15 14:28:22 +00001005
1006.. function:: mkdir(path[, mode])
1007
Georg Brandlf4a41232008-05-26 17:55:52 +00001008 Create a directory named *path* with numeric mode *mode*. The default *mode*
1009 is ``0o777`` (octal). On some systems, *mode* is ignored. Where it is used,
Georg Brandlc62efa82010-07-11 10:41:07 +00001010 the current umask value is first masked out. If the directory already
1011 exists, :exc:`OSError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +00001012
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001013 It is also possible to create temporary directories; see the
1014 :mod:`tempfile` module's :func:`tempfile.mkdtemp` function.
1015
Benjamin Petersond91203b2010-05-06 23:20:40 +00001016 Availability: Unix, Windows.
1017
Georg Brandl116aa622007-08-15 14:28:22 +00001018
1019.. function:: makedirs(path[, mode])
1020
1021 .. index::
1022 single: directory; creating
1023 single: UNC paths; and os.makedirs()
1024
1025 Recursive directory creation function. Like :func:`mkdir`, but makes all
Georg Brandlf4a41232008-05-26 17:55:52 +00001026 intermediate-level directories needed to contain the leaf directory. Throws
1027 an :exc:`error` exception if the leaf directory already exists or cannot be
1028 created. The default *mode* is ``0o777`` (octal). On some systems, *mode*
1029 is ignored. Where it is used, the current umask value is first masked out.
Georg Brandl116aa622007-08-15 14:28:22 +00001030
1031 .. note::
1032
1033 :func:`makedirs` will become confused if the path elements to create include
Christian Heimesfaf2f632008-01-06 16:59:19 +00001034 :data:`os.pardir`.
Georg Brandl116aa622007-08-15 14:28:22 +00001035
Georg Brandl55ac8f02007-09-01 13:51:09 +00001036 This function handles UNC paths correctly.
Georg Brandl116aa622007-08-15 14:28:22 +00001037
1038
1039.. function:: pathconf(path, name)
1040
1041 Return system configuration information relevant to a named file. *name*
1042 specifies the configuration value to retrieve; it may be a string which is the
1043 name of a defined system value; these names are specified in a number of
1044 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
1045 additional names as well. The names known to the host operating system are
1046 given in the ``pathconf_names`` dictionary. For configuration variables not
1047 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +00001048
1049 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1050 specific value for *name* is not supported by the host system, even if it is
1051 included in ``pathconf_names``, an :exc:`OSError` is raised with
1052 :const:`errno.EINVAL` for the error number.
1053
Benjamin Petersond91203b2010-05-06 23:20:40 +00001054 Availability: Unix.
1055
Georg Brandl116aa622007-08-15 14:28:22 +00001056
1057.. data:: pathconf_names
1058
1059 Dictionary mapping names accepted by :func:`pathconf` and :func:`fpathconf` to
1060 the integer values defined for those names by the host operating system. This
1061 can be used to determine the set of names known to the system. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001062 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001063
1064
1065.. function:: readlink(path)
1066
1067 Return a string representing the path to which the symbolic link points. The
1068 result may be either an absolute or relative pathname; if it is relative, it may
1069 be converted to an absolute pathname using ``os.path.join(os.path.dirname(path),
1070 result)``.
1071
Georg Brandl76e55382008-10-08 16:34:57 +00001072 If the *path* is a string object, the result will also be a string object,
1073 and the call may raise an UnicodeDecodeError. If the *path* is a bytes
1074 object, the result will be a bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +00001075
Georg Brandlc575c902008-09-13 17:46:05 +00001076 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001077
1078
1079.. function:: remove(path)
1080
Georg Brandl7baf6252009-09-01 08:13:16 +00001081 Remove (delete) the file *path*. If *path* is a directory, :exc:`OSError` is
1082 raised; see :func:`rmdir` below to remove a directory. This is identical to
1083 the :func:`unlink` function documented below. On Windows, attempting to
1084 remove a file that is in use causes an exception to be raised; on Unix, the
1085 directory entry is removed but the storage allocated to the file is not made
Benjamin Petersond91203b2010-05-06 23:20:40 +00001086 available until the original file is no longer in use.
1087
1088 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001089
1090
1091.. function:: removedirs(path)
1092
1093 .. index:: single: directory; deleting
1094
Christian Heimesfaf2f632008-01-06 16:59:19 +00001095 Remove directories recursively. Works like :func:`rmdir` except that, if the
Georg Brandl116aa622007-08-15 14:28:22 +00001096 leaf directory is successfully removed, :func:`removedirs` tries to
1097 successively remove every parent directory mentioned in *path* until an error
1098 is raised (which is ignored, because it generally means that a parent directory
1099 is not empty). For example, ``os.removedirs('foo/bar/baz')`` will first remove
1100 the directory ``'foo/bar/baz'``, and then remove ``'foo/bar'`` and ``'foo'`` if
1101 they are empty. Raises :exc:`OSError` if the leaf directory could not be
1102 successfully removed.
1103
Georg Brandl116aa622007-08-15 14:28:22 +00001104
1105.. function:: rename(src, dst)
1106
1107 Rename the file or directory *src* to *dst*. If *dst* is a directory,
1108 :exc:`OSError` will be raised. On Unix, if *dst* exists and is a file, it will
Christian Heimesfaf2f632008-01-06 16:59:19 +00001109 be replaced silently if the user has permission. The operation may fail on some
Georg Brandl116aa622007-08-15 14:28:22 +00001110 Unix flavors if *src* and *dst* are on different filesystems. If successful,
1111 the renaming will be an atomic operation (this is a POSIX requirement). On
1112 Windows, if *dst* already exists, :exc:`OSError` will be raised even if it is a
1113 file; there may be no way to implement an atomic rename when *dst* names an
Benjamin Petersond91203b2010-05-06 23:20:40 +00001114 existing file.
1115
1116 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001117
1118
1119.. function:: renames(old, new)
1120
1121 Recursive directory or file renaming function. Works like :func:`rename`, except
1122 creation of any intermediate directories needed to make the new pathname good is
1123 attempted first. After the rename, directories corresponding to rightmost path
1124 segments of the old name will be pruned away using :func:`removedirs`.
1125
Georg Brandl116aa622007-08-15 14:28:22 +00001126 .. note::
1127
1128 This function can fail with the new directory structure made if you lack
1129 permissions needed to remove the leaf directory or file.
1130
1131
1132.. function:: rmdir(path)
1133
Georg Brandl7baf6252009-09-01 08:13:16 +00001134 Remove (delete) the directory *path*. Only works when the directory is
1135 empty, otherwise, :exc:`OSError` is raised. In order to remove whole
Benjamin Petersond91203b2010-05-06 23:20:40 +00001136 directory trees, :func:`shutil.rmtree` can be used.
1137
1138 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001139
1140
1141.. function:: stat(path)
1142
1143 Perform a :cfunc:`stat` system call on the given path. The return value is an
1144 object whose attributes correspond to the members of the :ctype:`stat`
1145 structure, namely: :attr:`st_mode` (protection bits), :attr:`st_ino` (inode
1146 number), :attr:`st_dev` (device), :attr:`st_nlink` (number of hard links),
Christian Heimesfaf2f632008-01-06 16:59:19 +00001147 :attr:`st_uid` (user id of owner), :attr:`st_gid` (group id of owner),
Georg Brandl116aa622007-08-15 14:28:22 +00001148 :attr:`st_size` (size of file, in bytes), :attr:`st_atime` (time of most recent
1149 access), :attr:`st_mtime` (time of most recent content modification),
1150 :attr:`st_ctime` (platform dependent; time of most recent metadata change on
1151 Unix, or the time of creation on Windows)::
1152
1153 >>> import os
1154 >>> statinfo = os.stat('somefile.txt')
1155 >>> statinfo
Ezio Melotti9ce52612010-01-16 14:52:34 +00001156 (33188, 422511, 769, 1, 1032, 100, 926, 1105022698,1105022732, 1105022732)
Georg Brandl116aa622007-08-15 14:28:22 +00001157 >>> statinfo.st_size
Ezio Melotti9ce52612010-01-16 14:52:34 +00001158 926
Georg Brandl116aa622007-08-15 14:28:22 +00001159 >>>
1160
Georg Brandl116aa622007-08-15 14:28:22 +00001161
1162 On some Unix systems (such as Linux), the following attributes may also be
1163 available: :attr:`st_blocks` (number of blocks allocated for file),
1164 :attr:`st_blksize` (filesystem blocksize), :attr:`st_rdev` (type of device if an
1165 inode device). :attr:`st_flags` (user defined flags for file).
1166
1167 On other Unix systems (such as FreeBSD), the following attributes may be
1168 available (but may be only filled out if root tries to use them): :attr:`st_gen`
1169 (file generation number), :attr:`st_birthtime` (time of file creation).
1170
1171 On Mac OS systems, the following attributes may also be available:
1172 :attr:`st_rsize`, :attr:`st_creator`, :attr:`st_type`.
1173
Georg Brandl116aa622007-08-15 14:28:22 +00001174 .. index:: module: stat
1175
1176 For backward compatibility, the return value of :func:`stat` is also accessible
1177 as a tuple of at least 10 integers giving the most important (and portable)
1178 members of the :ctype:`stat` structure, in the order :attr:`st_mode`,
1179 :attr:`st_ino`, :attr:`st_dev`, :attr:`st_nlink`, :attr:`st_uid`,
1180 :attr:`st_gid`, :attr:`st_size`, :attr:`st_atime`, :attr:`st_mtime`,
1181 :attr:`st_ctime`. More items may be added at the end by some implementations.
1182 The standard module :mod:`stat` defines functions and constants that are useful
1183 for extracting information from a :ctype:`stat` structure. (On Windows, some
1184 items are filled with dummy values.)
1185
1186 .. note::
1187
1188 The exact meaning and resolution of the :attr:`st_atime`, :attr:`st_mtime`, and
1189 :attr:`st_ctime` members depends on the operating system and the file system.
1190 For example, on Windows systems using the FAT or FAT32 file systems,
1191 :attr:`st_mtime` has 2-second resolution, and :attr:`st_atime` has only 1-day
1192 resolution. See your operating system documentation for details.
1193
Georg Brandlc575c902008-09-13 17:46:05 +00001194 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001195
Georg Brandl116aa622007-08-15 14:28:22 +00001196
1197.. function:: stat_float_times([newvalue])
1198
1199 Determine whether :class:`stat_result` represents time stamps as float objects.
1200 If *newvalue* is ``True``, future calls to :func:`stat` return floats, if it is
1201 ``False``, future calls return ints. If *newvalue* is omitted, return the
1202 current setting.
1203
1204 For compatibility with older Python versions, accessing :class:`stat_result` as
1205 a tuple always returns integers.
1206
Georg Brandl55ac8f02007-09-01 13:51:09 +00001207 Python now returns float values by default. Applications which do not work
1208 correctly with floating point time stamps can use this function to restore the
1209 old behaviour.
Georg Brandl116aa622007-08-15 14:28:22 +00001210
1211 The resolution of the timestamps (that is the smallest possible fraction)
1212 depends on the system. Some systems only support second resolution; on these
1213 systems, the fraction will always be zero.
1214
1215 It is recommended that this setting is only changed at program startup time in
1216 the *__main__* module; libraries should never change this setting. If an
1217 application uses a library that works incorrectly if floating point time stamps
1218 are processed, this application should turn the feature off until the library
1219 has been corrected.
1220
1221
1222.. function:: statvfs(path)
1223
1224 Perform a :cfunc:`statvfs` system call on the given path. The return value is
1225 an object whose attributes describe the filesystem on the given path, and
1226 correspond to the members of the :ctype:`statvfs` structure, namely:
1227 :attr:`f_bsize`, :attr:`f_frsize`, :attr:`f_blocks`, :attr:`f_bfree`,
1228 :attr:`f_bavail`, :attr:`f_files`, :attr:`f_ffree`, :attr:`f_favail`,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001229 :attr:`f_flag`, :attr:`f_namemax`.
1230
1231 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001232
Georg Brandl116aa622007-08-15 14:28:22 +00001233
Benjamin Peterson5879d412009-03-30 14:51:56 +00001234.. function:: symlink(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +00001235
Benjamin Petersond91203b2010-05-06 23:20:40 +00001236 Create a symbolic link pointing to *source* named *link_name*.
1237
1238 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001239
1240
Georg Brandl116aa622007-08-15 14:28:22 +00001241.. function:: unlink(path)
1242
Georg Brandl7baf6252009-09-01 08:13:16 +00001243 Remove (delete) the file *path*. This is the same function as
1244 :func:`remove`; the :func:`unlink` name is its traditional Unix
Benjamin Petersond91203b2010-05-06 23:20:40 +00001245 name.
1246
1247 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001248
1249
1250.. function:: utime(path, times)
1251
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001252 Set the access and modified times of the file specified by *path*. If *times*
1253 is ``None``, then the file's access and modified times are set to the current
1254 time. (The effect is similar to running the Unix program :program:`touch` on
1255 the path.) Otherwise, *times* must be a 2-tuple of numbers, of the form
1256 ``(atime, mtime)`` which is used to set the access and modified times,
1257 respectively. Whether a directory can be given for *path* depends on whether
1258 the operating system implements directories as files (for example, Windows
1259 does not). Note that the exact times you set here may not be returned by a
1260 subsequent :func:`stat` call, depending on the resolution with which your
1261 operating system records access and modification times; see :func:`stat`.
Georg Brandl116aa622007-08-15 14:28:22 +00001262
Georg Brandlc575c902008-09-13 17:46:05 +00001263 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001264
1265
1266.. function:: walk(top[, topdown=True [, onerror=None[, followlinks=False]]])
1267
1268 .. index::
1269 single: directory; walking
1270 single: directory; traversal
1271
Christian Heimesfaf2f632008-01-06 16:59:19 +00001272 Generate the file names in a directory tree by walking the tree
1273 either top-down or bottom-up. For each directory in the tree rooted at directory
Georg Brandl116aa622007-08-15 14:28:22 +00001274 *top* (including *top* itself), it yields a 3-tuple ``(dirpath, dirnames,
1275 filenames)``.
1276
1277 *dirpath* is a string, the path to the directory. *dirnames* is a list of the
1278 names of the subdirectories in *dirpath* (excluding ``'.'`` and ``'..'``).
1279 *filenames* is a list of the names of the non-directory files in *dirpath*.
1280 Note that the names in the lists contain no path components. To get a full path
1281 (which begins with *top*) to a file or directory in *dirpath*, do
1282 ``os.path.join(dirpath, name)``.
1283
Christian Heimesfaf2f632008-01-06 16:59:19 +00001284 If optional argument *topdown* is ``True`` or not specified, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001285 directory is generated before the triples for any of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001286 (directories are generated top-down). If *topdown* is ``False``, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001287 directory is generated after the triples for all of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001288 (directories are generated bottom-up).
Georg Brandl116aa622007-08-15 14:28:22 +00001289
Christian Heimesfaf2f632008-01-06 16:59:19 +00001290 When *topdown* is ``True``, the caller can modify the *dirnames* list in-place
Georg Brandl116aa622007-08-15 14:28:22 +00001291 (perhaps using :keyword:`del` or slice assignment), and :func:`walk` will only
1292 recurse into the subdirectories whose names remain in *dirnames*; this can be
1293 used to prune the search, impose a specific order of visiting, or even to inform
1294 :func:`walk` about directories the caller creates or renames before it resumes
Christian Heimesfaf2f632008-01-06 16:59:19 +00001295 :func:`walk` again. Modifying *dirnames* when *topdown* is ``False`` is
Georg Brandl116aa622007-08-15 14:28:22 +00001296 ineffective, because in bottom-up mode the directories in *dirnames* are
1297 generated before *dirpath* itself is generated.
1298
Christian Heimesfaf2f632008-01-06 16:59:19 +00001299 By default errors from the :func:`listdir` call are ignored. If optional
Georg Brandl116aa622007-08-15 14:28:22 +00001300 argument *onerror* is specified, it should be a function; it will be called with
1301 one argument, an :exc:`OSError` instance. It can report the error to continue
1302 with the walk, or raise the exception to abort the walk. Note that the filename
1303 is available as the ``filename`` attribute of the exception object.
1304
1305 By default, :func:`walk` will not walk down into symbolic links that resolve to
Christian Heimesfaf2f632008-01-06 16:59:19 +00001306 directories. Set *followlinks* to ``True`` to visit directories pointed to by
Georg Brandl116aa622007-08-15 14:28:22 +00001307 symlinks, on systems that support them.
1308
Georg Brandl116aa622007-08-15 14:28:22 +00001309 .. note::
1310
Christian Heimesfaf2f632008-01-06 16:59:19 +00001311 Be aware that setting *followlinks* to ``True`` can lead to infinite recursion if a
Georg Brandl116aa622007-08-15 14:28:22 +00001312 link points to a parent directory of itself. :func:`walk` does not keep track of
1313 the directories it visited already.
1314
1315 .. note::
1316
1317 If you pass a relative pathname, don't change the current working directory
1318 between resumptions of :func:`walk`. :func:`walk` never changes the current
1319 directory, and assumes that its caller doesn't either.
1320
1321 This example displays the number of bytes taken by non-directory files in each
1322 directory under the starting directory, except that it doesn't look under any
1323 CVS subdirectory::
1324
1325 import os
1326 from os.path import join, getsize
1327 for root, dirs, files in os.walk('python/Lib/email'):
Georg Brandl6911e3c2007-09-04 07:15:32 +00001328 print(root, "consumes", end=" ")
1329 print(sum(getsize(join(root, name)) for name in files), end=" ")
1330 print("bytes in", len(files), "non-directory files")
Georg Brandl116aa622007-08-15 14:28:22 +00001331 if 'CVS' in dirs:
1332 dirs.remove('CVS') # don't visit CVS directories
1333
Christian Heimesfaf2f632008-01-06 16:59:19 +00001334 In the next example, walking the tree bottom-up is essential: :func:`rmdir`
Georg Brandl116aa622007-08-15 14:28:22 +00001335 doesn't allow deleting a directory before the directory is empty::
1336
Christian Heimesfaf2f632008-01-06 16:59:19 +00001337 # Delete everything reachable from the directory named in "top",
Georg Brandl116aa622007-08-15 14:28:22 +00001338 # assuming there are no symbolic links.
1339 # CAUTION: This is dangerous! For example, if top == '/', it
1340 # could delete all your disk files.
1341 import os
1342 for root, dirs, files in os.walk(top, topdown=False):
1343 for name in files:
1344 os.remove(os.path.join(root, name))
1345 for name in dirs:
1346 os.rmdir(os.path.join(root, name))
1347
Georg Brandl116aa622007-08-15 14:28:22 +00001348
1349.. _os-process:
1350
1351Process Management
1352------------------
1353
1354These functions may be used to create and manage processes.
1355
1356The various :func:`exec\*` functions take a list of arguments for the new
1357program loaded into the process. In each case, the first of these arguments is
1358passed to the new program as its own name rather than as an argument a user may
1359have typed on a command line. For the C programmer, this is the ``argv[0]``
1360passed to a program's :cfunc:`main`. For example, ``os.execv('/bin/echo',
1361['foo', 'bar'])`` will only print ``bar`` on standard output; ``foo`` will seem
1362to be ignored.
1363
1364
1365.. function:: abort()
1366
1367 Generate a :const:`SIGABRT` signal to the current process. On Unix, the default
1368 behavior is to produce a core dump; on Windows, the process immediately returns
1369 an exit code of ``3``. Be aware that programs which use :func:`signal.signal`
1370 to register a handler for :const:`SIGABRT` will behave differently.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001371
Georg Brandlc575c902008-09-13 17:46:05 +00001372 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001373
1374
1375.. function:: execl(path, arg0, arg1, ...)
1376 execle(path, arg0, arg1, ..., env)
1377 execlp(file, arg0, arg1, ...)
1378 execlpe(file, arg0, arg1, ..., env)
1379 execv(path, args)
1380 execve(path, args, env)
1381 execvp(file, args)
1382 execvpe(file, args, env)
1383
1384 These functions all execute a new program, replacing the current process; they
1385 do not return. On Unix, the new executable is loaded into the current process,
Christian Heimesfaf2f632008-01-06 16:59:19 +00001386 and will have the same process id as the caller. Errors will be reported as
Georg Brandl48310cd2009-01-03 21:18:54 +00001387 :exc:`OSError` exceptions.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001388
1389 The current process is replaced immediately. Open file objects and
1390 descriptors are not flushed, so if there may be data buffered
1391 on these open files, you should flush them using
1392 :func:`sys.stdout.flush` or :func:`os.fsync` before calling an
1393 :func:`exec\*` function.
Georg Brandl116aa622007-08-15 14:28:22 +00001394
Christian Heimesfaf2f632008-01-06 16:59:19 +00001395 The "l" and "v" variants of the :func:`exec\*` functions differ in how
1396 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001397 to work with if the number of parameters is fixed when the code is written; the
1398 individual parameters simply become additional parameters to the :func:`execl\*`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001399 functions. The "v" variants are good when the number of parameters is
Georg Brandl116aa622007-08-15 14:28:22 +00001400 variable, with the arguments being passed in a list or tuple as the *args*
1401 parameter. In either case, the arguments to the child process should start with
1402 the name of the command being run, but this is not enforced.
1403
Christian Heimesfaf2f632008-01-06 16:59:19 +00001404 The variants which include a "p" near the end (:func:`execlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001405 :func:`execlpe`, :func:`execvp`, and :func:`execvpe`) will use the
1406 :envvar:`PATH` environment variable to locate the program *file*. When the
1407 environment is being replaced (using one of the :func:`exec\*e` variants,
1408 discussed in the next paragraph), the new environment is used as the source of
1409 the :envvar:`PATH` variable. The other variants, :func:`execl`, :func:`execle`,
1410 :func:`execv`, and :func:`execve`, will not use the :envvar:`PATH` variable to
1411 locate the executable; *path* must contain an appropriate absolute or relative
1412 path.
1413
1414 For :func:`execle`, :func:`execlpe`, :func:`execve`, and :func:`execvpe` (note
Christian Heimesfaf2f632008-01-06 16:59:19 +00001415 that these all end in "e"), the *env* parameter must be a mapping which is
Christian Heimesa342c012008-04-20 21:01:16 +00001416 used to define the environment variables for the new process (these are used
1417 instead of the current process' environment); the functions :func:`execl`,
Georg Brandl116aa622007-08-15 14:28:22 +00001418 :func:`execlp`, :func:`execv`, and :func:`execvp` all cause the new process to
Georg Brandl48310cd2009-01-03 21:18:54 +00001419 inherit the environment of the current process.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001420
1421 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001422
1423
1424.. function:: _exit(n)
1425
1426 Exit to the system with status *n*, without calling cleanup handlers, flushing
Benjamin Petersond91203b2010-05-06 23:20:40 +00001427 stdio buffers, etc.
1428
1429 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001430
1431 .. note::
1432
1433 The standard way to exit is ``sys.exit(n)``. :func:`_exit` should normally only
1434 be used in the child process after a :func:`fork`.
1435
Christian Heimesfaf2f632008-01-06 16:59:19 +00001436The following exit codes are defined and can be used with :func:`_exit`,
Georg Brandl116aa622007-08-15 14:28:22 +00001437although they are not required. These are typically used for system programs
1438written in Python, such as a mail server's external command delivery program.
1439
1440.. note::
1441
1442 Some of these may not be available on all Unix platforms, since there is some
1443 variation. These constants are defined where they are defined by the underlying
1444 platform.
1445
1446
1447.. data:: EX_OK
1448
Benjamin Petersond91203b2010-05-06 23:20:40 +00001449 Exit code that means no error occurred.
1450
1451 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001452
Georg Brandl116aa622007-08-15 14:28:22 +00001453
1454.. data:: EX_USAGE
1455
1456 Exit code that means the command was used incorrectly, such as when the wrong
Benjamin Petersond91203b2010-05-06 23:20:40 +00001457 number of arguments are given.
1458
1459 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001460
Georg Brandl116aa622007-08-15 14:28:22 +00001461
1462.. data:: EX_DATAERR
1463
Benjamin Petersond91203b2010-05-06 23:20:40 +00001464 Exit code that means the input data was incorrect.
1465
1466 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001467
Georg Brandl116aa622007-08-15 14:28:22 +00001468
1469.. data:: EX_NOINPUT
1470
1471 Exit code that means an input file did not exist or was not readable.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001472
Georg Brandlc575c902008-09-13 17:46:05 +00001473 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001474
Georg Brandl116aa622007-08-15 14:28:22 +00001475
1476.. data:: EX_NOUSER
1477
Benjamin Petersond91203b2010-05-06 23:20:40 +00001478 Exit code that means a specified user did not exist.
1479
1480 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001481
Georg Brandl116aa622007-08-15 14:28:22 +00001482
1483.. data:: EX_NOHOST
1484
Benjamin Petersond91203b2010-05-06 23:20:40 +00001485 Exit code that means a specified host did not exist.
1486
1487 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001488
Georg Brandl116aa622007-08-15 14:28:22 +00001489
1490.. data:: EX_UNAVAILABLE
1491
Benjamin Petersond91203b2010-05-06 23:20:40 +00001492 Exit code that means that a required service is unavailable.
1493
1494 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001495
Georg Brandl116aa622007-08-15 14:28:22 +00001496
1497.. data:: EX_SOFTWARE
1498
Benjamin Petersond91203b2010-05-06 23:20:40 +00001499 Exit code that means an internal software error was detected.
1500
1501 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001502
Georg Brandl116aa622007-08-15 14:28:22 +00001503
1504.. data:: EX_OSERR
1505
1506 Exit code that means an operating system error was detected, such as the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001507 inability to fork or create a pipe.
1508
1509 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001510
Georg Brandl116aa622007-08-15 14:28:22 +00001511
1512.. data:: EX_OSFILE
1513
1514 Exit code that means some system file did not exist, could not be opened, or had
Benjamin Petersond91203b2010-05-06 23:20:40 +00001515 some other kind of error.
1516
1517 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001518
Georg Brandl116aa622007-08-15 14:28:22 +00001519
1520.. data:: EX_CANTCREAT
1521
1522 Exit code that means a user specified output file could not be created.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001523
Georg Brandlc575c902008-09-13 17:46:05 +00001524 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001525
Georg Brandl116aa622007-08-15 14:28:22 +00001526
1527.. data:: EX_IOERR
1528
1529 Exit code that means that an error occurred while doing I/O on some file.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001530
Georg Brandlc575c902008-09-13 17:46:05 +00001531 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001532
Georg Brandl116aa622007-08-15 14:28:22 +00001533
1534.. data:: EX_TEMPFAIL
1535
1536 Exit code that means a temporary failure occurred. This indicates something
1537 that may not really be an error, such as a network connection that couldn't be
Benjamin Petersond91203b2010-05-06 23:20:40 +00001538 made during a retryable operation.
1539
1540 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001541
Georg Brandl116aa622007-08-15 14:28:22 +00001542
1543.. data:: EX_PROTOCOL
1544
1545 Exit code that means that a protocol exchange was illegal, invalid, or not
Benjamin Petersond91203b2010-05-06 23:20:40 +00001546 understood.
1547
1548 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001549
Georg Brandl116aa622007-08-15 14:28:22 +00001550
1551.. data:: EX_NOPERM
1552
1553 Exit code that means that there were insufficient permissions to perform the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001554 operation (but not intended for file system problems).
1555
1556 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001557
Georg Brandl116aa622007-08-15 14:28:22 +00001558
1559.. data:: EX_CONFIG
1560
1561 Exit code that means that some kind of configuration error occurred.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001562
Georg Brandlc575c902008-09-13 17:46:05 +00001563 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001564
Georg Brandl116aa622007-08-15 14:28:22 +00001565
1566.. data:: EX_NOTFOUND
1567
Benjamin Petersond91203b2010-05-06 23:20:40 +00001568 Exit code that means something like "an entry was not found".
1569
1570 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001571
Georg Brandl116aa622007-08-15 14:28:22 +00001572
1573.. function:: fork()
1574
Christian Heimesfaf2f632008-01-06 16:59:19 +00001575 Fork a child process. Return ``0`` in the child and the child's process id in the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001576 parent. If an error occurs :exc:`OSError` is raised.
Benjamin Petersonbcd8ac32008-10-10 22:20:52 +00001577
1578 Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have
1579 known issues when using fork() from a thread.
1580
Georg Brandlc575c902008-09-13 17:46:05 +00001581 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001582
1583
1584.. function:: forkpty()
1585
1586 Fork a child process, using a new pseudo-terminal as the child's controlling
1587 terminal. Return a pair of ``(pid, fd)``, where *pid* is ``0`` in the child, the
1588 new child's process id in the parent, and *fd* is the file descriptor of the
1589 master end of the pseudo-terminal. For a more portable approach, use the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001590 :mod:`pty` module. If an error occurs :exc:`OSError` is raised.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001591
Georg Brandlc575c902008-09-13 17:46:05 +00001592 Availability: some flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001593
1594
1595.. function:: kill(pid, sig)
1596
1597 .. index::
1598 single: process; killing
1599 single: process; signalling
1600
1601 Send signal *sig* to the process *pid*. Constants for the specific signals
1602 available on the host platform are defined in the :mod:`signal` module.
Georg Brandlc575c902008-09-13 17:46:05 +00001603 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001604
1605
1606.. function:: killpg(pgid, sig)
1607
1608 .. index::
1609 single: process; killing
1610 single: process; signalling
1611
Benjamin Petersond91203b2010-05-06 23:20:40 +00001612 Send the signal *sig* to the process group *pgid*.
1613
1614 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001615
Georg Brandl116aa622007-08-15 14:28:22 +00001616
1617.. function:: nice(increment)
1618
1619 Add *increment* to the process's "niceness". Return the new niceness.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001620
Georg Brandlc575c902008-09-13 17:46:05 +00001621 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001622
1623
1624.. function:: plock(op)
1625
1626 Lock program segments into memory. The value of *op* (defined in
Benjamin Petersond91203b2010-05-06 23:20:40 +00001627 ``<sys/lock.h>``) determines which segments are locked.
1628
1629 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001630
1631
1632.. function:: popen(...)
1633 :noindex:
1634
1635 Run child processes, returning opened pipes for communications. These functions
1636 are described in section :ref:`os-newstreams`.
1637
1638
1639.. function:: spawnl(mode, path, ...)
1640 spawnle(mode, path, ..., env)
1641 spawnlp(mode, file, ...)
1642 spawnlpe(mode, file, ..., env)
1643 spawnv(mode, path, args)
1644 spawnve(mode, path, args, env)
1645 spawnvp(mode, file, args)
1646 spawnvpe(mode, file, args, env)
1647
1648 Execute the program *path* in a new process.
1649
1650 (Note that the :mod:`subprocess` module provides more powerful facilities for
1651 spawning new processes and retrieving their results; using that module is
Benjamin Peterson87c8d872009-06-11 22:54:11 +00001652 preferable to using these functions. Check especially the
1653 :ref:`subprocess-replacements` section.)
Georg Brandl116aa622007-08-15 14:28:22 +00001654
Christian Heimesfaf2f632008-01-06 16:59:19 +00001655 If *mode* is :const:`P_NOWAIT`, this function returns the process id of the new
Georg Brandl116aa622007-08-15 14:28:22 +00001656 process; if *mode* is :const:`P_WAIT`, returns the process's exit code if it
1657 exits normally, or ``-signal``, where *signal* is the signal that killed the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001658 process. On Windows, the process id will actually be the process handle, so can
Georg Brandl116aa622007-08-15 14:28:22 +00001659 be used with the :func:`waitpid` function.
1660
Christian Heimesfaf2f632008-01-06 16:59:19 +00001661 The "l" and "v" variants of the :func:`spawn\*` functions differ in how
1662 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001663 to work with if the number of parameters is fixed when the code is written; the
1664 individual parameters simply become additional parameters to the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001665 :func:`spawnl\*` functions. The "v" variants are good when the number of
Georg Brandl116aa622007-08-15 14:28:22 +00001666 parameters is variable, with the arguments being passed in a list or tuple as
1667 the *args* parameter. In either case, the arguments to the child process must
1668 start with the name of the command being run.
1669
Christian Heimesfaf2f632008-01-06 16:59:19 +00001670 The variants which include a second "p" near the end (:func:`spawnlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001671 :func:`spawnlpe`, :func:`spawnvp`, and :func:`spawnvpe`) will use the
1672 :envvar:`PATH` environment variable to locate the program *file*. When the
1673 environment is being replaced (using one of the :func:`spawn\*e` variants,
1674 discussed in the next paragraph), the new environment is used as the source of
1675 the :envvar:`PATH` variable. The other variants, :func:`spawnl`,
1676 :func:`spawnle`, :func:`spawnv`, and :func:`spawnve`, will not use the
1677 :envvar:`PATH` variable to locate the executable; *path* must contain an
1678 appropriate absolute or relative path.
1679
1680 For :func:`spawnle`, :func:`spawnlpe`, :func:`spawnve`, and :func:`spawnvpe`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001681 (note that these all end in "e"), the *env* parameter must be a mapping
Christian Heimesa342c012008-04-20 21:01:16 +00001682 which is used to define the environment variables for the new process (they are
1683 used instead of the current process' environment); the functions
Georg Brandl116aa622007-08-15 14:28:22 +00001684 :func:`spawnl`, :func:`spawnlp`, :func:`spawnv`, and :func:`spawnvp` all cause
Benjamin Petersond23f8222009-04-05 19:13:16 +00001685 the new process to inherit the environment of the current process. Note that
1686 keys and values in the *env* dictionary must be strings; invalid keys or
1687 values will cause the function to fail, with a return value of ``127``.
Georg Brandl116aa622007-08-15 14:28:22 +00001688
1689 As an example, the following calls to :func:`spawnlp` and :func:`spawnvpe` are
1690 equivalent::
1691
1692 import os
1693 os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1694
1695 L = ['cp', 'index.html', '/dev/null']
1696 os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1697
1698 Availability: Unix, Windows. :func:`spawnlp`, :func:`spawnlpe`, :func:`spawnvp`
1699 and :func:`spawnvpe` are not available on Windows.
1700
Georg Brandl116aa622007-08-15 14:28:22 +00001701
1702.. data:: P_NOWAIT
1703 P_NOWAITO
1704
1705 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1706 functions. If either of these values is given, the :func:`spawn\*` functions
Christian Heimesfaf2f632008-01-06 16:59:19 +00001707 will return as soon as the new process has been created, with the process id as
Benjamin Petersond91203b2010-05-06 23:20:40 +00001708 the return value.
1709
1710 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001711
Georg Brandl116aa622007-08-15 14:28:22 +00001712
1713.. data:: P_WAIT
1714
1715 Possible value for the *mode* parameter to the :func:`spawn\*` family of
1716 functions. If this is given as *mode*, the :func:`spawn\*` functions will not
1717 return until the new process has run to completion and will return the exit code
1718 of the process the run is successful, or ``-signal`` if a signal kills the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001719 process.
1720
1721 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001722
Georg Brandl116aa622007-08-15 14:28:22 +00001723
1724.. data:: P_DETACH
1725 P_OVERLAY
1726
1727 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1728 functions. These are less portable than those listed above. :const:`P_DETACH`
1729 is similar to :const:`P_NOWAIT`, but the new process is detached from the
1730 console of the calling process. If :const:`P_OVERLAY` is used, the current
1731 process will be replaced; the :func:`spawn\*` function will not return.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001732
Georg Brandl116aa622007-08-15 14:28:22 +00001733 Availability: Windows.
1734
Georg Brandl116aa622007-08-15 14:28:22 +00001735
1736.. function:: startfile(path[, operation])
1737
1738 Start a file with its associated application.
1739
1740 When *operation* is not specified or ``'open'``, this acts like double-clicking
1741 the file in Windows Explorer, or giving the file name as an argument to the
1742 :program:`start` command from the interactive command shell: the file is opened
1743 with whatever application (if any) its extension is associated.
1744
1745 When another *operation* is given, it must be a "command verb" that specifies
1746 what should be done with the file. Common verbs documented by Microsoft are
1747 ``'print'`` and ``'edit'`` (to be used on files) as well as ``'explore'`` and
1748 ``'find'`` (to be used on directories).
1749
1750 :func:`startfile` returns as soon as the associated application is launched.
1751 There is no option to wait for the application to close, and no way to retrieve
1752 the application's exit status. The *path* parameter is relative to the current
1753 directory. If you want to use an absolute path, make sure the first character
1754 is not a slash (``'/'``); the underlying Win32 :cfunc:`ShellExecute` function
1755 doesn't work if it is. Use the :func:`os.path.normpath` function to ensure that
Benjamin Petersond91203b2010-05-06 23:20:40 +00001756 the path is properly encoded for Win32.
1757
1758 Availability: Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001759
Georg Brandl116aa622007-08-15 14:28:22 +00001760
1761.. function:: system(command)
1762
1763 Execute the command (a string) in a subshell. This is implemented by calling
Georg Brandl628e6f92009-10-27 20:24:45 +00001764 the Standard C function :cfunc:`system`, and has the same limitations.
1765 Changes to :data:`sys.stdin`, etc. are not reflected in the environment of the
1766 executed command.
Georg Brandl116aa622007-08-15 14:28:22 +00001767
1768 On Unix, the return value is the exit status of the process encoded in the
1769 format specified for :func:`wait`. Note that POSIX does not specify the meaning
1770 of the return value of the C :cfunc:`system` function, so the return value of
1771 the Python function is system-dependent.
1772
1773 On Windows, the return value is that returned by the system shell after running
1774 *command*, given by the Windows environment variable :envvar:`COMSPEC`: on
1775 :program:`command.com` systems (Windows 95, 98 and ME) this is always ``0``; on
1776 :program:`cmd.exe` systems (Windows NT, 2000 and XP) this is the exit status of
1777 the command run; on systems using a non-native shell, consult your shell
1778 documentation.
1779
Georg Brandl116aa622007-08-15 14:28:22 +00001780 The :mod:`subprocess` module provides more powerful facilities for spawning new
1781 processes and retrieving their results; using that module is preferable to using
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001782 this function. Use the :mod:`subprocess` module. Check especially the
1783 :ref:`subprocess-replacements` section.
Georg Brandl116aa622007-08-15 14:28:22 +00001784
Benjamin Petersond91203b2010-05-06 23:20:40 +00001785 Availability: Unix, Windows.
1786
Georg Brandl116aa622007-08-15 14:28:22 +00001787
1788.. function:: times()
1789
Benjamin Petersond91203b2010-05-06 23:20:40 +00001790 Return a 5-tuple of floating point numbers indicating accumulated (processor
1791 or other) times, in seconds. The items are: user time, system time,
1792 children's user time, children's system time, and elapsed real time since a
1793 fixed point in the past, in that order. See the Unix manual page
1794 :manpage:`times(2)` or the corresponding Windows Platform API documentation.
1795 On Windows, only the first two items are filled, the others are zero.
1796
1797 Availability: Unix, Windows
Georg Brandl116aa622007-08-15 14:28:22 +00001798
1799
1800.. function:: wait()
1801
1802 Wait for completion of a child process, and return a tuple containing its pid
1803 and exit status indication: a 16-bit number, whose low byte is the signal number
1804 that killed the process, and whose high byte is the exit status (if the signal
1805 number is zero); the high bit of the low byte is set if a core file was
Benjamin Petersond91203b2010-05-06 23:20:40 +00001806 produced.
1807
1808 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001809
1810
1811.. function:: waitpid(pid, options)
1812
1813 The details of this function differ on Unix and Windows.
1814
1815 On Unix: Wait for completion of a child process given by process id *pid*, and
1816 return a tuple containing its process id and exit status indication (encoded as
1817 for :func:`wait`). The semantics of the call are affected by the value of the
1818 integer *options*, which should be ``0`` for normal operation.
1819
1820 If *pid* is greater than ``0``, :func:`waitpid` requests status information for
1821 that specific process. If *pid* is ``0``, the request is for the status of any
1822 child in the process group of the current process. If *pid* is ``-1``, the
1823 request pertains to any child of the current process. If *pid* is less than
1824 ``-1``, status is requested for any process in the process group ``-pid`` (the
1825 absolute value of *pid*).
1826
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001827 An :exc:`OSError` is raised with the value of errno when the syscall
1828 returns -1.
1829
Georg Brandl116aa622007-08-15 14:28:22 +00001830 On Windows: Wait for completion of a process given by process handle *pid*, and
1831 return a tuple containing *pid*, and its exit status shifted left by 8 bits
1832 (shifting makes cross-platform use of the function easier). A *pid* less than or
1833 equal to ``0`` has no special meaning on Windows, and raises an exception. The
1834 value of integer *options* has no effect. *pid* can refer to any process whose
1835 id is known, not necessarily a child process. The :func:`spawn` functions called
1836 with :const:`P_NOWAIT` return suitable process handles.
1837
1838
1839.. function:: wait3([options])
1840
1841 Similar to :func:`waitpid`, except no process id argument is given and a
1842 3-element tuple containing the child's process id, exit status indication, and
1843 resource usage information is returned. Refer to :mod:`resource`.\
1844 :func:`getrusage` for details on resource usage information. The option
1845 argument is the same as that provided to :func:`waitpid` and :func:`wait4`.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001846
Georg Brandl116aa622007-08-15 14:28:22 +00001847 Availability: Unix.
1848
Georg Brandl116aa622007-08-15 14:28:22 +00001849
1850.. function:: wait4(pid, options)
1851
1852 Similar to :func:`waitpid`, except a 3-element tuple, containing the child's
1853 process id, exit status indication, and resource usage information is returned.
1854 Refer to :mod:`resource`.\ :func:`getrusage` for details on resource usage
1855 information. The arguments to :func:`wait4` are the same as those provided to
Benjamin Petersond91203b2010-05-06 23:20:40 +00001856 :func:`waitpid`.
1857
1858 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001859
Georg Brandl116aa622007-08-15 14:28:22 +00001860
1861.. data:: WNOHANG
1862
1863 The option for :func:`waitpid` to return immediately if no child process status
1864 is available immediately. The function returns ``(0, 0)`` in this case.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001865
Georg Brandlc575c902008-09-13 17:46:05 +00001866 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001867
1868
1869.. data:: WCONTINUED
1870
1871 This option causes child processes to be reported if they have been continued
Benjamin Petersond91203b2010-05-06 23:20:40 +00001872 from a job control stop since their status was last reported.
1873
1874 Availability: Some Unix systems.
Georg Brandl116aa622007-08-15 14:28:22 +00001875
Georg Brandl116aa622007-08-15 14:28:22 +00001876
1877.. data:: WUNTRACED
1878
1879 This option causes child processes to be reported if they have been stopped but
Benjamin Petersond91203b2010-05-06 23:20:40 +00001880 their current state has not been reported since they were stopped.
1881
1882 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001883
Georg Brandl116aa622007-08-15 14:28:22 +00001884
1885The following functions take a process status code as returned by
1886:func:`system`, :func:`wait`, or :func:`waitpid` as a parameter. They may be
1887used to determine the disposition of a process.
1888
Georg Brandl116aa622007-08-15 14:28:22 +00001889.. function:: WCOREDUMP(status)
1890
Christian Heimesfaf2f632008-01-06 16:59:19 +00001891 Return ``True`` if a core dump was generated for the process, otherwise
Benjamin Petersond91203b2010-05-06 23:20:40 +00001892 return ``False``.
1893
1894 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001895
Georg Brandl116aa622007-08-15 14:28:22 +00001896
1897.. function:: WIFCONTINUED(status)
1898
Christian Heimesfaf2f632008-01-06 16:59:19 +00001899 Return ``True`` if the process has been continued from a job control stop,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001900 otherwise return ``False``.
1901
1902 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001903
Georg Brandl116aa622007-08-15 14:28:22 +00001904
1905.. function:: WIFSTOPPED(status)
1906
Christian Heimesfaf2f632008-01-06 16:59:19 +00001907 Return ``True`` if the process has been stopped, otherwise return
Benjamin Petersond91203b2010-05-06 23:20:40 +00001908 ``False``.
1909
1910 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001911
1912
1913.. function:: WIFSIGNALED(status)
1914
Christian Heimesfaf2f632008-01-06 16:59:19 +00001915 Return ``True`` if the process exited due to a signal, otherwise return
Benjamin Petersond91203b2010-05-06 23:20:40 +00001916 ``False``.
1917
1918 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001919
1920
1921.. function:: WIFEXITED(status)
1922
Christian Heimesfaf2f632008-01-06 16:59:19 +00001923 Return ``True`` if the process exited using the :manpage:`exit(2)` system call,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001924 otherwise return ``False``.
1925
1926 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001927
1928
1929.. function:: WEXITSTATUS(status)
1930
1931 If ``WIFEXITED(status)`` is true, return the integer parameter to the
1932 :manpage:`exit(2)` system call. Otherwise, the return value is meaningless.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001933
Georg Brandlc575c902008-09-13 17:46:05 +00001934 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001935
1936
1937.. function:: WSTOPSIG(status)
1938
Benjamin Petersond91203b2010-05-06 23:20:40 +00001939 Return the signal which caused the process to stop.
1940
1941 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001942
1943
1944.. function:: WTERMSIG(status)
1945
Benjamin Petersond91203b2010-05-06 23:20:40 +00001946 Return the signal which caused the process to exit.
1947
1948 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001949
1950
1951.. _os-path:
1952
1953Miscellaneous System Information
1954--------------------------------
1955
1956
1957.. function:: confstr(name)
1958
1959 Return string-valued system configuration values. *name* specifies the
1960 configuration value to retrieve; it may be a string which is the name of a
1961 defined system value; these names are specified in a number of standards (POSIX,
1962 Unix 95, Unix 98, and others). Some platforms define additional names as well.
1963 The names known to the host operating system are given as the keys of the
1964 ``confstr_names`` dictionary. For configuration variables not included in that
Benjamin Petersond91203b2010-05-06 23:20:40 +00001965 mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +00001966
1967 If the configuration value specified by *name* isn't defined, ``None`` is
1968 returned.
1969
1970 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1971 specific value for *name* is not supported by the host system, even if it is
1972 included in ``confstr_names``, an :exc:`OSError` is raised with
1973 :const:`errno.EINVAL` for the error number.
1974
Benjamin Petersond91203b2010-05-06 23:20:40 +00001975 Availability: Unix
1976
Georg Brandl116aa622007-08-15 14:28:22 +00001977
1978.. data:: confstr_names
1979
1980 Dictionary mapping names accepted by :func:`confstr` to the integer values
1981 defined for those names by the host operating system. This can be used to
Benjamin Petersond91203b2010-05-06 23:20:40 +00001982 determine the set of names known to the system.
1983
1984 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001985
1986
1987.. function:: getloadavg()
1988
Christian Heimesa62da1d2008-01-12 19:39:10 +00001989 Return the number of processes in the system run queue averaged over the last
1990 1, 5, and 15 minutes or raises :exc:`OSError` if the load average was
Benjamin Petersond91203b2010-05-06 23:20:40 +00001991 unobtainable.
1992
1993 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001994
Georg Brandl116aa622007-08-15 14:28:22 +00001995
1996.. function:: sysconf(name)
1997
1998 Return integer-valued system configuration values. If the configuration value
1999 specified by *name* isn't defined, ``-1`` is returned. The comments regarding
2000 the *name* parameter for :func:`confstr` apply here as well; the dictionary that
2001 provides information on the known names is given by ``sysconf_names``.
Benjamin Petersond91203b2010-05-06 23:20:40 +00002002
Georg Brandlc575c902008-09-13 17:46:05 +00002003 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002004
2005
2006.. data:: sysconf_names
2007
2008 Dictionary mapping names accepted by :func:`sysconf` to the integer values
2009 defined for those names by the host operating system. This can be used to
Benjamin Petersond91203b2010-05-06 23:20:40 +00002010 determine the set of names known to the system.
2011
2012 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002013
Christian Heimesfaf2f632008-01-06 16:59:19 +00002014The following data values are used to support path manipulation operations. These
Georg Brandl116aa622007-08-15 14:28:22 +00002015are defined for all platforms.
2016
2017Higher-level operations on pathnames are defined in the :mod:`os.path` module.
2018
2019
2020.. data:: curdir
2021
2022 The constant string used by the operating system to refer to the current
Georg Brandlc575c902008-09-13 17:46:05 +00002023 directory. This is ``'.'`` for Windows and POSIX. Also available via
2024 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002025
2026
2027.. data:: pardir
2028
2029 The constant string used by the operating system to refer to the parent
Georg Brandlc575c902008-09-13 17:46:05 +00002030 directory. This is ``'..'`` for Windows and POSIX. Also available via
2031 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002032
2033
2034.. data:: sep
2035
Georg Brandlc575c902008-09-13 17:46:05 +00002036 The character used by the operating system to separate pathname components.
2037 This is ``'/'`` for POSIX and ``'\\'`` for Windows. Note that knowing this
2038 is not sufficient to be able to parse or concatenate pathnames --- use
Georg Brandl116aa622007-08-15 14:28:22 +00002039 :func:`os.path.split` and :func:`os.path.join` --- but it is occasionally
2040 useful. Also available via :mod:`os.path`.
2041
2042
2043.. data:: altsep
2044
2045 An alternative character used by the operating system to separate pathname
2046 components, or ``None`` if only one separator character exists. This is set to
2047 ``'/'`` on Windows systems where ``sep`` is a backslash. Also available via
2048 :mod:`os.path`.
2049
2050
2051.. data:: extsep
2052
2053 The character which separates the base filename from the extension; for example,
2054 the ``'.'`` in :file:`os.py`. Also available via :mod:`os.path`.
2055
Georg Brandl116aa622007-08-15 14:28:22 +00002056
2057.. data:: pathsep
2058
2059 The character conventionally used by the operating system to separate search
2060 path components (as in :envvar:`PATH`), such as ``':'`` for POSIX or ``';'`` for
2061 Windows. Also available via :mod:`os.path`.
2062
2063
2064.. data:: defpath
2065
2066 The default search path used by :func:`exec\*p\*` and :func:`spawn\*p\*` if the
2067 environment doesn't have a ``'PATH'`` key. Also available via :mod:`os.path`.
2068
2069
2070.. data:: linesep
2071
2072 The string used to separate (or, rather, terminate) lines on the current
Georg Brandlc575c902008-09-13 17:46:05 +00002073 platform. This may be a single character, such as ``'\n'`` for POSIX, or
2074 multiple characters, for example, ``'\r\n'`` for Windows. Do not use
2075 *os.linesep* as a line terminator when writing files opened in text mode (the
2076 default); use a single ``'\n'`` instead, on all platforms.
Georg Brandl116aa622007-08-15 14:28:22 +00002077
2078
2079.. data:: devnull
2080
Georg Brandlc52eeab2010-05-21 22:05:15 +00002081 The file path of the null device. For example: ``'/dev/null'`` for
2082 POSIX, ``'nul'`` for Windows. Also available via :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002083
Georg Brandl116aa622007-08-15 14:28:22 +00002084
2085.. _os-miscfunc:
2086
2087Miscellaneous Functions
2088-----------------------
2089
2090
2091.. function:: urandom(n)
2092
2093 Return a string of *n* random bytes suitable for cryptographic use.
2094
2095 This function returns random bytes from an OS-specific randomness source. The
2096 returned data should be unpredictable enough for cryptographic applications,
2097 though its exact quality depends on the OS implementation. On a UNIX-like
2098 system this will query /dev/urandom, and on Windows it will use CryptGenRandom.
2099 If a randomness source is not found, :exc:`NotImplementedError` will be raised.