blob: fac37b0e07a8488b6a46636b1fe8edf78edc0dbd [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 Brandl57fe0f22008-01-12 10:53:29 +000016The design of all built-in operating system dependent modules of Python is such
17that as long as the same functionality is available, it uses the same interface;
18for example, the function ``os.stat(path)`` returns stat information about
19*path* in the same format (which happens to have originated with the POSIX
Georg Brandl8ec7f652007-08-15 14:28:01 +000020interface).
21
22Extensions peculiar to a particular operating system are also available through
23the :mod:`os` module, but using them is of course a threat to portability!
24
Georg Brandl57fe0f22008-01-12 10:53:29 +000025.. note::
Georg Brandl8ec7f652007-08-15 14:28:01 +000026
Georg Brandl9af94982008-09-13 17:41:16 +000027 If not separately noted, all functions that claim "Availability: Unix" are
28 supported on Mac OS X, which builds on a Unix core.
29
30.. note::
31
Georg Brandl57fe0f22008-01-12 10:53:29 +000032 All functions in this module raise :exc:`OSError` in the case of invalid or
33 inaccessible file names and paths, or other arguments that have the correct
34 type, but are not accepted by the operating system.
Georg Brandl8ec7f652007-08-15 14:28:01 +000035
Georg Brandl8ec7f652007-08-15 14:28:01 +000036
37.. exception:: error
38
Georg Brandl57fe0f22008-01-12 10:53:29 +000039 An alias for the built-in :exc:`OSError` exception.
Georg Brandl8ec7f652007-08-15 14:28:01 +000040
41
42.. data:: name
43
44 The name of the operating system dependent module imported. The following names
45 have currently been registered: ``'posix'``, ``'nt'``, ``'mac'``, ``'os2'``,
46 ``'ce'``, ``'java'``, ``'riscos'``.
47
48
Georg Brandl8ec7f652007-08-15 14:28:01 +000049.. _os-procinfo:
50
51Process Parameters
52------------------
53
54These functions and data items provide information and operate on the current
55process and user.
56
57
58.. data:: environ
59
60 A mapping object representing the string environment. For example,
61 ``environ['HOME']`` is the pathname of your home directory (on some platforms),
62 and is equivalent to ``getenv("HOME")`` in C.
63
64 This mapping is captured the first time the :mod:`os` module is imported,
65 typically during Python startup as part of processing :file:`site.py`. Changes
66 to the environment made after this time are not reflected in ``os.environ``,
67 except for changes made by modifying ``os.environ`` directly.
68
69 If the platform supports the :func:`putenv` function, this mapping may be used
70 to modify the environment as well as query the environment. :func:`putenv` will
71 be called automatically when the mapping is modified.
72
73 .. note::
74
75 Calling :func:`putenv` directly does not change ``os.environ``, so it's better
76 to modify ``os.environ``.
77
78 .. note::
79
Georg Brandl9af94982008-09-13 17:41:16 +000080 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
81 cause memory leaks. Refer to the system documentation for
82 :cfunc:`putenv`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000083
84 If :func:`putenv` is not provided, a modified copy of this mapping may be
85 passed to the appropriate process-creation functions to cause child processes
86 to use a modified environment.
87
Georg Brandl4a212682007-09-20 17:57:59 +000088 If the platform supports the :func:`unsetenv` function, you can delete items in
Georg Brandl8ec7f652007-08-15 14:28:01 +000089 this mapping to unset environment variables. :func:`unsetenv` will be called
Georg Brandl4a212682007-09-20 17:57:59 +000090 automatically when an item is deleted from ``os.environ``, and when
Georg Brandl1a94ec22007-10-24 21:40:38 +000091 one of the :meth:`pop` or :meth:`clear` methods is called.
Georg Brandl4a212682007-09-20 17:57:59 +000092
93 .. versionchanged:: 2.6
Georg Brandl1a94ec22007-10-24 21:40:38 +000094 Also unset environment variables when calling :meth:`os.environ.clear`
95 and :meth:`os.environ.pop`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000096
97
98.. function:: chdir(path)
99 fchdir(fd)
100 getcwd()
101 :noindex:
102
103 These functions are described in :ref:`os-file-dir`.
104
105
106.. function:: ctermid()
107
108 Return the filename corresponding to the controlling terminal of the process.
109 Availability: Unix.
110
111
112.. function:: getegid()
113
114 Return the effective group id of the current process. This corresponds to the
Georg Brandlf725b952008-01-05 19:44:22 +0000115 "set id" bit on the file being executed in the current process. Availability:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000116 Unix.
117
118
119.. function:: geteuid()
120
121 .. index:: single: user; effective id
122
Georg Brandlf725b952008-01-05 19:44:22 +0000123 Return the current process's effective user id. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000124
125
126.. function:: getgid()
127
128 .. index:: single: process; group
129
130 Return the real group id of the current process. Availability: Unix.
131
132
133.. function:: getgroups()
134
135 Return list of supplemental group ids associated with the current process.
136 Availability: Unix.
137
138
Antoine Pitrou30b3b352009-12-02 20:37:54 +0000139.. function:: initgroups(username, gid)
140
141 Call the system initgroups() to initialize the group access list with all of
142 the groups of which the specified username is a member, plus the specified
143 group id. Availability: Unix.
144
145 .. versionadded:: 2.7
146
147
Georg Brandl8ec7f652007-08-15 14:28:01 +0000148.. function:: getlogin()
149
150 Return the name of the user logged in on the controlling terminal of the
151 process. For most purposes, it is more useful to use the environment variable
152 :envvar:`LOGNAME` to find out who the user is, or
153 ``pwd.getpwuid(os.getuid())[0]`` to get the login name of the currently
Georg Brandlf725b952008-01-05 19:44:22 +0000154 effective user id. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000155
156
157.. function:: getpgid(pid)
158
159 Return the process group id of the process with process id *pid*. If *pid* is 0,
160 the process group id of the current process is returned. Availability: Unix.
161
162 .. versionadded:: 2.3
163
164
165.. function:: getpgrp()
166
167 .. index:: single: process; group
168
169 Return the id of the current process group. Availability: Unix.
170
171
172.. function:: getpid()
173
174 .. index:: single: process; id
175
176 Return the current process id. Availability: Unix, Windows.
177
178
179.. function:: getppid()
180
181 .. index:: single: process; id of parent
182
183 Return the parent's process id. Availability: Unix.
184
Georg Brandl8d8f8742009-11-28 11:11:50 +0000185
Gregory P. Smith761ae0b2009-11-27 17:51:12 +0000186.. function:: getresuid()
Martin v. Löwis50ea4562009-11-27 13:56:01 +0000187
188 Return a tuple (ruid, euid, suid) denoting the current process's
189 real, effective, and saved user ids. Availability: Unix.
190
Georg Brandl8d8f8742009-11-28 11:11:50 +0000191 .. versionadded:: 2.7
192
Martin v. Löwis50ea4562009-11-27 13:56:01 +0000193
Gregory P. Smith761ae0b2009-11-27 17:51:12 +0000194.. function:: getresgid()
Martin v. Löwis50ea4562009-11-27 13:56:01 +0000195
196 Return a tuple (rgid, egid, sgid) denoting the current process's
197 real, effective, and saved user ids. Availability: Unix.
198
Georg Brandl8d8f8742009-11-28 11:11:50 +0000199 .. versionadded:: 2.7
200
Georg Brandl8ec7f652007-08-15 14:28:01 +0000201
202.. function:: getuid()
203
204 .. index:: single: user; id
205
Georg Brandlf725b952008-01-05 19:44:22 +0000206 Return the current process's user id. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000207
208
209.. function:: getenv(varname[, value])
210
211 Return the value of the environment variable *varname* if it exists, or *value*
212 if it doesn't. *value* defaults to ``None``. Availability: most flavors of
213 Unix, Windows.
214
215
216.. function:: putenv(varname, value)
217
218 .. index:: single: environment variables; setting
219
220 Set the environment variable named *varname* to the string *value*. Such
221 changes to the environment affect subprocesses started with :func:`os.system`,
222 :func:`popen` or :func:`fork` and :func:`execv`. Availability: most flavors of
223 Unix, Windows.
224
225 .. note::
226
Georg Brandl9af94982008-09-13 17:41:16 +0000227 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
228 cause memory leaks. Refer to the system documentation for putenv.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000229
230 When :func:`putenv` is supported, assignments to items in ``os.environ`` are
231 automatically translated into corresponding calls to :func:`putenv`; however,
232 calls to :func:`putenv` don't update ``os.environ``, so it is actually
233 preferable to assign to items of ``os.environ``.
234
235
236.. function:: setegid(egid)
237
238 Set the current process's effective group id. Availability: Unix.
239
240
241.. function:: seteuid(euid)
242
243 Set the current process's effective user id. Availability: Unix.
244
245
246.. function:: setgid(gid)
247
248 Set the current process' group id. Availability: Unix.
249
250
251.. function:: setgroups(groups)
252
253 Set the list of supplemental group ids associated with the current process to
254 *groups*. *groups* must be a sequence, and each element must be an integer
Georg Brandlf725b952008-01-05 19:44:22 +0000255 identifying a group. This operation is typically available only to the superuser.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000256 Availability: Unix.
257
258 .. versionadded:: 2.2
259
260
261.. function:: setpgrp()
262
Georg Brandlf725b952008-01-05 19:44:22 +0000263 Call the system call :cfunc:`setpgrp` or :cfunc:`setpgrp(0, 0)` depending on
Georg Brandl8ec7f652007-08-15 14:28:01 +0000264 which version is implemented (if any). See the Unix manual for the semantics.
265 Availability: Unix.
266
267
268.. function:: setpgid(pid, pgrp)
269
Georg Brandlf725b952008-01-05 19:44:22 +0000270 Call the system call :cfunc:`setpgid` to set the process group id of the
Georg Brandl8ec7f652007-08-15 14:28:01 +0000271 process with id *pid* to the process group with id *pgrp*. See the Unix manual
272 for the semantics. Availability: Unix.
273
274
Georg Brandl8ec7f652007-08-15 14:28:01 +0000275.. function:: setregid(rgid, egid)
276
277 Set the current process's real and effective group ids. Availability: Unix.
278
Georg Brandl8d8f8742009-11-28 11:11:50 +0000279
Martin v. Löwis50ea4562009-11-27 13:56:01 +0000280.. function:: setresgid(rgid, egid, sgid)
281
282 Set the current process's real, effective, and saved group ids.
283 Availability: Unix.
284
Georg Brandl8d8f8742009-11-28 11:11:50 +0000285 .. versionadded:: 2.7
286
Martin v. Löwis50ea4562009-11-27 13:56:01 +0000287
288.. function:: setresuid(ruid, euid, suid)
289
290 Set the current process's real, effective, and saved user ids.
291 Availibility: Unix.
292
Georg Brandl8d8f8742009-11-28 11:11:50 +0000293 .. versionadded:: 2.7
294
Martin v. Löwis50ea4562009-11-27 13:56:01 +0000295
296.. function:: setreuid(ruid, euid)
297
298 Set the current process's real and effective user ids. Availability: Unix.
299
Georg Brandl8ec7f652007-08-15 14:28:01 +0000300
301.. function:: getsid(pid)
302
Georg Brandlf725b952008-01-05 19:44:22 +0000303 Call the system call :cfunc:`getsid`. See the Unix manual for the semantics.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000304 Availability: Unix.
305
306 .. versionadded:: 2.4
307
308
309.. function:: setsid()
310
Georg Brandlf725b952008-01-05 19:44:22 +0000311 Call the system call :cfunc:`setsid`. See the Unix manual for the semantics.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000312 Availability: Unix.
313
314
315.. function:: setuid(uid)
316
317 .. index:: single: user; id, setting
318
Georg Brandlf725b952008-01-05 19:44:22 +0000319 Set the current process's user id. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000320
Georg Brandl8ec7f652007-08-15 14:28:01 +0000321
Georg Brandlb19be572007-12-29 10:57:00 +0000322.. placed in this section since it relates to errno.... a little weak
Georg Brandl8ec7f652007-08-15 14:28:01 +0000323.. function:: strerror(code)
324
325 Return the error message corresponding to the error code in *code*.
Georg Brandl3fc974f2008-05-11 21:16:37 +0000326 On platforms where :cfunc:`strerror` returns ``NULL`` when given an unknown
327 error number, :exc:`ValueError` is raised. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000328
329
330.. function:: umask(mask)
331
Georg Brandlf725b952008-01-05 19:44:22 +0000332 Set the current numeric umask and return the previous umask. Availability:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000333 Unix, Windows.
334
335
336.. function:: uname()
337
338 .. index::
339 single: gethostname() (in module socket)
340 single: gethostbyaddr() (in module socket)
341
342 Return a 5-tuple containing information identifying the current operating
343 system. The tuple contains 5 strings: ``(sysname, nodename, release, version,
344 machine)``. Some systems truncate the nodename to 8 characters or to the
345 leading component; a better way to get the hostname is
346 :func:`socket.gethostname` or even
347 ``socket.gethostbyaddr(socket.gethostname())``. Availability: recent flavors of
348 Unix.
349
350
351.. function:: unsetenv(varname)
352
353 .. index:: single: environment variables; deleting
354
355 Unset (delete) the environment variable named *varname*. Such changes to the
356 environment affect subprocesses started with :func:`os.system`, :func:`popen` or
357 :func:`fork` and :func:`execv`. Availability: most flavors of Unix, Windows.
358
359 When :func:`unsetenv` is supported, deletion of items in ``os.environ`` is
360 automatically translated into a corresponding call to :func:`unsetenv`; however,
361 calls to :func:`unsetenv` don't update ``os.environ``, so it is actually
362 preferable to delete items of ``os.environ``.
363
364
365.. _os-newstreams:
366
367File Object Creation
368--------------------
369
370These functions create new file objects. (See also :func:`open`.)
371
372
373.. function:: fdopen(fd[, mode[, bufsize]])
374
375 .. index:: single: I/O control; buffering
376
377 Return an open file object connected to the file descriptor *fd*. The *mode*
378 and *bufsize* arguments have the same meaning as the corresponding arguments to
Georg Brandl9af94982008-09-13 17:41:16 +0000379 the built-in :func:`open` function. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000380
381 .. versionchanged:: 2.3
382 When specified, the *mode* argument must now start with one of the letters
383 ``'r'``, ``'w'``, or ``'a'``, otherwise a :exc:`ValueError` is raised.
384
385 .. versionchanged:: 2.5
386 On Unix, when the *mode* argument starts with ``'a'``, the *O_APPEND* flag is
387 set on the file descriptor (which the :cfunc:`fdopen` implementation already
388 does on most platforms).
389
390
391.. function:: popen(command[, mode[, bufsize]])
392
393 Open a pipe to or from *command*. The return value is an open file object
394 connected to the pipe, which can be read or written depending on whether *mode*
395 is ``'r'`` (default) or ``'w'``. The *bufsize* argument has the same meaning as
396 the corresponding argument to the built-in :func:`open` function. The exit
397 status of the command (encoded in the format specified for :func:`wait`) is
Georg Brandl012408c2009-05-22 09:43:17 +0000398 available as the return value of the :meth:`~file.close` method of the file object,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000399 except that when the exit status is zero (termination without errors), ``None``
Georg Brandl9af94982008-09-13 17:41:16 +0000400 is returned. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000401
402 .. deprecated:: 2.6
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000403 This function is obsolete. Use the :mod:`subprocess` module. Check
Georg Brandl0ba92b22008-06-22 09:05:29 +0000404 especially the :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000405
406 .. versionchanged:: 2.0
407 This function worked unreliably under Windows in earlier versions of Python.
408 This was due to the use of the :cfunc:`_popen` function from the libraries
409 provided with Windows. Newer versions of Python do not use the broken
410 implementation from the Windows libraries.
411
412
413.. function:: tmpfile()
414
415 Return a new file object opened in update mode (``w+b``). The file has no
416 directory entries associated with it and will be automatically deleted once
Georg Brandl9af94982008-09-13 17:41:16 +0000417 there are no file descriptors for the file. Availability: Unix,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000418 Windows.
419
420There are a number of different :func:`popen\*` functions that provide slightly
421different ways to create subprocesses.
422
423.. deprecated:: 2.6
424 All of the :func:`popen\*` functions are obsolete. Use the :mod:`subprocess`
425 module.
426
427For each of the :func:`popen\*` variants, if *bufsize* is specified, it
428specifies the buffer size for the I/O pipes. *mode*, if provided, should be the
429string ``'b'`` or ``'t'``; on Windows this is needed to determine whether the
430file objects should be opened in binary or text mode. The default value for
431*mode* is ``'t'``.
432
433Also, for each of these variants, on Unix, *cmd* may be a sequence, in which
434case arguments will be passed directly to the program without shell intervention
435(as with :func:`os.spawnv`). If *cmd* is a string it will be passed to the shell
436(as with :func:`os.system`).
437
438These methods do not make it possible to retrieve the exit status from the child
439processes. The only way to control the input and output streams and also
440retrieve the return codes is to use the :mod:`subprocess` module; these are only
441available on Unix.
442
443For a discussion of possible deadlock conditions related to the use of these
444functions, see :ref:`popen2-flow-control`.
445
446
447.. function:: popen2(cmd[, mode[, bufsize]])
448
Georg Brandlf725b952008-01-05 19:44:22 +0000449 Execute *cmd* as a sub-process and return the file objects ``(child_stdin,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000450 child_stdout)``.
451
452 .. deprecated:: 2.6
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000453 This function is obsolete. Use the :mod:`subprocess` module. Check
Georg Brandl0ba92b22008-06-22 09:05:29 +0000454 especially the :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000455
Georg Brandl9af94982008-09-13 17:41:16 +0000456 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000457
458 .. versionadded:: 2.0
459
460
461.. function:: popen3(cmd[, mode[, bufsize]])
462
Georg Brandlf725b952008-01-05 19:44:22 +0000463 Execute *cmd* as a sub-process and return the file objects ``(child_stdin,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000464 child_stdout, child_stderr)``.
465
466 .. deprecated:: 2.6
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000467 This function is obsolete. Use the :mod:`subprocess` module. Check
Georg Brandl0ba92b22008-06-22 09:05:29 +0000468 especially the :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000469
Georg Brandl9af94982008-09-13 17:41:16 +0000470 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000471
472 .. versionadded:: 2.0
473
474
475.. function:: popen4(cmd[, mode[, bufsize]])
476
Georg Brandlf725b952008-01-05 19:44:22 +0000477 Execute *cmd* as a sub-process and return the file objects ``(child_stdin,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000478 child_stdout_and_stderr)``.
479
480 .. deprecated:: 2.6
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000481 This function is obsolete. Use the :mod:`subprocess` module. Check
Georg Brandl0ba92b22008-06-22 09:05:29 +0000482 especially the :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000483
Georg Brandl9af94982008-09-13 17:41:16 +0000484 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000485
486 .. versionadded:: 2.0
487
488(Note that ``child_stdin, child_stdout, and child_stderr`` are named from the
489point of view of the child process, so *child_stdin* is the child's standard
490input.)
491
492This functionality is also available in the :mod:`popen2` module using functions
493of the same names, but the return values of those functions have a different
494order.
495
496
497.. _os-fd-ops:
498
499File Descriptor Operations
500--------------------------
501
502These functions operate on I/O streams referenced using file descriptors.
503
504File descriptors are small integers corresponding to a file that has been opened
505by the current process. For example, standard input is usually file descriptor
5060, standard output is 1, and standard error is 2. Further files opened by a
507process will then be assigned 3, 4, 5, and so forth. The name "file descriptor"
508is slightly deceptive; on Unix platforms, sockets and pipes are also referenced
509by file descriptors.
510
511
512.. function:: close(fd)
513
Georg Brandl9af94982008-09-13 17:41:16 +0000514 Close file descriptor *fd*. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000515
516 .. note::
517
518 This function is intended for low-level I/O and must be applied to a file
Georg Brandl012408c2009-05-22 09:43:17 +0000519 descriptor as returned by :func:`os.open` or :func:`pipe`. To close a "file
Georg Brandl8ec7f652007-08-15 14:28:01 +0000520 object" returned by the built-in function :func:`open` or by :func:`popen` or
Georg Brandl012408c2009-05-22 09:43:17 +0000521 :func:`fdopen`, use its :meth:`~file.close` method.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000522
523
Georg Brandl309501a2008-01-19 20:22:13 +0000524.. function:: closerange(fd_low, fd_high)
525
526 Close all file descriptors from *fd_low* (inclusive) to *fd_high* (exclusive),
Georg Brandl9af94982008-09-13 17:41:16 +0000527 ignoring errors. Availability: Unix, Windows. Equivalent to::
Georg Brandl309501a2008-01-19 20:22:13 +0000528
529 for fd in xrange(fd_low, fd_high):
530 try:
531 os.close(fd)
532 except OSError:
533 pass
534
535 .. versionadded:: 2.6
536
537
Georg Brandl8ec7f652007-08-15 14:28:01 +0000538.. function:: dup(fd)
539
Georg Brandl9af94982008-09-13 17:41:16 +0000540 Return a duplicate of file descriptor *fd*. Availability: Unix,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000541 Windows.
542
543
544.. function:: dup2(fd, fd2)
545
546 Duplicate file descriptor *fd* to *fd2*, closing the latter first if necessary.
Georg Brandl9af94982008-09-13 17:41:16 +0000547 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000548
549
Christian Heimes36281872007-11-30 21:11:28 +0000550.. function:: fchmod(fd, mode)
551
552 Change the mode of the file given by *fd* to the numeric *mode*. See the docs
553 for :func:`chmod` for possible values of *mode*. Availability: Unix.
554
Georg Brandl81ddc1a2007-11-30 22:04:45 +0000555 .. versionadded:: 2.6
556
Christian Heimes36281872007-11-30 21:11:28 +0000557
558.. function:: fchown(fd, uid, gid)
559
560 Change the owner and group id of the file given by *fd* to the numeric *uid*
561 and *gid*. To leave one of the ids unchanged, set it to -1.
562 Availability: Unix.
563
Georg Brandl81ddc1a2007-11-30 22:04:45 +0000564 .. versionadded:: 2.6
565
Christian Heimes36281872007-11-30 21:11:28 +0000566
Georg Brandl8ec7f652007-08-15 14:28:01 +0000567.. function:: fdatasync(fd)
568
569 Force write of file with filedescriptor *fd* to disk. Does not force update of
570 metadata. Availability: Unix.
571
Benjamin Petersonecf3c622009-05-30 03:10:52 +0000572 .. note::
573 This function is not available on MacOS.
574
Georg Brandl8ec7f652007-08-15 14:28:01 +0000575
576.. function:: fpathconf(fd, name)
577
578 Return system configuration information relevant to an open file. *name*
579 specifies the configuration value to retrieve; it may be a string which is the
580 name of a defined system value; these names are specified in a number of
581 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
582 additional names as well. The names known to the host operating system are
583 given in the ``pathconf_names`` dictionary. For configuration variables not
584 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl9af94982008-09-13 17:41:16 +0000585 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000586
587 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
588 specific value for *name* is not supported by the host system, even if it is
589 included in ``pathconf_names``, an :exc:`OSError` is raised with
590 :const:`errno.EINVAL` for the error number.
591
592
593.. function:: fstat(fd)
594
595 Return status for file descriptor *fd*, like :func:`stat`. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +0000596 Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000597
598
599.. function:: fstatvfs(fd)
600
601 Return information about the filesystem containing the file associated with file
602 descriptor *fd*, like :func:`statvfs`. Availability: Unix.
603
604
605.. function:: fsync(fd)
606
607 Force write of file with filedescriptor *fd* to disk. On Unix, this calls the
608 native :cfunc:`fsync` function; on Windows, the MS :cfunc:`_commit` function.
609
610 If you're starting with a Python file object *f*, first do ``f.flush()``, and
611 then do ``os.fsync(f.fileno())``, to ensure that all internal buffers associated
Georg Brandl9af94982008-09-13 17:41:16 +0000612 with *f* are written to disk. Availability: Unix, and Windows
Georg Brandl8ec7f652007-08-15 14:28:01 +0000613 starting in 2.2.3.
614
615
616.. function:: ftruncate(fd, length)
617
618 Truncate the file corresponding to file descriptor *fd*, so that it is at most
Georg Brandl9af94982008-09-13 17:41:16 +0000619 *length* bytes in size. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000620
621
622.. function:: isatty(fd)
623
624 Return ``True`` if the file descriptor *fd* is open and connected to a
Georg Brandl9af94982008-09-13 17:41:16 +0000625 tty(-like) device, else ``False``. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000626
627
628.. function:: lseek(fd, pos, how)
629
Georg Brandlf725b952008-01-05 19:44:22 +0000630 Set the current position of file descriptor *fd* to position *pos*, modified
631 by *how*: :const:`SEEK_SET` or ``0`` to set the position relative to the
632 beginning of the file; :const:`SEEK_CUR` or ``1`` to set it relative to the
633 current position; :const:`os.SEEK_END` or ``2`` to set it relative to the end of
Georg Brandl9af94982008-09-13 17:41:16 +0000634 the file. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000635
636
637.. function:: open(file, flags[, mode])
638
639 Open the file *file* and set various flags according to *flags* and possibly its
640 mode according to *mode*. The default *mode* is ``0777`` (octal), and the
641 current umask value is first masked out. Return the file descriptor for the
Georg Brandl9af94982008-09-13 17:41:16 +0000642 newly opened file. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000643
644 For a description of the flag and mode values, see the C run-time documentation;
645 flag constants (like :const:`O_RDONLY` and :const:`O_WRONLY`) are defined in
646 this module too (see below).
647
648 .. note::
649
Georg Brandl9fa61bb2009-07-26 14:19:57 +0000650 This function is intended for low-level I/O. For normal usage, use the
651 built-in function :func:`open`, which returns a "file object" with
652 :meth:`~file.read` and :meth:`~file.write` methods (and many more). To
653 wrap a file descriptor in a "file object", use :func:`fdopen`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000654
655
656.. function:: openpty()
657
658 .. index:: module: pty
659
660 Open a new pseudo-terminal pair. Return a pair of file descriptors ``(master,
661 slave)`` for the pty and the tty, respectively. For a (slightly) more portable
Georg Brandl9af94982008-09-13 17:41:16 +0000662 approach, use the :mod:`pty` module. Availability: some flavors of
Georg Brandl8ec7f652007-08-15 14:28:01 +0000663 Unix.
664
665
666.. function:: pipe()
667
668 Create a pipe. Return a pair of file descriptors ``(r, w)`` usable for reading
Georg Brandl9af94982008-09-13 17:41:16 +0000669 and writing, respectively. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000670
671
672.. function:: read(fd, n)
673
674 Read at most *n* bytes from file descriptor *fd*. Return a string containing the
675 bytes read. If the end of the file referred to by *fd* has been reached, an
Georg Brandl9af94982008-09-13 17:41:16 +0000676 empty string is returned. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000677
678 .. note::
679
680 This function is intended for low-level I/O and must be applied to a file
Georg Brandl012408c2009-05-22 09:43:17 +0000681 descriptor as returned by :func:`os.open` or :func:`pipe`. To read a "file object"
Georg Brandl8ec7f652007-08-15 14:28:01 +0000682 returned by the built-in function :func:`open` or by :func:`popen` or
Georg Brandl012408c2009-05-22 09:43:17 +0000683 :func:`fdopen`, or :data:`sys.stdin`, use its :meth:`~file.read` or
684 :meth:`~file.readline` methods.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000685
686
687.. function:: tcgetpgrp(fd)
688
689 Return the process group associated with the terminal given by *fd* (an open
Georg Brandl012408c2009-05-22 09:43:17 +0000690 file descriptor as returned by :func:`os.open`). Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000691
692
693.. function:: tcsetpgrp(fd, pg)
694
695 Set the process group associated with the terminal given by *fd* (an open file
Georg Brandl012408c2009-05-22 09:43:17 +0000696 descriptor as returned by :func:`os.open`) to *pg*. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000697
698
699.. function:: ttyname(fd)
700
701 Return a string which specifies the terminal device associated with
Georg Brandlbb75e4e2007-10-21 10:46:24 +0000702 file descriptor *fd*. If *fd* is not associated with a terminal device, an
Georg Brandl9af94982008-09-13 17:41:16 +0000703 exception is raised. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000704
705
706.. function:: write(fd, str)
707
708 Write the string *str* to file descriptor *fd*. Return the number of bytes
Georg Brandl9af94982008-09-13 17:41:16 +0000709 actually written. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000710
711 .. note::
712
713 This function is intended for low-level I/O and must be applied to a file
Georg Brandl012408c2009-05-22 09:43:17 +0000714 descriptor as returned by :func:`os.open` or :func:`pipe`. To write a "file
Georg Brandl8ec7f652007-08-15 14:28:01 +0000715 object" returned by the built-in function :func:`open` or by :func:`popen` or
Georg Brandl012408c2009-05-22 09:43:17 +0000716 :func:`fdopen`, or :data:`sys.stdout` or :data:`sys.stderr`, use its
717 :meth:`~file.write` method.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000718
Georg Brandl0c880bd2008-12-05 08:02:17 +0000719The following constants are options for the *flags* parameter to the
Georg Brandl012408c2009-05-22 09:43:17 +0000720:func:`~os.open` function. They can be combined using the bitwise OR operator
Georg Brandl0c880bd2008-12-05 08:02:17 +0000721``|``. Some of them are not available on all platforms. For descriptions of
Georg Brandle70ff4b2008-12-05 09:25:32 +0000722their availability and use, consult the :manpage:`open(2)` manual page on Unix
Doug Hellmann1d18b5b2009-09-20 20:44:13 +0000723or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000724
725
726.. data:: O_RDONLY
727 O_WRONLY
728 O_RDWR
729 O_APPEND
730 O_CREAT
731 O_EXCL
732 O_TRUNC
733
Georg Brandl0c880bd2008-12-05 08:02:17 +0000734 These constants are available on Unix and Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000735
736
737.. data:: O_DSYNC
738 O_RSYNC
739 O_SYNC
740 O_NDELAY
741 O_NONBLOCK
742 O_NOCTTY
743 O_SHLOCK
744 O_EXLOCK
745
Georg Brandl0c880bd2008-12-05 08:02:17 +0000746 These constants are only available on Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000747
748
749.. data:: O_BINARY
Georg Brandlb67da6e2007-11-24 13:56:09 +0000750 O_NOINHERIT
Georg Brandl8ec7f652007-08-15 14:28:01 +0000751 O_SHORT_LIVED
752 O_TEMPORARY
753 O_RANDOM
754 O_SEQUENTIAL
755 O_TEXT
756
Georg Brandl0c880bd2008-12-05 08:02:17 +0000757 These constants are only available on Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000758
759
Georg Brandlae6b9f32008-05-16 13:41:26 +0000760.. data:: O_ASYNC
761 O_DIRECT
Georg Brandlb67da6e2007-11-24 13:56:09 +0000762 O_DIRECTORY
763 O_NOFOLLOW
764 O_NOATIME
765
Georg Brandl0c880bd2008-12-05 08:02:17 +0000766 These constants are GNU extensions and not present if they are not defined by
767 the C library.
Georg Brandlb67da6e2007-11-24 13:56:09 +0000768
769
Georg Brandl8ec7f652007-08-15 14:28:01 +0000770.. data:: SEEK_SET
771 SEEK_CUR
772 SEEK_END
773
774 Parameters to the :func:`lseek` function. Their values are 0, 1, and 2,
Georg Brandl9af94982008-09-13 17:41:16 +0000775 respectively. Availability: Windows, Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000776
777 .. versionadded:: 2.5
778
779
780.. _os-file-dir:
781
782Files and Directories
783---------------------
784
Georg Brandl8ec7f652007-08-15 14:28:01 +0000785.. function:: access(path, mode)
786
787 Use the real uid/gid to test for access to *path*. Note that most operations
788 will use the effective uid/gid, therefore this routine can be used in a
789 suid/sgid environment to test if the invoking user has the specified access to
790 *path*. *mode* should be :const:`F_OK` to test the existence of *path*, or it
791 can be the inclusive OR of one or more of :const:`R_OK`, :const:`W_OK`, and
792 :const:`X_OK` to test permissions. Return :const:`True` if access is allowed,
793 :const:`False` if not. See the Unix man page :manpage:`access(2)` for more
Georg Brandl9af94982008-09-13 17:41:16 +0000794 information. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000795
796 .. note::
797
Georg Brandl9fa61bb2009-07-26 14:19:57 +0000798 Using :func:`access` to check if a user is authorized to e.g. open a file
799 before actually doing so using :func:`open` creates a security hole,
800 because the user might exploit the short time interval between checking
801 and opening the file to manipulate it.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000802
803 .. note::
804
805 I/O operations may fail even when :func:`access` indicates that they would
806 succeed, particularly for operations on network filesystems which may have
807 permissions semantics beyond the usual POSIX permission-bit model.
808
809
810.. data:: F_OK
811
812 Value to pass as the *mode* parameter of :func:`access` to test the existence of
813 *path*.
814
815
816.. data:: R_OK
817
818 Value to include in the *mode* parameter of :func:`access` to test the
819 readability of *path*.
820
821
822.. data:: W_OK
823
824 Value to include in the *mode* parameter of :func:`access` to test the
825 writability of *path*.
826
827
828.. data:: X_OK
829
830 Value to include in the *mode* parameter of :func:`access` to determine if
831 *path* can be executed.
832
833
834.. function:: chdir(path)
835
836 .. index:: single: directory; changing
837
Georg Brandl9af94982008-09-13 17:41:16 +0000838 Change the current working directory to *path*. Availability: Unix,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000839 Windows.
840
841
842.. function:: fchdir(fd)
843
844 Change the current working directory to the directory represented by the file
845 descriptor *fd*. The descriptor must refer to an opened directory, not an open
846 file. Availability: Unix.
847
848 .. versionadded:: 2.3
849
850
851.. function:: getcwd()
852
853 Return a string representing the current working directory. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +0000854 Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000855
856
857.. function:: getcwdu()
858
859 Return a Unicode object representing the current working directory.
Georg Brandl9af94982008-09-13 17:41:16 +0000860 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000861
862 .. versionadded:: 2.3
863
864
865.. function:: chflags(path, flags)
866
867 Set the flags of *path* to the numeric *flags*. *flags* may take a combination
868 (bitwise OR) of the following values (as defined in the :mod:`stat` module):
869
870 * ``UF_NODUMP``
871 * ``UF_IMMUTABLE``
872 * ``UF_APPEND``
873 * ``UF_OPAQUE``
874 * ``UF_NOUNLINK``
875 * ``SF_ARCHIVED``
876 * ``SF_IMMUTABLE``
877 * ``SF_APPEND``
878 * ``SF_NOUNLINK``
879 * ``SF_SNAPSHOT``
880
Georg Brandl9af94982008-09-13 17:41:16 +0000881 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000882
883 .. versionadded:: 2.6
884
885
886.. function:: chroot(path)
887
888 Change the root directory of the current process to *path*. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +0000889 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000890
891 .. versionadded:: 2.2
892
893
894.. function:: chmod(path, mode)
895
896 Change the mode of *path* to the numeric *mode*. *mode* may take one of the
Georg Brandlf725b952008-01-05 19:44:22 +0000897 following values (as defined in the :mod:`stat` module) or bitwise ORed
Georg Brandl8ec7f652007-08-15 14:28:01 +0000898 combinations of them:
899
900
R. David Murrayfbba7cd2009-07-02 18:19:20 +0000901 * :data:`stat.S_ISUID`
902 * :data:`stat.S_ISGID`
903 * :data:`stat.S_ENFMT`
904 * :data:`stat.S_ISVTX`
905 * :data:`stat.S_IREAD`
906 * :data:`stat.S_IWRITE`
907 * :data:`stat.S_IEXEC`
908 * :data:`stat.S_IRWXU`
909 * :data:`stat.S_IRUSR`
910 * :data:`stat.S_IWUSR`
911 * :data:`stat.S_IXUSR`
912 * :data:`stat.S_IRWXG`
913 * :data:`stat.S_IRGRP`
914 * :data:`stat.S_IWGRP`
915 * :data:`stat.S_IXGRP`
916 * :data:`stat.S_IRWXO`
917 * :data:`stat.S_IROTH`
918 * :data:`stat.S_IWOTH`
919 * :data:`stat.S_IXOTH`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000920
Georg Brandl9af94982008-09-13 17:41:16 +0000921 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000922
923 .. note::
924
925 Although Windows supports :func:`chmod`, you can only set the file's read-only
926 flag with it (via the ``stat.S_IWRITE`` and ``stat.S_IREAD``
927 constants or a corresponding integer value). All other bits are
928 ignored.
929
930
931.. function:: chown(path, uid, gid)
932
933 Change the owner and group id of *path* to the numeric *uid* and *gid*. To leave
Georg Brandl9af94982008-09-13 17:41:16 +0000934 one of the ids unchanged, set it to -1. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000935
936
937.. function:: lchflags(path, flags)
938
939 Set the flags of *path* to the numeric *flags*, like :func:`chflags`, but do not
940 follow symbolic links. Availability: Unix.
941
942 .. versionadded:: 2.6
943
944
Georg Brandl81ddc1a2007-11-30 22:04:45 +0000945.. function:: lchmod(path, mode)
946
947 Change the mode of *path* to the numeric *mode*. If path is a symlink, this
948 affects the symlink rather than the target. See the docs for :func:`chmod`
949 for possible values of *mode*. Availability: Unix.
950
951 .. versionadded:: 2.6
952
953
Georg Brandl8ec7f652007-08-15 14:28:01 +0000954.. function:: lchown(path, uid, gid)
955
Georg Brandlf725b952008-01-05 19:44:22 +0000956 Change the owner and group id of *path* to the numeric *uid* and *gid*. This
Georg Brandl9af94982008-09-13 17:41:16 +0000957 function will not follow symbolic links. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000958
959 .. versionadded:: 2.3
960
961
Benjamin Peterson0e928582009-03-28 19:16:10 +0000962.. function:: link(source, link_name)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000963
Benjamin Peterson0e928582009-03-28 19:16:10 +0000964 Create a hard link pointing to *source* named *link_name*. Availability:
965 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000966
967
968.. function:: listdir(path)
969
Georg Brandl62342912008-11-24 19:56:47 +0000970 Return a list containing the names of the entries in the directory given by
971 *path*. The list is in arbitrary order. It does not include the special
972 entries ``'.'`` and ``'..'`` even if they are present in the
973 directory. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000974
975 .. versionchanged:: 2.3
976 On Windows NT/2k/XP and Unix, if *path* is a Unicode object, the result will be
Georg Brandld933cc22009-05-16 11:21:29 +0000977 a list of Unicode objects. Undecodable filenames will still be returned as
978 string objects.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000979
980
981.. function:: lstat(path)
982
Georg Brandl03b15c62007-11-01 17:19:33 +0000983 Like :func:`stat`, but do not follow symbolic links. This is an alias for
984 :func:`stat` on platforms that do not support symbolic links, such as
985 Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000986
987
988.. function:: mkfifo(path[, mode])
989
990 Create a FIFO (a named pipe) named *path* with numeric mode *mode*. The default
991 *mode* is ``0666`` (octal). The current umask value is first masked out from
Georg Brandl9af94982008-09-13 17:41:16 +0000992 the mode. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000993
994 FIFOs are pipes that can be accessed like regular files. FIFOs exist until they
995 are deleted (for example with :func:`os.unlink`). Generally, FIFOs are used as
996 rendezvous between "client" and "server" type processes: the server opens the
997 FIFO for reading, and the client opens it for writing. Note that :func:`mkfifo`
998 doesn't open the FIFO --- it just creates the rendezvous point.
999
1000
1001.. function:: mknod(filename[, mode=0600, device])
1002
1003 Create a filesystem node (file, device special file or named pipe) named
1004 *filename*. *mode* specifies both the permissions to use and the type of node to
1005 be created, being combined (bitwise OR) with one of ``stat.S_IFREG``,
1006 ``stat.S_IFCHR``, ``stat.S_IFBLK``,
1007 and ``stat.S_IFIFO`` (those constants are available in :mod:`stat`).
1008 For ``stat.S_IFCHR`` and
1009 ``stat.S_IFBLK``, *device* defines the newly created device special file (probably using
1010 :func:`os.makedev`), otherwise it is ignored.
1011
1012 .. versionadded:: 2.3
1013
1014
1015.. function:: major(device)
1016
Georg Brandlf725b952008-01-05 19:44:22 +00001017 Extract the device major number from a raw device number (usually the
Georg Brandl8ec7f652007-08-15 14:28:01 +00001018 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
1019
1020 .. versionadded:: 2.3
1021
1022
1023.. function:: minor(device)
1024
Georg Brandlf725b952008-01-05 19:44:22 +00001025 Extract the device minor number from a raw device number (usually the
Georg Brandl8ec7f652007-08-15 14:28:01 +00001026 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
1027
1028 .. versionadded:: 2.3
1029
1030
1031.. function:: makedev(major, minor)
1032
Georg Brandlf725b952008-01-05 19:44:22 +00001033 Compose a raw device number from the major and minor device numbers.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001034
1035 .. versionadded:: 2.3
1036
1037
1038.. function:: mkdir(path[, mode])
1039
1040 Create a directory named *path* with numeric mode *mode*. The default *mode* is
1041 ``0777`` (octal). On some systems, *mode* is ignored. Where it is used, the
Georg Brandl9af94982008-09-13 17:41:16 +00001042 current umask value is first masked out. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001043
Mark Summerfieldac3d4292007-11-02 08:24:59 +00001044 It is also possible to create temporary directories; see the
1045 :mod:`tempfile` module's :func:`tempfile.mkdtemp` function.
1046
Georg Brandl8ec7f652007-08-15 14:28:01 +00001047
1048.. function:: makedirs(path[, mode])
1049
1050 .. index::
1051 single: directory; creating
1052 single: UNC paths; and os.makedirs()
1053
1054 Recursive directory creation function. Like :func:`mkdir`, but makes all
1055 intermediate-level directories needed to contain the leaf directory. Throws an
1056 :exc:`error` exception if the leaf directory already exists or cannot be
1057 created. The default *mode* is ``0777`` (octal). On some systems, *mode* is
1058 ignored. Where it is used, the current umask value is first masked out.
1059
1060 .. note::
1061
1062 :func:`makedirs` will become confused if the path elements to create include
Georg Brandlf725b952008-01-05 19:44:22 +00001063 :data:`os.pardir`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001064
1065 .. versionadded:: 1.5.2
1066
1067 .. versionchanged:: 2.3
1068 This function now handles UNC paths correctly.
1069
1070
1071.. function:: pathconf(path, name)
1072
1073 Return system configuration information relevant to a named file. *name*
1074 specifies the configuration value to retrieve; it may be a string which is the
1075 name of a defined system value; these names are specified in a number of
1076 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
1077 additional names as well. The names known to the host operating system are
1078 given in the ``pathconf_names`` dictionary. For configuration variables not
1079 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl9af94982008-09-13 17:41:16 +00001080 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001081
1082 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1083 specific value for *name* is not supported by the host system, even if it is
1084 included in ``pathconf_names``, an :exc:`OSError` is raised with
1085 :const:`errno.EINVAL` for the error number.
1086
1087
1088.. data:: pathconf_names
1089
1090 Dictionary mapping names accepted by :func:`pathconf` and :func:`fpathconf` to
1091 the integer values defined for those names by the host operating system. This
1092 can be used to determine the set of names known to the system. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00001093 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001094
1095
1096.. function:: readlink(path)
1097
1098 Return a string representing the path to which the symbolic link points. The
1099 result may be either an absolute or relative pathname; if it is relative, it may
1100 be converted to an absolute pathname using ``os.path.join(os.path.dirname(path),
1101 result)``.
1102
1103 .. versionchanged:: 2.6
1104 If the *path* is a Unicode object the result will also be a Unicode object.
1105
Georg Brandl9af94982008-09-13 17:41:16 +00001106 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001107
1108
1109.. function:: remove(path)
1110
Georg Brandl75439972009-08-24 17:24:27 +00001111 Remove (delete) the file *path*. If *path* is a directory, :exc:`OSError` is
1112 raised; see :func:`rmdir` below to remove a directory. This is identical to
1113 the :func:`unlink` function documented below. On Windows, attempting to
1114 remove a file that is in use causes an exception to be raised; on Unix, the
1115 directory entry is removed but the storage allocated to the file is not made
1116 available until the original file is no longer in use. Availability: Unix,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001117 Windows.
1118
1119
1120.. function:: removedirs(path)
1121
1122 .. index:: single: directory; deleting
1123
Georg Brandlf725b952008-01-05 19:44:22 +00001124 Remove directories recursively. Works like :func:`rmdir` except that, if the
Georg Brandl8ec7f652007-08-15 14:28:01 +00001125 leaf directory is successfully removed, :func:`removedirs` tries to
1126 successively remove every parent directory mentioned in *path* until an error
1127 is raised (which is ignored, because it generally means that a parent directory
1128 is not empty). For example, ``os.removedirs('foo/bar/baz')`` will first remove
1129 the directory ``'foo/bar/baz'``, and then remove ``'foo/bar'`` and ``'foo'`` if
1130 they are empty. Raises :exc:`OSError` if the leaf directory could not be
1131 successfully removed.
1132
1133 .. versionadded:: 1.5.2
1134
1135
1136.. function:: rename(src, dst)
1137
1138 Rename the file or directory *src* to *dst*. If *dst* is a directory,
1139 :exc:`OSError` will be raised. On Unix, if *dst* exists and is a file, it will
Georg Brandlf725b952008-01-05 19:44:22 +00001140 be replaced silently if the user has permission. The operation may fail on some
Georg Brandl8ec7f652007-08-15 14:28:01 +00001141 Unix flavors if *src* and *dst* are on different filesystems. If successful,
1142 the renaming will be an atomic operation (this is a POSIX requirement). On
1143 Windows, if *dst* already exists, :exc:`OSError` will be raised even if it is a
1144 file; there may be no way to implement an atomic rename when *dst* names an
Georg Brandl9af94982008-09-13 17:41:16 +00001145 existing file. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001146
1147
1148.. function:: renames(old, new)
1149
1150 Recursive directory or file renaming function. Works like :func:`rename`, except
1151 creation of any intermediate directories needed to make the new pathname good is
1152 attempted first. After the rename, directories corresponding to rightmost path
1153 segments of the old name will be pruned away using :func:`removedirs`.
1154
1155 .. versionadded:: 1.5.2
1156
1157 .. note::
1158
1159 This function can fail with the new directory structure made if you lack
1160 permissions needed to remove the leaf directory or file.
1161
1162
1163.. function:: rmdir(path)
1164
Georg Brandl1b2695a2009-08-24 17:48:40 +00001165 Remove (delete) the directory *path*. Only works when the directory is
1166 empty, otherwise, :exc:`OSError` is raised. In order to remove whole
1167 directory trees, :func:`shutil.rmtree` can be used. Availability: Unix,
1168 Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001169
1170
1171.. function:: stat(path)
1172
1173 Perform a :cfunc:`stat` system call on the given path. The return value is an
1174 object whose attributes correspond to the members of the :ctype:`stat`
1175 structure, namely: :attr:`st_mode` (protection bits), :attr:`st_ino` (inode
1176 number), :attr:`st_dev` (device), :attr:`st_nlink` (number of hard links),
Georg Brandlf725b952008-01-05 19:44:22 +00001177 :attr:`st_uid` (user id of owner), :attr:`st_gid` (group id of owner),
Georg Brandl8ec7f652007-08-15 14:28:01 +00001178 :attr:`st_size` (size of file, in bytes), :attr:`st_atime` (time of most recent
1179 access), :attr:`st_mtime` (time of most recent content modification),
1180 :attr:`st_ctime` (platform dependent; time of most recent metadata change on
1181 Unix, or the time of creation on Windows)::
1182
1183 >>> import os
1184 >>> statinfo = os.stat('somefile.txt')
1185 >>> statinfo
1186 (33188, 422511L, 769L, 1, 1032, 100, 926L, 1105022698,1105022732, 1105022732)
1187 >>> statinfo.st_size
1188 926L
1189 >>>
1190
1191 .. versionchanged:: 2.3
Georg Brandlf725b952008-01-05 19:44:22 +00001192 If :func:`stat_float_times` returns ``True``, the time values are floats, measuring
Georg Brandl8ec7f652007-08-15 14:28:01 +00001193 seconds. Fractions of a second may be reported if the system supports that. On
1194 Mac OS, the times are always floats. See :func:`stat_float_times` for further
1195 discussion.
1196
1197 On some Unix systems (such as Linux), the following attributes may also be
1198 available: :attr:`st_blocks` (number of blocks allocated for file),
1199 :attr:`st_blksize` (filesystem blocksize), :attr:`st_rdev` (type of device if an
1200 inode device). :attr:`st_flags` (user defined flags for file).
1201
1202 On other Unix systems (such as FreeBSD), the following attributes may be
1203 available (but may be only filled out if root tries to use them): :attr:`st_gen`
1204 (file generation number), :attr:`st_birthtime` (time of file creation).
1205
1206 On Mac OS systems, the following attributes may also be available:
1207 :attr:`st_rsize`, :attr:`st_creator`, :attr:`st_type`.
1208
1209 On RISCOS systems, the following attributes are also available: :attr:`st_ftype`
1210 (file type), :attr:`st_attrs` (attributes), :attr:`st_obtype` (object type).
1211
1212 .. index:: module: stat
1213
1214 For backward compatibility, the return value of :func:`stat` is also accessible
1215 as a tuple of at least 10 integers giving the most important (and portable)
1216 members of the :ctype:`stat` structure, in the order :attr:`st_mode`,
1217 :attr:`st_ino`, :attr:`st_dev`, :attr:`st_nlink`, :attr:`st_uid`,
1218 :attr:`st_gid`, :attr:`st_size`, :attr:`st_atime`, :attr:`st_mtime`,
1219 :attr:`st_ctime`. More items may be added at the end by some implementations.
1220 The standard module :mod:`stat` defines functions and constants that are useful
1221 for extracting information from a :ctype:`stat` structure. (On Windows, some
1222 items are filled with dummy values.)
1223
1224 .. note::
1225
1226 The exact meaning and resolution of the :attr:`st_atime`, :attr:`st_mtime`, and
1227 :attr:`st_ctime` members depends on the operating system and the file system.
1228 For example, on Windows systems using the FAT or FAT32 file systems,
1229 :attr:`st_mtime` has 2-second resolution, and :attr:`st_atime` has only 1-day
1230 resolution. See your operating system documentation for details.
1231
Georg Brandl9af94982008-09-13 17:41:16 +00001232 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001233
1234 .. versionchanged:: 2.2
1235 Added access to values as attributes of the returned object.
1236
1237 .. versionchanged:: 2.5
Georg Brandlf725b952008-01-05 19:44:22 +00001238 Added :attr:`st_gen` and :attr:`st_birthtime`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001239
1240
1241.. function:: stat_float_times([newvalue])
1242
1243 Determine whether :class:`stat_result` represents time stamps as float objects.
1244 If *newvalue* is ``True``, future calls to :func:`stat` return floats, if it is
1245 ``False``, future calls return ints. If *newvalue* is omitted, return the
1246 current setting.
1247
1248 For compatibility with older Python versions, accessing :class:`stat_result` as
1249 a tuple always returns integers.
1250
1251 .. versionchanged:: 2.5
1252 Python now returns float values by default. Applications which do not work
1253 correctly with floating point time stamps can use this function to restore the
1254 old behaviour.
1255
1256 The resolution of the timestamps (that is the smallest possible fraction)
1257 depends on the system. Some systems only support second resolution; on these
1258 systems, the fraction will always be zero.
1259
1260 It is recommended that this setting is only changed at program startup time in
1261 the *__main__* module; libraries should never change this setting. If an
1262 application uses a library that works incorrectly if floating point time stamps
1263 are processed, this application should turn the feature off until the library
1264 has been corrected.
1265
1266
1267.. function:: statvfs(path)
1268
1269 Perform a :cfunc:`statvfs` system call on the given path. The return value is
1270 an object whose attributes describe the filesystem on the given path, and
1271 correspond to the members of the :ctype:`statvfs` structure, namely:
1272 :attr:`f_bsize`, :attr:`f_frsize`, :attr:`f_blocks`, :attr:`f_bfree`,
1273 :attr:`f_bavail`, :attr:`f_files`, :attr:`f_ffree`, :attr:`f_favail`,
1274 :attr:`f_flag`, :attr:`f_namemax`. Availability: Unix.
1275
1276 .. index:: module: statvfs
1277
1278 For backward compatibility, the return value is also accessible as a tuple whose
1279 values correspond to the attributes, in the order given above. The standard
1280 module :mod:`statvfs` defines constants that are useful for extracting
1281 information from a :ctype:`statvfs` structure when accessing it as a sequence;
1282 this remains useful when writing code that needs to work with versions of Python
1283 that don't support accessing the fields as attributes.
1284
1285 .. versionchanged:: 2.2
1286 Added access to values as attributes of the returned object.
1287
1288
Benjamin Peterson0e928582009-03-28 19:16:10 +00001289.. function:: symlink(source, link_name)
Georg Brandl8ec7f652007-08-15 14:28:01 +00001290
Benjamin Peterson0e928582009-03-28 19:16:10 +00001291 Create a symbolic link pointing to *source* named *link_name*. Availability:
1292 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001293
1294
1295.. function:: tempnam([dir[, prefix]])
1296
1297 Return a unique path name that is reasonable for creating a temporary file.
1298 This will be an absolute path that names a potential directory entry in the
1299 directory *dir* or a common location for temporary files if *dir* is omitted or
1300 ``None``. If given and not ``None``, *prefix* is used to provide a short prefix
1301 to the filename. Applications are responsible for properly creating and
1302 managing files created using paths returned by :func:`tempnam`; no automatic
1303 cleanup is provided. On Unix, the environment variable :envvar:`TMPDIR`
Georg Brandlf725b952008-01-05 19:44:22 +00001304 overrides *dir*, while on Windows :envvar:`TMP` is used. The specific
Georg Brandl8ec7f652007-08-15 14:28:01 +00001305 behavior of this function depends on the C library implementation; some aspects
1306 are underspecified in system documentation.
1307
1308 .. warning::
1309
1310 Use of :func:`tempnam` is vulnerable to symlink attacks; consider using
1311 :func:`tmpfile` (section :ref:`os-newstreams`) instead.
1312
Georg Brandl9af94982008-09-13 17:41:16 +00001313 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001314
1315
1316.. function:: tmpnam()
1317
1318 Return a unique path name that is reasonable for creating a temporary file.
1319 This will be an absolute path that names a potential directory entry in a common
1320 location for temporary files. Applications are responsible for properly
1321 creating and managing files created using paths returned by :func:`tmpnam`; no
1322 automatic cleanup is provided.
1323
1324 .. warning::
1325
1326 Use of :func:`tmpnam` is vulnerable to symlink attacks; consider using
1327 :func:`tmpfile` (section :ref:`os-newstreams`) instead.
1328
1329 Availability: Unix, Windows. This function probably shouldn't be used on
1330 Windows, though: Microsoft's implementation of :func:`tmpnam` always creates a
1331 name in the root directory of the current drive, and that's generally a poor
1332 location for a temp file (depending on privileges, you may not even be able to
1333 open a file using this name).
1334
1335
1336.. data:: TMP_MAX
1337
1338 The maximum number of unique names that :func:`tmpnam` will generate before
1339 reusing names.
1340
1341
1342.. function:: unlink(path)
1343
Georg Brandl75439972009-08-24 17:24:27 +00001344 Remove (delete) the file *path*. This is the same function as
1345 :func:`remove`; the :func:`unlink` name is its traditional Unix
1346 name. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001347
1348
1349.. function:: utime(path, times)
1350
Benjamin Peterson5b02ef32008-08-16 03:13:07 +00001351 Set the access and modified times of the file specified by *path*. If *times*
1352 is ``None``, then the file's access and modified times are set to the current
1353 time. (The effect is similar to running the Unix program :program:`touch` on
1354 the path.) Otherwise, *times* must be a 2-tuple of numbers, of the form
1355 ``(atime, mtime)`` which is used to set the access and modified times,
1356 respectively. Whether a directory can be given for *path* depends on whether
1357 the operating system implements directories as files (for example, Windows
1358 does not). Note that the exact times you set here may not be returned by a
1359 subsequent :func:`stat` call, depending on the resolution with which your
1360 operating system records access and modification times; see :func:`stat`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001361
1362 .. versionchanged:: 2.0
1363 Added support for ``None`` for *times*.
1364
Georg Brandl9af94982008-09-13 17:41:16 +00001365 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001366
1367
1368.. function:: walk(top[, topdown=True [, onerror=None[, followlinks=False]]])
1369
1370 .. index::
1371 single: directory; walking
1372 single: directory; traversal
1373
Georg Brandlf725b952008-01-05 19:44:22 +00001374 Generate the file names in a directory tree by walking the tree
1375 either top-down or bottom-up. For each directory in the tree rooted at directory
Georg Brandl8ec7f652007-08-15 14:28:01 +00001376 *top* (including *top* itself), it yields a 3-tuple ``(dirpath, dirnames,
1377 filenames)``.
1378
1379 *dirpath* is a string, the path to the directory. *dirnames* is a list of the
1380 names of the subdirectories in *dirpath* (excluding ``'.'`` and ``'..'``).
1381 *filenames* is a list of the names of the non-directory files in *dirpath*.
1382 Note that the names in the lists contain no path components. To get a full path
1383 (which begins with *top*) to a file or directory in *dirpath*, do
1384 ``os.path.join(dirpath, name)``.
1385
Georg Brandlf725b952008-01-05 19:44:22 +00001386 If optional argument *topdown* is ``True`` or not specified, the triple for a
Georg Brandl8ec7f652007-08-15 14:28:01 +00001387 directory is generated before the triples for any of its subdirectories
Georg Brandlf725b952008-01-05 19:44:22 +00001388 (directories are generated top-down). If *topdown* is ``False``, the triple for a
Georg Brandl8ec7f652007-08-15 14:28:01 +00001389 directory is generated after the triples for all of its subdirectories
Georg Brandlf725b952008-01-05 19:44:22 +00001390 (directories are generated bottom-up).
Georg Brandl8ec7f652007-08-15 14:28:01 +00001391
Georg Brandlf725b952008-01-05 19:44:22 +00001392 When *topdown* is ``True``, the caller can modify the *dirnames* list in-place
Georg Brandl8ec7f652007-08-15 14:28:01 +00001393 (perhaps using :keyword:`del` or slice assignment), and :func:`walk` will only
1394 recurse into the subdirectories whose names remain in *dirnames*; this can be
1395 used to prune the search, impose a specific order of visiting, or even to inform
1396 :func:`walk` about directories the caller creates or renames before it resumes
Georg Brandlf725b952008-01-05 19:44:22 +00001397 :func:`walk` again. Modifying *dirnames* when *topdown* is ``False`` is
Georg Brandl8ec7f652007-08-15 14:28:01 +00001398 ineffective, because in bottom-up mode the directories in *dirnames* are
1399 generated before *dirpath* itself is generated.
1400
Georg Brandlf725b952008-01-05 19:44:22 +00001401 By default errors from the :func:`listdir` call are ignored. If optional
Georg Brandl8ec7f652007-08-15 14:28:01 +00001402 argument *onerror* is specified, it should be a function; it will be called with
1403 one argument, an :exc:`OSError` instance. It can report the error to continue
1404 with the walk, or raise the exception to abort the walk. Note that the filename
1405 is available as the ``filename`` attribute of the exception object.
1406
1407 By default, :func:`walk` will not walk down into symbolic links that resolve to
Georg Brandlf725b952008-01-05 19:44:22 +00001408 directories. Set *followlinks* to ``True`` to visit directories pointed to by
Georg Brandl8ec7f652007-08-15 14:28:01 +00001409 symlinks, on systems that support them.
1410
1411 .. versionadded:: 2.6
1412 The *followlinks* parameter.
1413
1414 .. note::
1415
Georg Brandlf725b952008-01-05 19:44:22 +00001416 Be aware that setting *followlinks* to ``True`` can lead to infinite recursion if a
Georg Brandl8ec7f652007-08-15 14:28:01 +00001417 link points to a parent directory of itself. :func:`walk` does not keep track of
1418 the directories it visited already.
1419
1420 .. note::
1421
1422 If you pass a relative pathname, don't change the current working directory
1423 between resumptions of :func:`walk`. :func:`walk` never changes the current
1424 directory, and assumes that its caller doesn't either.
1425
1426 This example displays the number of bytes taken by non-directory files in each
1427 directory under the starting directory, except that it doesn't look under any
1428 CVS subdirectory::
1429
1430 import os
1431 from os.path import join, getsize
1432 for root, dirs, files in os.walk('python/Lib/email'):
1433 print root, "consumes",
1434 print sum(getsize(join(root, name)) for name in files),
1435 print "bytes in", len(files), "non-directory files"
1436 if 'CVS' in dirs:
1437 dirs.remove('CVS') # don't visit CVS directories
1438
Georg Brandlf725b952008-01-05 19:44:22 +00001439 In the next example, walking the tree bottom-up is essential: :func:`rmdir`
Georg Brandl8ec7f652007-08-15 14:28:01 +00001440 doesn't allow deleting a directory before the directory is empty::
1441
Georg Brandlf725b952008-01-05 19:44:22 +00001442 # Delete everything reachable from the directory named in "top",
Georg Brandl8ec7f652007-08-15 14:28:01 +00001443 # assuming there are no symbolic links.
1444 # CAUTION: This is dangerous! For example, if top == '/', it
1445 # could delete all your disk files.
1446 import os
1447 for root, dirs, files in os.walk(top, topdown=False):
1448 for name in files:
1449 os.remove(os.path.join(root, name))
1450 for name in dirs:
1451 os.rmdir(os.path.join(root, name))
1452
1453 .. versionadded:: 2.3
1454
1455
1456.. _os-process:
1457
1458Process Management
1459------------------
1460
1461These functions may be used to create and manage processes.
1462
1463The various :func:`exec\*` functions take a list of arguments for the new
1464program loaded into the process. In each case, the first of these arguments is
1465passed to the new program as its own name rather than as an argument a user may
1466have typed on a command line. For the C programmer, this is the ``argv[0]``
1467passed to a program's :cfunc:`main`. For example, ``os.execv('/bin/echo',
1468['foo', 'bar'])`` will only print ``bar`` on standard output; ``foo`` will seem
1469to be ignored.
1470
1471
1472.. function:: abort()
1473
1474 Generate a :const:`SIGABRT` signal to the current process. On Unix, the default
1475 behavior is to produce a core dump; on Windows, the process immediately returns
1476 an exit code of ``3``. Be aware that programs which use :func:`signal.signal`
1477 to register a handler for :const:`SIGABRT` will behave differently.
Georg Brandl9af94982008-09-13 17:41:16 +00001478 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001479
1480
1481.. function:: execl(path, arg0, arg1, ...)
1482 execle(path, arg0, arg1, ..., env)
1483 execlp(file, arg0, arg1, ...)
1484 execlpe(file, arg0, arg1, ..., env)
1485 execv(path, args)
1486 execve(path, args, env)
1487 execvp(file, args)
1488 execvpe(file, args, env)
1489
1490 These functions all execute a new program, replacing the current process; they
1491 do not return. On Unix, the new executable is loaded into the current process,
Georg Brandlf725b952008-01-05 19:44:22 +00001492 and will have the same process id as the caller. Errors will be reported as
Georg Brandlc62ef8b2009-01-03 20:55:06 +00001493 :exc:`OSError` exceptions.
Andrew M. Kuchlingac771662008-09-28 00:15:27 +00001494
1495 The current process is replaced immediately. Open file objects and
1496 descriptors are not flushed, so if there may be data buffered
1497 on these open files, you should flush them using
1498 :func:`sys.stdout.flush` or :func:`os.fsync` before calling an
1499 :func:`exec\*` function.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001500
Georg Brandlf725b952008-01-05 19:44:22 +00001501 The "l" and "v" variants of the :func:`exec\*` functions differ in how
1502 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl8ec7f652007-08-15 14:28:01 +00001503 to work with if the number of parameters is fixed when the code is written; the
1504 individual parameters simply become additional parameters to the :func:`execl\*`
Georg Brandlf725b952008-01-05 19:44:22 +00001505 functions. The "v" variants are good when the number of parameters is
Georg Brandl8ec7f652007-08-15 14:28:01 +00001506 variable, with the arguments being passed in a list or tuple as the *args*
1507 parameter. In either case, the arguments to the child process should start with
1508 the name of the command being run, but this is not enforced.
1509
Georg Brandlf725b952008-01-05 19:44:22 +00001510 The variants which include a "p" near the end (:func:`execlp`,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001511 :func:`execlpe`, :func:`execvp`, and :func:`execvpe`) will use the
1512 :envvar:`PATH` environment variable to locate the program *file*. When the
1513 environment is being replaced (using one of the :func:`exec\*e` variants,
1514 discussed in the next paragraph), the new environment is used as the source of
1515 the :envvar:`PATH` variable. The other variants, :func:`execl`, :func:`execle`,
1516 :func:`execv`, and :func:`execve`, will not use the :envvar:`PATH` variable to
1517 locate the executable; *path* must contain an appropriate absolute or relative
1518 path.
1519
1520 For :func:`execle`, :func:`execlpe`, :func:`execve`, and :func:`execvpe` (note
Georg Brandlf725b952008-01-05 19:44:22 +00001521 that these all end in "e"), the *env* parameter must be a mapping which is
Georg Brandlfb246c42008-04-19 16:58:28 +00001522 used to define the environment variables for the new process (these are used
1523 instead of the current process' environment); the functions :func:`execl`,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001524 :func:`execlp`, :func:`execv`, and :func:`execvp` all cause the new process to
Georg Brandlc62ef8b2009-01-03 20:55:06 +00001525 inherit the environment of the current process.
Andrew M. Kuchlingac771662008-09-28 00:15:27 +00001526
1527 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001528
1529
1530.. function:: _exit(n)
1531
1532 Exit to the system with status *n*, without calling cleanup handlers, flushing
Georg Brandl9af94982008-09-13 17:41:16 +00001533 stdio buffers, etc. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001534
1535 .. note::
1536
1537 The standard way to exit is ``sys.exit(n)``. :func:`_exit` should normally only
1538 be used in the child process after a :func:`fork`.
1539
Georg Brandlf725b952008-01-05 19:44:22 +00001540The following exit codes are defined and can be used with :func:`_exit`,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001541although they are not required. These are typically used for system programs
1542written in Python, such as a mail server's external command delivery program.
1543
1544.. note::
1545
1546 Some of these may not be available on all Unix platforms, since there is some
1547 variation. These constants are defined where they are defined by the underlying
1548 platform.
1549
1550
1551.. data:: EX_OK
1552
Georg Brandl9af94982008-09-13 17:41:16 +00001553 Exit code that means no error occurred. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001554
1555 .. versionadded:: 2.3
1556
1557
1558.. data:: EX_USAGE
1559
1560 Exit code that means the command was used incorrectly, such as when the wrong
Georg Brandl9af94982008-09-13 17:41:16 +00001561 number of arguments are given. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001562
1563 .. versionadded:: 2.3
1564
1565
1566.. data:: EX_DATAERR
1567
Georg Brandl9af94982008-09-13 17:41:16 +00001568 Exit code that means the input data was incorrect. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001569
1570 .. versionadded:: 2.3
1571
1572
1573.. data:: EX_NOINPUT
1574
1575 Exit code that means an input file did not exist or was not readable.
Georg Brandl9af94982008-09-13 17:41:16 +00001576 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001577
1578 .. versionadded:: 2.3
1579
1580
1581.. data:: EX_NOUSER
1582
Georg Brandl9af94982008-09-13 17:41:16 +00001583 Exit code that means a specified user did not exist. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001584
1585 .. versionadded:: 2.3
1586
1587
1588.. data:: EX_NOHOST
1589
Georg Brandl9af94982008-09-13 17:41:16 +00001590 Exit code that means a specified host did not exist. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001591
1592 .. versionadded:: 2.3
1593
1594
1595.. data:: EX_UNAVAILABLE
1596
1597 Exit code that means that a required service is unavailable. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00001598 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001599
1600 .. versionadded:: 2.3
1601
1602
1603.. data:: EX_SOFTWARE
1604
1605 Exit code that means an internal software error was detected. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00001606 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001607
1608 .. versionadded:: 2.3
1609
1610
1611.. data:: EX_OSERR
1612
1613 Exit code that means an operating system error was detected, such as the
Georg Brandl9af94982008-09-13 17:41:16 +00001614 inability to fork or create a pipe. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001615
1616 .. versionadded:: 2.3
1617
1618
1619.. data:: EX_OSFILE
1620
1621 Exit code that means some system file did not exist, could not be opened, or had
Georg Brandl9af94982008-09-13 17:41:16 +00001622 some other kind of error. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001623
1624 .. versionadded:: 2.3
1625
1626
1627.. data:: EX_CANTCREAT
1628
1629 Exit code that means a user specified output file could not be created.
Georg Brandl9af94982008-09-13 17:41:16 +00001630 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001631
1632 .. versionadded:: 2.3
1633
1634
1635.. data:: EX_IOERR
1636
1637 Exit code that means that an error occurred while doing I/O on some file.
Georg Brandl9af94982008-09-13 17:41:16 +00001638 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001639
1640 .. versionadded:: 2.3
1641
1642
1643.. data:: EX_TEMPFAIL
1644
1645 Exit code that means a temporary failure occurred. This indicates something
1646 that may not really be an error, such as a network connection that couldn't be
Georg Brandl9af94982008-09-13 17:41:16 +00001647 made during a retryable operation. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001648
1649 .. versionadded:: 2.3
1650
1651
1652.. data:: EX_PROTOCOL
1653
1654 Exit code that means that a protocol exchange was illegal, invalid, or not
Georg Brandl9af94982008-09-13 17:41:16 +00001655 understood. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001656
1657 .. versionadded:: 2.3
1658
1659
1660.. data:: EX_NOPERM
1661
1662 Exit code that means that there were insufficient permissions to perform the
Georg Brandl9af94982008-09-13 17:41:16 +00001663 operation (but not intended for file system problems). Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001664
1665 .. versionadded:: 2.3
1666
1667
1668.. data:: EX_CONFIG
1669
1670 Exit code that means that some kind of configuration error occurred.
Georg Brandl9af94982008-09-13 17:41:16 +00001671 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001672
1673 .. versionadded:: 2.3
1674
1675
1676.. data:: EX_NOTFOUND
1677
1678 Exit code that means something like "an entry was not found". Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00001679 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001680
1681 .. versionadded:: 2.3
1682
1683
1684.. function:: fork()
1685
Georg Brandlf725b952008-01-05 19:44:22 +00001686 Fork a child process. Return ``0`` in the child and the child's process id in the
Skip Montanaro75e51682008-03-15 02:32:49 +00001687 parent. If an error occurs :exc:`OSError` is raised.
Gregory P. Smith08067492008-09-30 20:41:13 +00001688
1689 Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have
1690 known issues when using fork() from a thread.
1691
Georg Brandl9af94982008-09-13 17:41:16 +00001692 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001693
1694
1695.. function:: forkpty()
1696
1697 Fork a child process, using a new pseudo-terminal as the child's controlling
1698 terminal. Return a pair of ``(pid, fd)``, where *pid* is ``0`` in the child, the
1699 new child's process id in the parent, and *fd* is the file descriptor of the
1700 master end of the pseudo-terminal. For a more portable approach, use the
Skip Montanaro75e51682008-03-15 02:32:49 +00001701 :mod:`pty` module. If an error occurs :exc:`OSError` is raised.
Georg Brandl9af94982008-09-13 17:41:16 +00001702 Availability: some flavors of Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001703
1704
1705.. function:: kill(pid, sig)
1706
1707 .. index::
1708 single: process; killing
1709 single: process; signalling
1710
1711 Send signal *sig* to the process *pid*. Constants for the specific signals
1712 available on the host platform are defined in the :mod:`signal` module.
Georg Brandl9af94982008-09-13 17:41:16 +00001713 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001714
1715
1716.. function:: killpg(pgid, sig)
1717
1718 .. index::
1719 single: process; killing
1720 single: process; signalling
1721
Georg Brandl9af94982008-09-13 17:41:16 +00001722 Send the signal *sig* to the process group *pgid*. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001723
1724 .. versionadded:: 2.3
1725
1726
1727.. function:: nice(increment)
1728
1729 Add *increment* to the process's "niceness". Return the new niceness.
Georg Brandl9af94982008-09-13 17:41:16 +00001730 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001731
1732
1733.. function:: plock(op)
1734
1735 Lock program segments into memory. The value of *op* (defined in
Georg Brandl9af94982008-09-13 17:41:16 +00001736 ``<sys/lock.h>``) determines which segments are locked. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001737
1738
1739.. function:: popen(...)
1740 popen2(...)
1741 popen3(...)
1742 popen4(...)
1743 :noindex:
1744
1745 Run child processes, returning opened pipes for communications. These functions
1746 are described in section :ref:`os-newstreams`.
1747
1748
1749.. function:: spawnl(mode, path, ...)
1750 spawnle(mode, path, ..., env)
1751 spawnlp(mode, file, ...)
1752 spawnlpe(mode, file, ..., env)
1753 spawnv(mode, path, args)
1754 spawnve(mode, path, args, env)
1755 spawnvp(mode, file, args)
1756 spawnvpe(mode, file, args, env)
1757
1758 Execute the program *path* in a new process.
1759
1760 (Note that the :mod:`subprocess` module provides more powerful facilities for
1761 spawning new processes and retrieving their results; using that module is
R. David Murrayccb9d4b2009-06-09 00:44:22 +00001762 preferable to using these functions. Check especially the
1763 :ref:`subprocess-replacements` section.)
Georg Brandl8ec7f652007-08-15 14:28:01 +00001764
Georg Brandlf725b952008-01-05 19:44:22 +00001765 If *mode* is :const:`P_NOWAIT`, this function returns the process id of the new
Georg Brandl8ec7f652007-08-15 14:28:01 +00001766 process; if *mode* is :const:`P_WAIT`, returns the process's exit code if it
1767 exits normally, or ``-signal``, where *signal* is the signal that killed the
Georg Brandlf725b952008-01-05 19:44:22 +00001768 process. On Windows, the process id will actually be the process handle, so can
Georg Brandl8ec7f652007-08-15 14:28:01 +00001769 be used with the :func:`waitpid` function.
1770
Georg Brandlf725b952008-01-05 19:44:22 +00001771 The "l" and "v" variants of the :func:`spawn\*` functions differ in how
1772 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl8ec7f652007-08-15 14:28:01 +00001773 to work with if the number of parameters is fixed when the code is written; the
1774 individual parameters simply become additional parameters to the
Georg Brandlf725b952008-01-05 19:44:22 +00001775 :func:`spawnl\*` functions. The "v" variants are good when the number of
Georg Brandl8ec7f652007-08-15 14:28:01 +00001776 parameters is variable, with the arguments being passed in a list or tuple as
1777 the *args* parameter. In either case, the arguments to the child process must
1778 start with the name of the command being run.
1779
Georg Brandlf725b952008-01-05 19:44:22 +00001780 The variants which include a second "p" near the end (:func:`spawnlp`,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001781 :func:`spawnlpe`, :func:`spawnvp`, and :func:`spawnvpe`) will use the
1782 :envvar:`PATH` environment variable to locate the program *file*. When the
1783 environment is being replaced (using one of the :func:`spawn\*e` variants,
1784 discussed in the next paragraph), the new environment is used as the source of
1785 the :envvar:`PATH` variable. The other variants, :func:`spawnl`,
1786 :func:`spawnle`, :func:`spawnv`, and :func:`spawnve`, will not use the
1787 :envvar:`PATH` variable to locate the executable; *path* must contain an
1788 appropriate absolute or relative path.
1789
1790 For :func:`spawnle`, :func:`spawnlpe`, :func:`spawnve`, and :func:`spawnvpe`
Georg Brandlf725b952008-01-05 19:44:22 +00001791 (note that these all end in "e"), the *env* parameter must be a mapping
Georg Brandlfb246c42008-04-19 16:58:28 +00001792 which is used to define the environment variables for the new process (they are
1793 used instead of the current process' environment); the functions
Georg Brandl8ec7f652007-08-15 14:28:01 +00001794 :func:`spawnl`, :func:`spawnlp`, :func:`spawnv`, and :func:`spawnvp` all cause
Georg Brandl22717df2009-03-31 18:26:55 +00001795 the new process to inherit the environment of the current process. Note that
1796 keys and values in the *env* dictionary must be strings; invalid keys or
1797 values will cause the function to fail, with a return value of ``127``.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001798
1799 As an example, the following calls to :func:`spawnlp` and :func:`spawnvpe` are
1800 equivalent::
1801
1802 import os
1803 os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1804
1805 L = ['cp', 'index.html', '/dev/null']
1806 os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1807
1808 Availability: Unix, Windows. :func:`spawnlp`, :func:`spawnlpe`, :func:`spawnvp`
1809 and :func:`spawnvpe` are not available on Windows.
1810
1811 .. versionadded:: 1.6
1812
1813
1814.. data:: P_NOWAIT
1815 P_NOWAITO
1816
1817 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1818 functions. If either of these values is given, the :func:`spawn\*` functions
Georg Brandlf725b952008-01-05 19:44:22 +00001819 will return as soon as the new process has been created, with the process id as
Georg Brandl9af94982008-09-13 17:41:16 +00001820 the return value. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001821
1822 .. versionadded:: 1.6
1823
1824
1825.. data:: P_WAIT
1826
1827 Possible value for the *mode* parameter to the :func:`spawn\*` family of
1828 functions. If this is given as *mode*, the :func:`spawn\*` functions will not
1829 return until the new process has run to completion and will return the exit code
1830 of the process the run is successful, or ``-signal`` if a signal kills the
Georg Brandl9af94982008-09-13 17:41:16 +00001831 process. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001832
1833 .. versionadded:: 1.6
1834
1835
1836.. data:: P_DETACH
1837 P_OVERLAY
1838
1839 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1840 functions. These are less portable than those listed above. :const:`P_DETACH`
1841 is similar to :const:`P_NOWAIT`, but the new process is detached from the
1842 console of the calling process. If :const:`P_OVERLAY` is used, the current
1843 process will be replaced; the :func:`spawn\*` function will not return.
1844 Availability: Windows.
1845
1846 .. versionadded:: 1.6
1847
1848
1849.. function:: startfile(path[, operation])
1850
1851 Start a file with its associated application.
1852
1853 When *operation* is not specified or ``'open'``, this acts like double-clicking
1854 the file in Windows Explorer, or giving the file name as an argument to the
1855 :program:`start` command from the interactive command shell: the file is opened
1856 with whatever application (if any) its extension is associated.
1857
1858 When another *operation* is given, it must be a "command verb" that specifies
1859 what should be done with the file. Common verbs documented by Microsoft are
1860 ``'print'`` and ``'edit'`` (to be used on files) as well as ``'explore'`` and
1861 ``'find'`` (to be used on directories).
1862
1863 :func:`startfile` returns as soon as the associated application is launched.
1864 There is no option to wait for the application to close, and no way to retrieve
1865 the application's exit status. The *path* parameter is relative to the current
1866 directory. If you want to use an absolute path, make sure the first character
1867 is not a slash (``'/'``); the underlying Win32 :cfunc:`ShellExecute` function
1868 doesn't work if it is. Use the :func:`os.path.normpath` function to ensure that
1869 the path is properly encoded for Win32. Availability: Windows.
1870
1871 .. versionadded:: 2.0
1872
1873 .. versionadded:: 2.5
1874 The *operation* parameter.
1875
1876
1877.. function:: system(command)
1878
1879 Execute the command (a string) in a subshell. This is implemented by calling
Georg Brandl647e9d22009-10-14 15:57:46 +00001880 the Standard C function :cfunc:`system`, and has the same limitations.
Georg Brandl11abfe62009-10-18 07:58:12 +00001881 Changes to :data:`sys.stdin`, etc. are not reflected in the environment of the
Georg Brandl647e9d22009-10-14 15:57:46 +00001882 executed command.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001883
1884 On Unix, the return value is the exit status of the process encoded in the
1885 format specified for :func:`wait`. Note that POSIX does not specify the meaning
1886 of the return value of the C :cfunc:`system` function, so the return value of
1887 the Python function is system-dependent.
1888
1889 On Windows, the return value is that returned by the system shell after running
1890 *command*, given by the Windows environment variable :envvar:`COMSPEC`: on
1891 :program:`command.com` systems (Windows 95, 98 and ME) this is always ``0``; on
1892 :program:`cmd.exe` systems (Windows NT, 2000 and XP) this is the exit status of
1893 the command run; on systems using a non-native shell, consult your shell
1894 documentation.
1895
Georg Brandl9af94982008-09-13 17:41:16 +00001896 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001897
1898 The :mod:`subprocess` module provides more powerful facilities for spawning new
1899 processes and retrieving their results; using that module is preferable to using
Georg Brandl0ba92b22008-06-22 09:05:29 +00001900 this function. Use the :mod:`subprocess` module. Check especially the
1901 :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001902
1903
1904.. function:: times()
1905
1906 Return a 5-tuple of floating point numbers indicating accumulated (processor or
1907 other) times, in seconds. The items are: user time, system time, children's
1908 user time, children's system time, and elapsed real time since a fixed point in
1909 the past, in that order. See the Unix manual page :manpage:`times(2)` or the
Georg Brandl9af94982008-09-13 17:41:16 +00001910 corresponding Windows Platform API documentation. Availability: Unix,
Georg Brandl0a40ffb2008-02-13 07:20:22 +00001911 Windows. On Windows, only the first two items are filled, the others are zero.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001912
1913
1914.. function:: wait()
1915
1916 Wait for completion of a child process, and return a tuple containing its pid
1917 and exit status indication: a 16-bit number, whose low byte is the signal number
1918 that killed the process, and whose high byte is the exit status (if the signal
1919 number is zero); the high bit of the low byte is set if a core file was
Georg Brandl9af94982008-09-13 17:41:16 +00001920 produced. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001921
1922
1923.. function:: waitpid(pid, options)
1924
1925 The details of this function differ on Unix and Windows.
1926
1927 On Unix: Wait for completion of a child process given by process id *pid*, and
1928 return a tuple containing its process id and exit status indication (encoded as
1929 for :func:`wait`). The semantics of the call are affected by the value of the
1930 integer *options*, which should be ``0`` for normal operation.
1931
1932 If *pid* is greater than ``0``, :func:`waitpid` requests status information for
1933 that specific process. If *pid* is ``0``, the request is for the status of any
1934 child in the process group of the current process. If *pid* is ``-1``, the
1935 request pertains to any child of the current process. If *pid* is less than
1936 ``-1``, status is requested for any process in the process group ``-pid`` (the
1937 absolute value of *pid*).
1938
Gregory P. Smith59de7f52008-08-15 23:14:00 +00001939 An :exc:`OSError` is raised with the value of errno when the syscall
1940 returns -1.
1941
Georg Brandl8ec7f652007-08-15 14:28:01 +00001942 On Windows: Wait for completion of a process given by process handle *pid*, and
1943 return a tuple containing *pid*, and its exit status shifted left by 8 bits
1944 (shifting makes cross-platform use of the function easier). A *pid* less than or
1945 equal to ``0`` has no special meaning on Windows, and raises an exception. The
1946 value of integer *options* has no effect. *pid* can refer to any process whose
1947 id is known, not necessarily a child process. The :func:`spawn` functions called
1948 with :const:`P_NOWAIT` return suitable process handles.
1949
1950
1951.. function:: wait3([options])
1952
1953 Similar to :func:`waitpid`, except no process id argument is given and a
1954 3-element tuple containing the child's process id, exit status indication, and
1955 resource usage information is returned. Refer to :mod:`resource`.\
1956 :func:`getrusage` for details on resource usage information. The option
1957 argument is the same as that provided to :func:`waitpid` and :func:`wait4`.
1958 Availability: Unix.
1959
1960 .. versionadded:: 2.5
1961
1962
1963.. function:: wait4(pid, options)
1964
1965 Similar to :func:`waitpid`, except a 3-element tuple, containing the child's
1966 process id, exit status indication, and resource usage information is returned.
1967 Refer to :mod:`resource`.\ :func:`getrusage` for details on resource usage
1968 information. The arguments to :func:`wait4` are the same as those provided to
1969 :func:`waitpid`. Availability: Unix.
1970
1971 .. versionadded:: 2.5
1972
1973
1974.. data:: WNOHANG
1975
1976 The option for :func:`waitpid` to return immediately if no child process status
1977 is available immediately. The function returns ``(0, 0)`` in this case.
Georg Brandl9af94982008-09-13 17:41:16 +00001978 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001979
1980
1981.. data:: WCONTINUED
1982
1983 This option causes child processes to be reported if they have been continued
1984 from a job control stop since their status was last reported. Availability: Some
1985 Unix systems.
1986
1987 .. versionadded:: 2.3
1988
1989
1990.. data:: WUNTRACED
1991
1992 This option causes child processes to be reported if they have been stopped but
1993 their current state has not been reported since they were stopped. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00001994 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001995
1996 .. versionadded:: 2.3
1997
1998The following functions take a process status code as returned by
1999:func:`system`, :func:`wait`, or :func:`waitpid` as a parameter. They may be
2000used to determine the disposition of a process.
2001
2002
2003.. function:: WCOREDUMP(status)
2004
Georg Brandlf725b952008-01-05 19:44:22 +00002005 Return ``True`` if a core dump was generated for the process, otherwise
Georg Brandl9af94982008-09-13 17:41:16 +00002006 return ``False``. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002007
2008 .. versionadded:: 2.3
2009
2010
2011.. function:: WIFCONTINUED(status)
2012
Georg Brandlf725b952008-01-05 19:44:22 +00002013 Return ``True`` if the process has been continued from a job control stop,
2014 otherwise return ``False``. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002015
2016 .. versionadded:: 2.3
2017
2018
2019.. function:: WIFSTOPPED(status)
2020
Georg Brandlf725b952008-01-05 19:44:22 +00002021 Return ``True`` if the process has been stopped, otherwise return
Georg Brandl8ec7f652007-08-15 14:28:01 +00002022 ``False``. Availability: Unix.
2023
2024
2025.. function:: WIFSIGNALED(status)
2026
Georg Brandlf725b952008-01-05 19:44:22 +00002027 Return ``True`` if the process exited due to a signal, otherwise return
Georg Brandl9af94982008-09-13 17:41:16 +00002028 ``False``. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002029
2030
2031.. function:: WIFEXITED(status)
2032
Georg Brandlf725b952008-01-05 19:44:22 +00002033 Return ``True`` if the process exited using the :manpage:`exit(2)` system call,
Georg Brandl9af94982008-09-13 17:41:16 +00002034 otherwise return ``False``. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002035
2036
2037.. function:: WEXITSTATUS(status)
2038
2039 If ``WIFEXITED(status)`` is true, return the integer parameter to the
2040 :manpage:`exit(2)` system call. Otherwise, the return value is meaningless.
Georg Brandl9af94982008-09-13 17:41:16 +00002041 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002042
2043
2044.. function:: WSTOPSIG(status)
2045
Georg Brandl9af94982008-09-13 17:41:16 +00002046 Return the signal which caused the process to stop. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002047
2048
2049.. function:: WTERMSIG(status)
2050
Georg Brandl9af94982008-09-13 17:41:16 +00002051 Return the signal which caused the process to exit. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002052
2053
2054.. _os-path:
2055
2056Miscellaneous System Information
2057--------------------------------
2058
2059
2060.. function:: confstr(name)
2061
2062 Return string-valued system configuration values. *name* specifies the
2063 configuration value to retrieve; it may be a string which is the name of a
2064 defined system value; these names are specified in a number of standards (POSIX,
2065 Unix 95, Unix 98, and others). Some platforms define additional names as well.
2066 The names known to the host operating system are given as the keys of the
2067 ``confstr_names`` dictionary. For configuration variables not included in that
2068 mapping, passing an integer for *name* is also accepted. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00002069 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002070
2071 If the configuration value specified by *name* isn't defined, ``None`` is
2072 returned.
2073
2074 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
2075 specific value for *name* is not supported by the host system, even if it is
2076 included in ``confstr_names``, an :exc:`OSError` is raised with
2077 :const:`errno.EINVAL` for the error number.
2078
2079
2080.. data:: confstr_names
2081
2082 Dictionary mapping names accepted by :func:`confstr` to the integer values
2083 defined for those names by the host operating system. This can be used to
Georg Brandl9af94982008-09-13 17:41:16 +00002084 determine the set of names known to the system. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002085
2086
2087.. function:: getloadavg()
2088
Georg Brandl57fe0f22008-01-12 10:53:29 +00002089 Return the number of processes in the system run queue averaged over the last
2090 1, 5, and 15 minutes or raises :exc:`OSError` if the load average was
Georg Brandl6bb7bcf2008-05-30 19:12:13 +00002091 unobtainable. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002092
2093 .. versionadded:: 2.3
2094
2095
2096.. function:: sysconf(name)
2097
2098 Return integer-valued system configuration values. If the configuration value
2099 specified by *name* isn't defined, ``-1`` is returned. The comments regarding
2100 the *name* parameter for :func:`confstr` apply here as well; the dictionary that
2101 provides information on the known names is given by ``sysconf_names``.
Georg Brandl9af94982008-09-13 17:41:16 +00002102 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002103
2104
2105.. data:: sysconf_names
2106
2107 Dictionary mapping names accepted by :func:`sysconf` to the integer values
2108 defined for those names by the host operating system. This can be used to
Georg Brandl9af94982008-09-13 17:41:16 +00002109 determine the set of names known to the system. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002110
Georg Brandlf725b952008-01-05 19:44:22 +00002111The following data values are used to support path manipulation operations. These
Georg Brandl8ec7f652007-08-15 14:28:01 +00002112are defined for all platforms.
2113
2114Higher-level operations on pathnames are defined in the :mod:`os.path` module.
2115
2116
2117.. data:: curdir
2118
2119 The constant string used by the operating system to refer to the current
Georg Brandl9af94982008-09-13 17:41:16 +00002120 directory. This is ``'.'`` for Windows and POSIX. Also available via
2121 :mod:`os.path`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002122
2123
2124.. data:: pardir
2125
2126 The constant string used by the operating system to refer to the parent
Georg Brandl9af94982008-09-13 17:41:16 +00002127 directory. This is ``'..'`` for Windows and POSIX. Also available via
2128 :mod:`os.path`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002129
2130
2131.. data:: sep
2132
Georg Brandl9af94982008-09-13 17:41:16 +00002133 The character used by the operating system to separate pathname components.
2134 This is ``'/'`` for POSIX and ``'\\'`` for Windows. Note that knowing this
2135 is not sufficient to be able to parse or concatenate pathnames --- use
Georg Brandl8ec7f652007-08-15 14:28:01 +00002136 :func:`os.path.split` and :func:`os.path.join` --- but it is occasionally
2137 useful. Also available via :mod:`os.path`.
2138
2139
2140.. data:: altsep
2141
2142 An alternative character used by the operating system to separate pathname
2143 components, or ``None`` if only one separator character exists. This is set to
2144 ``'/'`` on Windows systems where ``sep`` is a backslash. Also available via
2145 :mod:`os.path`.
2146
2147
2148.. data:: extsep
2149
2150 The character which separates the base filename from the extension; for example,
2151 the ``'.'`` in :file:`os.py`. Also available via :mod:`os.path`.
2152
2153 .. versionadded:: 2.2
2154
2155
2156.. data:: pathsep
2157
2158 The character conventionally used by the operating system to separate search
2159 path components (as in :envvar:`PATH`), such as ``':'`` for POSIX or ``';'`` for
2160 Windows. Also available via :mod:`os.path`.
2161
2162
2163.. data:: defpath
2164
2165 The default search path used by :func:`exec\*p\*` and :func:`spawn\*p\*` if the
2166 environment doesn't have a ``'PATH'`` key. Also available via :mod:`os.path`.
2167
2168
2169.. data:: linesep
2170
2171 The string used to separate (or, rather, terminate) lines on the current
Georg Brandl9af94982008-09-13 17:41:16 +00002172 platform. This may be a single character, such as ``'\n'`` for POSIX, or
2173 multiple characters, for example, ``'\r\n'`` for Windows. Do not use
2174 *os.linesep* as a line terminator when writing files opened in text mode (the
2175 default); use a single ``'\n'`` instead, on all platforms.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002176
2177
2178.. data:: devnull
2179
Georg Brandl9af94982008-09-13 17:41:16 +00002180 The file path of the null device. For example: ``'/dev/null'`` for POSIX.
2181 Also available via :mod:`os.path`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002182
2183 .. versionadded:: 2.4
2184
2185
2186.. _os-miscfunc:
2187
2188Miscellaneous Functions
2189-----------------------
2190
2191
2192.. function:: urandom(n)
2193
2194 Return a string of *n* random bytes suitable for cryptographic use.
2195
2196 This function returns random bytes from an OS-specific randomness source. The
2197 returned data should be unpredictable enough for cryptographic applications,
2198 though its exact quality depends on the OS implementation. On a UNIX-like
2199 system this will query /dev/urandom, and on Windows it will use CryptGenRandom.
2200 If a randomness source is not found, :exc:`NotImplementedError` will be raised.
2201
2202 .. versionadded:: 2.4
2203