blob: b14dc9256316e06eff8f7347f3423cf0721964c3 [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
404These functions create new file objects. (See also :func:`open`.)
405
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
439
440.. function:: close(fd)
441
Benjamin Petersond91203b2010-05-06 23:20:40 +0000442 Close file descriptor *fd*.
443
444 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000445
446 .. note::
447
448 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000449 descriptor as returned by :func:`os.open` or :func:`pipe`. To close a "file
Georg Brandl116aa622007-08-15 14:28:22 +0000450 object" returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000451 :func:`fdopen`, use its :meth:`~file.close` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000452
453
Christian Heimesfdab48e2008-01-20 09:06:41 +0000454.. function:: closerange(fd_low, fd_high)
455
456 Close all file descriptors from *fd_low* (inclusive) to *fd_high* (exclusive),
Benjamin Petersond91203b2010-05-06 23:20:40 +0000457 ignoring errors. Equivalent to::
Christian Heimesfdab48e2008-01-20 09:06:41 +0000458
Georg Brandl7baf6252009-09-01 08:13:16 +0000459 for fd in range(fd_low, fd_high):
Christian Heimesfdab48e2008-01-20 09:06:41 +0000460 try:
461 os.close(fd)
462 except OSError:
463 pass
464
Benjamin Petersond91203b2010-05-06 23:20:40 +0000465 Availability: Unix, Windows.
466
Christian Heimesfdab48e2008-01-20 09:06:41 +0000467
Georg Brandl81f11302007-12-21 08:45:42 +0000468.. function:: device_encoding(fd)
469
470 Return a string describing the encoding of the device associated with *fd*
471 if it is connected to a terminal; else return :const:`None`.
472
473
Georg Brandl116aa622007-08-15 14:28:22 +0000474.. function:: dup(fd)
475
Benjamin Petersond91203b2010-05-06 23:20:40 +0000476 Return a duplicate of file descriptor *fd*.
477
478 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000479
480
481.. function:: dup2(fd, fd2)
482
483 Duplicate file descriptor *fd* to *fd2*, closing the latter first if necessary.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000484
Georg Brandlc575c902008-09-13 17:46:05 +0000485 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000486
487
Christian Heimes4e30a842007-11-30 22:12:06 +0000488.. function:: fchmod(fd, mode)
489
490 Change the mode of the file given by *fd* to the numeric *mode*. See the docs
Benjamin Petersond91203b2010-05-06 23:20:40 +0000491 for :func:`chmod` for possible values of *mode*.
492
493 Availability: Unix.
Christian Heimes4e30a842007-11-30 22:12:06 +0000494
495
496.. function:: fchown(fd, uid, gid)
497
498 Change the owner and group id of the file given by *fd* to the numeric *uid*
499 and *gid*. To leave one of the ids unchanged, set it to -1.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000500
Christian Heimes4e30a842007-11-30 22:12:06 +0000501 Availability: Unix.
502
503
Georg Brandl116aa622007-08-15 14:28:22 +0000504.. function:: fdatasync(fd)
505
506 Force write of file with filedescriptor *fd* to disk. Does not force update of
Benjamin Petersond91203b2010-05-06 23:20:40 +0000507 metadata.
508
509 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000510
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000511 .. note::
512 This function is not available on MacOS.
513
Georg Brandl116aa622007-08-15 14:28:22 +0000514
515.. function:: fpathconf(fd, name)
516
517 Return system configuration information relevant to an open file. *name*
518 specifies the configuration value to retrieve; it may be a string which is the
519 name of a defined system value; these names are specified in a number of
520 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
521 additional names as well. The names known to the host operating system are
522 given in the ``pathconf_names`` dictionary. For configuration variables not
523 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +0000524
525 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
526 specific value for *name* is not supported by the host system, even if it is
527 included in ``pathconf_names``, an :exc:`OSError` is raised with
528 :const:`errno.EINVAL` for the error number.
529
Benjamin Petersond91203b2010-05-06 23:20:40 +0000530 Availability: Unix.
531
Georg Brandl116aa622007-08-15 14:28:22 +0000532
533.. function:: fstat(fd)
534
Benjamin Petersond91203b2010-05-06 23:20:40 +0000535 Return status for file descriptor *fd*, like :func:`stat`.
536
537 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000538
539
540.. function:: fstatvfs(fd)
541
542 Return information about the filesystem containing the file associated with file
Benjamin Petersond91203b2010-05-06 23:20:40 +0000543 descriptor *fd*, like :func:`statvfs`.
544
545 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000546
547
548.. function:: fsync(fd)
549
550 Force write of file with filedescriptor *fd* to disk. On Unix, this calls the
551 native :cfunc:`fsync` function; on Windows, the MS :cfunc:`_commit` function.
552
553 If you're starting with a Python file object *f*, first do ``f.flush()``, and
554 then do ``os.fsync(f.fileno())``, to ensure that all internal buffers associated
Benjamin Petersond91203b2010-05-06 23:20:40 +0000555 with *f* are written to disk.
556
557 Availability: Unix, and Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000558
559
560.. function:: ftruncate(fd, length)
561
562 Truncate the file corresponding to file descriptor *fd*, so that it is at most
Benjamin Petersond91203b2010-05-06 23:20:40 +0000563 *length* bytes in size.
564
565 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000566
567
568.. function:: isatty(fd)
569
570 Return ``True`` if the file descriptor *fd* is open and connected to a
Benjamin Petersond91203b2010-05-06 23:20:40 +0000571 tty(-like) device, else ``False``.
572
573 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000574
575
576.. function:: lseek(fd, pos, how)
577
Christian Heimesfaf2f632008-01-06 16:59:19 +0000578 Set the current position of file descriptor *fd* to position *pos*, modified
579 by *how*: :const:`SEEK_SET` or ``0`` to set the position relative to the
580 beginning of the file; :const:`SEEK_CUR` or ``1`` to set it relative to the
581 current position; :const:`os.SEEK_END` or ``2`` to set it relative to the end of
Benjamin Petersond91203b2010-05-06 23:20:40 +0000582 the file.
583
584 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000585
586
587.. function:: open(file, flags[, mode])
588
Georg Brandlf4a41232008-05-26 17:55:52 +0000589 Open the file *file* and set various flags according to *flags* and possibly
590 its mode according to *mode*. The default *mode* is ``0o777`` (octal), and
591 the current umask value is first masked out. Return the file descriptor for
Benjamin Petersond91203b2010-05-06 23:20:40 +0000592 the newly opened file.
Georg Brandl116aa622007-08-15 14:28:22 +0000593
594 For a description of the flag and mode values, see the C run-time documentation;
595 flag constants (like :const:`O_RDONLY` and :const:`O_WRONLY`) are defined in
596 this module too (see below).
597
Benjamin Petersond91203b2010-05-06 23:20:40 +0000598 Availability: Unix, Windows.
599
Georg Brandl116aa622007-08-15 14:28:22 +0000600 .. note::
601
Georg Brandlc5605df2009-08-13 08:26:44 +0000602 This function is intended for low-level I/O. For normal usage, use the
603 built-in function :func:`open`, which returns a "file object" with
Benjamin Petersond91203b2010-05-06 23:20:40 +0000604 :meth:`~file.read` and :meth:`~file.wprite` methods (and many more). To
Georg Brandlc5605df2009-08-13 08:26:44 +0000605 wrap a file descriptor in a "file object", use :func:`fdopen`.
Georg Brandl116aa622007-08-15 14:28:22 +0000606
607
608.. function:: openpty()
609
610 .. index:: module: pty
611
612 Open a new pseudo-terminal pair. Return a pair of file descriptors ``(master,
613 slave)`` for the pty and the tty, respectively. For a (slightly) more portable
Benjamin Petersond91203b2010-05-06 23:20:40 +0000614 approach, use the :mod:`pty` module.
615
616 Availability: some flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000617
618
619.. function:: pipe()
620
621 Create a pipe. Return a pair of file descriptors ``(r, w)`` usable for reading
Benjamin Petersond91203b2010-05-06 23:20:40 +0000622 and writing, respectively.
623
624 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000625
626
627.. function:: read(fd, n)
628
Georg Brandlc5605df2009-08-13 08:26:44 +0000629 Read at most *n* bytes from file descriptor *fd*. Return a bytestring containing the
Georg Brandl116aa622007-08-15 14:28:22 +0000630 bytes read. If the end of the file referred to by *fd* has been reached, an
Benjamin Petersond91203b2010-05-06 23:20:40 +0000631 empty bytes object is returned.
632
633 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000634
635 .. note::
636
637 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000638 descriptor as returned by :func:`os.open` or :func:`pipe`. To read a "file object"
Georg Brandl116aa622007-08-15 14:28:22 +0000639 returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000640 :func:`fdopen`, or :data:`sys.stdin`, use its :meth:`~file.read` or
641 :meth:`~file.readline` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000642
643
644.. function:: tcgetpgrp(fd)
645
646 Return the process group associated with the terminal given by *fd* (an open
Benjamin Petersond91203b2010-05-06 23:20:40 +0000647 file descriptor as returned by :func:`os.open`).
648
649 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000650
651
652.. function:: tcsetpgrp(fd, pg)
653
654 Set the process group associated with the terminal given by *fd* (an open file
Benjamin Petersond91203b2010-05-06 23:20:40 +0000655 descriptor as returned by :func:`os.open`) to *pg*.
656
657 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000658
659
660.. function:: ttyname(fd)
661
662 Return a string which specifies the terminal device associated with
Georg Brandl9afde1c2007-11-01 20:32:30 +0000663 file descriptor *fd*. If *fd* is not associated with a terminal device, an
Benjamin Petersond91203b2010-05-06 23:20:40 +0000664 exception is raised.
665
666 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000667
668
669.. function:: write(fd, str)
670
Georg Brandlc5605df2009-08-13 08:26:44 +0000671 Write the bytestring in *str* to file descriptor *fd*. Return the number of
Benjamin Petersond91203b2010-05-06 23:20:40 +0000672 bytes actually written.
673
674 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000675
676 .. note::
677
678 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000679 descriptor as returned by :func:`os.open` or :func:`pipe`. To write a "file
Georg Brandl116aa622007-08-15 14:28:22 +0000680 object" returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000681 :func:`fdopen`, or :data:`sys.stdout` or :data:`sys.stderr`, use its
682 :meth:`~file.write` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000683
Georg Brandlaf265f42008-12-07 15:06:20 +0000684The following constants are options for the *flags* parameter to the
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000685:func:`~os.open` function. They can be combined using the bitwise OR operator
Georg Brandlaf265f42008-12-07 15:06:20 +0000686``|``. Some of them are not available on all platforms. For descriptions of
687their availability and use, consult the :manpage:`open(2)` manual page on Unix
Doug Hellmann14214262009-09-21 12:16:43 +0000688or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000689
690
691.. data:: O_RDONLY
692 O_WRONLY
693 O_RDWR
694 O_APPEND
695 O_CREAT
696 O_EXCL
697 O_TRUNC
698
Georg Brandlaf265f42008-12-07 15:06:20 +0000699 These constants are available on Unix and Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000700
701
702.. data:: O_DSYNC
703 O_RSYNC
704 O_SYNC
705 O_NDELAY
706 O_NONBLOCK
707 O_NOCTTY
708 O_SHLOCK
709 O_EXLOCK
710
Georg Brandlaf265f42008-12-07 15:06:20 +0000711 These constants are only available on Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000712
713
714.. data:: O_BINARY
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000715 O_NOINHERIT
Georg Brandl116aa622007-08-15 14:28:22 +0000716 O_SHORT_LIVED
717 O_TEMPORARY
718 O_RANDOM
719 O_SEQUENTIAL
720 O_TEXT
721
Georg Brandlaf265f42008-12-07 15:06:20 +0000722 These constants are only available on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000723
724
Alexandre Vassalottibee32532008-05-16 18:15:12 +0000725.. data:: O_ASYNC
726 O_DIRECT
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000727 O_DIRECTORY
728 O_NOFOLLOW
729 O_NOATIME
730
Georg Brandlaf265f42008-12-07 15:06:20 +0000731 These constants are GNU extensions and not present if they are not defined by
732 the C library.
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000733
734
Georg Brandl116aa622007-08-15 14:28:22 +0000735.. data:: SEEK_SET
736 SEEK_CUR
737 SEEK_END
738
739 Parameters to the :func:`lseek` function. Their values are 0, 1, and 2,
Georg Brandlc575c902008-09-13 17:46:05 +0000740 respectively. Availability: Windows, Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000741
Georg Brandl116aa622007-08-15 14:28:22 +0000742
743.. _os-file-dir:
744
745Files and Directories
746---------------------
747
Georg Brandl116aa622007-08-15 14:28:22 +0000748.. function:: access(path, mode)
749
750 Use the real uid/gid to test for access to *path*. Note that most operations
751 will use the effective uid/gid, therefore this routine can be used in a
752 suid/sgid environment to test if the invoking user has the specified access to
753 *path*. *mode* should be :const:`F_OK` to test the existence of *path*, or it
754 can be the inclusive OR of one or more of :const:`R_OK`, :const:`W_OK`, and
755 :const:`X_OK` to test permissions. Return :const:`True` if access is allowed,
756 :const:`False` if not. See the Unix man page :manpage:`access(2)` for more
Benjamin Petersond91203b2010-05-06 23:20:40 +0000757 information.
758
759 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000760
761 .. note::
762
Georg Brandlc5605df2009-08-13 08:26:44 +0000763 Using :func:`access` to check if a user is authorized to e.g. open a file
764 before actually doing so using :func:`open` creates a security hole,
765 because the user might exploit the short time interval between checking
766 and opening the file to manipulate it.
Georg Brandl116aa622007-08-15 14:28:22 +0000767
768 .. note::
769
770 I/O operations may fail even when :func:`access` indicates that they would
771 succeed, particularly for operations on network filesystems which may have
772 permissions semantics beyond the usual POSIX permission-bit model.
773
774
775.. data:: F_OK
776
777 Value to pass as the *mode* parameter of :func:`access` to test the existence of
778 *path*.
779
780
781.. data:: R_OK
782
783 Value to include in the *mode* parameter of :func:`access` to test the
784 readability of *path*.
785
786
787.. data:: W_OK
788
789 Value to include in the *mode* parameter of :func:`access` to test the
790 writability of *path*.
791
792
793.. data:: X_OK
794
795 Value to include in the *mode* parameter of :func:`access` to determine if
796 *path* can be executed.
797
798
799.. function:: chdir(path)
800
801 .. index:: single: directory; changing
802
Benjamin Petersond91203b2010-05-06 23:20:40 +0000803 Change the current working directory to *path*.
804
805 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000806
807
808.. function:: fchdir(fd)
809
810 Change the current working directory to the directory represented by the file
811 descriptor *fd*. The descriptor must refer to an opened directory, not an open
Benjamin Petersond91203b2010-05-06 23:20:40 +0000812 file.
813
814 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000815
Georg Brandl116aa622007-08-15 14:28:22 +0000816
817.. function:: getcwd()
818
Martin v. Löwis011e8422009-05-05 04:43:17 +0000819 Return a string representing the current working directory.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000820
Martin v. Löwis011e8422009-05-05 04:43:17 +0000821 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000822
Benjamin Petersond91203b2010-05-06 23:20:40 +0000823
Martin v. Löwisa731b992008-10-07 06:36:31 +0000824.. function:: getcwdb()
Georg Brandl116aa622007-08-15 14:28:22 +0000825
Georg Brandl76e55382008-10-08 16:34:57 +0000826 Return a bytestring representing the current working directory.
Benjamin Petersond91203b2010-05-06 23:20:40 +0000827
Georg Brandlc575c902008-09-13 17:46:05 +0000828 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000829
Georg Brandl116aa622007-08-15 14:28:22 +0000830
831.. function:: chflags(path, flags)
832
833 Set the flags of *path* to the numeric *flags*. *flags* may take a combination
834 (bitwise OR) of the following values (as defined in the :mod:`stat` module):
835
836 * ``UF_NODUMP``
837 * ``UF_IMMUTABLE``
838 * ``UF_APPEND``
839 * ``UF_OPAQUE``
840 * ``UF_NOUNLINK``
841 * ``SF_ARCHIVED``
842 * ``SF_IMMUTABLE``
843 * ``SF_APPEND``
844 * ``SF_NOUNLINK``
845 * ``SF_SNAPSHOT``
846
Georg Brandlc575c902008-09-13 17:46:05 +0000847 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000848
Georg Brandl116aa622007-08-15 14:28:22 +0000849
850.. function:: chroot(path)
851
852 Change the root directory of the current process to *path*. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +0000853 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000854
Georg Brandl116aa622007-08-15 14:28:22 +0000855
856.. function:: chmod(path, mode)
857
858 Change the mode of *path* to the numeric *mode*. *mode* may take one of the
Christian Heimesfaf2f632008-01-06 16:59:19 +0000859 following values (as defined in the :mod:`stat` module) or bitwise ORed
Georg Brandl116aa622007-08-15 14:28:22 +0000860 combinations of them:
861
R. David Murrayba426142009-07-21 14:29:59 +0000862 * :data:`stat.S_ISUID`
863 * :data:`stat.S_ISGID`
864 * :data:`stat.S_ENFMT`
865 * :data:`stat.S_ISVTX`
866 * :data:`stat.S_IREAD`
867 * :data:`stat.S_IWRITE`
868 * :data:`stat.S_IEXEC`
869 * :data:`stat.S_IRWXU`
870 * :data:`stat.S_IRUSR`
871 * :data:`stat.S_IWUSR`
872 * :data:`stat.S_IXUSR`
873 * :data:`stat.S_IRWXG`
874 * :data:`stat.S_IRGRP`
875 * :data:`stat.S_IWGRP`
876 * :data:`stat.S_IXGRP`
877 * :data:`stat.S_IRWXO`
878 * :data:`stat.S_IROTH`
879 * :data:`stat.S_IWOTH`
880 * :data:`stat.S_IXOTH`
Georg Brandl116aa622007-08-15 14:28:22 +0000881
Georg Brandlc575c902008-09-13 17:46:05 +0000882 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000883
884 .. note::
885
886 Although Windows supports :func:`chmod`, you can only set the file's read-only
887 flag with it (via the ``stat.S_IWRITE`` and ``stat.S_IREAD``
888 constants or a corresponding integer value). All other bits are
889 ignored.
890
891
892.. function:: chown(path, uid, gid)
893
894 Change the owner and group id of *path* to the numeric *uid* and *gid*. To leave
Benjamin Petersond91203b2010-05-06 23:20:40 +0000895 one of the ids unchanged, set it to -1.
896
897 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000898
899
900.. function:: lchflags(path, flags)
901
902 Set the flags of *path* to the numeric *flags*, like :func:`chflags`, but do not
Benjamin Petersond91203b2010-05-06 23:20:40 +0000903 follow symbolic links.
904
905 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000906
Georg Brandl116aa622007-08-15 14:28:22 +0000907
Christian Heimes93852662007-12-01 12:22:32 +0000908.. function:: lchmod(path, mode)
909
910 Change the mode of *path* to the numeric *mode*. If path is a symlink, this
911 affects the symlink rather than the target. See the docs for :func:`chmod`
Benjamin Petersond91203b2010-05-06 23:20:40 +0000912 for possible values of *mode*.
913
914 Availability: Unix.
Christian Heimes93852662007-12-01 12:22:32 +0000915
Christian Heimes93852662007-12-01 12:22:32 +0000916
Georg Brandl116aa622007-08-15 14:28:22 +0000917.. function:: lchown(path, uid, gid)
918
Christian Heimesfaf2f632008-01-06 16:59:19 +0000919 Change the owner and group id of *path* to the numeric *uid* and *gid*. This
Benjamin Petersond91203b2010-05-06 23:20:40 +0000920 function will not follow symbolic links.
921
922 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000923
Georg Brandl116aa622007-08-15 14:28:22 +0000924
Benjamin Peterson5879d412009-03-30 14:51:56 +0000925.. function:: link(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +0000926
Benjamin Petersond91203b2010-05-06 23:20:40 +0000927 Create a hard link pointing to *source* named *link_name*.
928
929 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000930
931
932.. function:: listdir(path)
933
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000934 Return a list containing the names of the entries in the directory given by
935 *path*. The list is in arbitrary order. It does not include the special
936 entries ``'.'`` and ``'..'`` even if they are present in the directory.
Georg Brandl116aa622007-08-15 14:28:22 +0000937
Martin v. Löwis011e8422009-05-05 04:43:17 +0000938 This function can be called with a bytes or string argument, and returns
939 filenames of the same datatype.
Georg Brandl116aa622007-08-15 14:28:22 +0000940
Benjamin Petersond91203b2010-05-06 23:20:40 +0000941 Availability: Unix, Windows.
942
Georg Brandl116aa622007-08-15 14:28:22 +0000943
944.. function:: lstat(path)
945
Guido van Rossum2cc30da2007-11-02 23:46:40 +0000946 Like :func:`stat`, but do not follow symbolic links. This is an alias for
947 :func:`stat` on platforms that do not support symbolic links, such as
948 Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000949
950
951.. function:: mkfifo(path[, mode])
952
Georg Brandlf4a41232008-05-26 17:55:52 +0000953 Create a FIFO (a named pipe) named *path* with numeric mode *mode*. The
954 default *mode* is ``0o666`` (octal). The current umask value is first masked
Benjamin Petersond91203b2010-05-06 23:20:40 +0000955 out from the mode.
Georg Brandl116aa622007-08-15 14:28:22 +0000956
957 FIFOs are pipes that can be accessed like regular files. FIFOs exist until they
958 are deleted (for example with :func:`os.unlink`). Generally, FIFOs are used as
959 rendezvous between "client" and "server" type processes: the server opens the
960 FIFO for reading, and the client opens it for writing. Note that :func:`mkfifo`
961 doesn't open the FIFO --- it just creates the rendezvous point.
962
Benjamin Petersond91203b2010-05-06 23:20:40 +0000963 Availability: Unix.
964
Georg Brandl116aa622007-08-15 14:28:22 +0000965
Georg Brandlf4a41232008-05-26 17:55:52 +0000966.. function:: mknod(filename[, mode=0o600, device])
Georg Brandl116aa622007-08-15 14:28:22 +0000967
968 Create a filesystem node (file, device special file or named pipe) named
969 *filename*. *mode* specifies both the permissions to use and the type of node to
970 be created, being combined (bitwise OR) with one of ``stat.S_IFREG``,
971 ``stat.S_IFCHR``, ``stat.S_IFBLK``,
972 and ``stat.S_IFIFO`` (those constants are available in :mod:`stat`).
973 For ``stat.S_IFCHR`` and
974 ``stat.S_IFBLK``, *device* defines the newly created device special file (probably using
975 :func:`os.makedev`), otherwise it is ignored.
976
Georg Brandl116aa622007-08-15 14:28:22 +0000977
978.. function:: major(device)
979
Christian Heimesfaf2f632008-01-06 16:59:19 +0000980 Extract the device major number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000981 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
982
Georg Brandl116aa622007-08-15 14:28:22 +0000983
984.. function:: minor(device)
985
Christian Heimesfaf2f632008-01-06 16:59:19 +0000986 Extract the device minor number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000987 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
988
Georg Brandl116aa622007-08-15 14:28:22 +0000989
990.. function:: makedev(major, minor)
991
Christian Heimesfaf2f632008-01-06 16:59:19 +0000992 Compose a raw device number from the major and minor device numbers.
Georg Brandl116aa622007-08-15 14:28:22 +0000993
Georg Brandl116aa622007-08-15 14:28:22 +0000994
995.. function:: mkdir(path[, mode])
996
Georg Brandlf4a41232008-05-26 17:55:52 +0000997 Create a directory named *path* with numeric mode *mode*. The default *mode*
998 is ``0o777`` (octal). On some systems, *mode* is ignored. Where it is used,
Benjamin Petersond91203b2010-05-06 23:20:40 +0000999 the current umask value is first masked out.
Georg Brandl116aa622007-08-15 14:28:22 +00001000
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001001 It is also possible to create temporary directories; see the
1002 :mod:`tempfile` module's :func:`tempfile.mkdtemp` function.
1003
Benjamin Petersond91203b2010-05-06 23:20:40 +00001004 Availability: Unix, Windows.
1005
Georg Brandl116aa622007-08-15 14:28:22 +00001006
1007.. function:: makedirs(path[, mode])
1008
1009 .. index::
1010 single: directory; creating
1011 single: UNC paths; and os.makedirs()
1012
1013 Recursive directory creation function. Like :func:`mkdir`, but makes all
Georg Brandlf4a41232008-05-26 17:55:52 +00001014 intermediate-level directories needed to contain the leaf directory. Throws
1015 an :exc:`error` exception if the leaf directory already exists or cannot be
1016 created. The default *mode* is ``0o777`` (octal). On some systems, *mode*
1017 is ignored. Where it is used, the current umask value is first masked out.
Georg Brandl116aa622007-08-15 14:28:22 +00001018
1019 .. note::
1020
1021 :func:`makedirs` will become confused if the path elements to create include
Christian Heimesfaf2f632008-01-06 16:59:19 +00001022 :data:`os.pardir`.
Georg Brandl116aa622007-08-15 14:28:22 +00001023
Georg Brandl55ac8f02007-09-01 13:51:09 +00001024 This function handles UNC paths correctly.
Georg Brandl116aa622007-08-15 14:28:22 +00001025
1026
1027.. function:: pathconf(path, name)
1028
1029 Return system configuration information relevant to a named file. *name*
1030 specifies the configuration value to retrieve; it may be a string which is the
1031 name of a defined system value; these names are specified in a number of
1032 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
1033 additional names as well. The names known to the host operating system are
1034 given in the ``pathconf_names`` dictionary. For configuration variables not
1035 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +00001036
1037 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1038 specific value for *name* is not supported by the host system, even if it is
1039 included in ``pathconf_names``, an :exc:`OSError` is raised with
1040 :const:`errno.EINVAL` for the error number.
1041
Benjamin Petersond91203b2010-05-06 23:20:40 +00001042 Availability: Unix.
1043
Georg Brandl116aa622007-08-15 14:28:22 +00001044
1045.. data:: pathconf_names
1046
1047 Dictionary mapping names accepted by :func:`pathconf` and :func:`fpathconf` to
1048 the integer values defined for those names by the host operating system. This
1049 can be used to determine the set of names known to the system. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001050 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001051
1052
1053.. function:: readlink(path)
1054
1055 Return a string representing the path to which the symbolic link points. The
1056 result may be either an absolute or relative pathname; if it is relative, it may
1057 be converted to an absolute pathname using ``os.path.join(os.path.dirname(path),
1058 result)``.
1059
Georg Brandl76e55382008-10-08 16:34:57 +00001060 If the *path* is a string object, the result will also be a string object,
1061 and the call may raise an UnicodeDecodeError. If the *path* is a bytes
1062 object, the result will be a bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +00001063
Georg Brandlc575c902008-09-13 17:46:05 +00001064 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001065
1066
1067.. function:: remove(path)
1068
Georg Brandl7baf6252009-09-01 08:13:16 +00001069 Remove (delete) the file *path*. If *path* is a directory, :exc:`OSError` is
1070 raised; see :func:`rmdir` below to remove a directory. This is identical to
1071 the :func:`unlink` function documented below. On Windows, attempting to
1072 remove a file that is in use causes an exception to be raised; on Unix, the
1073 directory entry is removed but the storage allocated to the file is not made
Benjamin Petersond91203b2010-05-06 23:20:40 +00001074 available until the original file is no longer in use.
1075
1076 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001077
1078
1079.. function:: removedirs(path)
1080
1081 .. index:: single: directory; deleting
1082
Christian Heimesfaf2f632008-01-06 16:59:19 +00001083 Remove directories recursively. Works like :func:`rmdir` except that, if the
Georg Brandl116aa622007-08-15 14:28:22 +00001084 leaf directory is successfully removed, :func:`removedirs` tries to
1085 successively remove every parent directory mentioned in *path* until an error
1086 is raised (which is ignored, because it generally means that a parent directory
1087 is not empty). For example, ``os.removedirs('foo/bar/baz')`` will first remove
1088 the directory ``'foo/bar/baz'``, and then remove ``'foo/bar'`` and ``'foo'`` if
1089 they are empty. Raises :exc:`OSError` if the leaf directory could not be
1090 successfully removed.
1091
Georg Brandl116aa622007-08-15 14:28:22 +00001092
1093.. function:: rename(src, dst)
1094
1095 Rename the file or directory *src* to *dst*. If *dst* is a directory,
1096 :exc:`OSError` will be raised. On Unix, if *dst* exists and is a file, it will
Christian Heimesfaf2f632008-01-06 16:59:19 +00001097 be replaced silently if the user has permission. The operation may fail on some
Georg Brandl116aa622007-08-15 14:28:22 +00001098 Unix flavors if *src* and *dst* are on different filesystems. If successful,
1099 the renaming will be an atomic operation (this is a POSIX requirement). On
1100 Windows, if *dst* already exists, :exc:`OSError` will be raised even if it is a
1101 file; there may be no way to implement an atomic rename when *dst* names an
Benjamin Petersond91203b2010-05-06 23:20:40 +00001102 existing file.
1103
1104 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001105
1106
1107.. function:: renames(old, new)
1108
1109 Recursive directory or file renaming function. Works like :func:`rename`, except
1110 creation of any intermediate directories needed to make the new pathname good is
1111 attempted first. After the rename, directories corresponding to rightmost path
1112 segments of the old name will be pruned away using :func:`removedirs`.
1113
Georg Brandl116aa622007-08-15 14:28:22 +00001114 .. note::
1115
1116 This function can fail with the new directory structure made if you lack
1117 permissions needed to remove the leaf directory or file.
1118
1119
1120.. function:: rmdir(path)
1121
Georg Brandl7baf6252009-09-01 08:13:16 +00001122 Remove (delete) the directory *path*. Only works when the directory is
1123 empty, otherwise, :exc:`OSError` is raised. In order to remove whole
Benjamin Petersond91203b2010-05-06 23:20:40 +00001124 directory trees, :func:`shutil.rmtree` can be used.
1125
1126 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001127
1128
1129.. function:: stat(path)
1130
1131 Perform a :cfunc:`stat` system call on the given path. The return value is an
1132 object whose attributes correspond to the members of the :ctype:`stat`
1133 structure, namely: :attr:`st_mode` (protection bits), :attr:`st_ino` (inode
1134 number), :attr:`st_dev` (device), :attr:`st_nlink` (number of hard links),
Christian Heimesfaf2f632008-01-06 16:59:19 +00001135 :attr:`st_uid` (user id of owner), :attr:`st_gid` (group id of owner),
Georg Brandl116aa622007-08-15 14:28:22 +00001136 :attr:`st_size` (size of file, in bytes), :attr:`st_atime` (time of most recent
1137 access), :attr:`st_mtime` (time of most recent content modification),
1138 :attr:`st_ctime` (platform dependent; time of most recent metadata change on
1139 Unix, or the time of creation on Windows)::
1140
1141 >>> import os
1142 >>> statinfo = os.stat('somefile.txt')
1143 >>> statinfo
Ezio Melotti9ce52612010-01-16 14:52:34 +00001144 (33188, 422511, 769, 1, 1032, 100, 926, 1105022698,1105022732, 1105022732)
Georg Brandl116aa622007-08-15 14:28:22 +00001145 >>> statinfo.st_size
Ezio Melotti9ce52612010-01-16 14:52:34 +00001146 926
Georg Brandl116aa622007-08-15 14:28:22 +00001147 >>>
1148
Georg Brandl116aa622007-08-15 14:28:22 +00001149
1150 On some Unix systems (such as Linux), the following attributes may also be
1151 available: :attr:`st_blocks` (number of blocks allocated for file),
1152 :attr:`st_blksize` (filesystem blocksize), :attr:`st_rdev` (type of device if an
1153 inode device). :attr:`st_flags` (user defined flags for file).
1154
1155 On other Unix systems (such as FreeBSD), the following attributes may be
1156 available (but may be only filled out if root tries to use them): :attr:`st_gen`
1157 (file generation number), :attr:`st_birthtime` (time of file creation).
1158
1159 On Mac OS systems, the following attributes may also be available:
1160 :attr:`st_rsize`, :attr:`st_creator`, :attr:`st_type`.
1161
Georg Brandl116aa622007-08-15 14:28:22 +00001162 .. index:: module: stat
1163
1164 For backward compatibility, the return value of :func:`stat` is also accessible
1165 as a tuple of at least 10 integers giving the most important (and portable)
1166 members of the :ctype:`stat` structure, in the order :attr:`st_mode`,
1167 :attr:`st_ino`, :attr:`st_dev`, :attr:`st_nlink`, :attr:`st_uid`,
1168 :attr:`st_gid`, :attr:`st_size`, :attr:`st_atime`, :attr:`st_mtime`,
1169 :attr:`st_ctime`. More items may be added at the end by some implementations.
1170 The standard module :mod:`stat` defines functions and constants that are useful
1171 for extracting information from a :ctype:`stat` structure. (On Windows, some
1172 items are filled with dummy values.)
1173
1174 .. note::
1175
1176 The exact meaning and resolution of the :attr:`st_atime`, :attr:`st_mtime`, and
1177 :attr:`st_ctime` members depends on the operating system and the file system.
1178 For example, on Windows systems using the FAT or FAT32 file systems,
1179 :attr:`st_mtime` has 2-second resolution, and :attr:`st_atime` has only 1-day
1180 resolution. See your operating system documentation for details.
1181
Georg Brandlc575c902008-09-13 17:46:05 +00001182 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001183
Georg Brandl116aa622007-08-15 14:28:22 +00001184
1185.. function:: stat_float_times([newvalue])
1186
1187 Determine whether :class:`stat_result` represents time stamps as float objects.
1188 If *newvalue* is ``True``, future calls to :func:`stat` return floats, if it is
1189 ``False``, future calls return ints. If *newvalue* is omitted, return the
1190 current setting.
1191
1192 For compatibility with older Python versions, accessing :class:`stat_result` as
1193 a tuple always returns integers.
1194
Georg Brandl55ac8f02007-09-01 13:51:09 +00001195 Python now returns float values by default. Applications which do not work
1196 correctly with floating point time stamps can use this function to restore the
1197 old behaviour.
Georg Brandl116aa622007-08-15 14:28:22 +00001198
1199 The resolution of the timestamps (that is the smallest possible fraction)
1200 depends on the system. Some systems only support second resolution; on these
1201 systems, the fraction will always be zero.
1202
1203 It is recommended that this setting is only changed at program startup time in
1204 the *__main__* module; libraries should never change this setting. If an
1205 application uses a library that works incorrectly if floating point time stamps
1206 are processed, this application should turn the feature off until the library
1207 has been corrected.
1208
1209
1210.. function:: statvfs(path)
1211
1212 Perform a :cfunc:`statvfs` system call on the given path. The return value is
1213 an object whose attributes describe the filesystem on the given path, and
1214 correspond to the members of the :ctype:`statvfs` structure, namely:
1215 :attr:`f_bsize`, :attr:`f_frsize`, :attr:`f_blocks`, :attr:`f_bfree`,
1216 :attr:`f_bavail`, :attr:`f_files`, :attr:`f_ffree`, :attr:`f_favail`,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001217 :attr:`f_flag`, :attr:`f_namemax`.
1218
1219 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001220
Georg Brandl116aa622007-08-15 14:28:22 +00001221
Benjamin Peterson5879d412009-03-30 14:51:56 +00001222.. function:: symlink(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +00001223
Benjamin Petersond91203b2010-05-06 23:20:40 +00001224 Create a symbolic link pointing to *source* named *link_name*.
1225
1226 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001227
1228
Georg Brandl116aa622007-08-15 14:28:22 +00001229.. function:: unlink(path)
1230
Georg Brandl7baf6252009-09-01 08:13:16 +00001231 Remove (delete) the file *path*. This is the same function as
1232 :func:`remove`; the :func:`unlink` name is its traditional Unix
Benjamin Petersond91203b2010-05-06 23:20:40 +00001233 name.
1234
1235 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001236
1237
1238.. function:: utime(path, times)
1239
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001240 Set the access and modified times of the file specified by *path*. If *times*
1241 is ``None``, then the file's access and modified times are set to the current
1242 time. (The effect is similar to running the Unix program :program:`touch` on
1243 the path.) Otherwise, *times* must be a 2-tuple of numbers, of the form
1244 ``(atime, mtime)`` which is used to set the access and modified times,
1245 respectively. Whether a directory can be given for *path* depends on whether
1246 the operating system implements directories as files (for example, Windows
1247 does not). Note that the exact times you set here may not be returned by a
1248 subsequent :func:`stat` call, depending on the resolution with which your
1249 operating system records access and modification times; see :func:`stat`.
Georg Brandl116aa622007-08-15 14:28:22 +00001250
Georg Brandlc575c902008-09-13 17:46:05 +00001251 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001252
1253
1254.. function:: walk(top[, topdown=True [, onerror=None[, followlinks=False]]])
1255
1256 .. index::
1257 single: directory; walking
1258 single: directory; traversal
1259
Christian Heimesfaf2f632008-01-06 16:59:19 +00001260 Generate the file names in a directory tree by walking the tree
1261 either top-down or bottom-up. For each directory in the tree rooted at directory
Georg Brandl116aa622007-08-15 14:28:22 +00001262 *top* (including *top* itself), it yields a 3-tuple ``(dirpath, dirnames,
1263 filenames)``.
1264
1265 *dirpath* is a string, the path to the directory. *dirnames* is a list of the
1266 names of the subdirectories in *dirpath* (excluding ``'.'`` and ``'..'``).
1267 *filenames* is a list of the names of the non-directory files in *dirpath*.
1268 Note that the names in the lists contain no path components. To get a full path
1269 (which begins with *top*) to a file or directory in *dirpath*, do
1270 ``os.path.join(dirpath, name)``.
1271
Christian Heimesfaf2f632008-01-06 16:59:19 +00001272 If optional argument *topdown* is ``True`` or not specified, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001273 directory is generated before the triples for any of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001274 (directories are generated top-down). If *topdown* is ``False``, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001275 directory is generated after the triples for all of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001276 (directories are generated bottom-up).
Georg Brandl116aa622007-08-15 14:28:22 +00001277
Christian Heimesfaf2f632008-01-06 16:59:19 +00001278 When *topdown* is ``True``, the caller can modify the *dirnames* list in-place
Georg Brandl116aa622007-08-15 14:28:22 +00001279 (perhaps using :keyword:`del` or slice assignment), and :func:`walk` will only
1280 recurse into the subdirectories whose names remain in *dirnames*; this can be
1281 used to prune the search, impose a specific order of visiting, or even to inform
1282 :func:`walk` about directories the caller creates or renames before it resumes
Christian Heimesfaf2f632008-01-06 16:59:19 +00001283 :func:`walk` again. Modifying *dirnames* when *topdown* is ``False`` is
Georg Brandl116aa622007-08-15 14:28:22 +00001284 ineffective, because in bottom-up mode the directories in *dirnames* are
1285 generated before *dirpath* itself is generated.
1286
Christian Heimesfaf2f632008-01-06 16:59:19 +00001287 By default errors from the :func:`listdir` call are ignored. If optional
Georg Brandl116aa622007-08-15 14:28:22 +00001288 argument *onerror* is specified, it should be a function; it will be called with
1289 one argument, an :exc:`OSError` instance. It can report the error to continue
1290 with the walk, or raise the exception to abort the walk. Note that the filename
1291 is available as the ``filename`` attribute of the exception object.
1292
1293 By default, :func:`walk` will not walk down into symbolic links that resolve to
Christian Heimesfaf2f632008-01-06 16:59:19 +00001294 directories. Set *followlinks* to ``True`` to visit directories pointed to by
Georg Brandl116aa622007-08-15 14:28:22 +00001295 symlinks, on systems that support them.
1296
Georg Brandl116aa622007-08-15 14:28:22 +00001297 .. note::
1298
Christian Heimesfaf2f632008-01-06 16:59:19 +00001299 Be aware that setting *followlinks* to ``True`` can lead to infinite recursion if a
Georg Brandl116aa622007-08-15 14:28:22 +00001300 link points to a parent directory of itself. :func:`walk` does not keep track of
1301 the directories it visited already.
1302
1303 .. note::
1304
1305 If you pass a relative pathname, don't change the current working directory
1306 between resumptions of :func:`walk`. :func:`walk` never changes the current
1307 directory, and assumes that its caller doesn't either.
1308
1309 This example displays the number of bytes taken by non-directory files in each
1310 directory under the starting directory, except that it doesn't look under any
1311 CVS subdirectory::
1312
1313 import os
1314 from os.path import join, getsize
1315 for root, dirs, files in os.walk('python/Lib/email'):
Georg Brandl6911e3c2007-09-04 07:15:32 +00001316 print(root, "consumes", end=" ")
1317 print(sum(getsize(join(root, name)) for name in files), end=" ")
1318 print("bytes in", len(files), "non-directory files")
Georg Brandl116aa622007-08-15 14:28:22 +00001319 if 'CVS' in dirs:
1320 dirs.remove('CVS') # don't visit CVS directories
1321
Christian Heimesfaf2f632008-01-06 16:59:19 +00001322 In the next example, walking the tree bottom-up is essential: :func:`rmdir`
Georg Brandl116aa622007-08-15 14:28:22 +00001323 doesn't allow deleting a directory before the directory is empty::
1324
Christian Heimesfaf2f632008-01-06 16:59:19 +00001325 # Delete everything reachable from the directory named in "top",
Georg Brandl116aa622007-08-15 14:28:22 +00001326 # assuming there are no symbolic links.
1327 # CAUTION: This is dangerous! For example, if top == '/', it
1328 # could delete all your disk files.
1329 import os
1330 for root, dirs, files in os.walk(top, topdown=False):
1331 for name in files:
1332 os.remove(os.path.join(root, name))
1333 for name in dirs:
1334 os.rmdir(os.path.join(root, name))
1335
Georg Brandl116aa622007-08-15 14:28:22 +00001336
1337.. _os-process:
1338
1339Process Management
1340------------------
1341
1342These functions may be used to create and manage processes.
1343
1344The various :func:`exec\*` functions take a list of arguments for the new
1345program loaded into the process. In each case, the first of these arguments is
1346passed to the new program as its own name rather than as an argument a user may
1347have typed on a command line. For the C programmer, this is the ``argv[0]``
1348passed to a program's :cfunc:`main`. For example, ``os.execv('/bin/echo',
1349['foo', 'bar'])`` will only print ``bar`` on standard output; ``foo`` will seem
1350to be ignored.
1351
1352
1353.. function:: abort()
1354
1355 Generate a :const:`SIGABRT` signal to the current process. On Unix, the default
1356 behavior is to produce a core dump; on Windows, the process immediately returns
1357 an exit code of ``3``. Be aware that programs which use :func:`signal.signal`
1358 to register a handler for :const:`SIGABRT` will behave differently.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001359
Georg Brandlc575c902008-09-13 17:46:05 +00001360 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001361
1362
1363.. function:: execl(path, arg0, arg1, ...)
1364 execle(path, arg0, arg1, ..., env)
1365 execlp(file, arg0, arg1, ...)
1366 execlpe(file, arg0, arg1, ..., env)
1367 execv(path, args)
1368 execve(path, args, env)
1369 execvp(file, args)
1370 execvpe(file, args, env)
1371
1372 These functions all execute a new program, replacing the current process; they
1373 do not return. On Unix, the new executable is loaded into the current process,
Christian Heimesfaf2f632008-01-06 16:59:19 +00001374 and will have the same process id as the caller. Errors will be reported as
Georg Brandl48310cd2009-01-03 21:18:54 +00001375 :exc:`OSError` exceptions.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001376
1377 The current process is replaced immediately. Open file objects and
1378 descriptors are not flushed, so if there may be data buffered
1379 on these open files, you should flush them using
1380 :func:`sys.stdout.flush` or :func:`os.fsync` before calling an
1381 :func:`exec\*` function.
Georg Brandl116aa622007-08-15 14:28:22 +00001382
Christian Heimesfaf2f632008-01-06 16:59:19 +00001383 The "l" and "v" variants of the :func:`exec\*` functions differ in how
1384 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001385 to work with if the number of parameters is fixed when the code is written; the
1386 individual parameters simply become additional parameters to the :func:`execl\*`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001387 functions. The "v" variants are good when the number of parameters is
Georg Brandl116aa622007-08-15 14:28:22 +00001388 variable, with the arguments being passed in a list or tuple as the *args*
1389 parameter. In either case, the arguments to the child process should start with
1390 the name of the command being run, but this is not enforced.
1391
Christian Heimesfaf2f632008-01-06 16:59:19 +00001392 The variants which include a "p" near the end (:func:`execlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001393 :func:`execlpe`, :func:`execvp`, and :func:`execvpe`) will use the
1394 :envvar:`PATH` environment variable to locate the program *file*. When the
1395 environment is being replaced (using one of the :func:`exec\*e` variants,
1396 discussed in the next paragraph), the new environment is used as the source of
1397 the :envvar:`PATH` variable. The other variants, :func:`execl`, :func:`execle`,
1398 :func:`execv`, and :func:`execve`, will not use the :envvar:`PATH` variable to
1399 locate the executable; *path* must contain an appropriate absolute or relative
1400 path.
1401
1402 For :func:`execle`, :func:`execlpe`, :func:`execve`, and :func:`execvpe` (note
Christian Heimesfaf2f632008-01-06 16:59:19 +00001403 that these all end in "e"), the *env* parameter must be a mapping which is
Christian Heimesa342c012008-04-20 21:01:16 +00001404 used to define the environment variables for the new process (these are used
1405 instead of the current process' environment); the functions :func:`execl`,
Georg Brandl116aa622007-08-15 14:28:22 +00001406 :func:`execlp`, :func:`execv`, and :func:`execvp` all cause the new process to
Georg Brandl48310cd2009-01-03 21:18:54 +00001407 inherit the environment of the current process.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001408
1409 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001410
1411
1412.. function:: _exit(n)
1413
1414 Exit to the system with status *n*, without calling cleanup handlers, flushing
Benjamin Petersond91203b2010-05-06 23:20:40 +00001415 stdio buffers, etc.
1416
1417 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001418
1419 .. note::
1420
1421 The standard way to exit is ``sys.exit(n)``. :func:`_exit` should normally only
1422 be used in the child process after a :func:`fork`.
1423
Christian Heimesfaf2f632008-01-06 16:59:19 +00001424The following exit codes are defined and can be used with :func:`_exit`,
Georg Brandl116aa622007-08-15 14:28:22 +00001425although they are not required. These are typically used for system programs
1426written in Python, such as a mail server's external command delivery program.
1427
1428.. note::
1429
1430 Some of these may not be available on all Unix platforms, since there is some
1431 variation. These constants are defined where they are defined by the underlying
1432 platform.
1433
1434
1435.. data:: EX_OK
1436
Benjamin Petersond91203b2010-05-06 23:20:40 +00001437 Exit code that means no error occurred.
1438
1439 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001440
Georg Brandl116aa622007-08-15 14:28:22 +00001441
1442.. data:: EX_USAGE
1443
1444 Exit code that means the command was used incorrectly, such as when the wrong
Benjamin Petersond91203b2010-05-06 23:20:40 +00001445 number of arguments are given.
1446
1447 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001448
Georg Brandl116aa622007-08-15 14:28:22 +00001449
1450.. data:: EX_DATAERR
1451
Benjamin Petersond91203b2010-05-06 23:20:40 +00001452 Exit code that means the input data was incorrect.
1453
1454 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001455
Georg Brandl116aa622007-08-15 14:28:22 +00001456
1457.. data:: EX_NOINPUT
1458
1459 Exit code that means an input file did not exist or was not readable.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001460
Georg Brandlc575c902008-09-13 17:46:05 +00001461 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001462
Georg Brandl116aa622007-08-15 14:28:22 +00001463
1464.. data:: EX_NOUSER
1465
Benjamin Petersond91203b2010-05-06 23:20:40 +00001466 Exit code that means a specified user did not exist.
1467
1468 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001469
Georg Brandl116aa622007-08-15 14:28:22 +00001470
1471.. data:: EX_NOHOST
1472
Benjamin Petersond91203b2010-05-06 23:20:40 +00001473 Exit code that means a specified host did not exist.
1474
1475 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001476
Georg Brandl116aa622007-08-15 14:28:22 +00001477
1478.. data:: EX_UNAVAILABLE
1479
Benjamin Petersond91203b2010-05-06 23:20:40 +00001480 Exit code that means that a required service is unavailable.
1481
1482 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001483
Georg Brandl116aa622007-08-15 14:28:22 +00001484
1485.. data:: EX_SOFTWARE
1486
Benjamin Petersond91203b2010-05-06 23:20:40 +00001487 Exit code that means an internal software error was detected.
1488
1489 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001490
Georg Brandl116aa622007-08-15 14:28:22 +00001491
1492.. data:: EX_OSERR
1493
1494 Exit code that means an operating system error was detected, such as the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001495 inability to fork or create a pipe.
1496
1497 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001498
Georg Brandl116aa622007-08-15 14:28:22 +00001499
1500.. data:: EX_OSFILE
1501
1502 Exit code that means some system file did not exist, could not be opened, or had
Benjamin Petersond91203b2010-05-06 23:20:40 +00001503 some other kind of error.
1504
1505 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001506
Georg Brandl116aa622007-08-15 14:28:22 +00001507
1508.. data:: EX_CANTCREAT
1509
1510 Exit code that means a user specified output file could not be created.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001511
Georg Brandlc575c902008-09-13 17:46:05 +00001512 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001513
Georg Brandl116aa622007-08-15 14:28:22 +00001514
1515.. data:: EX_IOERR
1516
1517 Exit code that means that an error occurred while doing I/O on some file.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001518
Georg Brandlc575c902008-09-13 17:46:05 +00001519 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001520
Georg Brandl116aa622007-08-15 14:28:22 +00001521
1522.. data:: EX_TEMPFAIL
1523
1524 Exit code that means a temporary failure occurred. This indicates something
1525 that may not really be an error, such as a network connection that couldn't be
Benjamin Petersond91203b2010-05-06 23:20:40 +00001526 made during a retryable operation.
1527
1528 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001529
Georg Brandl116aa622007-08-15 14:28:22 +00001530
1531.. data:: EX_PROTOCOL
1532
1533 Exit code that means that a protocol exchange was illegal, invalid, or not
Benjamin Petersond91203b2010-05-06 23:20:40 +00001534 understood.
1535
1536 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001537
Georg Brandl116aa622007-08-15 14:28:22 +00001538
1539.. data:: EX_NOPERM
1540
1541 Exit code that means that there were insufficient permissions to perform the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001542 operation (but not intended for file system problems).
1543
1544 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001545
Georg Brandl116aa622007-08-15 14:28:22 +00001546
1547.. data:: EX_CONFIG
1548
1549 Exit code that means that some kind of configuration error occurred.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001550
Georg Brandlc575c902008-09-13 17:46:05 +00001551 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001552
Georg Brandl116aa622007-08-15 14:28:22 +00001553
1554.. data:: EX_NOTFOUND
1555
Benjamin Petersond91203b2010-05-06 23:20:40 +00001556 Exit code that means something like "an entry was not found".
1557
1558 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001559
Georg Brandl116aa622007-08-15 14:28:22 +00001560
1561.. function:: fork()
1562
Christian Heimesfaf2f632008-01-06 16:59:19 +00001563 Fork a child process. Return ``0`` in the child and the child's process id in the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001564 parent. If an error occurs :exc:`OSError` is raised.
Benjamin Petersonbcd8ac32008-10-10 22:20:52 +00001565
1566 Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have
1567 known issues when using fork() from a thread.
1568
Georg Brandlc575c902008-09-13 17:46:05 +00001569 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001570
1571
1572.. function:: forkpty()
1573
1574 Fork a child process, using a new pseudo-terminal as the child's controlling
1575 terminal. Return a pair of ``(pid, fd)``, where *pid* is ``0`` in the child, the
1576 new child's process id in the parent, and *fd* is the file descriptor of the
1577 master end of the pseudo-terminal. For a more portable approach, use the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001578 :mod:`pty` module. If an error occurs :exc:`OSError` is raised.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001579
Georg Brandlc575c902008-09-13 17:46:05 +00001580 Availability: some flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001581
1582
1583.. function:: kill(pid, sig)
1584
1585 .. index::
1586 single: process; killing
1587 single: process; signalling
1588
1589 Send signal *sig* to the process *pid*. Constants for the specific signals
1590 available on the host platform are defined in the :mod:`signal` module.
Georg Brandlc575c902008-09-13 17:46:05 +00001591 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001592
1593
1594.. function:: killpg(pgid, sig)
1595
1596 .. index::
1597 single: process; killing
1598 single: process; signalling
1599
Benjamin Petersond91203b2010-05-06 23:20:40 +00001600 Send the signal *sig* to the process group *pgid*.
1601
1602 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001603
Georg Brandl116aa622007-08-15 14:28:22 +00001604
1605.. function:: nice(increment)
1606
1607 Add *increment* to the process's "niceness". Return the new niceness.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001608
Georg Brandlc575c902008-09-13 17:46:05 +00001609 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001610
1611
1612.. function:: plock(op)
1613
1614 Lock program segments into memory. The value of *op* (defined in
Benjamin Petersond91203b2010-05-06 23:20:40 +00001615 ``<sys/lock.h>``) determines which segments are locked.
1616
1617 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001618
1619
1620.. function:: popen(...)
1621 :noindex:
1622
1623 Run child processes, returning opened pipes for communications. These functions
1624 are described in section :ref:`os-newstreams`.
1625
1626
1627.. function:: spawnl(mode, path, ...)
1628 spawnle(mode, path, ..., env)
1629 spawnlp(mode, file, ...)
1630 spawnlpe(mode, file, ..., env)
1631 spawnv(mode, path, args)
1632 spawnve(mode, path, args, env)
1633 spawnvp(mode, file, args)
1634 spawnvpe(mode, file, args, env)
1635
1636 Execute the program *path* in a new process.
1637
1638 (Note that the :mod:`subprocess` module provides more powerful facilities for
1639 spawning new processes and retrieving their results; using that module is
Benjamin Peterson87c8d872009-06-11 22:54:11 +00001640 preferable to using these functions. Check especially the
1641 :ref:`subprocess-replacements` section.)
Georg Brandl116aa622007-08-15 14:28:22 +00001642
Christian Heimesfaf2f632008-01-06 16:59:19 +00001643 If *mode* is :const:`P_NOWAIT`, this function returns the process id of the new
Georg Brandl116aa622007-08-15 14:28:22 +00001644 process; if *mode* is :const:`P_WAIT`, returns the process's exit code if it
1645 exits normally, or ``-signal``, where *signal* is the signal that killed the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001646 process. On Windows, the process id will actually be the process handle, so can
Georg Brandl116aa622007-08-15 14:28:22 +00001647 be used with the :func:`waitpid` function.
1648
Christian Heimesfaf2f632008-01-06 16:59:19 +00001649 The "l" and "v" variants of the :func:`spawn\*` functions differ in how
1650 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001651 to work with if the number of parameters is fixed when the code is written; the
1652 individual parameters simply become additional parameters to the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001653 :func:`spawnl\*` functions. The "v" variants are good when the number of
Georg Brandl116aa622007-08-15 14:28:22 +00001654 parameters is variable, with the arguments being passed in a list or tuple as
1655 the *args* parameter. In either case, the arguments to the child process must
1656 start with the name of the command being run.
1657
Christian Heimesfaf2f632008-01-06 16:59:19 +00001658 The variants which include a second "p" near the end (:func:`spawnlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001659 :func:`spawnlpe`, :func:`spawnvp`, and :func:`spawnvpe`) will use the
1660 :envvar:`PATH` environment variable to locate the program *file*. When the
1661 environment is being replaced (using one of the :func:`spawn\*e` variants,
1662 discussed in the next paragraph), the new environment is used as the source of
1663 the :envvar:`PATH` variable. The other variants, :func:`spawnl`,
1664 :func:`spawnle`, :func:`spawnv`, and :func:`spawnve`, will not use the
1665 :envvar:`PATH` variable to locate the executable; *path* must contain an
1666 appropriate absolute or relative path.
1667
1668 For :func:`spawnle`, :func:`spawnlpe`, :func:`spawnve`, and :func:`spawnvpe`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001669 (note that these all end in "e"), the *env* parameter must be a mapping
Christian Heimesa342c012008-04-20 21:01:16 +00001670 which is used to define the environment variables for the new process (they are
1671 used instead of the current process' environment); the functions
Georg Brandl116aa622007-08-15 14:28:22 +00001672 :func:`spawnl`, :func:`spawnlp`, :func:`spawnv`, and :func:`spawnvp` all cause
Benjamin Petersond23f8222009-04-05 19:13:16 +00001673 the new process to inherit the environment of the current process. Note that
1674 keys and values in the *env* dictionary must be strings; invalid keys or
1675 values will cause the function to fail, with a return value of ``127``.
Georg Brandl116aa622007-08-15 14:28:22 +00001676
1677 As an example, the following calls to :func:`spawnlp` and :func:`spawnvpe` are
1678 equivalent::
1679
1680 import os
1681 os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1682
1683 L = ['cp', 'index.html', '/dev/null']
1684 os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1685
1686 Availability: Unix, Windows. :func:`spawnlp`, :func:`spawnlpe`, :func:`spawnvp`
1687 and :func:`spawnvpe` are not available on Windows.
1688
Georg Brandl116aa622007-08-15 14:28:22 +00001689
1690.. data:: P_NOWAIT
1691 P_NOWAITO
1692
1693 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1694 functions. If either of these values is given, the :func:`spawn\*` functions
Christian Heimesfaf2f632008-01-06 16:59:19 +00001695 will return as soon as the new process has been created, with the process id as
Benjamin Petersond91203b2010-05-06 23:20:40 +00001696 the return value.
1697
1698 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001699
Georg Brandl116aa622007-08-15 14:28:22 +00001700
1701.. data:: P_WAIT
1702
1703 Possible value for the *mode* parameter to the :func:`spawn\*` family of
1704 functions. If this is given as *mode*, the :func:`spawn\*` functions will not
1705 return until the new process has run to completion and will return the exit code
1706 of the process the run is successful, or ``-signal`` if a signal kills the
Benjamin Petersond91203b2010-05-06 23:20:40 +00001707 process.
1708
1709 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001710
Georg Brandl116aa622007-08-15 14:28:22 +00001711
1712.. data:: P_DETACH
1713 P_OVERLAY
1714
1715 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1716 functions. These are less portable than those listed above. :const:`P_DETACH`
1717 is similar to :const:`P_NOWAIT`, but the new process is detached from the
1718 console of the calling process. If :const:`P_OVERLAY` is used, the current
1719 process will be replaced; the :func:`spawn\*` function will not return.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001720
Georg Brandl116aa622007-08-15 14:28:22 +00001721 Availability: Windows.
1722
Georg Brandl116aa622007-08-15 14:28:22 +00001723
1724.. function:: startfile(path[, operation])
1725
1726 Start a file with its associated application.
1727
1728 When *operation* is not specified or ``'open'``, this acts like double-clicking
1729 the file in Windows Explorer, or giving the file name as an argument to the
1730 :program:`start` command from the interactive command shell: the file is opened
1731 with whatever application (if any) its extension is associated.
1732
1733 When another *operation* is given, it must be a "command verb" that specifies
1734 what should be done with the file. Common verbs documented by Microsoft are
1735 ``'print'`` and ``'edit'`` (to be used on files) as well as ``'explore'`` and
1736 ``'find'`` (to be used on directories).
1737
1738 :func:`startfile` returns as soon as the associated application is launched.
1739 There is no option to wait for the application to close, and no way to retrieve
1740 the application's exit status. The *path* parameter is relative to the current
1741 directory. If you want to use an absolute path, make sure the first character
1742 is not a slash (``'/'``); the underlying Win32 :cfunc:`ShellExecute` function
1743 doesn't work if it is. Use the :func:`os.path.normpath` function to ensure that
Benjamin Petersond91203b2010-05-06 23:20:40 +00001744 the path is properly encoded for Win32.
1745
1746 Availability: Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001747
Georg Brandl116aa622007-08-15 14:28:22 +00001748
1749.. function:: system(command)
1750
1751 Execute the command (a string) in a subshell. This is implemented by calling
Georg Brandl628e6f92009-10-27 20:24:45 +00001752 the Standard C function :cfunc:`system`, and has the same limitations.
1753 Changes to :data:`sys.stdin`, etc. are not reflected in the environment of the
1754 executed command.
Georg Brandl116aa622007-08-15 14:28:22 +00001755
1756 On Unix, the return value is the exit status of the process encoded in the
1757 format specified for :func:`wait`. Note that POSIX does not specify the meaning
1758 of the return value of the C :cfunc:`system` function, so the return value of
1759 the Python function is system-dependent.
1760
1761 On Windows, the return value is that returned by the system shell after running
1762 *command*, given by the Windows environment variable :envvar:`COMSPEC`: on
1763 :program:`command.com` systems (Windows 95, 98 and ME) this is always ``0``; on
1764 :program:`cmd.exe` systems (Windows NT, 2000 and XP) this is the exit status of
1765 the command run; on systems using a non-native shell, consult your shell
1766 documentation.
1767
Georg Brandl116aa622007-08-15 14:28:22 +00001768 The :mod:`subprocess` module provides more powerful facilities for spawning new
1769 processes and retrieving their results; using that module is preferable to using
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001770 this function. Use the :mod:`subprocess` module. Check especially the
1771 :ref:`subprocess-replacements` section.
Georg Brandl116aa622007-08-15 14:28:22 +00001772
Benjamin Petersond91203b2010-05-06 23:20:40 +00001773 Availability: Unix, Windows.
1774
Georg Brandl116aa622007-08-15 14:28:22 +00001775
1776.. function:: times()
1777
Benjamin Petersond91203b2010-05-06 23:20:40 +00001778 Return a 5-tuple of floating point numbers indicating accumulated (processor
1779 or other) times, in seconds. The items are: user time, system time,
1780 children's user time, children's system time, and elapsed real time since a
1781 fixed point in the past, in that order. See the Unix manual page
1782 :manpage:`times(2)` or the corresponding Windows Platform API documentation.
1783 On Windows, only the first two items are filled, the others are zero.
1784
1785 Availability: Unix, Windows
Georg Brandl116aa622007-08-15 14:28:22 +00001786
1787
1788.. function:: wait()
1789
1790 Wait for completion of a child process, and return a tuple containing its pid
1791 and exit status indication: a 16-bit number, whose low byte is the signal number
1792 that killed the process, and whose high byte is the exit status (if the signal
1793 number is zero); the high bit of the low byte is set if a core file was
Benjamin Petersond91203b2010-05-06 23:20:40 +00001794 produced.
1795
1796 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001797
1798
1799.. function:: waitpid(pid, options)
1800
1801 The details of this function differ on Unix and Windows.
1802
1803 On Unix: Wait for completion of a child process given by process id *pid*, and
1804 return a tuple containing its process id and exit status indication (encoded as
1805 for :func:`wait`). The semantics of the call are affected by the value of the
1806 integer *options*, which should be ``0`` for normal operation.
1807
1808 If *pid* is greater than ``0``, :func:`waitpid` requests status information for
1809 that specific process. If *pid* is ``0``, the request is for the status of any
1810 child in the process group of the current process. If *pid* is ``-1``, the
1811 request pertains to any child of the current process. If *pid* is less than
1812 ``-1``, status is requested for any process in the process group ``-pid`` (the
1813 absolute value of *pid*).
1814
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001815 An :exc:`OSError` is raised with the value of errno when the syscall
1816 returns -1.
1817
Georg Brandl116aa622007-08-15 14:28:22 +00001818 On Windows: Wait for completion of a process given by process handle *pid*, and
1819 return a tuple containing *pid*, and its exit status shifted left by 8 bits
1820 (shifting makes cross-platform use of the function easier). A *pid* less than or
1821 equal to ``0`` has no special meaning on Windows, and raises an exception. The
1822 value of integer *options* has no effect. *pid* can refer to any process whose
1823 id is known, not necessarily a child process. The :func:`spawn` functions called
1824 with :const:`P_NOWAIT` return suitable process handles.
1825
1826
1827.. function:: wait3([options])
1828
1829 Similar to :func:`waitpid`, except no process id argument is given and a
1830 3-element tuple containing the child's process id, exit status indication, and
1831 resource usage information is returned. Refer to :mod:`resource`.\
1832 :func:`getrusage` for details on resource usage information. The option
1833 argument is the same as that provided to :func:`waitpid` and :func:`wait4`.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001834
Georg Brandl116aa622007-08-15 14:28:22 +00001835 Availability: Unix.
1836
Georg Brandl116aa622007-08-15 14:28:22 +00001837
1838.. function:: wait4(pid, options)
1839
1840 Similar to :func:`waitpid`, except a 3-element tuple, containing the child's
1841 process id, exit status indication, and resource usage information is returned.
1842 Refer to :mod:`resource`.\ :func:`getrusage` for details on resource usage
1843 information. The arguments to :func:`wait4` are the same as those provided to
Benjamin Petersond91203b2010-05-06 23:20:40 +00001844 :func:`waitpid`.
1845
1846 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001847
Georg Brandl116aa622007-08-15 14:28:22 +00001848
1849.. data:: WNOHANG
1850
1851 The option for :func:`waitpid` to return immediately if no child process status
1852 is available immediately. The function returns ``(0, 0)`` in this case.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001853
Georg Brandlc575c902008-09-13 17:46:05 +00001854 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001855
1856
1857.. data:: WCONTINUED
1858
1859 This option causes child processes to be reported if they have been continued
Benjamin Petersond91203b2010-05-06 23:20:40 +00001860 from a job control stop since their status was last reported.
1861
1862 Availability: Some Unix systems.
Georg Brandl116aa622007-08-15 14:28:22 +00001863
Georg Brandl116aa622007-08-15 14:28:22 +00001864
1865.. data:: WUNTRACED
1866
1867 This option causes child processes to be reported if they have been stopped but
Benjamin Petersond91203b2010-05-06 23:20:40 +00001868 their current state has not been reported since they were stopped.
1869
1870 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001871
Georg Brandl116aa622007-08-15 14:28:22 +00001872
1873The following functions take a process status code as returned by
1874:func:`system`, :func:`wait`, or :func:`waitpid` as a parameter. They may be
1875used to determine the disposition of a process.
1876
Georg Brandl116aa622007-08-15 14:28:22 +00001877.. function:: WCOREDUMP(status)
1878
Christian Heimesfaf2f632008-01-06 16:59:19 +00001879 Return ``True`` if a core dump was generated for the process, otherwise
Benjamin Petersond91203b2010-05-06 23:20:40 +00001880 return ``False``.
1881
1882 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001883
Georg Brandl116aa622007-08-15 14:28:22 +00001884
1885.. function:: WIFCONTINUED(status)
1886
Christian Heimesfaf2f632008-01-06 16:59:19 +00001887 Return ``True`` if the process has been continued from a job control stop,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001888 otherwise return ``False``.
1889
1890 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001891
Georg Brandl116aa622007-08-15 14:28:22 +00001892
1893.. function:: WIFSTOPPED(status)
1894
Christian Heimesfaf2f632008-01-06 16:59:19 +00001895 Return ``True`` if the process has been stopped, otherwise return
Benjamin Petersond91203b2010-05-06 23:20:40 +00001896 ``False``.
1897
1898 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001899
1900
1901.. function:: WIFSIGNALED(status)
1902
Christian Heimesfaf2f632008-01-06 16:59:19 +00001903 Return ``True`` if the process exited due to a signal, otherwise return
Benjamin Petersond91203b2010-05-06 23:20:40 +00001904 ``False``.
1905
1906 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001907
1908
1909.. function:: WIFEXITED(status)
1910
Christian Heimesfaf2f632008-01-06 16:59:19 +00001911 Return ``True`` if the process exited using the :manpage:`exit(2)` system call,
Benjamin Petersond91203b2010-05-06 23:20:40 +00001912 otherwise return ``False``.
1913
1914 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001915
1916
1917.. function:: WEXITSTATUS(status)
1918
1919 If ``WIFEXITED(status)`` is true, return the integer parameter to the
1920 :manpage:`exit(2)` system call. Otherwise, the return value is meaningless.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001921
Georg Brandlc575c902008-09-13 17:46:05 +00001922 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001923
1924
1925.. function:: WSTOPSIG(status)
1926
Benjamin Petersond91203b2010-05-06 23:20:40 +00001927 Return the signal which caused the process to stop.
1928
1929 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001930
1931
1932.. function:: WTERMSIG(status)
1933
Benjamin Petersond91203b2010-05-06 23:20:40 +00001934 Return the signal which caused the process to exit.
1935
1936 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001937
1938
1939.. _os-path:
1940
1941Miscellaneous System Information
1942--------------------------------
1943
1944
1945.. function:: confstr(name)
1946
1947 Return string-valued system configuration values. *name* specifies the
1948 configuration value to retrieve; it may be a string which is the name of a
1949 defined system value; these names are specified in a number of standards (POSIX,
1950 Unix 95, Unix 98, and others). Some platforms define additional names as well.
1951 The names known to the host operating system are given as the keys of the
1952 ``confstr_names`` dictionary. For configuration variables not included in that
Benjamin Petersond91203b2010-05-06 23:20:40 +00001953 mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +00001954
1955 If the configuration value specified by *name* isn't defined, ``None`` is
1956 returned.
1957
1958 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1959 specific value for *name* is not supported by the host system, even if it is
1960 included in ``confstr_names``, an :exc:`OSError` is raised with
1961 :const:`errno.EINVAL` for the error number.
1962
Benjamin Petersond91203b2010-05-06 23:20:40 +00001963 Availability: Unix
1964
Georg Brandl116aa622007-08-15 14:28:22 +00001965
1966.. data:: confstr_names
1967
1968 Dictionary mapping names accepted by :func:`confstr` to the integer values
1969 defined for those names by the host operating system. This can be used to
Benjamin Petersond91203b2010-05-06 23:20:40 +00001970 determine the set of names known to the system.
1971
1972 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001973
1974
1975.. function:: getloadavg()
1976
Christian Heimesa62da1d2008-01-12 19:39:10 +00001977 Return the number of processes in the system run queue averaged over the last
1978 1, 5, and 15 minutes or raises :exc:`OSError` if the load average was
Benjamin Petersond91203b2010-05-06 23:20:40 +00001979 unobtainable.
1980
1981 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001982
Georg Brandl116aa622007-08-15 14:28:22 +00001983
1984.. function:: sysconf(name)
1985
1986 Return integer-valued system configuration values. If the configuration value
1987 specified by *name* isn't defined, ``-1`` is returned. The comments regarding
1988 the *name* parameter for :func:`confstr` apply here as well; the dictionary that
1989 provides information on the known names is given by ``sysconf_names``.
Benjamin Petersond91203b2010-05-06 23:20:40 +00001990
Georg Brandlc575c902008-09-13 17:46:05 +00001991 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001992
1993
1994.. data:: sysconf_names
1995
1996 Dictionary mapping names accepted by :func:`sysconf` to the integer values
1997 defined for those names by the host operating system. This can be used to
Benjamin Petersond91203b2010-05-06 23:20:40 +00001998 determine the set of names known to the system.
1999
2000 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002001
Christian Heimesfaf2f632008-01-06 16:59:19 +00002002The following data values are used to support path manipulation operations. These
Georg Brandl116aa622007-08-15 14:28:22 +00002003are defined for all platforms.
2004
2005Higher-level operations on pathnames are defined in the :mod:`os.path` module.
2006
2007
2008.. data:: curdir
2009
2010 The constant string used by the operating system to refer to the current
Georg Brandlc575c902008-09-13 17:46:05 +00002011 directory. This is ``'.'`` for Windows and POSIX. Also available via
2012 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002013
2014
2015.. data:: pardir
2016
2017 The constant string used by the operating system to refer to the parent
Georg Brandlc575c902008-09-13 17:46:05 +00002018 directory. This is ``'..'`` for Windows and POSIX. Also available via
2019 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002020
2021
2022.. data:: sep
2023
Georg Brandlc575c902008-09-13 17:46:05 +00002024 The character used by the operating system to separate pathname components.
2025 This is ``'/'`` for POSIX and ``'\\'`` for Windows. Note that knowing this
2026 is not sufficient to be able to parse or concatenate pathnames --- use
Georg Brandl116aa622007-08-15 14:28:22 +00002027 :func:`os.path.split` and :func:`os.path.join` --- but it is occasionally
2028 useful. Also available via :mod:`os.path`.
2029
2030
2031.. data:: altsep
2032
2033 An alternative character used by the operating system to separate pathname
2034 components, or ``None`` if only one separator character exists. This is set to
2035 ``'/'`` on Windows systems where ``sep`` is a backslash. Also available via
2036 :mod:`os.path`.
2037
2038
2039.. data:: extsep
2040
2041 The character which separates the base filename from the extension; for example,
2042 the ``'.'`` in :file:`os.py`. Also available via :mod:`os.path`.
2043
Georg Brandl116aa622007-08-15 14:28:22 +00002044
2045.. data:: pathsep
2046
2047 The character conventionally used by the operating system to separate search
2048 path components (as in :envvar:`PATH`), such as ``':'`` for POSIX or ``';'`` for
2049 Windows. Also available via :mod:`os.path`.
2050
2051
2052.. data:: defpath
2053
2054 The default search path used by :func:`exec\*p\*` and :func:`spawn\*p\*` if the
2055 environment doesn't have a ``'PATH'`` key. Also available via :mod:`os.path`.
2056
2057
2058.. data:: linesep
2059
2060 The string used to separate (or, rather, terminate) lines on the current
Georg Brandlc575c902008-09-13 17:46:05 +00002061 platform. This may be a single character, such as ``'\n'`` for POSIX, or
2062 multiple characters, for example, ``'\r\n'`` for Windows. Do not use
2063 *os.linesep* as a line terminator when writing files opened in text mode (the
2064 default); use a single ``'\n'`` instead, on all platforms.
Georg Brandl116aa622007-08-15 14:28:22 +00002065
2066
2067.. data:: devnull
2068
Georg Brandlc575c902008-09-13 17:46:05 +00002069 The file path of the null device. For example: ``'/dev/null'`` for POSIX.
2070 Also available via :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00002071
Georg Brandl116aa622007-08-15 14:28:22 +00002072
2073.. _os-miscfunc:
2074
2075Miscellaneous Functions
2076-----------------------
2077
2078
2079.. function:: urandom(n)
2080
2081 Return a string of *n* random bytes suitable for cryptographic use.
2082
2083 This function returns random bytes from an OS-specific randomness source. The
2084 returned data should be unpredictable enough for cryptographic applications,
2085 though its exact quality depends on the OS implementation. On a UNIX-like
2086 system this will query /dev/urandom, and on Windows it will use CryptGenRandom.
2087 If a randomness source is not found, :exc:`NotImplementedError` will be raised.