blob: b4e39a425d78ff01fd149105b7757513a451d840 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`os` --- Miscellaneous operating system interfaces
2=======================================================
3
4.. module:: os
5 :synopsis: Miscellaneous operating system interfaces.
6
7
Georg Brandl57fe0f22008-01-12 10:53:29 +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 Brandl8ec7f652007-08-15 14:28:01 +000015
Georg Brandlc51d1f02009-12-19 18:16:31 +000016Notes on the availability of these functions:
Georg Brandl8ec7f652007-08-15 14:28:01 +000017
Georg Brandlc51d1f02009-12-19 18:16:31 +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 Brandl8ec7f652007-08-15 14:28:01 +000023
Georg Brandlc51d1f02009-12-19 18:16:31 +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 Brandl8ec7f652007-08-15 14:28:01 +000027
Georg Brandlc51d1f02009-12-19 18:16:31 +000028* An "Availability: Unix" note means that this function is commonly found on
29 Unix systems. It does not make any claims about its existence on a specific
30 operating system.
31
32* If not separately noted, all functions that claim "Availability: Unix" are
33 supported on Mac OS X, which builds on a Unix core.
Georg Brandl9af94982008-09-13 17:41:16 +000034
35.. note::
36
Georg Brandl57fe0f22008-01-12 10:53:29 +000037 All functions in this module raise :exc:`OSError` in the case of invalid or
38 inaccessible file names and paths, or other arguments that have the correct
39 type, but are not accepted by the operating system.
Georg Brandl8ec7f652007-08-15 14:28:01 +000040
Georg Brandl8ec7f652007-08-15 14:28:01 +000041
42.. exception:: error
43
Georg Brandl57fe0f22008-01-12 10:53:29 +000044 An alias for the built-in :exc:`OSError` exception.
Georg Brandl8ec7f652007-08-15 14:28:01 +000045
46
47.. data:: name
48
Georg Brandlc51d1f02009-12-19 18:16:31 +000049 The name of the operating system dependent module imported. The following
50 names have currently been registered: ``'posix'``, ``'nt'``, ``'mac'``,
51 ``'os2'``, ``'ce'``, ``'java'``, ``'riscos'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +000052
53
Georg Brandl8ec7f652007-08-15 14:28:01 +000054.. _os-procinfo:
55
56Process Parameters
57------------------
58
59These functions and data items provide information and operate on the current
60process and user.
61
62
63.. data:: environ
64
65 A mapping object representing the string environment. For example,
66 ``environ['HOME']`` is the pathname of your home directory (on some platforms),
67 and is equivalent to ``getenv("HOME")`` in C.
68
69 This mapping is captured the first time the :mod:`os` module is imported,
70 typically during Python startup as part of processing :file:`site.py`. Changes
71 to the environment made after this time are not reflected in ``os.environ``,
72 except for changes made by modifying ``os.environ`` directly.
73
74 If the platform supports the :func:`putenv` function, this mapping may be used
75 to modify the environment as well as query the environment. :func:`putenv` will
76 be called automatically when the mapping is modified.
77
78 .. note::
79
80 Calling :func:`putenv` directly does not change ``os.environ``, so it's better
81 to modify ``os.environ``.
82
83 .. note::
84
Georg Brandl9af94982008-09-13 17:41:16 +000085 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
86 cause memory leaks. Refer to the system documentation for
87 :cfunc:`putenv`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000088
89 If :func:`putenv` is not provided, a modified copy of this mapping may be
90 passed to the appropriate process-creation functions to cause child processes
91 to use a modified environment.
92
Georg Brandl4a212682007-09-20 17:57:59 +000093 If the platform supports the :func:`unsetenv` function, you can delete items in
Georg Brandl8ec7f652007-08-15 14:28:01 +000094 this mapping to unset environment variables. :func:`unsetenv` will be called
Georg Brandl4a212682007-09-20 17:57:59 +000095 automatically when an item is deleted from ``os.environ``, and when
Georg Brandl1a94ec22007-10-24 21:40:38 +000096 one of the :meth:`pop` or :meth:`clear` methods is called.
Georg Brandl4a212682007-09-20 17:57:59 +000097
98 .. versionchanged:: 2.6
Georg Brandl1a94ec22007-10-24 21:40:38 +000099 Also unset environment variables when calling :meth:`os.environ.clear`
100 and :meth:`os.environ.pop`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000101
102
103.. function:: chdir(path)
104 fchdir(fd)
105 getcwd()
106 :noindex:
107
108 These functions are described in :ref:`os-file-dir`.
109
110
111.. function:: ctermid()
112
113 Return the filename corresponding to the controlling terminal of the process.
114 Availability: Unix.
115
116
117.. function:: getegid()
118
119 Return the effective group id of the current process. This corresponds to the
Georg Brandlf725b952008-01-05 19:44:22 +0000120 "set id" bit on the file being executed in the current process. Availability:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000121 Unix.
122
123
124.. function:: geteuid()
125
126 .. index:: single: user; effective id
127
Georg Brandlf725b952008-01-05 19:44:22 +0000128 Return the current process's effective user id. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000129
130
131.. function:: getgid()
132
133 .. index:: single: process; group
134
135 Return the real group id of the current process. Availability: Unix.
136
137
138.. function:: getgroups()
139
140 Return list of supplemental group ids associated with the current process.
141 Availability: Unix.
142
143
Antoine Pitrou30b3b352009-12-02 20:37:54 +0000144.. function:: initgroups(username, gid)
145
146 Call the system initgroups() to initialize the group access list with all of
147 the groups of which the specified username is a member, plus the specified
148 group id. Availability: Unix.
149
150 .. versionadded:: 2.7
151
152
Georg Brandl8ec7f652007-08-15 14:28:01 +0000153.. function:: getlogin()
154
155 Return the name of the user logged in on the controlling terminal of the
156 process. For most purposes, it is more useful to use the environment variable
157 :envvar:`LOGNAME` to find out who the user is, or
158 ``pwd.getpwuid(os.getuid())[0]`` to get the login name of the currently
Georg Brandlf725b952008-01-05 19:44:22 +0000159 effective user id. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000160
161
162.. function:: getpgid(pid)
163
164 Return the process group id of the process with process id *pid*. If *pid* is 0,
165 the process group id of the current process is returned. Availability: Unix.
166
167 .. versionadded:: 2.3
168
169
170.. function:: getpgrp()
171
172 .. index:: single: process; group
173
174 Return the id of the current process group. Availability: Unix.
175
176
177.. function:: getpid()
178
179 .. index:: single: process; id
180
181 Return the current process id. Availability: Unix, Windows.
182
183
184.. function:: getppid()
185
186 .. index:: single: process; id of parent
187
188 Return the parent's process id. Availability: Unix.
189
Georg Brandl8d8f8742009-11-28 11:11:50 +0000190
Gregory P. Smith761ae0b2009-11-27 17:51:12 +0000191.. function:: getresuid()
Martin v. Löwis50ea4562009-11-27 13:56:01 +0000192
193 Return a tuple (ruid, euid, suid) denoting the current process's
194 real, effective, and saved user ids. Availability: Unix.
195
Georg Brandl8d8f8742009-11-28 11:11:50 +0000196 .. versionadded:: 2.7
197
Martin v. Löwis50ea4562009-11-27 13:56:01 +0000198
Gregory P. Smith761ae0b2009-11-27 17:51:12 +0000199.. function:: getresgid()
Martin v. Löwis50ea4562009-11-27 13:56:01 +0000200
201 Return a tuple (rgid, egid, sgid) denoting the current process's
202 real, effective, and saved user ids. Availability: Unix.
203
Georg Brandl8d8f8742009-11-28 11:11:50 +0000204 .. versionadded:: 2.7
205
Georg Brandl8ec7f652007-08-15 14:28:01 +0000206
207.. function:: getuid()
208
209 .. index:: single: user; id
210
Georg Brandlf725b952008-01-05 19:44:22 +0000211 Return the current process's user id. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000212
213
214.. function:: getenv(varname[, value])
215
216 Return the value of the environment variable *varname* if it exists, or *value*
217 if it doesn't. *value* defaults to ``None``. Availability: most flavors of
218 Unix, Windows.
219
220
221.. function:: putenv(varname, value)
222
223 .. index:: single: environment variables; setting
224
225 Set the environment variable named *varname* to the string *value*. Such
226 changes to the environment affect subprocesses started with :func:`os.system`,
227 :func:`popen` or :func:`fork` and :func:`execv`. Availability: most flavors of
228 Unix, Windows.
229
230 .. note::
231
Georg Brandl9af94982008-09-13 17:41:16 +0000232 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
233 cause memory leaks. Refer to the system documentation for putenv.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000234
235 When :func:`putenv` is supported, assignments to items in ``os.environ`` are
236 automatically translated into corresponding calls to :func:`putenv`; however,
237 calls to :func:`putenv` don't update ``os.environ``, so it is actually
238 preferable to assign to items of ``os.environ``.
239
240
241.. function:: setegid(egid)
242
243 Set the current process's effective group id. Availability: Unix.
244
245
246.. function:: seteuid(euid)
247
248 Set the current process's effective user id. Availability: Unix.
249
250
251.. function:: setgid(gid)
252
253 Set the current process' group id. Availability: Unix.
254
255
256.. function:: setgroups(groups)
257
258 Set the list of supplemental group ids associated with the current process to
259 *groups*. *groups* must be a sequence, and each element must be an integer
Georg Brandlf725b952008-01-05 19:44:22 +0000260 identifying a group. This operation is typically available only to the superuser.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000261 Availability: Unix.
262
263 .. versionadded:: 2.2
264
265
266.. function:: setpgrp()
267
Georg Brandlf725b952008-01-05 19:44:22 +0000268 Call the system call :cfunc:`setpgrp` or :cfunc:`setpgrp(0, 0)` depending on
Georg Brandl8ec7f652007-08-15 14:28:01 +0000269 which version is implemented (if any). See the Unix manual for the semantics.
270 Availability: Unix.
271
272
273.. function:: setpgid(pid, pgrp)
274
Georg Brandlf725b952008-01-05 19:44:22 +0000275 Call the system call :cfunc:`setpgid` to set the process group id of the
Georg Brandl8ec7f652007-08-15 14:28:01 +0000276 process with id *pid* to the process group with id *pgrp*. See the Unix manual
277 for the semantics. Availability: Unix.
278
279
Georg Brandl8ec7f652007-08-15 14:28:01 +0000280.. function:: setregid(rgid, egid)
281
282 Set the current process's real and effective group ids. Availability: Unix.
283
Georg Brandl8d8f8742009-11-28 11:11:50 +0000284
Martin v. Löwis50ea4562009-11-27 13:56:01 +0000285.. function:: setresgid(rgid, egid, sgid)
286
287 Set the current process's real, effective, and saved group ids.
288 Availability: Unix.
289
Georg Brandl8d8f8742009-11-28 11:11:50 +0000290 .. versionadded:: 2.7
291
Martin v. Löwis50ea4562009-11-27 13:56:01 +0000292
293.. function:: setresuid(ruid, euid, suid)
294
295 Set the current process's real, effective, and saved user ids.
296 Availibility: Unix.
297
Georg Brandl8d8f8742009-11-28 11:11:50 +0000298 .. versionadded:: 2.7
299
Martin v. Löwis50ea4562009-11-27 13:56:01 +0000300
301.. function:: setreuid(ruid, euid)
302
303 Set the current process's real and effective user ids. Availability: Unix.
304
Georg Brandl8ec7f652007-08-15 14:28:01 +0000305
306.. function:: getsid(pid)
307
Georg Brandlf725b952008-01-05 19:44:22 +0000308 Call the system call :cfunc:`getsid`. See the Unix manual for the semantics.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000309 Availability: Unix.
310
311 .. versionadded:: 2.4
312
313
314.. function:: setsid()
315
Georg Brandlf725b952008-01-05 19:44:22 +0000316 Call the system call :cfunc:`setsid`. See the Unix manual for the semantics.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000317 Availability: Unix.
318
319
320.. function:: setuid(uid)
321
322 .. index:: single: user; id, setting
323
Georg Brandlf725b952008-01-05 19:44:22 +0000324 Set the current process's user id. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000325
Georg Brandl8ec7f652007-08-15 14:28:01 +0000326
Georg Brandlb19be572007-12-29 10:57:00 +0000327.. placed in this section since it relates to errno.... a little weak
Georg Brandl8ec7f652007-08-15 14:28:01 +0000328.. function:: strerror(code)
329
330 Return the error message corresponding to the error code in *code*.
Georg Brandl3fc974f2008-05-11 21:16:37 +0000331 On platforms where :cfunc:`strerror` returns ``NULL`` when given an unknown
332 error number, :exc:`ValueError` is raised. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000333
334
335.. function:: umask(mask)
336
Georg Brandlf725b952008-01-05 19:44:22 +0000337 Set the current numeric umask and return the previous umask. Availability:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000338 Unix, Windows.
339
340
341.. function:: uname()
342
343 .. index::
344 single: gethostname() (in module socket)
345 single: gethostbyaddr() (in module socket)
346
347 Return a 5-tuple containing information identifying the current operating
348 system. The tuple contains 5 strings: ``(sysname, nodename, release, version,
349 machine)``. Some systems truncate the nodename to 8 characters or to the
350 leading component; a better way to get the hostname is
351 :func:`socket.gethostname` or even
352 ``socket.gethostbyaddr(socket.gethostname())``. Availability: recent flavors of
353 Unix.
354
355
356.. function:: unsetenv(varname)
357
358 .. index:: single: environment variables; deleting
359
360 Unset (delete) the environment variable named *varname*. Such changes to the
361 environment affect subprocesses started with :func:`os.system`, :func:`popen` or
362 :func:`fork` and :func:`execv`. Availability: most flavors of Unix, Windows.
363
364 When :func:`unsetenv` is supported, deletion of items in ``os.environ`` is
365 automatically translated into a corresponding call to :func:`unsetenv`; however,
366 calls to :func:`unsetenv` don't update ``os.environ``, so it is actually
367 preferable to delete items of ``os.environ``.
368
369
370.. _os-newstreams:
371
372File Object Creation
373--------------------
374
375These functions create new file objects. (See also :func:`open`.)
376
377
378.. function:: fdopen(fd[, mode[, bufsize]])
379
380 .. index:: single: I/O control; buffering
381
382 Return an open file object connected to the file descriptor *fd*. The *mode*
383 and *bufsize* arguments have the same meaning as the corresponding arguments to
Georg Brandl9af94982008-09-13 17:41:16 +0000384 the built-in :func:`open` function. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000385
386 .. versionchanged:: 2.3
387 When specified, the *mode* argument must now start with one of the letters
388 ``'r'``, ``'w'``, or ``'a'``, otherwise a :exc:`ValueError` is raised.
389
390 .. versionchanged:: 2.5
391 On Unix, when the *mode* argument starts with ``'a'``, the *O_APPEND* flag is
392 set on the file descriptor (which the :cfunc:`fdopen` implementation already
393 does on most platforms).
394
395
396.. function:: popen(command[, mode[, bufsize]])
397
398 Open a pipe to or from *command*. The return value is an open file object
399 connected to the pipe, which can be read or written depending on whether *mode*
400 is ``'r'`` (default) or ``'w'``. The *bufsize* argument has the same meaning as
401 the corresponding argument to the built-in :func:`open` function. The exit
402 status of the command (encoded in the format specified for :func:`wait`) is
Georg Brandl012408c2009-05-22 09:43:17 +0000403 available as the return value of the :meth:`~file.close` method of the file object,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000404 except that when the exit status is zero (termination without errors), ``None``
Georg Brandl9af94982008-09-13 17:41:16 +0000405 is returned. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000406
407 .. deprecated:: 2.6
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000408 This function is obsolete. Use the :mod:`subprocess` module. Check
Georg Brandl0ba92b22008-06-22 09:05:29 +0000409 especially the :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000410
411 .. versionchanged:: 2.0
412 This function worked unreliably under Windows in earlier versions of Python.
413 This was due to the use of the :cfunc:`_popen` function from the libraries
414 provided with Windows. Newer versions of Python do not use the broken
415 implementation from the Windows libraries.
416
417
418.. function:: tmpfile()
419
420 Return a new file object opened in update mode (``w+b``). The file has no
421 directory entries associated with it and will be automatically deleted once
Georg Brandl9af94982008-09-13 17:41:16 +0000422 there are no file descriptors for the file. Availability: Unix,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000423 Windows.
424
425There are a number of different :func:`popen\*` functions that provide slightly
426different ways to create subprocesses.
427
428.. deprecated:: 2.6
429 All of the :func:`popen\*` functions are obsolete. Use the :mod:`subprocess`
430 module.
431
432For each of the :func:`popen\*` variants, if *bufsize* is specified, it
433specifies the buffer size for the I/O pipes. *mode*, if provided, should be the
434string ``'b'`` or ``'t'``; on Windows this is needed to determine whether the
435file objects should be opened in binary or text mode. The default value for
436*mode* is ``'t'``.
437
438Also, for each of these variants, on Unix, *cmd* may be a sequence, in which
439case arguments will be passed directly to the program without shell intervention
440(as with :func:`os.spawnv`). If *cmd* is a string it will be passed to the shell
441(as with :func:`os.system`).
442
443These methods do not make it possible to retrieve the exit status from the child
444processes. The only way to control the input and output streams and also
445retrieve the return codes is to use the :mod:`subprocess` module; these are only
446available on Unix.
447
448For a discussion of possible deadlock conditions related to the use of these
449functions, see :ref:`popen2-flow-control`.
450
451
452.. function:: popen2(cmd[, mode[, bufsize]])
453
Georg Brandlf725b952008-01-05 19:44:22 +0000454 Execute *cmd* as a sub-process and return the file objects ``(child_stdin,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000455 child_stdout)``.
456
457 .. deprecated:: 2.6
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000458 This function is obsolete. Use the :mod:`subprocess` module. Check
Georg Brandl0ba92b22008-06-22 09:05:29 +0000459 especially the :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000460
Georg Brandl9af94982008-09-13 17:41:16 +0000461 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000462
463 .. versionadded:: 2.0
464
465
466.. function:: popen3(cmd[, mode[, bufsize]])
467
Georg Brandlf725b952008-01-05 19:44:22 +0000468 Execute *cmd* as a sub-process and return the file objects ``(child_stdin,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000469 child_stdout, child_stderr)``.
470
471 .. deprecated:: 2.6
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000472 This function is obsolete. Use the :mod:`subprocess` module. Check
Georg Brandl0ba92b22008-06-22 09:05:29 +0000473 especially the :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000474
Georg Brandl9af94982008-09-13 17:41:16 +0000475 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000476
477 .. versionadded:: 2.0
478
479
480.. function:: popen4(cmd[, mode[, bufsize]])
481
Georg Brandlf725b952008-01-05 19:44:22 +0000482 Execute *cmd* as a sub-process and return the file objects ``(child_stdin,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000483 child_stdout_and_stderr)``.
484
485 .. deprecated:: 2.6
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000486 This function is obsolete. Use the :mod:`subprocess` module. Check
Georg Brandl0ba92b22008-06-22 09:05:29 +0000487 especially the :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000488
Georg Brandl9af94982008-09-13 17:41:16 +0000489 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000490
491 .. versionadded:: 2.0
492
493(Note that ``child_stdin, child_stdout, and child_stderr`` are named from the
494point of view of the child process, so *child_stdin* is the child's standard
495input.)
496
497This functionality is also available in the :mod:`popen2` module using functions
498of the same names, but the return values of those functions have a different
499order.
500
501
502.. _os-fd-ops:
503
504File Descriptor Operations
505--------------------------
506
507These functions operate on I/O streams referenced using file descriptors.
508
509File descriptors are small integers corresponding to a file that has been opened
510by the current process. For example, standard input is usually file descriptor
5110, standard output is 1, and standard error is 2. Further files opened by a
512process will then be assigned 3, 4, 5, and so forth. The name "file descriptor"
513is slightly deceptive; on Unix platforms, sockets and pipes are also referenced
514by file descriptors.
515
Georg Brandl49b91922010-04-02 08:39:09 +0000516The :meth:`~file.fileno` method can be used to obtain the file descriptor
517associated with a file object when required. Note that using the file
518descriptor directly will bypass the file object methods, ignoring aspects such
519as internal buffering of data.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000520
521.. function:: close(fd)
522
Georg Brandl9af94982008-09-13 17:41:16 +0000523 Close file descriptor *fd*. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000524
525 .. note::
526
527 This function is intended for low-level I/O and must be applied to a file
Georg Brandl012408c2009-05-22 09:43:17 +0000528 descriptor as returned by :func:`os.open` or :func:`pipe`. To close a "file
Georg Brandl8ec7f652007-08-15 14:28:01 +0000529 object" returned by the built-in function :func:`open` or by :func:`popen` or
Georg Brandl012408c2009-05-22 09:43:17 +0000530 :func:`fdopen`, use its :meth:`~file.close` method.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000531
532
Georg Brandl309501a2008-01-19 20:22:13 +0000533.. function:: closerange(fd_low, fd_high)
534
535 Close all file descriptors from *fd_low* (inclusive) to *fd_high* (exclusive),
Georg Brandl9af94982008-09-13 17:41:16 +0000536 ignoring errors. Availability: Unix, Windows. Equivalent to::
Georg Brandl309501a2008-01-19 20:22:13 +0000537
538 for fd in xrange(fd_low, fd_high):
539 try:
540 os.close(fd)
541 except OSError:
542 pass
543
544 .. versionadded:: 2.6
545
546
Georg Brandl8ec7f652007-08-15 14:28:01 +0000547.. function:: dup(fd)
548
Georg Brandl9af94982008-09-13 17:41:16 +0000549 Return a duplicate of file descriptor *fd*. Availability: Unix,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000550 Windows.
551
552
553.. function:: dup2(fd, fd2)
554
555 Duplicate file descriptor *fd* to *fd2*, closing the latter first if necessary.
Georg Brandl9af94982008-09-13 17:41:16 +0000556 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000557
558
Christian Heimes36281872007-11-30 21:11:28 +0000559.. function:: fchmod(fd, mode)
560
561 Change the mode of the file given by *fd* to the numeric *mode*. See the docs
562 for :func:`chmod` for possible values of *mode*. Availability: Unix.
563
Georg Brandl81ddc1a2007-11-30 22:04:45 +0000564 .. versionadded:: 2.6
565
Christian Heimes36281872007-11-30 21:11:28 +0000566
567.. function:: fchown(fd, uid, gid)
568
569 Change the owner and group id of the file given by *fd* to the numeric *uid*
570 and *gid*. To leave one of the ids unchanged, set it to -1.
571 Availability: Unix.
572
Georg Brandl81ddc1a2007-11-30 22:04:45 +0000573 .. versionadded:: 2.6
574
Christian Heimes36281872007-11-30 21:11:28 +0000575
Georg Brandl8ec7f652007-08-15 14:28:01 +0000576.. function:: fdatasync(fd)
577
578 Force write of file with filedescriptor *fd* to disk. Does not force update of
579 metadata. Availability: Unix.
580
Benjamin Petersonecf3c622009-05-30 03:10:52 +0000581 .. note::
582 This function is not available on MacOS.
583
Georg Brandl8ec7f652007-08-15 14:28:01 +0000584
585.. function:: fpathconf(fd, name)
586
587 Return system configuration information relevant to an open file. *name*
588 specifies the configuration value to retrieve; it may be a string which is the
589 name of a defined system value; these names are specified in a number of
590 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
591 additional names as well. The names known to the host operating system are
592 given in the ``pathconf_names`` dictionary. For configuration variables not
593 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl9af94982008-09-13 17:41:16 +0000594 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000595
596 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
597 specific value for *name* is not supported by the host system, even if it is
598 included in ``pathconf_names``, an :exc:`OSError` is raised with
599 :const:`errno.EINVAL` for the error number.
600
601
602.. function:: fstat(fd)
603
604 Return status for file descriptor *fd*, like :func:`stat`. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +0000605 Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000606
607
608.. function:: fstatvfs(fd)
609
610 Return information about the filesystem containing the file associated with file
611 descriptor *fd*, like :func:`statvfs`. Availability: Unix.
612
613
614.. function:: fsync(fd)
615
616 Force write of file with filedescriptor *fd* to disk. On Unix, this calls the
617 native :cfunc:`fsync` function; on Windows, the MS :cfunc:`_commit` function.
618
619 If you're starting with a Python file object *f*, first do ``f.flush()``, and
620 then do ``os.fsync(f.fileno())``, to ensure that all internal buffers associated
Georg Brandl9af94982008-09-13 17:41:16 +0000621 with *f* are written to disk. Availability: Unix, and Windows
Georg Brandl8ec7f652007-08-15 14:28:01 +0000622 starting in 2.2.3.
623
624
625.. function:: ftruncate(fd, length)
626
627 Truncate the file corresponding to file descriptor *fd*, so that it is at most
Georg Brandl9af94982008-09-13 17:41:16 +0000628 *length* bytes in size. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000629
630
631.. function:: isatty(fd)
632
633 Return ``True`` if the file descriptor *fd* is open and connected to a
Georg Brandl9af94982008-09-13 17:41:16 +0000634 tty(-like) device, else ``False``. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000635
636
637.. function:: lseek(fd, pos, how)
638
Georg Brandlf725b952008-01-05 19:44:22 +0000639 Set the current position of file descriptor *fd* to position *pos*, modified
640 by *how*: :const:`SEEK_SET` or ``0`` to set the position relative to the
641 beginning of the file; :const:`SEEK_CUR` or ``1`` to set it relative to the
642 current position; :const:`os.SEEK_END` or ``2`` to set it relative to the end of
Georg Brandl9af94982008-09-13 17:41:16 +0000643 the file. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000644
645
646.. function:: open(file, flags[, mode])
647
648 Open the file *file* and set various flags according to *flags* and possibly its
649 mode according to *mode*. The default *mode* is ``0777`` (octal), and the
650 current umask value is first masked out. Return the file descriptor for the
Georg Brandl9af94982008-09-13 17:41:16 +0000651 newly opened file. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000652
653 For a description of the flag and mode values, see the C run-time documentation;
654 flag constants (like :const:`O_RDONLY` and :const:`O_WRONLY`) are defined in
655 this module too (see below).
656
657 .. note::
658
Georg Brandl9fa61bb2009-07-26 14:19:57 +0000659 This function is intended for low-level I/O. For normal usage, use the
660 built-in function :func:`open`, which returns a "file object" with
661 :meth:`~file.read` and :meth:`~file.write` methods (and many more). To
662 wrap a file descriptor in a "file object", use :func:`fdopen`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000663
664
665.. function:: openpty()
666
667 .. index:: module: pty
668
669 Open a new pseudo-terminal pair. Return a pair of file descriptors ``(master,
670 slave)`` for the pty and the tty, respectively. For a (slightly) more portable
Georg Brandl9af94982008-09-13 17:41:16 +0000671 approach, use the :mod:`pty` module. Availability: some flavors of
Georg Brandl8ec7f652007-08-15 14:28:01 +0000672 Unix.
673
674
675.. function:: pipe()
676
677 Create a pipe. Return a pair of file descriptors ``(r, w)`` usable for reading
Georg Brandl9af94982008-09-13 17:41:16 +0000678 and writing, respectively. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000679
680
681.. function:: read(fd, n)
682
683 Read at most *n* bytes from file descriptor *fd*. Return a string containing the
684 bytes read. If the end of the file referred to by *fd* has been reached, an
Georg Brandl9af94982008-09-13 17:41:16 +0000685 empty string is returned. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000686
687 .. note::
688
689 This function is intended for low-level I/O and must be applied to a file
Georg Brandl012408c2009-05-22 09:43:17 +0000690 descriptor as returned by :func:`os.open` or :func:`pipe`. To read a "file object"
Georg Brandl8ec7f652007-08-15 14:28:01 +0000691 returned by the built-in function :func:`open` or by :func:`popen` or
Georg Brandl012408c2009-05-22 09:43:17 +0000692 :func:`fdopen`, or :data:`sys.stdin`, use its :meth:`~file.read` or
693 :meth:`~file.readline` methods.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000694
695
696.. function:: tcgetpgrp(fd)
697
698 Return the process group associated with the terminal given by *fd* (an open
Georg Brandl012408c2009-05-22 09:43:17 +0000699 file descriptor as returned by :func:`os.open`). Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000700
701
702.. function:: tcsetpgrp(fd, pg)
703
704 Set the process group associated with the terminal given by *fd* (an open file
Georg Brandl012408c2009-05-22 09:43:17 +0000705 descriptor as returned by :func:`os.open`) to *pg*. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000706
707
708.. function:: ttyname(fd)
709
710 Return a string which specifies the terminal device associated with
Georg Brandlbb75e4e2007-10-21 10:46:24 +0000711 file descriptor *fd*. If *fd* is not associated with a terminal device, an
Georg Brandl9af94982008-09-13 17:41:16 +0000712 exception is raised. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000713
714
715.. function:: write(fd, str)
716
717 Write the string *str* to file descriptor *fd*. Return the number of bytes
Georg Brandl9af94982008-09-13 17:41:16 +0000718 actually written. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000719
720 .. note::
721
722 This function is intended for low-level I/O and must be applied to a file
Georg Brandl012408c2009-05-22 09:43:17 +0000723 descriptor as returned by :func:`os.open` or :func:`pipe`. To write a "file
Georg Brandl8ec7f652007-08-15 14:28:01 +0000724 object" returned by the built-in function :func:`open` or by :func:`popen` or
Georg Brandl012408c2009-05-22 09:43:17 +0000725 :func:`fdopen`, or :data:`sys.stdout` or :data:`sys.stderr`, use its
726 :meth:`~file.write` method.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000727
Georg Brandl0c880bd2008-12-05 08:02:17 +0000728The following constants are options for the *flags* parameter to the
Georg Brandl012408c2009-05-22 09:43:17 +0000729:func:`~os.open` function. They can be combined using the bitwise OR operator
Georg Brandl0c880bd2008-12-05 08:02:17 +0000730``|``. Some of them are not available on all platforms. For descriptions of
Georg Brandle70ff4b2008-12-05 09:25:32 +0000731their availability and use, consult the :manpage:`open(2)` manual page on Unix
Doug Hellmann1d18b5b2009-09-20 20:44:13 +0000732or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000733
734
735.. data:: O_RDONLY
736 O_WRONLY
737 O_RDWR
738 O_APPEND
739 O_CREAT
740 O_EXCL
741 O_TRUNC
742
Georg Brandl0c880bd2008-12-05 08:02:17 +0000743 These constants are available on Unix and Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000744
745
746.. data:: O_DSYNC
747 O_RSYNC
748 O_SYNC
749 O_NDELAY
750 O_NONBLOCK
751 O_NOCTTY
752 O_SHLOCK
753 O_EXLOCK
754
Georg Brandl0c880bd2008-12-05 08:02:17 +0000755 These constants are only available on Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000756
757
758.. data:: O_BINARY
Georg Brandlb67da6e2007-11-24 13:56:09 +0000759 O_NOINHERIT
Georg Brandl8ec7f652007-08-15 14:28:01 +0000760 O_SHORT_LIVED
761 O_TEMPORARY
762 O_RANDOM
763 O_SEQUENTIAL
764 O_TEXT
765
Georg Brandl0c880bd2008-12-05 08:02:17 +0000766 These constants are only available on Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000767
768
Georg Brandlae6b9f32008-05-16 13:41:26 +0000769.. data:: O_ASYNC
770 O_DIRECT
Georg Brandlb67da6e2007-11-24 13:56:09 +0000771 O_DIRECTORY
772 O_NOFOLLOW
773 O_NOATIME
774
Georg Brandl0c880bd2008-12-05 08:02:17 +0000775 These constants are GNU extensions and not present if they are not defined by
776 the C library.
Georg Brandlb67da6e2007-11-24 13:56:09 +0000777
778
Georg Brandl8ec7f652007-08-15 14:28:01 +0000779.. data:: SEEK_SET
780 SEEK_CUR
781 SEEK_END
782
783 Parameters to the :func:`lseek` function. Their values are 0, 1, and 2,
Georg Brandl9af94982008-09-13 17:41:16 +0000784 respectively. Availability: Windows, Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000785
786 .. versionadded:: 2.5
787
788
789.. _os-file-dir:
790
791Files and Directories
792---------------------
793
Georg Brandl8ec7f652007-08-15 14:28:01 +0000794.. function:: access(path, mode)
795
796 Use the real uid/gid to test for access to *path*. Note that most operations
797 will use the effective uid/gid, therefore this routine can be used in a
798 suid/sgid environment to test if the invoking user has the specified access to
799 *path*. *mode* should be :const:`F_OK` to test the existence of *path*, or it
800 can be the inclusive OR of one or more of :const:`R_OK`, :const:`W_OK`, and
801 :const:`X_OK` to test permissions. Return :const:`True` if access is allowed,
802 :const:`False` if not. See the Unix man page :manpage:`access(2)` for more
Georg Brandl9af94982008-09-13 17:41:16 +0000803 information. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000804
805 .. note::
806
Georg Brandl9fa61bb2009-07-26 14:19:57 +0000807 Using :func:`access` to check if a user is authorized to e.g. open a file
808 before actually doing so using :func:`open` creates a security hole,
809 because the user might exploit the short time interval between checking
810 and opening the file to manipulate it.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000811
812 .. note::
813
814 I/O operations may fail even when :func:`access` indicates that they would
815 succeed, particularly for operations on network filesystems which may have
816 permissions semantics beyond the usual POSIX permission-bit model.
817
818
819.. data:: F_OK
820
821 Value to pass as the *mode* parameter of :func:`access` to test the existence of
822 *path*.
823
824
825.. data:: R_OK
826
827 Value to include in the *mode* parameter of :func:`access` to test the
828 readability of *path*.
829
830
831.. data:: W_OK
832
833 Value to include in the *mode* parameter of :func:`access` to test the
834 writability of *path*.
835
836
837.. data:: X_OK
838
839 Value to include in the *mode* parameter of :func:`access` to determine if
840 *path* can be executed.
841
842
843.. function:: chdir(path)
844
845 .. index:: single: directory; changing
846
Georg Brandl9af94982008-09-13 17:41:16 +0000847 Change the current working directory to *path*. Availability: Unix,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000848 Windows.
849
850
851.. function:: fchdir(fd)
852
853 Change the current working directory to the directory represented by the file
854 descriptor *fd*. The descriptor must refer to an opened directory, not an open
855 file. Availability: Unix.
856
857 .. versionadded:: 2.3
858
859
860.. function:: getcwd()
861
862 Return a string representing the current working directory. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +0000863 Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000864
865
866.. function:: getcwdu()
867
868 Return a Unicode object representing the current working directory.
Georg Brandl9af94982008-09-13 17:41:16 +0000869 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000870
871 .. versionadded:: 2.3
872
873
874.. function:: chflags(path, flags)
875
876 Set the flags of *path* to the numeric *flags*. *flags* may take a combination
877 (bitwise OR) of the following values (as defined in the :mod:`stat` module):
878
879 * ``UF_NODUMP``
880 * ``UF_IMMUTABLE``
881 * ``UF_APPEND``
882 * ``UF_OPAQUE``
883 * ``UF_NOUNLINK``
884 * ``SF_ARCHIVED``
885 * ``SF_IMMUTABLE``
886 * ``SF_APPEND``
887 * ``SF_NOUNLINK``
888 * ``SF_SNAPSHOT``
889
Georg Brandl9af94982008-09-13 17:41:16 +0000890 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000891
892 .. versionadded:: 2.6
893
894
895.. function:: chroot(path)
896
897 Change the root directory of the current process to *path*. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +0000898 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000899
900 .. versionadded:: 2.2
901
902
903.. function:: chmod(path, mode)
904
905 Change the mode of *path* to the numeric *mode*. *mode* may take one of the
Georg Brandlf725b952008-01-05 19:44:22 +0000906 following values (as defined in the :mod:`stat` module) or bitwise ORed
Georg Brandl8ec7f652007-08-15 14:28:01 +0000907 combinations of them:
908
909
R. David Murrayfbba7cd2009-07-02 18:19:20 +0000910 * :data:`stat.S_ISUID`
911 * :data:`stat.S_ISGID`
912 * :data:`stat.S_ENFMT`
913 * :data:`stat.S_ISVTX`
914 * :data:`stat.S_IREAD`
915 * :data:`stat.S_IWRITE`
916 * :data:`stat.S_IEXEC`
917 * :data:`stat.S_IRWXU`
918 * :data:`stat.S_IRUSR`
919 * :data:`stat.S_IWUSR`
920 * :data:`stat.S_IXUSR`
921 * :data:`stat.S_IRWXG`
922 * :data:`stat.S_IRGRP`
923 * :data:`stat.S_IWGRP`
924 * :data:`stat.S_IXGRP`
925 * :data:`stat.S_IRWXO`
926 * :data:`stat.S_IROTH`
927 * :data:`stat.S_IWOTH`
928 * :data:`stat.S_IXOTH`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000929
Georg Brandl9af94982008-09-13 17:41:16 +0000930 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000931
932 .. note::
933
934 Although Windows supports :func:`chmod`, you can only set the file's read-only
935 flag with it (via the ``stat.S_IWRITE`` and ``stat.S_IREAD``
936 constants or a corresponding integer value). All other bits are
937 ignored.
938
939
940.. function:: chown(path, uid, gid)
941
942 Change the owner and group id of *path* to the numeric *uid* and *gid*. To leave
Georg Brandl9af94982008-09-13 17:41:16 +0000943 one of the ids unchanged, set it to -1. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000944
945
946.. function:: lchflags(path, flags)
947
948 Set the flags of *path* to the numeric *flags*, like :func:`chflags`, but do not
949 follow symbolic links. Availability: Unix.
950
951 .. versionadded:: 2.6
952
953
Georg Brandl81ddc1a2007-11-30 22:04:45 +0000954.. function:: lchmod(path, mode)
955
956 Change the mode of *path* to the numeric *mode*. If path is a symlink, this
957 affects the symlink rather than the target. See the docs for :func:`chmod`
958 for possible values of *mode*. Availability: Unix.
959
960 .. versionadded:: 2.6
961
962
Georg Brandl8ec7f652007-08-15 14:28:01 +0000963.. function:: lchown(path, uid, gid)
964
Georg Brandlf725b952008-01-05 19:44:22 +0000965 Change the owner and group id of *path* to the numeric *uid* and *gid*. This
Georg Brandl9af94982008-09-13 17:41:16 +0000966 function will not follow symbolic links. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000967
968 .. versionadded:: 2.3
969
970
Benjamin Peterson0e928582009-03-28 19:16:10 +0000971.. function:: link(source, link_name)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000972
Benjamin Peterson0e928582009-03-28 19:16:10 +0000973 Create a hard link pointing to *source* named *link_name*. Availability:
974 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000975
976
977.. function:: listdir(path)
978
Georg Brandl62342912008-11-24 19:56:47 +0000979 Return a list containing the names of the entries in the directory given by
980 *path*. The list is in arbitrary order. It does not include the special
981 entries ``'.'`` and ``'..'`` even if they are present in the
982 directory. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000983
984 .. versionchanged:: 2.3
985 On Windows NT/2k/XP and Unix, if *path* is a Unicode object, the result will be
Georg Brandld933cc22009-05-16 11:21:29 +0000986 a list of Unicode objects. Undecodable filenames will still be returned as
987 string objects.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000988
989
990.. function:: lstat(path)
991
Georg Brandl03b15c62007-11-01 17:19:33 +0000992 Like :func:`stat`, but do not follow symbolic links. This is an alias for
993 :func:`stat` on platforms that do not support symbolic links, such as
994 Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000995
996
997.. function:: mkfifo(path[, mode])
998
999 Create a FIFO (a named pipe) named *path* with numeric mode *mode*. The default
1000 *mode* is ``0666`` (octal). The current umask value is first masked out from
Georg Brandl9af94982008-09-13 17:41:16 +00001001 the mode. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001002
1003 FIFOs are pipes that can be accessed like regular files. FIFOs exist until they
1004 are deleted (for example with :func:`os.unlink`). Generally, FIFOs are used as
1005 rendezvous between "client" and "server" type processes: the server opens the
1006 FIFO for reading, and the client opens it for writing. Note that :func:`mkfifo`
1007 doesn't open the FIFO --- it just creates the rendezvous point.
1008
1009
1010.. function:: mknod(filename[, mode=0600, device])
1011
1012 Create a filesystem node (file, device special file or named pipe) named
1013 *filename*. *mode* specifies both the permissions to use and the type of node to
1014 be created, being combined (bitwise OR) with one of ``stat.S_IFREG``,
1015 ``stat.S_IFCHR``, ``stat.S_IFBLK``,
1016 and ``stat.S_IFIFO`` (those constants are available in :mod:`stat`).
1017 For ``stat.S_IFCHR`` and
1018 ``stat.S_IFBLK``, *device* defines the newly created device special file (probably using
1019 :func:`os.makedev`), otherwise it is ignored.
1020
1021 .. versionadded:: 2.3
1022
1023
1024.. function:: major(device)
1025
Georg Brandlf725b952008-01-05 19:44:22 +00001026 Extract the device major number from a raw device number (usually the
Georg Brandl8ec7f652007-08-15 14:28:01 +00001027 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
1028
1029 .. versionadded:: 2.3
1030
1031
1032.. function:: minor(device)
1033
Georg Brandlf725b952008-01-05 19:44:22 +00001034 Extract the device minor number from a raw device number (usually the
Georg Brandl8ec7f652007-08-15 14:28:01 +00001035 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
1036
1037 .. versionadded:: 2.3
1038
1039
1040.. function:: makedev(major, minor)
1041
Georg Brandlf725b952008-01-05 19:44:22 +00001042 Compose a raw device number from the major and minor device numbers.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001043
1044 .. versionadded:: 2.3
1045
1046
1047.. function:: mkdir(path[, mode])
1048
1049 Create a directory named *path* with numeric mode *mode*. The default *mode* is
1050 ``0777`` (octal). On some systems, *mode* is ignored. Where it is used, the
Georg Brandl9af94982008-09-13 17:41:16 +00001051 current umask value is first masked out. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001052
Mark Summerfieldac3d4292007-11-02 08:24:59 +00001053 It is also possible to create temporary directories; see the
1054 :mod:`tempfile` module's :func:`tempfile.mkdtemp` function.
1055
Georg Brandl8ec7f652007-08-15 14:28:01 +00001056
1057.. function:: makedirs(path[, mode])
1058
1059 .. index::
1060 single: directory; creating
1061 single: UNC paths; and os.makedirs()
1062
1063 Recursive directory creation function. Like :func:`mkdir`, but makes all
1064 intermediate-level directories needed to contain the leaf directory. Throws an
1065 :exc:`error` exception if the leaf directory already exists or cannot be
1066 created. The default *mode* is ``0777`` (octal). On some systems, *mode* is
1067 ignored. Where it is used, the current umask value is first masked out.
1068
1069 .. note::
1070
1071 :func:`makedirs` will become confused if the path elements to create include
Georg Brandlf725b952008-01-05 19:44:22 +00001072 :data:`os.pardir`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001073
1074 .. versionadded:: 1.5.2
1075
1076 .. versionchanged:: 2.3
1077 This function now handles UNC paths correctly.
1078
1079
1080.. function:: pathconf(path, name)
1081
1082 Return system configuration information relevant to a named file. *name*
1083 specifies the configuration value to retrieve; it may be a string which is the
1084 name of a defined system value; these names are specified in a number of
1085 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
1086 additional names as well. The names known to the host operating system are
1087 given in the ``pathconf_names`` dictionary. For configuration variables not
1088 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl9af94982008-09-13 17:41:16 +00001089 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001090
1091 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1092 specific value for *name* is not supported by the host system, even if it is
1093 included in ``pathconf_names``, an :exc:`OSError` is raised with
1094 :const:`errno.EINVAL` for the error number.
1095
1096
1097.. data:: pathconf_names
1098
1099 Dictionary mapping names accepted by :func:`pathconf` and :func:`fpathconf` to
1100 the integer values defined for those names by the host operating system. This
1101 can be used to determine the set of names known to the system. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00001102 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001103
1104
1105.. function:: readlink(path)
1106
1107 Return a string representing the path to which the symbolic link points. The
1108 result may be either an absolute or relative pathname; if it is relative, it may
1109 be converted to an absolute pathname using ``os.path.join(os.path.dirname(path),
1110 result)``.
1111
1112 .. versionchanged:: 2.6
1113 If the *path* is a Unicode object the result will also be a Unicode object.
1114
Georg Brandl9af94982008-09-13 17:41:16 +00001115 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001116
1117
1118.. function:: remove(path)
1119
Georg Brandl75439972009-08-24 17:24:27 +00001120 Remove (delete) the file *path*. If *path* is a directory, :exc:`OSError` is
1121 raised; see :func:`rmdir` below to remove a directory. This is identical to
1122 the :func:`unlink` function documented below. On Windows, attempting to
1123 remove a file that is in use causes an exception to be raised; on Unix, the
1124 directory entry is removed but the storage allocated to the file is not made
1125 available until the original file is no longer in use. Availability: Unix,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001126 Windows.
1127
1128
1129.. function:: removedirs(path)
1130
1131 .. index:: single: directory; deleting
1132
Georg Brandlf725b952008-01-05 19:44:22 +00001133 Remove directories recursively. Works like :func:`rmdir` except that, if the
Georg Brandl8ec7f652007-08-15 14:28:01 +00001134 leaf directory is successfully removed, :func:`removedirs` tries to
1135 successively remove every parent directory mentioned in *path* until an error
1136 is raised (which is ignored, because it generally means that a parent directory
1137 is not empty). For example, ``os.removedirs('foo/bar/baz')`` will first remove
1138 the directory ``'foo/bar/baz'``, and then remove ``'foo/bar'`` and ``'foo'`` if
1139 they are empty. Raises :exc:`OSError` if the leaf directory could not be
1140 successfully removed.
1141
1142 .. versionadded:: 1.5.2
1143
1144
1145.. function:: rename(src, dst)
1146
1147 Rename the file or directory *src* to *dst*. If *dst* is a directory,
1148 :exc:`OSError` will be raised. On Unix, if *dst* exists and is a file, it will
Georg Brandlf725b952008-01-05 19:44:22 +00001149 be replaced silently if the user has permission. The operation may fail on some
Georg Brandl8ec7f652007-08-15 14:28:01 +00001150 Unix flavors if *src* and *dst* are on different filesystems. If successful,
1151 the renaming will be an atomic operation (this is a POSIX requirement). On
1152 Windows, if *dst* already exists, :exc:`OSError` will be raised even if it is a
1153 file; there may be no way to implement an atomic rename when *dst* names an
Georg Brandl9af94982008-09-13 17:41:16 +00001154 existing file. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001155
1156
1157.. function:: renames(old, new)
1158
1159 Recursive directory or file renaming function. Works like :func:`rename`, except
1160 creation of any intermediate directories needed to make the new pathname good is
1161 attempted first. After the rename, directories corresponding to rightmost path
1162 segments of the old name will be pruned away using :func:`removedirs`.
1163
1164 .. versionadded:: 1.5.2
1165
1166 .. note::
1167
1168 This function can fail with the new directory structure made if you lack
1169 permissions needed to remove the leaf directory or file.
1170
1171
1172.. function:: rmdir(path)
1173
Georg Brandl1b2695a2009-08-24 17:48:40 +00001174 Remove (delete) the directory *path*. Only works when the directory is
1175 empty, otherwise, :exc:`OSError` is raised. In order to remove whole
1176 directory trees, :func:`shutil.rmtree` can be used. Availability: Unix,
1177 Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001178
1179
1180.. function:: stat(path)
1181
1182 Perform a :cfunc:`stat` system call on the given path. The return value is an
1183 object whose attributes correspond to the members of the :ctype:`stat`
1184 structure, namely: :attr:`st_mode` (protection bits), :attr:`st_ino` (inode
1185 number), :attr:`st_dev` (device), :attr:`st_nlink` (number of hard links),
Georg Brandlf725b952008-01-05 19:44:22 +00001186 :attr:`st_uid` (user id of owner), :attr:`st_gid` (group id of owner),
Georg Brandl8ec7f652007-08-15 14:28:01 +00001187 :attr:`st_size` (size of file, in bytes), :attr:`st_atime` (time of most recent
1188 access), :attr:`st_mtime` (time of most recent content modification),
1189 :attr:`st_ctime` (platform dependent; time of most recent metadata change on
1190 Unix, or the time of creation on Windows)::
1191
1192 >>> import os
1193 >>> statinfo = os.stat('somefile.txt')
1194 >>> statinfo
1195 (33188, 422511L, 769L, 1, 1032, 100, 926L, 1105022698,1105022732, 1105022732)
1196 >>> statinfo.st_size
1197 926L
1198 >>>
1199
1200 .. versionchanged:: 2.3
Georg Brandlf725b952008-01-05 19:44:22 +00001201 If :func:`stat_float_times` returns ``True``, the time values are floats, measuring
Georg Brandl8ec7f652007-08-15 14:28:01 +00001202 seconds. Fractions of a second may be reported if the system supports that. On
1203 Mac OS, the times are always floats. See :func:`stat_float_times` for further
1204 discussion.
1205
1206 On some Unix systems (such as Linux), the following attributes may also be
1207 available: :attr:`st_blocks` (number of blocks allocated for file),
1208 :attr:`st_blksize` (filesystem blocksize), :attr:`st_rdev` (type of device if an
1209 inode device). :attr:`st_flags` (user defined flags for file).
1210
1211 On other Unix systems (such as FreeBSD), the following attributes may be
1212 available (but may be only filled out if root tries to use them): :attr:`st_gen`
1213 (file generation number), :attr:`st_birthtime` (time of file creation).
1214
1215 On Mac OS systems, the following attributes may also be available:
1216 :attr:`st_rsize`, :attr:`st_creator`, :attr:`st_type`.
1217
1218 On RISCOS systems, the following attributes are also available: :attr:`st_ftype`
1219 (file type), :attr:`st_attrs` (attributes), :attr:`st_obtype` (object type).
1220
1221 .. index:: module: stat
1222
1223 For backward compatibility, the return value of :func:`stat` is also accessible
1224 as a tuple of at least 10 integers giving the most important (and portable)
1225 members of the :ctype:`stat` structure, in the order :attr:`st_mode`,
1226 :attr:`st_ino`, :attr:`st_dev`, :attr:`st_nlink`, :attr:`st_uid`,
1227 :attr:`st_gid`, :attr:`st_size`, :attr:`st_atime`, :attr:`st_mtime`,
1228 :attr:`st_ctime`. More items may be added at the end by some implementations.
1229 The standard module :mod:`stat` defines functions and constants that are useful
1230 for extracting information from a :ctype:`stat` structure. (On Windows, some
1231 items are filled with dummy values.)
1232
1233 .. note::
1234
1235 The exact meaning and resolution of the :attr:`st_atime`, :attr:`st_mtime`, and
1236 :attr:`st_ctime` members depends on the operating system and the file system.
1237 For example, on Windows systems using the FAT or FAT32 file systems,
1238 :attr:`st_mtime` has 2-second resolution, and :attr:`st_atime` has only 1-day
1239 resolution. See your operating system documentation for details.
1240
Georg Brandl9af94982008-09-13 17:41:16 +00001241 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001242
1243 .. versionchanged:: 2.2
1244 Added access to values as attributes of the returned object.
1245
1246 .. versionchanged:: 2.5
Georg Brandlf725b952008-01-05 19:44:22 +00001247 Added :attr:`st_gen` and :attr:`st_birthtime`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001248
1249
1250.. function:: stat_float_times([newvalue])
1251
1252 Determine whether :class:`stat_result` represents time stamps as float objects.
1253 If *newvalue* is ``True``, future calls to :func:`stat` return floats, if it is
1254 ``False``, future calls return ints. If *newvalue* is omitted, return the
1255 current setting.
1256
1257 For compatibility with older Python versions, accessing :class:`stat_result` as
1258 a tuple always returns integers.
1259
1260 .. versionchanged:: 2.5
1261 Python now returns float values by default. Applications which do not work
1262 correctly with floating point time stamps can use this function to restore the
1263 old behaviour.
1264
1265 The resolution of the timestamps (that is the smallest possible fraction)
1266 depends on the system. Some systems only support second resolution; on these
1267 systems, the fraction will always be zero.
1268
1269 It is recommended that this setting is only changed at program startup time in
1270 the *__main__* module; libraries should never change this setting. If an
1271 application uses a library that works incorrectly if floating point time stamps
1272 are processed, this application should turn the feature off until the library
1273 has been corrected.
1274
1275
1276.. function:: statvfs(path)
1277
1278 Perform a :cfunc:`statvfs` system call on the given path. The return value is
1279 an object whose attributes describe the filesystem on the given path, and
1280 correspond to the members of the :ctype:`statvfs` structure, namely:
1281 :attr:`f_bsize`, :attr:`f_frsize`, :attr:`f_blocks`, :attr:`f_bfree`,
1282 :attr:`f_bavail`, :attr:`f_files`, :attr:`f_ffree`, :attr:`f_favail`,
1283 :attr:`f_flag`, :attr:`f_namemax`. Availability: Unix.
1284
1285 .. index:: module: statvfs
1286
1287 For backward compatibility, the return value is also accessible as a tuple whose
1288 values correspond to the attributes, in the order given above. The standard
1289 module :mod:`statvfs` defines constants that are useful for extracting
1290 information from a :ctype:`statvfs` structure when accessing it as a sequence;
1291 this remains useful when writing code that needs to work with versions of Python
1292 that don't support accessing the fields as attributes.
1293
1294 .. versionchanged:: 2.2
1295 Added access to values as attributes of the returned object.
1296
1297
Benjamin Peterson0e928582009-03-28 19:16:10 +00001298.. function:: symlink(source, link_name)
Georg Brandl8ec7f652007-08-15 14:28:01 +00001299
Benjamin Peterson0e928582009-03-28 19:16:10 +00001300 Create a symbolic link pointing to *source* named *link_name*. Availability:
1301 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001302
1303
1304.. function:: tempnam([dir[, prefix]])
1305
1306 Return a unique path name that is reasonable for creating a temporary file.
1307 This will be an absolute path that names a potential directory entry in the
1308 directory *dir* or a common location for temporary files if *dir* is omitted or
1309 ``None``. If given and not ``None``, *prefix* is used to provide a short prefix
1310 to the filename. Applications are responsible for properly creating and
1311 managing files created using paths returned by :func:`tempnam`; no automatic
1312 cleanup is provided. On Unix, the environment variable :envvar:`TMPDIR`
Georg Brandlf725b952008-01-05 19:44:22 +00001313 overrides *dir*, while on Windows :envvar:`TMP` is used. The specific
Georg Brandl8ec7f652007-08-15 14:28:01 +00001314 behavior of this function depends on the C library implementation; some aspects
1315 are underspecified in system documentation.
1316
1317 .. warning::
1318
1319 Use of :func:`tempnam` is vulnerable to symlink attacks; consider using
1320 :func:`tmpfile` (section :ref:`os-newstreams`) instead.
1321
Georg Brandl9af94982008-09-13 17:41:16 +00001322 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001323
1324
1325.. function:: tmpnam()
1326
1327 Return a unique path name that is reasonable for creating a temporary file.
1328 This will be an absolute path that names a potential directory entry in a common
1329 location for temporary files. Applications are responsible for properly
1330 creating and managing files created using paths returned by :func:`tmpnam`; no
1331 automatic cleanup is provided.
1332
1333 .. warning::
1334
1335 Use of :func:`tmpnam` is vulnerable to symlink attacks; consider using
1336 :func:`tmpfile` (section :ref:`os-newstreams`) instead.
1337
1338 Availability: Unix, Windows. This function probably shouldn't be used on
1339 Windows, though: Microsoft's implementation of :func:`tmpnam` always creates a
1340 name in the root directory of the current drive, and that's generally a poor
1341 location for a temp file (depending on privileges, you may not even be able to
1342 open a file using this name).
1343
1344
1345.. data:: TMP_MAX
1346
1347 The maximum number of unique names that :func:`tmpnam` will generate before
1348 reusing names.
1349
1350
1351.. function:: unlink(path)
1352
Georg Brandl75439972009-08-24 17:24:27 +00001353 Remove (delete) the file *path*. This is the same function as
1354 :func:`remove`; the :func:`unlink` name is its traditional Unix
1355 name. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001356
1357
1358.. function:: utime(path, times)
1359
Benjamin Peterson5b02ef32008-08-16 03:13:07 +00001360 Set the access and modified times of the file specified by *path*. If *times*
1361 is ``None``, then the file's access and modified times are set to the current
1362 time. (The effect is similar to running the Unix program :program:`touch` on
1363 the path.) Otherwise, *times* must be a 2-tuple of numbers, of the form
1364 ``(atime, mtime)`` which is used to set the access and modified times,
1365 respectively. Whether a directory can be given for *path* depends on whether
1366 the operating system implements directories as files (for example, Windows
1367 does not). Note that the exact times you set here may not be returned by a
1368 subsequent :func:`stat` call, depending on the resolution with which your
1369 operating system records access and modification times; see :func:`stat`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001370
1371 .. versionchanged:: 2.0
1372 Added support for ``None`` for *times*.
1373
Georg Brandl9af94982008-09-13 17:41:16 +00001374 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001375
1376
1377.. function:: walk(top[, topdown=True [, onerror=None[, followlinks=False]]])
1378
1379 .. index::
1380 single: directory; walking
1381 single: directory; traversal
1382
Georg Brandlf725b952008-01-05 19:44:22 +00001383 Generate the file names in a directory tree by walking the tree
1384 either top-down or bottom-up. For each directory in the tree rooted at directory
Georg Brandl8ec7f652007-08-15 14:28:01 +00001385 *top* (including *top* itself), it yields a 3-tuple ``(dirpath, dirnames,
1386 filenames)``.
1387
1388 *dirpath* is a string, the path to the directory. *dirnames* is a list of the
1389 names of the subdirectories in *dirpath* (excluding ``'.'`` and ``'..'``).
1390 *filenames* is a list of the names of the non-directory files in *dirpath*.
1391 Note that the names in the lists contain no path components. To get a full path
1392 (which begins with *top*) to a file or directory in *dirpath*, do
1393 ``os.path.join(dirpath, name)``.
1394
Georg Brandlf725b952008-01-05 19:44:22 +00001395 If optional argument *topdown* is ``True`` or not specified, the triple for a
Georg Brandl8ec7f652007-08-15 14:28:01 +00001396 directory is generated before the triples for any of its subdirectories
Georg Brandlf725b952008-01-05 19:44:22 +00001397 (directories are generated top-down). If *topdown* is ``False``, the triple for a
Georg Brandl8ec7f652007-08-15 14:28:01 +00001398 directory is generated after the triples for all of its subdirectories
Georg Brandlf725b952008-01-05 19:44:22 +00001399 (directories are generated bottom-up).
Georg Brandl8ec7f652007-08-15 14:28:01 +00001400
Georg Brandlf725b952008-01-05 19:44:22 +00001401 When *topdown* is ``True``, the caller can modify the *dirnames* list in-place
Georg Brandl8ec7f652007-08-15 14:28:01 +00001402 (perhaps using :keyword:`del` or slice assignment), and :func:`walk` will only
1403 recurse into the subdirectories whose names remain in *dirnames*; this can be
1404 used to prune the search, impose a specific order of visiting, or even to inform
1405 :func:`walk` about directories the caller creates or renames before it resumes
Georg Brandlf725b952008-01-05 19:44:22 +00001406 :func:`walk` again. Modifying *dirnames* when *topdown* is ``False`` is
Georg Brandl8ec7f652007-08-15 14:28:01 +00001407 ineffective, because in bottom-up mode the directories in *dirnames* are
1408 generated before *dirpath* itself is generated.
1409
Georg Brandlf725b952008-01-05 19:44:22 +00001410 By default errors from the :func:`listdir` call are ignored. If optional
Georg Brandl8ec7f652007-08-15 14:28:01 +00001411 argument *onerror* is specified, it should be a function; it will be called with
1412 one argument, an :exc:`OSError` instance. It can report the error to continue
1413 with the walk, or raise the exception to abort the walk. Note that the filename
1414 is available as the ``filename`` attribute of the exception object.
1415
1416 By default, :func:`walk` will not walk down into symbolic links that resolve to
Georg Brandlf725b952008-01-05 19:44:22 +00001417 directories. Set *followlinks* to ``True`` to visit directories pointed to by
Georg Brandl8ec7f652007-08-15 14:28:01 +00001418 symlinks, on systems that support them.
1419
1420 .. versionadded:: 2.6
1421 The *followlinks* parameter.
1422
1423 .. note::
1424
Georg Brandlf725b952008-01-05 19:44:22 +00001425 Be aware that setting *followlinks* to ``True`` can lead to infinite recursion if a
Georg Brandl8ec7f652007-08-15 14:28:01 +00001426 link points to a parent directory of itself. :func:`walk` does not keep track of
1427 the directories it visited already.
1428
1429 .. note::
1430
1431 If you pass a relative pathname, don't change the current working directory
1432 between resumptions of :func:`walk`. :func:`walk` never changes the current
1433 directory, and assumes that its caller doesn't either.
1434
1435 This example displays the number of bytes taken by non-directory files in each
1436 directory under the starting directory, except that it doesn't look under any
1437 CVS subdirectory::
1438
1439 import os
1440 from os.path import join, getsize
1441 for root, dirs, files in os.walk('python/Lib/email'):
1442 print root, "consumes",
1443 print sum(getsize(join(root, name)) for name in files),
1444 print "bytes in", len(files), "non-directory files"
1445 if 'CVS' in dirs:
1446 dirs.remove('CVS') # don't visit CVS directories
1447
Georg Brandlf725b952008-01-05 19:44:22 +00001448 In the next example, walking the tree bottom-up is essential: :func:`rmdir`
Georg Brandl8ec7f652007-08-15 14:28:01 +00001449 doesn't allow deleting a directory before the directory is empty::
1450
Georg Brandlf725b952008-01-05 19:44:22 +00001451 # Delete everything reachable from the directory named in "top",
Georg Brandl8ec7f652007-08-15 14:28:01 +00001452 # assuming there are no symbolic links.
1453 # CAUTION: This is dangerous! For example, if top == '/', it
1454 # could delete all your disk files.
1455 import os
1456 for root, dirs, files in os.walk(top, topdown=False):
1457 for name in files:
1458 os.remove(os.path.join(root, name))
1459 for name in dirs:
1460 os.rmdir(os.path.join(root, name))
1461
1462 .. versionadded:: 2.3
1463
1464
1465.. _os-process:
1466
1467Process Management
1468------------------
1469
1470These functions may be used to create and manage processes.
1471
1472The various :func:`exec\*` functions take a list of arguments for the new
1473program loaded into the process. In each case, the first of these arguments is
1474passed to the new program as its own name rather than as an argument a user may
1475have typed on a command line. For the C programmer, this is the ``argv[0]``
1476passed to a program's :cfunc:`main`. For example, ``os.execv('/bin/echo',
1477['foo', 'bar'])`` will only print ``bar`` on standard output; ``foo`` will seem
1478to be ignored.
1479
1480
1481.. function:: abort()
1482
1483 Generate a :const:`SIGABRT` signal to the current process. On Unix, the default
1484 behavior is to produce a core dump; on Windows, the process immediately returns
1485 an exit code of ``3``. Be aware that programs which use :func:`signal.signal`
1486 to register a handler for :const:`SIGABRT` will behave differently.
Georg Brandl9af94982008-09-13 17:41:16 +00001487 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001488
1489
1490.. function:: execl(path, arg0, arg1, ...)
1491 execle(path, arg0, arg1, ..., env)
1492 execlp(file, arg0, arg1, ...)
1493 execlpe(file, arg0, arg1, ..., env)
1494 execv(path, args)
1495 execve(path, args, env)
1496 execvp(file, args)
1497 execvpe(file, args, env)
1498
1499 These functions all execute a new program, replacing the current process; they
1500 do not return. On Unix, the new executable is loaded into the current process,
Georg Brandlf725b952008-01-05 19:44:22 +00001501 and will have the same process id as the caller. Errors will be reported as
Georg Brandlc62ef8b2009-01-03 20:55:06 +00001502 :exc:`OSError` exceptions.
Andrew M. Kuchlingac771662008-09-28 00:15:27 +00001503
1504 The current process is replaced immediately. Open file objects and
1505 descriptors are not flushed, so if there may be data buffered
1506 on these open files, you should flush them using
1507 :func:`sys.stdout.flush` or :func:`os.fsync` before calling an
1508 :func:`exec\*` function.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001509
Georg Brandlf725b952008-01-05 19:44:22 +00001510 The "l" and "v" variants of the :func:`exec\*` functions differ in how
1511 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl8ec7f652007-08-15 14:28:01 +00001512 to work with if the number of parameters is fixed when the code is written; the
1513 individual parameters simply become additional parameters to the :func:`execl\*`
Georg Brandlf725b952008-01-05 19:44:22 +00001514 functions. The "v" variants are good when the number of parameters is
Georg Brandl8ec7f652007-08-15 14:28:01 +00001515 variable, with the arguments being passed in a list or tuple as the *args*
1516 parameter. In either case, the arguments to the child process should start with
1517 the name of the command being run, but this is not enforced.
1518
Georg Brandlf725b952008-01-05 19:44:22 +00001519 The variants which include a "p" near the end (:func:`execlp`,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001520 :func:`execlpe`, :func:`execvp`, and :func:`execvpe`) will use the
1521 :envvar:`PATH` environment variable to locate the program *file*. When the
1522 environment is being replaced (using one of the :func:`exec\*e` variants,
1523 discussed in the next paragraph), the new environment is used as the source of
1524 the :envvar:`PATH` variable. The other variants, :func:`execl`, :func:`execle`,
1525 :func:`execv`, and :func:`execve`, will not use the :envvar:`PATH` variable to
1526 locate the executable; *path* must contain an appropriate absolute or relative
1527 path.
1528
1529 For :func:`execle`, :func:`execlpe`, :func:`execve`, and :func:`execvpe` (note
Georg Brandlf725b952008-01-05 19:44:22 +00001530 that these all end in "e"), the *env* parameter must be a mapping which is
Georg Brandlfb246c42008-04-19 16:58:28 +00001531 used to define the environment variables for the new process (these are used
1532 instead of the current process' environment); the functions :func:`execl`,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001533 :func:`execlp`, :func:`execv`, and :func:`execvp` all cause the new process to
Georg Brandlc62ef8b2009-01-03 20:55:06 +00001534 inherit the environment of the current process.
Andrew M. Kuchlingac771662008-09-28 00:15:27 +00001535
1536 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001537
1538
1539.. function:: _exit(n)
1540
1541 Exit to the system with status *n*, without calling cleanup handlers, flushing
Georg Brandl9af94982008-09-13 17:41:16 +00001542 stdio buffers, etc. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001543
1544 .. note::
1545
1546 The standard way to exit is ``sys.exit(n)``. :func:`_exit` should normally only
1547 be used in the child process after a :func:`fork`.
1548
Georg Brandlf725b952008-01-05 19:44:22 +00001549The following exit codes are defined and can be used with :func:`_exit`,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001550although they are not required. These are typically used for system programs
1551written in Python, such as a mail server's external command delivery program.
1552
1553.. note::
1554
1555 Some of these may not be available on all Unix platforms, since there is some
1556 variation. These constants are defined where they are defined by the underlying
1557 platform.
1558
1559
1560.. data:: EX_OK
1561
Georg Brandl9af94982008-09-13 17:41:16 +00001562 Exit code that means no error occurred. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001563
1564 .. versionadded:: 2.3
1565
1566
1567.. data:: EX_USAGE
1568
1569 Exit code that means the command was used incorrectly, such as when the wrong
Georg Brandl9af94982008-09-13 17:41:16 +00001570 number of arguments are given. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001571
1572 .. versionadded:: 2.3
1573
1574
1575.. data:: EX_DATAERR
1576
Georg Brandl9af94982008-09-13 17:41:16 +00001577 Exit code that means the input data was incorrect. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001578
1579 .. versionadded:: 2.3
1580
1581
1582.. data:: EX_NOINPUT
1583
1584 Exit code that means an input file did not exist or was not readable.
Georg Brandl9af94982008-09-13 17:41:16 +00001585 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001586
1587 .. versionadded:: 2.3
1588
1589
1590.. data:: EX_NOUSER
1591
Georg Brandl9af94982008-09-13 17:41:16 +00001592 Exit code that means a specified user did not exist. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001593
1594 .. versionadded:: 2.3
1595
1596
1597.. data:: EX_NOHOST
1598
Georg Brandl9af94982008-09-13 17:41:16 +00001599 Exit code that means a specified host did not exist. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001600
1601 .. versionadded:: 2.3
1602
1603
1604.. data:: EX_UNAVAILABLE
1605
1606 Exit code that means that a required service is unavailable. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00001607 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001608
1609 .. versionadded:: 2.3
1610
1611
1612.. data:: EX_SOFTWARE
1613
1614 Exit code that means an internal software error was detected. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00001615 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001616
1617 .. versionadded:: 2.3
1618
1619
1620.. data:: EX_OSERR
1621
1622 Exit code that means an operating system error was detected, such as the
Georg Brandl9af94982008-09-13 17:41:16 +00001623 inability to fork or create a pipe. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001624
1625 .. versionadded:: 2.3
1626
1627
1628.. data:: EX_OSFILE
1629
1630 Exit code that means some system file did not exist, could not be opened, or had
Georg Brandl9af94982008-09-13 17:41:16 +00001631 some other kind of error. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001632
1633 .. versionadded:: 2.3
1634
1635
1636.. data:: EX_CANTCREAT
1637
1638 Exit code that means a user specified output file could not be created.
Georg Brandl9af94982008-09-13 17:41:16 +00001639 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001640
1641 .. versionadded:: 2.3
1642
1643
1644.. data:: EX_IOERR
1645
1646 Exit code that means that an error occurred while doing I/O on some file.
Georg Brandl9af94982008-09-13 17:41:16 +00001647 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001648
1649 .. versionadded:: 2.3
1650
1651
1652.. data:: EX_TEMPFAIL
1653
1654 Exit code that means a temporary failure occurred. This indicates something
1655 that may not really be an error, such as a network connection that couldn't be
Georg Brandl9af94982008-09-13 17:41:16 +00001656 made during a retryable operation. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001657
1658 .. versionadded:: 2.3
1659
1660
1661.. data:: EX_PROTOCOL
1662
1663 Exit code that means that a protocol exchange was illegal, invalid, or not
Georg Brandl9af94982008-09-13 17:41:16 +00001664 understood. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001665
1666 .. versionadded:: 2.3
1667
1668
1669.. data:: EX_NOPERM
1670
1671 Exit code that means that there were insufficient permissions to perform the
Georg Brandl9af94982008-09-13 17:41:16 +00001672 operation (but not intended for file system problems). Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001673
1674 .. versionadded:: 2.3
1675
1676
1677.. data:: EX_CONFIG
1678
1679 Exit code that means that some kind of configuration error occurred.
Georg Brandl9af94982008-09-13 17:41:16 +00001680 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001681
1682 .. versionadded:: 2.3
1683
1684
1685.. data:: EX_NOTFOUND
1686
1687 Exit code that means something like "an entry was not found". Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00001688 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001689
1690 .. versionadded:: 2.3
1691
1692
1693.. function:: fork()
1694
Georg Brandlf725b952008-01-05 19:44:22 +00001695 Fork a child process. Return ``0`` in the child and the child's process id in the
Skip Montanaro75e51682008-03-15 02:32:49 +00001696 parent. If an error occurs :exc:`OSError` is raised.
Gregory P. Smith08067492008-09-30 20:41:13 +00001697
1698 Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have
1699 known issues when using fork() from a thread.
1700
Georg Brandl9af94982008-09-13 17:41:16 +00001701 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001702
1703
1704.. function:: forkpty()
1705
1706 Fork a child process, using a new pseudo-terminal as the child's controlling
1707 terminal. Return a pair of ``(pid, fd)``, where *pid* is ``0`` in the child, the
1708 new child's process id in the parent, and *fd* is the file descriptor of the
1709 master end of the pseudo-terminal. For a more portable approach, use the
Skip Montanaro75e51682008-03-15 02:32:49 +00001710 :mod:`pty` module. If an error occurs :exc:`OSError` is raised.
Georg Brandl9af94982008-09-13 17:41:16 +00001711 Availability: some flavors of Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001712
1713
1714.. function:: kill(pid, sig)
1715
1716 .. index::
1717 single: process; killing
1718 single: process; signalling
1719
1720 Send signal *sig* to the process *pid*. Constants for the specific signals
1721 available on the host platform are defined in the :mod:`signal` module.
Georg Brandl9af94982008-09-13 17:41:16 +00001722 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001723
1724
1725.. function:: killpg(pgid, sig)
1726
1727 .. index::
1728 single: process; killing
1729 single: process; signalling
1730
Georg Brandl9af94982008-09-13 17:41:16 +00001731 Send the signal *sig* to the process group *pgid*. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001732
1733 .. versionadded:: 2.3
1734
1735
1736.. function:: nice(increment)
1737
1738 Add *increment* to the process's "niceness". Return the new niceness.
Georg Brandl9af94982008-09-13 17:41:16 +00001739 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001740
1741
1742.. function:: plock(op)
1743
1744 Lock program segments into memory. The value of *op* (defined in
Georg Brandl9af94982008-09-13 17:41:16 +00001745 ``<sys/lock.h>``) determines which segments are locked. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001746
1747
1748.. function:: popen(...)
1749 popen2(...)
1750 popen3(...)
1751 popen4(...)
1752 :noindex:
1753
1754 Run child processes, returning opened pipes for communications. These functions
1755 are described in section :ref:`os-newstreams`.
1756
1757
1758.. function:: spawnl(mode, path, ...)
1759 spawnle(mode, path, ..., env)
1760 spawnlp(mode, file, ...)
1761 spawnlpe(mode, file, ..., env)
1762 spawnv(mode, path, args)
1763 spawnve(mode, path, args, env)
1764 spawnvp(mode, file, args)
1765 spawnvpe(mode, file, args, env)
1766
1767 Execute the program *path* in a new process.
1768
1769 (Note that the :mod:`subprocess` module provides more powerful facilities for
1770 spawning new processes and retrieving their results; using that module is
R. David Murrayccb9d4b2009-06-09 00:44:22 +00001771 preferable to using these functions. Check especially the
1772 :ref:`subprocess-replacements` section.)
Georg Brandl8ec7f652007-08-15 14:28:01 +00001773
Georg Brandlf725b952008-01-05 19:44:22 +00001774 If *mode* is :const:`P_NOWAIT`, this function returns the process id of the new
Georg Brandl8ec7f652007-08-15 14:28:01 +00001775 process; if *mode* is :const:`P_WAIT`, returns the process's exit code if it
1776 exits normally, or ``-signal``, where *signal* is the signal that killed the
Georg Brandlf725b952008-01-05 19:44:22 +00001777 process. On Windows, the process id will actually be the process handle, so can
Georg Brandl8ec7f652007-08-15 14:28:01 +00001778 be used with the :func:`waitpid` function.
1779
Georg Brandlf725b952008-01-05 19:44:22 +00001780 The "l" and "v" variants of the :func:`spawn\*` functions differ in how
1781 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl8ec7f652007-08-15 14:28:01 +00001782 to work with if the number of parameters is fixed when the code is written; the
1783 individual parameters simply become additional parameters to the
Georg Brandlf725b952008-01-05 19:44:22 +00001784 :func:`spawnl\*` functions. The "v" variants are good when the number of
Georg Brandl8ec7f652007-08-15 14:28:01 +00001785 parameters is variable, with the arguments being passed in a list or tuple as
1786 the *args* parameter. In either case, the arguments to the child process must
1787 start with the name of the command being run.
1788
Georg Brandlf725b952008-01-05 19:44:22 +00001789 The variants which include a second "p" near the end (:func:`spawnlp`,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001790 :func:`spawnlpe`, :func:`spawnvp`, and :func:`spawnvpe`) will use the
1791 :envvar:`PATH` environment variable to locate the program *file*. When the
1792 environment is being replaced (using one of the :func:`spawn\*e` variants,
1793 discussed in the next paragraph), the new environment is used as the source of
1794 the :envvar:`PATH` variable. The other variants, :func:`spawnl`,
1795 :func:`spawnle`, :func:`spawnv`, and :func:`spawnve`, will not use the
1796 :envvar:`PATH` variable to locate the executable; *path* must contain an
1797 appropriate absolute or relative path.
1798
1799 For :func:`spawnle`, :func:`spawnlpe`, :func:`spawnve`, and :func:`spawnvpe`
Georg Brandlf725b952008-01-05 19:44:22 +00001800 (note that these all end in "e"), the *env* parameter must be a mapping
Georg Brandlfb246c42008-04-19 16:58:28 +00001801 which is used to define the environment variables for the new process (they are
1802 used instead of the current process' environment); the functions
Georg Brandl8ec7f652007-08-15 14:28:01 +00001803 :func:`spawnl`, :func:`spawnlp`, :func:`spawnv`, and :func:`spawnvp` all cause
Georg Brandl22717df2009-03-31 18:26:55 +00001804 the new process to inherit the environment of the current process. Note that
1805 keys and values in the *env* dictionary must be strings; invalid keys or
1806 values will cause the function to fail, with a return value of ``127``.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001807
1808 As an example, the following calls to :func:`spawnlp` and :func:`spawnvpe` are
1809 equivalent::
1810
1811 import os
1812 os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1813
1814 L = ['cp', 'index.html', '/dev/null']
1815 os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1816
1817 Availability: Unix, Windows. :func:`spawnlp`, :func:`spawnlpe`, :func:`spawnvp`
1818 and :func:`spawnvpe` are not available on Windows.
1819
1820 .. versionadded:: 1.6
1821
1822
1823.. data:: P_NOWAIT
1824 P_NOWAITO
1825
1826 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1827 functions. If either of these values is given, the :func:`spawn\*` functions
Georg Brandlf725b952008-01-05 19:44:22 +00001828 will return as soon as the new process has been created, with the process id as
Georg Brandl9af94982008-09-13 17:41:16 +00001829 the return value. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001830
1831 .. versionadded:: 1.6
1832
1833
1834.. data:: P_WAIT
1835
1836 Possible value for the *mode* parameter to the :func:`spawn\*` family of
1837 functions. If this is given as *mode*, the :func:`spawn\*` functions will not
1838 return until the new process has run to completion and will return the exit code
1839 of the process the run is successful, or ``-signal`` if a signal kills the
Georg Brandl9af94982008-09-13 17:41:16 +00001840 process. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001841
1842 .. versionadded:: 1.6
1843
1844
1845.. data:: P_DETACH
1846 P_OVERLAY
1847
1848 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1849 functions. These are less portable than those listed above. :const:`P_DETACH`
1850 is similar to :const:`P_NOWAIT`, but the new process is detached from the
1851 console of the calling process. If :const:`P_OVERLAY` is used, the current
1852 process will be replaced; the :func:`spawn\*` function will not return.
1853 Availability: Windows.
1854
1855 .. versionadded:: 1.6
1856
1857
1858.. function:: startfile(path[, operation])
1859
1860 Start a file with its associated application.
1861
1862 When *operation* is not specified or ``'open'``, this acts like double-clicking
1863 the file in Windows Explorer, or giving the file name as an argument to the
1864 :program:`start` command from the interactive command shell: the file is opened
1865 with whatever application (if any) its extension is associated.
1866
1867 When another *operation* is given, it must be a "command verb" that specifies
1868 what should be done with the file. Common verbs documented by Microsoft are
1869 ``'print'`` and ``'edit'`` (to be used on files) as well as ``'explore'`` and
1870 ``'find'`` (to be used on directories).
1871
1872 :func:`startfile` returns as soon as the associated application is launched.
1873 There is no option to wait for the application to close, and no way to retrieve
1874 the application's exit status. The *path* parameter is relative to the current
1875 directory. If you want to use an absolute path, make sure the first character
1876 is not a slash (``'/'``); the underlying Win32 :cfunc:`ShellExecute` function
1877 doesn't work if it is. Use the :func:`os.path.normpath` function to ensure that
1878 the path is properly encoded for Win32. Availability: Windows.
1879
1880 .. versionadded:: 2.0
1881
1882 .. versionadded:: 2.5
1883 The *operation* parameter.
1884
1885
1886.. function:: system(command)
1887
1888 Execute the command (a string) in a subshell. This is implemented by calling
Georg Brandl647e9d22009-10-14 15:57:46 +00001889 the Standard C function :cfunc:`system`, and has the same limitations.
Georg Brandl11abfe62009-10-18 07:58:12 +00001890 Changes to :data:`sys.stdin`, etc. are not reflected in the environment of the
Georg Brandl647e9d22009-10-14 15:57:46 +00001891 executed command.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001892
1893 On Unix, the return value is the exit status of the process encoded in the
1894 format specified for :func:`wait`. Note that POSIX does not specify the meaning
1895 of the return value of the C :cfunc:`system` function, so the return value of
1896 the Python function is system-dependent.
1897
1898 On Windows, the return value is that returned by the system shell after running
1899 *command*, given by the Windows environment variable :envvar:`COMSPEC`: on
1900 :program:`command.com` systems (Windows 95, 98 and ME) this is always ``0``; on
1901 :program:`cmd.exe` systems (Windows NT, 2000 and XP) this is the exit status of
1902 the command run; on systems using a non-native shell, consult your shell
1903 documentation.
1904
Georg Brandl9af94982008-09-13 17:41:16 +00001905 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001906
1907 The :mod:`subprocess` module provides more powerful facilities for spawning new
1908 processes and retrieving their results; using that module is preferable to using
Georg Brandl0ba92b22008-06-22 09:05:29 +00001909 this function. Use the :mod:`subprocess` module. Check especially the
1910 :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001911
1912
1913.. function:: times()
1914
1915 Return a 5-tuple of floating point numbers indicating accumulated (processor or
1916 other) times, in seconds. The items are: user time, system time, children's
1917 user time, children's system time, and elapsed real time since a fixed point in
1918 the past, in that order. See the Unix manual page :manpage:`times(2)` or the
Georg Brandl9af94982008-09-13 17:41:16 +00001919 corresponding Windows Platform API documentation. Availability: Unix,
Georg Brandl0a40ffb2008-02-13 07:20:22 +00001920 Windows. On Windows, only the first two items are filled, the others are zero.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001921
1922
1923.. function:: wait()
1924
1925 Wait for completion of a child process, and return a tuple containing its pid
1926 and exit status indication: a 16-bit number, whose low byte is the signal number
1927 that killed the process, and whose high byte is the exit status (if the signal
1928 number is zero); the high bit of the low byte is set if a core file was
Georg Brandl9af94982008-09-13 17:41:16 +00001929 produced. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001930
1931
1932.. function:: waitpid(pid, options)
1933
1934 The details of this function differ on Unix and Windows.
1935
1936 On Unix: Wait for completion of a child process given by process id *pid*, and
1937 return a tuple containing its process id and exit status indication (encoded as
1938 for :func:`wait`). The semantics of the call are affected by the value of the
1939 integer *options*, which should be ``0`` for normal operation.
1940
1941 If *pid* is greater than ``0``, :func:`waitpid` requests status information for
1942 that specific process. If *pid* is ``0``, the request is for the status of any
1943 child in the process group of the current process. If *pid* is ``-1``, the
1944 request pertains to any child of the current process. If *pid* is less than
1945 ``-1``, status is requested for any process in the process group ``-pid`` (the
1946 absolute value of *pid*).
1947
Gregory P. Smith59de7f52008-08-15 23:14:00 +00001948 An :exc:`OSError` is raised with the value of errno when the syscall
1949 returns -1.
1950
Georg Brandl8ec7f652007-08-15 14:28:01 +00001951 On Windows: Wait for completion of a process given by process handle *pid*, and
1952 return a tuple containing *pid*, and its exit status shifted left by 8 bits
1953 (shifting makes cross-platform use of the function easier). A *pid* less than or
1954 equal to ``0`` has no special meaning on Windows, and raises an exception. The
1955 value of integer *options* has no effect. *pid* can refer to any process whose
1956 id is known, not necessarily a child process. The :func:`spawn` functions called
1957 with :const:`P_NOWAIT` return suitable process handles.
1958
1959
1960.. function:: wait3([options])
1961
1962 Similar to :func:`waitpid`, except no process id argument is given and a
1963 3-element tuple containing the child's process id, exit status indication, and
1964 resource usage information is returned. Refer to :mod:`resource`.\
1965 :func:`getrusage` for details on resource usage information. The option
1966 argument is the same as that provided to :func:`waitpid` and :func:`wait4`.
1967 Availability: Unix.
1968
1969 .. versionadded:: 2.5
1970
1971
1972.. function:: wait4(pid, options)
1973
1974 Similar to :func:`waitpid`, except a 3-element tuple, containing the child's
1975 process id, exit status indication, and resource usage information is returned.
1976 Refer to :mod:`resource`.\ :func:`getrusage` for details on resource usage
1977 information. The arguments to :func:`wait4` are the same as those provided to
1978 :func:`waitpid`. Availability: Unix.
1979
1980 .. versionadded:: 2.5
1981
1982
1983.. data:: WNOHANG
1984
1985 The option for :func:`waitpid` to return immediately if no child process status
1986 is available immediately. The function returns ``(0, 0)`` in this case.
Georg Brandl9af94982008-09-13 17:41:16 +00001987 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001988
1989
1990.. data:: WCONTINUED
1991
1992 This option causes child processes to be reported if they have been continued
1993 from a job control stop since their status was last reported. Availability: Some
1994 Unix systems.
1995
1996 .. versionadded:: 2.3
1997
1998
1999.. data:: WUNTRACED
2000
2001 This option causes child processes to be reported if they have been stopped but
2002 their current state has not been reported since they were stopped. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00002003 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002004
2005 .. versionadded:: 2.3
2006
2007The following functions take a process status code as returned by
2008:func:`system`, :func:`wait`, or :func:`waitpid` as a parameter. They may be
2009used to determine the disposition of a process.
2010
2011
2012.. function:: WCOREDUMP(status)
2013
Georg Brandlf725b952008-01-05 19:44:22 +00002014 Return ``True`` if a core dump was generated for the process, otherwise
Georg Brandl9af94982008-09-13 17:41:16 +00002015 return ``False``. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002016
2017 .. versionadded:: 2.3
2018
2019
2020.. function:: WIFCONTINUED(status)
2021
Georg Brandlf725b952008-01-05 19:44:22 +00002022 Return ``True`` if the process has been continued from a job control stop,
2023 otherwise return ``False``. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002024
2025 .. versionadded:: 2.3
2026
2027
2028.. function:: WIFSTOPPED(status)
2029
Georg Brandlf725b952008-01-05 19:44:22 +00002030 Return ``True`` if the process has been stopped, otherwise return
Georg Brandl8ec7f652007-08-15 14:28:01 +00002031 ``False``. Availability: Unix.
2032
2033
2034.. function:: WIFSIGNALED(status)
2035
Georg Brandlf725b952008-01-05 19:44:22 +00002036 Return ``True`` if the process exited due to a signal, otherwise return
Georg Brandl9af94982008-09-13 17:41:16 +00002037 ``False``. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002038
2039
2040.. function:: WIFEXITED(status)
2041
Georg Brandlf725b952008-01-05 19:44:22 +00002042 Return ``True`` if the process exited using the :manpage:`exit(2)` system call,
Georg Brandl9af94982008-09-13 17:41:16 +00002043 otherwise return ``False``. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002044
2045
2046.. function:: WEXITSTATUS(status)
2047
2048 If ``WIFEXITED(status)`` is true, return the integer parameter to the
2049 :manpage:`exit(2)` system call. Otherwise, the return value is meaningless.
Georg Brandl9af94982008-09-13 17:41:16 +00002050 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002051
2052
2053.. function:: WSTOPSIG(status)
2054
Georg Brandl9af94982008-09-13 17:41:16 +00002055 Return the signal which caused the process to stop. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002056
2057
2058.. function:: WTERMSIG(status)
2059
Georg Brandl9af94982008-09-13 17:41:16 +00002060 Return the signal which caused the process to exit. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002061
2062
2063.. _os-path:
2064
2065Miscellaneous System Information
2066--------------------------------
2067
2068
2069.. function:: confstr(name)
2070
2071 Return string-valued system configuration values. *name* specifies the
2072 configuration value to retrieve; it may be a string which is the name of a
2073 defined system value; these names are specified in a number of standards (POSIX,
2074 Unix 95, Unix 98, and others). Some platforms define additional names as well.
2075 The names known to the host operating system are given as the keys of the
2076 ``confstr_names`` dictionary. For configuration variables not included in that
2077 mapping, passing an integer for *name* is also accepted. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00002078 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002079
2080 If the configuration value specified by *name* isn't defined, ``None`` is
2081 returned.
2082
2083 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
2084 specific value for *name* is not supported by the host system, even if it is
2085 included in ``confstr_names``, an :exc:`OSError` is raised with
2086 :const:`errno.EINVAL` for the error number.
2087
2088
2089.. data:: confstr_names
2090
2091 Dictionary mapping names accepted by :func:`confstr` to the integer values
2092 defined for those names by the host operating system. This can be used to
Georg Brandl9af94982008-09-13 17:41:16 +00002093 determine the set of names known to the system. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002094
2095
2096.. function:: getloadavg()
2097
Georg Brandl57fe0f22008-01-12 10:53:29 +00002098 Return the number of processes in the system run queue averaged over the last
2099 1, 5, and 15 minutes or raises :exc:`OSError` if the load average was
Georg Brandl6bb7bcf2008-05-30 19:12:13 +00002100 unobtainable. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002101
2102 .. versionadded:: 2.3
2103
2104
2105.. function:: sysconf(name)
2106
2107 Return integer-valued system configuration values. If the configuration value
2108 specified by *name* isn't defined, ``-1`` is returned. The comments regarding
2109 the *name* parameter for :func:`confstr` apply here as well; the dictionary that
2110 provides information on the known names is given by ``sysconf_names``.
Georg Brandl9af94982008-09-13 17:41:16 +00002111 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002112
2113
2114.. data:: sysconf_names
2115
2116 Dictionary mapping names accepted by :func:`sysconf` to the integer values
2117 defined for those names by the host operating system. This can be used to
Georg Brandl9af94982008-09-13 17:41:16 +00002118 determine the set of names known to the system. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002119
Georg Brandlf725b952008-01-05 19:44:22 +00002120The following data values are used to support path manipulation operations. These
Georg Brandl8ec7f652007-08-15 14:28:01 +00002121are defined for all platforms.
2122
2123Higher-level operations on pathnames are defined in the :mod:`os.path` module.
2124
2125
2126.. data:: curdir
2127
2128 The constant string used by the operating system to refer to the current
Georg Brandl9af94982008-09-13 17:41:16 +00002129 directory. This is ``'.'`` for Windows and POSIX. Also available via
2130 :mod:`os.path`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002131
2132
2133.. data:: pardir
2134
2135 The constant string used by the operating system to refer to the parent
Georg Brandl9af94982008-09-13 17:41:16 +00002136 directory. This is ``'..'`` for Windows and POSIX. Also available via
2137 :mod:`os.path`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002138
2139
2140.. data:: sep
2141
Georg Brandl9af94982008-09-13 17:41:16 +00002142 The character used by the operating system to separate pathname components.
2143 This is ``'/'`` for POSIX and ``'\\'`` for Windows. Note that knowing this
2144 is not sufficient to be able to parse or concatenate pathnames --- use
Georg Brandl8ec7f652007-08-15 14:28:01 +00002145 :func:`os.path.split` and :func:`os.path.join` --- but it is occasionally
2146 useful. Also available via :mod:`os.path`.
2147
2148
2149.. data:: altsep
2150
2151 An alternative character used by the operating system to separate pathname
2152 components, or ``None`` if only one separator character exists. This is set to
2153 ``'/'`` on Windows systems where ``sep`` is a backslash. Also available via
2154 :mod:`os.path`.
2155
2156
2157.. data:: extsep
2158
2159 The character which separates the base filename from the extension; for example,
2160 the ``'.'`` in :file:`os.py`. Also available via :mod:`os.path`.
2161
2162 .. versionadded:: 2.2
2163
2164
2165.. data:: pathsep
2166
2167 The character conventionally used by the operating system to separate search
2168 path components (as in :envvar:`PATH`), such as ``':'`` for POSIX or ``';'`` for
2169 Windows. Also available via :mod:`os.path`.
2170
2171
2172.. data:: defpath
2173
2174 The default search path used by :func:`exec\*p\*` and :func:`spawn\*p\*` if the
2175 environment doesn't have a ``'PATH'`` key. Also available via :mod:`os.path`.
2176
2177
2178.. data:: linesep
2179
2180 The string used to separate (or, rather, terminate) lines on the current
Georg Brandl9af94982008-09-13 17:41:16 +00002181 platform. This may be a single character, such as ``'\n'`` for POSIX, or
2182 multiple characters, for example, ``'\r\n'`` for Windows. Do not use
2183 *os.linesep* as a line terminator when writing files opened in text mode (the
2184 default); use a single ``'\n'`` instead, on all platforms.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002185
2186
2187.. data:: devnull
2188
Georg Brandl9af94982008-09-13 17:41:16 +00002189 The file path of the null device. For example: ``'/dev/null'`` for POSIX.
2190 Also available via :mod:`os.path`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002191
2192 .. versionadded:: 2.4
2193
2194
2195.. _os-miscfunc:
2196
2197Miscellaneous Functions
2198-----------------------
2199
2200
2201.. function:: urandom(n)
2202
2203 Return a string of *n* random bytes suitable for cryptographic use.
2204
2205 This function returns random bytes from an OS-specific randomness source. The
2206 returned data should be unpredictable enough for cryptographic applications,
2207 though its exact quality depends on the OS implementation. On a UNIX-like
2208 system this will query /dev/urandom, and on Windows it will use CryptGenRandom.
2209 If a randomness source is not found, :exc:`NotImplementedError` will be raised.
2210
2211 .. versionadded:: 2.4
2212