blob: 845f149008fc4f380ce9a612c0b75c902e8a8807 [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 Brandl953fe5f2010-03-21 19:06:51 +000016Notes on the availability of these functions:
Georg Brandl8ec7f652007-08-15 14:28:01 +000017
Georg Brandl953fe5f2010-03-21 19:06:51 +000018* The design of all built-in operating system dependent modules of Python is
19 such that as long as the same functionality is available, it uses the same
20 interface; for example, the function ``os.stat(path)`` returns stat
21 information about *path* in the same format (which happens to have originated
22 with the POSIX interface).
Georg Brandl8ec7f652007-08-15 14:28:01 +000023
Georg Brandl953fe5f2010-03-21 19:06:51 +000024* Extensions peculiar to a particular operating system are also available
25 through the :mod:`os` module, but using them is of course a threat to
26 portability.
Georg Brandl8ec7f652007-08-15 14:28:01 +000027
Georg Brandl953fe5f2010-03-21 19:06:51 +000028* An "Availability: Unix" note means that this function is commonly found on
29 Unix systems. It does not make any claims about its existence on a specific
30 operating system.
31
32* If not separately noted, all functions that claim "Availability: Unix" are
33 supported on Mac OS X, which builds on a Unix core.
Georg Brandl9af94982008-09-13 17:41:16 +000034
35.. note::
36
Georg Brandl57fe0f22008-01-12 10:53:29 +000037 All functions in this module raise :exc:`OSError` in the case of invalid or
38 inaccessible file names and paths, or other arguments that have the correct
39 type, but are not accepted by the operating system.
Georg Brandl8ec7f652007-08-15 14:28:01 +000040
Georg Brandl8ec7f652007-08-15 14:28:01 +000041
42.. exception:: error
43
Georg Brandl57fe0f22008-01-12 10:53:29 +000044 An alias for the built-in :exc:`OSError` exception.
Georg Brandl8ec7f652007-08-15 14:28:01 +000045
46
47.. data:: name
48
Georg Brandl953fe5f2010-03-21 19:06:51 +000049 The name of the operating system dependent module imported. The following
50 names have currently been registered: ``'posix'``, ``'nt'``, ``'mac'``,
51 ``'os2'``, ``'ce'``, ``'java'``, ``'riscos'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +000052
53
Georg Brandl8ec7f652007-08-15 14:28:01 +000054.. _os-procinfo:
55
56Process Parameters
57------------------
58
59These functions and data items provide information and operate on the current
60process and user.
61
62
63.. data:: environ
64
65 A mapping object representing the string environment. For example,
66 ``environ['HOME']`` is the pathname of your home directory (on some platforms),
67 and is equivalent to ``getenv("HOME")`` in C.
68
69 This mapping is captured the first time the :mod:`os` module is imported,
70 typically during Python startup as part of processing :file:`site.py`. Changes
71 to the environment made after this time are not reflected in ``os.environ``,
72 except for changes made by modifying ``os.environ`` directly.
73
74 If the platform supports the :func:`putenv` function, this mapping may be used
75 to modify the environment as well as query the environment. :func:`putenv` will
76 be called automatically when the mapping is modified.
77
78 .. note::
79
80 Calling :func:`putenv` directly does not change ``os.environ``, so it's better
81 to modify ``os.environ``.
82
83 .. note::
84
Georg Brandl9af94982008-09-13 17:41:16 +000085 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
86 cause memory leaks. Refer to the system documentation for
87 :cfunc:`putenv`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000088
89 If :func:`putenv` is not provided, a modified copy of this mapping may be
90 passed to the appropriate process-creation functions to cause child processes
91 to use a modified environment.
92
Georg Brandl4a212682007-09-20 17:57:59 +000093 If the platform supports the :func:`unsetenv` function, you can delete items in
Georg Brandl8ec7f652007-08-15 14:28:01 +000094 this mapping to unset environment variables. :func:`unsetenv` will be called
Georg Brandl4a212682007-09-20 17:57:59 +000095 automatically when an item is deleted from ``os.environ``, and when
Georg Brandl1a94ec22007-10-24 21:40:38 +000096 one of the :meth:`pop` or :meth:`clear` methods is called.
Georg Brandl4a212682007-09-20 17:57:59 +000097
98 .. versionchanged:: 2.6
Georg Brandl1a94ec22007-10-24 21:40:38 +000099 Also unset environment variables when calling :meth:`os.environ.clear`
100 and :meth:`os.environ.pop`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000101
102
103.. function:: chdir(path)
104 fchdir(fd)
105 getcwd()
106 :noindex:
107
108 These functions are described in :ref:`os-file-dir`.
109
110
111.. function:: ctermid()
112
113 Return the filename corresponding to the controlling terminal of the process.
114 Availability: Unix.
115
116
117.. function:: getegid()
118
119 Return the effective group id of the current process. This corresponds to the
Georg Brandlf725b952008-01-05 19:44:22 +0000120 "set id" bit on the file being executed in the current process. Availability:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000121 Unix.
122
123
124.. function:: geteuid()
125
126 .. index:: single: user; effective id
127
Georg Brandlf725b952008-01-05 19:44:22 +0000128 Return the current process's effective user id. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000129
130
131.. function:: getgid()
132
133 .. index:: single: process; group
134
135 Return the real group id of the current process. Availability: Unix.
136
137
138.. function:: getgroups()
139
140 Return list of supplemental group ids associated with the current process.
141 Availability: Unix.
142
143
144.. function:: getlogin()
145
146 Return the name of the user logged in on the controlling terminal of the
147 process. For most purposes, it is more useful to use the environment variable
148 :envvar:`LOGNAME` to find out who the user is, or
149 ``pwd.getpwuid(os.getuid())[0]`` to get the login name of the currently
Georg Brandlf725b952008-01-05 19:44:22 +0000150 effective user id. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000151
152
153.. function:: getpgid(pid)
154
155 Return the process group id of the process with process id *pid*. If *pid* is 0,
156 the process group id of the current process is returned. Availability: Unix.
157
158 .. versionadded:: 2.3
159
160
161.. function:: getpgrp()
162
163 .. index:: single: process; group
164
165 Return the id of the current process group. Availability: Unix.
166
167
168.. function:: getpid()
169
170 .. index:: single: process; id
171
172 Return the current process id. Availability: Unix, Windows.
173
174
175.. function:: getppid()
176
177 .. index:: single: process; id of parent
178
179 Return the parent's process id. Availability: Unix.
180
181
182.. function:: getuid()
183
184 .. index:: single: user; id
185
Georg Brandlf725b952008-01-05 19:44:22 +0000186 Return the current process's user id. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000187
188
189.. function:: getenv(varname[, value])
190
191 Return the value of the environment variable *varname* if it exists, or *value*
192 if it doesn't. *value* defaults to ``None``. Availability: most flavors of
193 Unix, Windows.
194
195
196.. function:: putenv(varname, value)
197
198 .. index:: single: environment variables; setting
199
200 Set the environment variable named *varname* to the string *value*. Such
201 changes to the environment affect subprocesses started with :func:`os.system`,
202 :func:`popen` or :func:`fork` and :func:`execv`. Availability: most flavors of
203 Unix, Windows.
204
205 .. note::
206
Georg Brandl9af94982008-09-13 17:41:16 +0000207 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
208 cause memory leaks. Refer to the system documentation for putenv.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000209
210 When :func:`putenv` is supported, assignments to items in ``os.environ`` are
211 automatically translated into corresponding calls to :func:`putenv`; however,
212 calls to :func:`putenv` don't update ``os.environ``, so it is actually
213 preferable to assign to items of ``os.environ``.
214
215
216.. function:: setegid(egid)
217
218 Set the current process's effective group id. Availability: Unix.
219
220
221.. function:: seteuid(euid)
222
223 Set the current process's effective user id. Availability: Unix.
224
225
226.. function:: setgid(gid)
227
228 Set the current process' group id. Availability: Unix.
229
230
231.. function:: setgroups(groups)
232
233 Set the list of supplemental group ids associated with the current process to
234 *groups*. *groups* must be a sequence, and each element must be an integer
Georg Brandlf725b952008-01-05 19:44:22 +0000235 identifying a group. This operation is typically available only to the superuser.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000236 Availability: Unix.
237
238 .. versionadded:: 2.2
239
240
241.. function:: setpgrp()
242
Georg Brandlf725b952008-01-05 19:44:22 +0000243 Call the system call :cfunc:`setpgrp` or :cfunc:`setpgrp(0, 0)` depending on
Georg Brandl8ec7f652007-08-15 14:28:01 +0000244 which version is implemented (if any). See the Unix manual for the semantics.
245 Availability: Unix.
246
247
248.. function:: setpgid(pid, pgrp)
249
Georg Brandlf725b952008-01-05 19:44:22 +0000250 Call the system call :cfunc:`setpgid` to set the process group id of the
Georg Brandl8ec7f652007-08-15 14:28:01 +0000251 process with id *pid* to the process group with id *pgrp*. See the Unix manual
252 for the semantics. Availability: Unix.
253
254
255.. function:: setreuid(ruid, euid)
256
257 Set the current process's real and effective user ids. Availability: Unix.
258
259
260.. function:: setregid(rgid, egid)
261
262 Set the current process's real and effective group ids. Availability: Unix.
263
264
265.. function:: getsid(pid)
266
Georg Brandlf725b952008-01-05 19:44:22 +0000267 Call the system call :cfunc:`getsid`. See the Unix manual for the semantics.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000268 Availability: Unix.
269
270 .. versionadded:: 2.4
271
272
273.. function:: setsid()
274
Georg Brandlf725b952008-01-05 19:44:22 +0000275 Call the system call :cfunc:`setsid`. See the Unix manual for the semantics.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000276 Availability: Unix.
277
278
279.. function:: setuid(uid)
280
281 .. index:: single: user; id, setting
282
Georg Brandlf725b952008-01-05 19:44:22 +0000283 Set the current process's user id. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000284
Georg Brandl8ec7f652007-08-15 14:28:01 +0000285
Georg Brandlb19be572007-12-29 10:57:00 +0000286.. placed in this section since it relates to errno.... a little weak
Georg Brandl8ec7f652007-08-15 14:28:01 +0000287.. function:: strerror(code)
288
289 Return the error message corresponding to the error code in *code*.
Georg Brandl3fc974f2008-05-11 21:16:37 +0000290 On platforms where :cfunc:`strerror` returns ``NULL`` when given an unknown
291 error number, :exc:`ValueError` is raised. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000292
293
294.. function:: umask(mask)
295
Georg Brandlf725b952008-01-05 19:44:22 +0000296 Set the current numeric umask and return the previous umask. Availability:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000297 Unix, Windows.
298
299
300.. function:: uname()
301
302 .. index::
303 single: gethostname() (in module socket)
304 single: gethostbyaddr() (in module socket)
305
306 Return a 5-tuple containing information identifying the current operating
307 system. The tuple contains 5 strings: ``(sysname, nodename, release, version,
308 machine)``. Some systems truncate the nodename to 8 characters or to the
309 leading component; a better way to get the hostname is
310 :func:`socket.gethostname` or even
311 ``socket.gethostbyaddr(socket.gethostname())``. Availability: recent flavors of
312 Unix.
313
314
315.. function:: unsetenv(varname)
316
317 .. index:: single: environment variables; deleting
318
319 Unset (delete) the environment variable named *varname*. Such changes to the
320 environment affect subprocesses started with :func:`os.system`, :func:`popen` or
321 :func:`fork` and :func:`execv`. Availability: most flavors of Unix, Windows.
322
323 When :func:`unsetenv` is supported, deletion of items in ``os.environ`` is
324 automatically translated into a corresponding call to :func:`unsetenv`; however,
325 calls to :func:`unsetenv` don't update ``os.environ``, so it is actually
326 preferable to delete items of ``os.environ``.
327
328
329.. _os-newstreams:
330
331File Object Creation
332--------------------
333
334These functions create new file objects. (See also :func:`open`.)
335
336
337.. function:: fdopen(fd[, mode[, bufsize]])
338
339 .. index:: single: I/O control; buffering
340
341 Return an open file object connected to the file descriptor *fd*. The *mode*
342 and *bufsize* arguments have the same meaning as the corresponding arguments to
Georg Brandl9af94982008-09-13 17:41:16 +0000343 the built-in :func:`open` function. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000344
345 .. versionchanged:: 2.3
346 When specified, the *mode* argument must now start with one of the letters
347 ``'r'``, ``'w'``, or ``'a'``, otherwise a :exc:`ValueError` is raised.
348
349 .. versionchanged:: 2.5
350 On Unix, when the *mode* argument starts with ``'a'``, the *O_APPEND* flag is
351 set on the file descriptor (which the :cfunc:`fdopen` implementation already
352 does on most platforms).
353
354
355.. function:: popen(command[, mode[, bufsize]])
356
357 Open a pipe to or from *command*. The return value is an open file object
358 connected to the pipe, which can be read or written depending on whether *mode*
359 is ``'r'`` (default) or ``'w'``. The *bufsize* argument has the same meaning as
360 the corresponding argument to the built-in :func:`open` function. The exit
361 status of the command (encoded in the format specified for :func:`wait`) is
Georg Brandle081eef2009-05-26 09:04:23 +0000362 available as the return value of the :meth:`~file.close` method of the file object,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000363 except that when the exit status is zero (termination without errors), ``None``
Georg Brandl9af94982008-09-13 17:41:16 +0000364 is returned. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000365
366 .. deprecated:: 2.6
Georg Brandl734373c2009-01-03 21:55:17 +0000367 This function is obsolete. Use the :mod:`subprocess` module. Check
Georg Brandl0ba92b22008-06-22 09:05:29 +0000368 especially the :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000369
370 .. versionchanged:: 2.0
371 This function worked unreliably under Windows in earlier versions of Python.
372 This was due to the use of the :cfunc:`_popen` function from the libraries
373 provided with Windows. Newer versions of Python do not use the broken
374 implementation from the Windows libraries.
375
376
377.. function:: tmpfile()
378
379 Return a new file object opened in update mode (``w+b``). The file has no
380 directory entries associated with it and will be automatically deleted once
Georg Brandl9af94982008-09-13 17:41:16 +0000381 there are no file descriptors for the file. Availability: Unix,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000382 Windows.
383
384There are a number of different :func:`popen\*` functions that provide slightly
385different ways to create subprocesses.
386
387.. deprecated:: 2.6
388 All of the :func:`popen\*` functions are obsolete. Use the :mod:`subprocess`
389 module.
390
391For each of the :func:`popen\*` variants, if *bufsize* is specified, it
392specifies the buffer size for the I/O pipes. *mode*, if provided, should be the
393string ``'b'`` or ``'t'``; on Windows this is needed to determine whether the
394file objects should be opened in binary or text mode. The default value for
395*mode* is ``'t'``.
396
397Also, for each of these variants, on Unix, *cmd* may be a sequence, in which
398case arguments will be passed directly to the program without shell intervention
399(as with :func:`os.spawnv`). If *cmd* is a string it will be passed to the shell
400(as with :func:`os.system`).
401
402These methods do not make it possible to retrieve the exit status from the child
403processes. The only way to control the input and output streams and also
404retrieve the return codes is to use the :mod:`subprocess` module; these are only
405available on Unix.
406
407For a discussion of possible deadlock conditions related to the use of these
408functions, see :ref:`popen2-flow-control`.
409
410
411.. function:: popen2(cmd[, mode[, bufsize]])
412
Georg Brandlf725b952008-01-05 19:44:22 +0000413 Execute *cmd* as a sub-process and return the file objects ``(child_stdin,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000414 child_stdout)``.
415
416 .. deprecated:: 2.6
Georg Brandl734373c2009-01-03 21:55:17 +0000417 This function is obsolete. Use the :mod:`subprocess` module. Check
Georg Brandl0ba92b22008-06-22 09:05:29 +0000418 especially the :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000419
Georg Brandl9af94982008-09-13 17:41:16 +0000420 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000421
422 .. versionadded:: 2.0
423
424
425.. function:: popen3(cmd[, mode[, bufsize]])
426
Georg Brandlf725b952008-01-05 19:44:22 +0000427 Execute *cmd* as a sub-process and return the file objects ``(child_stdin,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000428 child_stdout, child_stderr)``.
429
430 .. deprecated:: 2.6
Georg Brandl734373c2009-01-03 21:55:17 +0000431 This function is obsolete. Use the :mod:`subprocess` module. Check
Georg Brandl0ba92b22008-06-22 09:05:29 +0000432 especially the :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000433
Georg Brandl9af94982008-09-13 17:41:16 +0000434 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000435
436 .. versionadded:: 2.0
437
438
439.. function:: popen4(cmd[, mode[, bufsize]])
440
Georg Brandlf725b952008-01-05 19:44:22 +0000441 Execute *cmd* as a sub-process and return the file objects ``(child_stdin,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000442 child_stdout_and_stderr)``.
443
444 .. deprecated:: 2.6
Georg Brandl734373c2009-01-03 21:55:17 +0000445 This function is obsolete. Use the :mod:`subprocess` module. Check
Georg Brandl0ba92b22008-06-22 09:05:29 +0000446 especially the :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000447
Georg Brandl9af94982008-09-13 17:41:16 +0000448 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000449
450 .. versionadded:: 2.0
451
452(Note that ``child_stdin, child_stdout, and child_stderr`` are named from the
453point of view of the child process, so *child_stdin* is the child's standard
454input.)
455
456This functionality is also available in the :mod:`popen2` module using functions
457of the same names, but the return values of those functions have a different
458order.
459
460
461.. _os-fd-ops:
462
463File Descriptor Operations
464--------------------------
465
466These functions operate on I/O streams referenced using file descriptors.
467
468File descriptors are small integers corresponding to a file that has been opened
469by the current process. For example, standard input is usually file descriptor
4700, standard output is 1, and standard error is 2. Further files opened by a
471process will then be assigned 3, 4, 5, and so forth. The name "file descriptor"
472is slightly deceptive; on Unix platforms, sockets and pipes are also referenced
473by file descriptors.
474
475
476.. function:: close(fd)
477
Georg Brandl9af94982008-09-13 17:41:16 +0000478 Close file descriptor *fd*. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000479
480 .. note::
481
482 This function is intended for low-level I/O and must be applied to a file
Georg Brandle081eef2009-05-26 09:04:23 +0000483 descriptor as returned by :func:`os.open` or :func:`pipe`. To close a "file
Georg Brandl8ec7f652007-08-15 14:28:01 +0000484 object" returned by the built-in function :func:`open` or by :func:`popen` or
Georg Brandle081eef2009-05-26 09:04:23 +0000485 :func:`fdopen`, use its :meth:`~file.close` method.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000486
487
Georg Brandl309501a2008-01-19 20:22:13 +0000488.. function:: closerange(fd_low, fd_high)
489
490 Close all file descriptors from *fd_low* (inclusive) to *fd_high* (exclusive),
Georg Brandl9af94982008-09-13 17:41:16 +0000491 ignoring errors. Availability: Unix, Windows. Equivalent to::
Georg Brandl309501a2008-01-19 20:22:13 +0000492
493 for fd in xrange(fd_low, fd_high):
494 try:
495 os.close(fd)
496 except OSError:
497 pass
498
499 .. versionadded:: 2.6
500
501
Georg Brandl8ec7f652007-08-15 14:28:01 +0000502.. function:: dup(fd)
503
Georg Brandl9af94982008-09-13 17:41:16 +0000504 Return a duplicate of file descriptor *fd*. Availability: Unix,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000505 Windows.
506
507
508.. function:: dup2(fd, fd2)
509
510 Duplicate file descriptor *fd* to *fd2*, closing the latter first if necessary.
Georg Brandl9af94982008-09-13 17:41:16 +0000511 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000512
513
Christian Heimes36281872007-11-30 21:11:28 +0000514.. function:: fchmod(fd, mode)
515
516 Change the mode of the file given by *fd* to the numeric *mode*. See the docs
517 for :func:`chmod` for possible values of *mode*. Availability: Unix.
518
Georg Brandl81ddc1a2007-11-30 22:04:45 +0000519 .. versionadded:: 2.6
520
Christian Heimes36281872007-11-30 21:11:28 +0000521
522.. function:: fchown(fd, uid, gid)
523
524 Change the owner and group id of the file given by *fd* to the numeric *uid*
525 and *gid*. To leave one of the ids unchanged, set it to -1.
526 Availability: Unix.
527
Georg Brandl81ddc1a2007-11-30 22:04:45 +0000528 .. versionadded:: 2.6
529
Christian Heimes36281872007-11-30 21:11:28 +0000530
Georg Brandl8ec7f652007-08-15 14:28:01 +0000531.. function:: fdatasync(fd)
532
533 Force write of file with filedescriptor *fd* to disk. Does not force update of
534 metadata. Availability: Unix.
535
Georg Brandla3c242c2009-10-27 14:19:50 +0000536 .. note::
537 This function is not available on MacOS.
538
Georg Brandl8ec7f652007-08-15 14:28:01 +0000539
540.. function:: fpathconf(fd, name)
541
542 Return system configuration information relevant to an open file. *name*
543 specifies the configuration value to retrieve; it may be a string which is the
544 name of a defined system value; these names are specified in a number of
545 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
546 additional names as well. The names known to the host operating system are
547 given in the ``pathconf_names`` dictionary. For configuration variables not
548 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl9af94982008-09-13 17:41:16 +0000549 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000550
551 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
552 specific value for *name* is not supported by the host system, even if it is
553 included in ``pathconf_names``, an :exc:`OSError` is raised with
554 :const:`errno.EINVAL` for the error number.
555
556
557.. function:: fstat(fd)
558
559 Return status for file descriptor *fd*, like :func:`stat`. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +0000560 Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000561
562
563.. function:: fstatvfs(fd)
564
565 Return information about the filesystem containing the file associated with file
566 descriptor *fd*, like :func:`statvfs`. Availability: Unix.
567
568
569.. function:: fsync(fd)
570
571 Force write of file with filedescriptor *fd* to disk. On Unix, this calls the
572 native :cfunc:`fsync` function; on Windows, the MS :cfunc:`_commit` function.
573
574 If you're starting with a Python file object *f*, first do ``f.flush()``, and
575 then do ``os.fsync(f.fileno())``, to ensure that all internal buffers associated
Georg Brandl9af94982008-09-13 17:41:16 +0000576 with *f* are written to disk. Availability: Unix, and Windows
Georg Brandl8ec7f652007-08-15 14:28:01 +0000577 starting in 2.2.3.
578
579
580.. function:: ftruncate(fd, length)
581
582 Truncate the file corresponding to file descriptor *fd*, so that it is at most
Georg Brandl9af94982008-09-13 17:41:16 +0000583 *length* bytes in size. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000584
585
586.. function:: isatty(fd)
587
588 Return ``True`` if the file descriptor *fd* is open and connected to a
Georg Brandl9af94982008-09-13 17:41:16 +0000589 tty(-like) device, else ``False``. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000590
591
592.. function:: lseek(fd, pos, how)
593
Georg Brandlf725b952008-01-05 19:44:22 +0000594 Set the current position of file descriptor *fd* to position *pos*, modified
595 by *how*: :const:`SEEK_SET` or ``0`` to set the position relative to the
596 beginning of the file; :const:`SEEK_CUR` or ``1`` to set it relative to the
597 current position; :const:`os.SEEK_END` or ``2`` to set it relative to the end of
Georg Brandl9af94982008-09-13 17:41:16 +0000598 the file. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000599
600
601.. function:: open(file, flags[, mode])
602
603 Open the file *file* and set various flags according to *flags* and possibly its
604 mode according to *mode*. The default *mode* is ``0777`` (octal), and the
605 current umask value is first masked out. Return the file descriptor for the
Georg Brandl9af94982008-09-13 17:41:16 +0000606 newly opened file. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000607
608 For a description of the flag and mode values, see the C run-time documentation;
609 flag constants (like :const:`O_RDONLY` and :const:`O_WRONLY`) are defined in
610 this module too (see below).
611
612 .. note::
613
Georg Brandl0dfdf002009-10-27 14:36:50 +0000614 This function is intended for low-level I/O. For normal usage, use the
615 built-in function :func:`open`, which returns a "file object" with
616 :meth:`~file.read` and :meth:`~file.write` methods (and many more). To
617 wrap a file descriptor in a "file object", use :func:`fdopen`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000618
619
620.. function:: openpty()
621
622 .. index:: module: pty
623
624 Open a new pseudo-terminal pair. Return a pair of file descriptors ``(master,
625 slave)`` for the pty and the tty, respectively. For a (slightly) more portable
Georg Brandl9af94982008-09-13 17:41:16 +0000626 approach, use the :mod:`pty` module. Availability: some flavors of
Georg Brandl8ec7f652007-08-15 14:28:01 +0000627 Unix.
628
629
630.. function:: pipe()
631
632 Create a pipe. Return a pair of file descriptors ``(r, w)`` usable for reading
Georg Brandl9af94982008-09-13 17:41:16 +0000633 and writing, respectively. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000634
635
636.. function:: read(fd, n)
637
638 Read at most *n* bytes from file descriptor *fd*. Return a string containing the
639 bytes read. If the end of the file referred to by *fd* has been reached, an
Georg Brandl9af94982008-09-13 17:41:16 +0000640 empty string is returned. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000641
642 .. note::
643
644 This function is intended for low-level I/O and must be applied to a file
Georg Brandle081eef2009-05-26 09:04:23 +0000645 descriptor as returned by :func:`os.open` or :func:`pipe`. To read a "file object"
Georg Brandl8ec7f652007-08-15 14:28:01 +0000646 returned by the built-in function :func:`open` or by :func:`popen` or
Georg Brandle081eef2009-05-26 09:04:23 +0000647 :func:`fdopen`, or :data:`sys.stdin`, use its :meth:`~file.read` or
648 :meth:`~file.readline` methods.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000649
650
651.. function:: tcgetpgrp(fd)
652
653 Return the process group associated with the terminal given by *fd* (an open
Georg Brandle081eef2009-05-26 09:04:23 +0000654 file descriptor as returned by :func:`os.open`). Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000655
656
657.. function:: tcsetpgrp(fd, pg)
658
659 Set the process group associated with the terminal given by *fd* (an open file
Georg Brandle081eef2009-05-26 09:04:23 +0000660 descriptor as returned by :func:`os.open`) to *pg*. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000661
662
663.. function:: ttyname(fd)
664
665 Return a string which specifies the terminal device associated with
Georg Brandlbb75e4e2007-10-21 10:46:24 +0000666 file descriptor *fd*. If *fd* is not associated with a terminal device, an
Georg Brandl9af94982008-09-13 17:41:16 +0000667 exception is raised. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000668
669
670.. function:: write(fd, str)
671
672 Write the string *str* to file descriptor *fd*. Return the number of bytes
Georg Brandl9af94982008-09-13 17:41:16 +0000673 actually written. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000674
675 .. note::
676
677 This function is intended for low-level I/O and must be applied to a file
Georg Brandle081eef2009-05-26 09:04:23 +0000678 descriptor as returned by :func:`os.open` or :func:`pipe`. To write a "file
Georg Brandl8ec7f652007-08-15 14:28:01 +0000679 object" returned by the built-in function :func:`open` or by :func:`popen` or
Georg Brandle081eef2009-05-26 09:04:23 +0000680 :func:`fdopen`, or :data:`sys.stdout` or :data:`sys.stderr`, use its
681 :meth:`~file.write` method.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000682
Georg Brandlfa71a902008-12-05 09:08:28 +0000683The following constants are options for the *flags* parameter to the
Georg Brandle081eef2009-05-26 09:04:23 +0000684:func:`~os.open` function. They can be combined using the bitwise OR operator
Georg Brandlfa71a902008-12-05 09:08:28 +0000685``|``. Some of them are not available on all platforms. For descriptions of
Georg Brandlf3a0b862008-12-07 14:47:12 +0000686their availability and use, consult the :manpage:`open(2)` manual page on Unix
Doug Hellmann0108ed32009-09-20 20:55:04 +0000687or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000688
689
690.. data:: O_RDONLY
691 O_WRONLY
692 O_RDWR
693 O_APPEND
694 O_CREAT
695 O_EXCL
696 O_TRUNC
697
Georg Brandlfa71a902008-12-05 09:08:28 +0000698 These constants are available on Unix and Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000699
700
701.. data:: O_DSYNC
702 O_RSYNC
703 O_SYNC
704 O_NDELAY
705 O_NONBLOCK
706 O_NOCTTY
707 O_SHLOCK
708 O_EXLOCK
709
Georg Brandlfa71a902008-12-05 09:08:28 +0000710 These constants are only available on Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000711
712
713.. data:: O_BINARY
Georg Brandlb67da6e2007-11-24 13:56:09 +0000714 O_NOINHERIT
Georg Brandl8ec7f652007-08-15 14:28:01 +0000715 O_SHORT_LIVED
716 O_TEMPORARY
717 O_RANDOM
718 O_SEQUENTIAL
719 O_TEXT
720
Georg Brandlfa71a902008-12-05 09:08:28 +0000721 These constants are only available on Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000722
723
Georg Brandlae6b9f32008-05-16 13:41:26 +0000724.. data:: O_ASYNC
725 O_DIRECT
Georg Brandlb67da6e2007-11-24 13:56:09 +0000726 O_DIRECTORY
727 O_NOFOLLOW
728 O_NOATIME
729
Georg Brandlfa71a902008-12-05 09:08:28 +0000730 These constants are GNU extensions and not present if they are not defined by
731 the C library.
Georg Brandlb67da6e2007-11-24 13:56:09 +0000732
733
Georg Brandl8ec7f652007-08-15 14:28:01 +0000734.. data:: SEEK_SET
735 SEEK_CUR
736 SEEK_END
737
738 Parameters to the :func:`lseek` function. Their values are 0, 1, and 2,
Georg Brandl9af94982008-09-13 17:41:16 +0000739 respectively. Availability: Windows, Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000740
741 .. versionadded:: 2.5
742
743
744.. _os-file-dir:
745
746Files and Directories
747---------------------
748
Georg Brandl8ec7f652007-08-15 14:28:01 +0000749.. function:: access(path, mode)
750
751 Use the real uid/gid to test for access to *path*. Note that most operations
752 will use the effective uid/gid, therefore this routine can be used in a
753 suid/sgid environment to test if the invoking user has the specified access to
754 *path*. *mode* should be :const:`F_OK` to test the existence of *path*, or it
755 can be the inclusive OR of one or more of :const:`R_OK`, :const:`W_OK`, and
756 :const:`X_OK` to test permissions. Return :const:`True` if access is allowed,
757 :const:`False` if not. See the Unix man page :manpage:`access(2)` for more
Georg Brandl9af94982008-09-13 17:41:16 +0000758 information. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000759
760 .. note::
761
Georg Brandl0dfdf002009-10-27 14:36:50 +0000762 Using :func:`access` to check if a user is authorized to e.g. open a file
763 before actually doing so using :func:`open` creates a security hole,
764 because the user might exploit the short time interval between checking
765 and opening the file to manipulate it.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000766
767 .. note::
768
769 I/O operations may fail even when :func:`access` indicates that they would
770 succeed, particularly for operations on network filesystems which may have
771 permissions semantics beyond the usual POSIX permission-bit model.
772
773
774.. data:: F_OK
775
776 Value to pass as the *mode* parameter of :func:`access` to test the existence of
777 *path*.
778
779
780.. data:: R_OK
781
782 Value to include in the *mode* parameter of :func:`access` to test the
783 readability of *path*.
784
785
786.. data:: W_OK
787
788 Value to include in the *mode* parameter of :func:`access` to test the
789 writability of *path*.
790
791
792.. data:: X_OK
793
794 Value to include in the *mode* parameter of :func:`access` to determine if
795 *path* can be executed.
796
797
798.. function:: chdir(path)
799
800 .. index:: single: directory; changing
801
Georg Brandl9af94982008-09-13 17:41:16 +0000802 Change the current working directory to *path*. Availability: Unix,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000803 Windows.
804
805
806.. function:: fchdir(fd)
807
808 Change the current working directory to the directory represented by the file
809 descriptor *fd*. The descriptor must refer to an opened directory, not an open
810 file. Availability: Unix.
811
812 .. versionadded:: 2.3
813
814
815.. function:: getcwd()
816
817 Return a string representing the current working directory. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +0000818 Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000819
820
821.. function:: getcwdu()
822
823 Return a Unicode object representing the current working directory.
Georg Brandl9af94982008-09-13 17:41:16 +0000824 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000825
826 .. versionadded:: 2.3
827
828
829.. function:: chflags(path, flags)
830
831 Set the flags of *path* to the numeric *flags*. *flags* may take a combination
832 (bitwise OR) of the following values (as defined in the :mod:`stat` module):
833
834 * ``UF_NODUMP``
835 * ``UF_IMMUTABLE``
836 * ``UF_APPEND``
837 * ``UF_OPAQUE``
838 * ``UF_NOUNLINK``
839 * ``SF_ARCHIVED``
840 * ``SF_IMMUTABLE``
841 * ``SF_APPEND``
842 * ``SF_NOUNLINK``
843 * ``SF_SNAPSHOT``
844
Georg Brandl9af94982008-09-13 17:41:16 +0000845 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000846
847 .. versionadded:: 2.6
848
849
850.. function:: chroot(path)
851
852 Change the root directory of the current process to *path*. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +0000853 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000854
855 .. versionadded:: 2.2
856
857
858.. function:: chmod(path, mode)
859
860 Change the mode of *path* to the numeric *mode*. *mode* may take one of the
Georg Brandlf725b952008-01-05 19:44:22 +0000861 following values (as defined in the :mod:`stat` module) or bitwise ORed
Georg Brandl8ec7f652007-08-15 14:28:01 +0000862 combinations of them:
863
864
R. David Murrayba2c2b12009-07-21 14:23:11 +0000865 * :data:`stat.S_ISUID`
866 * :data:`stat.S_ISGID`
867 * :data:`stat.S_ENFMT`
868 * :data:`stat.S_ISVTX`
869 * :data:`stat.S_IREAD`
870 * :data:`stat.S_IWRITE`
871 * :data:`stat.S_IEXEC`
872 * :data:`stat.S_IRWXU`
873 * :data:`stat.S_IRUSR`
874 * :data:`stat.S_IWUSR`
875 * :data:`stat.S_IXUSR`
876 * :data:`stat.S_IRWXG`
877 * :data:`stat.S_IRGRP`
878 * :data:`stat.S_IWGRP`
879 * :data:`stat.S_IXGRP`
880 * :data:`stat.S_IRWXO`
881 * :data:`stat.S_IROTH`
882 * :data:`stat.S_IWOTH`
883 * :data:`stat.S_IXOTH`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000884
Georg Brandl9af94982008-09-13 17:41:16 +0000885 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000886
887 .. note::
888
889 Although Windows supports :func:`chmod`, you can only set the file's read-only
890 flag with it (via the ``stat.S_IWRITE`` and ``stat.S_IREAD``
891 constants or a corresponding integer value). All other bits are
892 ignored.
893
894
895.. function:: chown(path, uid, gid)
896
897 Change the owner and group id of *path* to the numeric *uid* and *gid*. To leave
Georg Brandl9af94982008-09-13 17:41:16 +0000898 one of the ids unchanged, set it to -1. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000899
900
901.. function:: lchflags(path, flags)
902
903 Set the flags of *path* to the numeric *flags*, like :func:`chflags`, but do not
904 follow symbolic links. Availability: Unix.
905
906 .. versionadded:: 2.6
907
908
Georg Brandl81ddc1a2007-11-30 22:04:45 +0000909.. function:: lchmod(path, mode)
910
911 Change the mode of *path* to the numeric *mode*. If path is a symlink, this
912 affects the symlink rather than the target. See the docs for :func:`chmod`
913 for possible values of *mode*. Availability: Unix.
914
915 .. versionadded:: 2.6
916
917
Georg Brandl8ec7f652007-08-15 14:28:01 +0000918.. function:: lchown(path, uid, gid)
919
Georg Brandlf725b952008-01-05 19:44:22 +0000920 Change the owner and group id of *path* to the numeric *uid* and *gid*. This
Georg Brandl9af94982008-09-13 17:41:16 +0000921 function will not follow symbolic links. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000922
923 .. versionadded:: 2.3
924
925
Georg Brandl78559542009-10-27 14:03:07 +0000926.. function:: link(source, link_name)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000927
Georg Brandl78559542009-10-27 14:03:07 +0000928 Create a hard link pointing to *source* named *link_name*. Availability:
929 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000930
931
932.. function:: listdir(path)
933
Georg Brandld2094602008-12-05 08:51:30 +0000934 Return a list containing the names of the entries in the directory given by
935 *path*. The list is in arbitrary order. It does not include the special
936 entries ``'.'`` and ``'..'`` even if they are present in the
937 directory. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000938
939 .. versionchanged:: 2.3
940 On Windows NT/2k/XP and Unix, if *path* is a Unicode object, the result will be
Georg Brandle081eef2009-05-26 09:04:23 +0000941 a list of Unicode objects. Undecodable filenames will still be returned as
942 string objects.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000943
944
945.. function:: lstat(path)
946
Georg Brandl03b15c62007-11-01 17:19:33 +0000947 Like :func:`stat`, but do not follow symbolic links. This is an alias for
948 :func:`stat` on platforms that do not support symbolic links, such as
949 Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000950
951
952.. function:: mkfifo(path[, mode])
953
954 Create a FIFO (a named pipe) named *path* with numeric mode *mode*. The default
955 *mode* is ``0666`` (octal). The current umask value is first masked out from
Georg Brandl9af94982008-09-13 17:41:16 +0000956 the mode. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000957
958 FIFOs are pipes that can be accessed like regular files. FIFOs exist until they
959 are deleted (for example with :func:`os.unlink`). Generally, FIFOs are used as
960 rendezvous between "client" and "server" type processes: the server opens the
961 FIFO for reading, and the client opens it for writing. Note that :func:`mkfifo`
962 doesn't open the FIFO --- it just creates the rendezvous point.
963
964
965.. function:: mknod(filename[, mode=0600, device])
966
967 Create a filesystem node (file, device special file or named pipe) named
968 *filename*. *mode* specifies both the permissions to use and the type of node to
969 be created, being combined (bitwise OR) with one of ``stat.S_IFREG``,
970 ``stat.S_IFCHR``, ``stat.S_IFBLK``,
971 and ``stat.S_IFIFO`` (those constants are available in :mod:`stat`).
972 For ``stat.S_IFCHR`` and
973 ``stat.S_IFBLK``, *device* defines the newly created device special file (probably using
974 :func:`os.makedev`), otherwise it is ignored.
975
976 .. versionadded:: 2.3
977
978
979.. function:: major(device)
980
Georg Brandlf725b952008-01-05 19:44:22 +0000981 Extract the device major number from a raw device number (usually the
Georg Brandl8ec7f652007-08-15 14:28:01 +0000982 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
983
984 .. versionadded:: 2.3
985
986
987.. function:: minor(device)
988
Georg Brandlf725b952008-01-05 19:44:22 +0000989 Extract the device minor number from a raw device number (usually the
Georg Brandl8ec7f652007-08-15 14:28:01 +0000990 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
991
992 .. versionadded:: 2.3
993
994
995.. function:: makedev(major, minor)
996
Georg Brandlf725b952008-01-05 19:44:22 +0000997 Compose a raw device number from the major and minor device numbers.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000998
999 .. versionadded:: 2.3
1000
1001
1002.. function:: mkdir(path[, mode])
1003
1004 Create a directory named *path* with numeric mode *mode*. The default *mode* is
1005 ``0777`` (octal). On some systems, *mode* is ignored. Where it is used, the
Georg Brandl9af94982008-09-13 17:41:16 +00001006 current umask value is first masked out. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001007
Mark Summerfieldac3d4292007-11-02 08:24:59 +00001008 It is also possible to create temporary directories; see the
1009 :mod:`tempfile` module's :func:`tempfile.mkdtemp` function.
1010
Georg Brandl8ec7f652007-08-15 14:28:01 +00001011
1012.. function:: makedirs(path[, mode])
1013
1014 .. index::
1015 single: directory; creating
1016 single: UNC paths; and os.makedirs()
1017
1018 Recursive directory creation function. Like :func:`mkdir`, but makes all
1019 intermediate-level directories needed to contain the leaf directory. Throws an
1020 :exc:`error` exception if the leaf directory already exists or cannot be
1021 created. The default *mode* is ``0777`` (octal). On some systems, *mode* is
1022 ignored. Where it is used, the current umask value is first masked out.
1023
1024 .. note::
1025
1026 :func:`makedirs` will become confused if the path elements to create include
Georg Brandlf725b952008-01-05 19:44:22 +00001027 :data:`os.pardir`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001028
1029 .. versionadded:: 1.5.2
1030
1031 .. versionchanged:: 2.3
1032 This function now handles UNC paths correctly.
1033
1034
1035.. function:: pathconf(path, name)
1036
1037 Return system configuration information relevant to a named file. *name*
1038 specifies the configuration value to retrieve; it may be a string which is the
1039 name of a defined system value; these names are specified in a number of
1040 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
1041 additional names as well. The names known to the host operating system are
1042 given in the ``pathconf_names`` dictionary. For configuration variables not
1043 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl9af94982008-09-13 17:41:16 +00001044 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001045
1046 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1047 specific value for *name* is not supported by the host system, even if it is
1048 included in ``pathconf_names``, an :exc:`OSError` is raised with
1049 :const:`errno.EINVAL` for the error number.
1050
1051
1052.. data:: pathconf_names
1053
1054 Dictionary mapping names accepted by :func:`pathconf` and :func:`fpathconf` to
1055 the integer values defined for those names by the host operating system. This
1056 can be used to determine the set of names known to the system. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00001057 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001058
1059
1060.. function:: readlink(path)
1061
1062 Return a string representing the path to which the symbolic link points. The
1063 result may be either an absolute or relative pathname; if it is relative, it may
1064 be converted to an absolute pathname using ``os.path.join(os.path.dirname(path),
1065 result)``.
1066
1067 .. versionchanged:: 2.6
1068 If the *path* is a Unicode object the result will also be a Unicode object.
1069
Georg Brandl9af94982008-09-13 17:41:16 +00001070 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001071
1072
1073.. function:: remove(path)
1074
Georg Brandl5be70d42009-10-27 14:50:20 +00001075 Remove (delete) the file *path*. If *path* is a directory, :exc:`OSError` is
1076 raised; see :func:`rmdir` below to remove a directory. This is identical to
1077 the :func:`unlink` function documented below. On Windows, attempting to
1078 remove a file that is in use causes an exception to be raised; on Unix, the
1079 directory entry is removed but the storage allocated to the file is not made
1080 available until the original file is no longer in use. Availability: Unix,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001081 Windows.
1082
1083
1084.. function:: removedirs(path)
1085
1086 .. index:: single: directory; deleting
1087
Georg Brandlf725b952008-01-05 19:44:22 +00001088 Remove directories recursively. Works like :func:`rmdir` except that, if the
Georg Brandl8ec7f652007-08-15 14:28:01 +00001089 leaf directory is successfully removed, :func:`removedirs` tries to
1090 successively remove every parent directory mentioned in *path* until an error
1091 is raised (which is ignored, because it generally means that a parent directory
1092 is not empty). For example, ``os.removedirs('foo/bar/baz')`` will first remove
1093 the directory ``'foo/bar/baz'``, and then remove ``'foo/bar'`` and ``'foo'`` if
1094 they are empty. Raises :exc:`OSError` if the leaf directory could not be
1095 successfully removed.
1096
1097 .. versionadded:: 1.5.2
1098
1099
1100.. function:: rename(src, dst)
1101
1102 Rename the file or directory *src* to *dst*. If *dst* is a directory,
1103 :exc:`OSError` will be raised. On Unix, if *dst* exists and is a file, it will
Georg Brandlf725b952008-01-05 19:44:22 +00001104 be replaced silently if the user has permission. The operation may fail on some
Georg Brandl8ec7f652007-08-15 14:28:01 +00001105 Unix flavors if *src* and *dst* are on different filesystems. If successful,
1106 the renaming will be an atomic operation (this is a POSIX requirement). On
1107 Windows, if *dst* already exists, :exc:`OSError` will be raised even if it is a
1108 file; there may be no way to implement an atomic rename when *dst* names an
Georg Brandl9af94982008-09-13 17:41:16 +00001109 existing file. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001110
1111
1112.. function:: renames(old, new)
1113
1114 Recursive directory or file renaming function. Works like :func:`rename`, except
1115 creation of any intermediate directories needed to make the new pathname good is
1116 attempted first. After the rename, directories corresponding to rightmost path
1117 segments of the old name will be pruned away using :func:`removedirs`.
1118
1119 .. versionadded:: 1.5.2
1120
1121 .. note::
1122
1123 This function can fail with the new directory structure made if you lack
1124 permissions needed to remove the leaf directory or file.
1125
1126
1127.. function:: rmdir(path)
1128
Georg Brandl5be70d42009-10-27 14:50:20 +00001129 Remove (delete) the directory *path*. Only works when the directory is
1130 empty, otherwise, :exc:`OSError` is raised. In order to remove whole
1131 directory trees, :func:`shutil.rmtree` can be used. Availability: Unix,
1132 Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001133
1134
1135.. function:: stat(path)
1136
1137 Perform a :cfunc:`stat` system call on the given path. The return value is an
1138 object whose attributes correspond to the members of the :ctype:`stat`
1139 structure, namely: :attr:`st_mode` (protection bits), :attr:`st_ino` (inode
1140 number), :attr:`st_dev` (device), :attr:`st_nlink` (number of hard links),
Georg Brandlf725b952008-01-05 19:44:22 +00001141 :attr:`st_uid` (user id of owner), :attr:`st_gid` (group id of owner),
Georg Brandl8ec7f652007-08-15 14:28:01 +00001142 :attr:`st_size` (size of file, in bytes), :attr:`st_atime` (time of most recent
1143 access), :attr:`st_mtime` (time of most recent content modification),
1144 :attr:`st_ctime` (platform dependent; time of most recent metadata change on
1145 Unix, or the time of creation on Windows)::
1146
1147 >>> import os
1148 >>> statinfo = os.stat('somefile.txt')
1149 >>> statinfo
1150 (33188, 422511L, 769L, 1, 1032, 100, 926L, 1105022698,1105022732, 1105022732)
1151 >>> statinfo.st_size
1152 926L
1153 >>>
1154
1155 .. versionchanged:: 2.3
Georg Brandlf725b952008-01-05 19:44:22 +00001156 If :func:`stat_float_times` returns ``True``, the time values are floats, measuring
Georg Brandl8ec7f652007-08-15 14:28:01 +00001157 seconds. Fractions of a second may be reported if the system supports that. On
1158 Mac OS, the times are always floats. See :func:`stat_float_times` for further
1159 discussion.
1160
1161 On some Unix systems (such as Linux), the following attributes may also be
1162 available: :attr:`st_blocks` (number of blocks allocated for file),
1163 :attr:`st_blksize` (filesystem blocksize), :attr:`st_rdev` (type of device if an
1164 inode device). :attr:`st_flags` (user defined flags for file).
1165
1166 On other Unix systems (such as FreeBSD), the following attributes may be
1167 available (but may be only filled out if root tries to use them): :attr:`st_gen`
1168 (file generation number), :attr:`st_birthtime` (time of file creation).
1169
1170 On Mac OS systems, the following attributes may also be available:
1171 :attr:`st_rsize`, :attr:`st_creator`, :attr:`st_type`.
1172
1173 On RISCOS systems, the following attributes are also available: :attr:`st_ftype`
1174 (file type), :attr:`st_attrs` (attributes), :attr:`st_obtype` (object type).
1175
1176 .. index:: module: stat
1177
1178 For backward compatibility, the return value of :func:`stat` is also accessible
1179 as a tuple of at least 10 integers giving the most important (and portable)
1180 members of the :ctype:`stat` structure, in the order :attr:`st_mode`,
1181 :attr:`st_ino`, :attr:`st_dev`, :attr:`st_nlink`, :attr:`st_uid`,
1182 :attr:`st_gid`, :attr:`st_size`, :attr:`st_atime`, :attr:`st_mtime`,
1183 :attr:`st_ctime`. More items may be added at the end by some implementations.
1184 The standard module :mod:`stat` defines functions and constants that are useful
1185 for extracting information from a :ctype:`stat` structure. (On Windows, some
1186 items are filled with dummy values.)
1187
1188 .. note::
1189
1190 The exact meaning and resolution of the :attr:`st_atime`, :attr:`st_mtime`, and
1191 :attr:`st_ctime` members depends on the operating system and the file system.
1192 For example, on Windows systems using the FAT or FAT32 file systems,
1193 :attr:`st_mtime` has 2-second resolution, and :attr:`st_atime` has only 1-day
1194 resolution. See your operating system documentation for details.
1195
Georg Brandl9af94982008-09-13 17:41:16 +00001196 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001197
1198 .. versionchanged:: 2.2
1199 Added access to values as attributes of the returned object.
1200
1201 .. versionchanged:: 2.5
Georg Brandlf725b952008-01-05 19:44:22 +00001202 Added :attr:`st_gen` and :attr:`st_birthtime`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001203
1204
1205.. function:: stat_float_times([newvalue])
1206
1207 Determine whether :class:`stat_result` represents time stamps as float objects.
1208 If *newvalue* is ``True``, future calls to :func:`stat` return floats, if it is
1209 ``False``, future calls return ints. If *newvalue* is omitted, return the
1210 current setting.
1211
1212 For compatibility with older Python versions, accessing :class:`stat_result` as
1213 a tuple always returns integers.
1214
1215 .. versionchanged:: 2.5
1216 Python now returns float values by default. Applications which do not work
1217 correctly with floating point time stamps can use this function to restore the
1218 old behaviour.
1219
1220 The resolution of the timestamps (that is the smallest possible fraction)
1221 depends on the system. Some systems only support second resolution; on these
1222 systems, the fraction will always be zero.
1223
1224 It is recommended that this setting is only changed at program startup time in
1225 the *__main__* module; libraries should never change this setting. If an
1226 application uses a library that works incorrectly if floating point time stamps
1227 are processed, this application should turn the feature off until the library
1228 has been corrected.
1229
1230
1231.. function:: statvfs(path)
1232
1233 Perform a :cfunc:`statvfs` system call on the given path. The return value is
1234 an object whose attributes describe the filesystem on the given path, and
1235 correspond to the members of the :ctype:`statvfs` structure, namely:
1236 :attr:`f_bsize`, :attr:`f_frsize`, :attr:`f_blocks`, :attr:`f_bfree`,
1237 :attr:`f_bavail`, :attr:`f_files`, :attr:`f_ffree`, :attr:`f_favail`,
1238 :attr:`f_flag`, :attr:`f_namemax`. Availability: Unix.
1239
1240 .. index:: module: statvfs
1241
1242 For backward compatibility, the return value is also accessible as a tuple whose
1243 values correspond to the attributes, in the order given above. The standard
1244 module :mod:`statvfs` defines constants that are useful for extracting
1245 information from a :ctype:`statvfs` structure when accessing it as a sequence;
1246 this remains useful when writing code that needs to work with versions of Python
1247 that don't support accessing the fields as attributes.
1248
1249 .. versionchanged:: 2.2
1250 Added access to values as attributes of the returned object.
1251
1252
Georg Brandl78559542009-10-27 14:03:07 +00001253.. function:: symlink(source, link_name)
Georg Brandl8ec7f652007-08-15 14:28:01 +00001254
Georg Brandl78559542009-10-27 14:03:07 +00001255 Create a symbolic link pointing to *source* named *link_name*. Availability:
1256 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001257
1258
1259.. function:: tempnam([dir[, prefix]])
1260
1261 Return a unique path name that is reasonable for creating a temporary file.
1262 This will be an absolute path that names a potential directory entry in the
1263 directory *dir* or a common location for temporary files if *dir* is omitted or
1264 ``None``. If given and not ``None``, *prefix* is used to provide a short prefix
1265 to the filename. Applications are responsible for properly creating and
1266 managing files created using paths returned by :func:`tempnam`; no automatic
1267 cleanup is provided. On Unix, the environment variable :envvar:`TMPDIR`
Georg Brandlf725b952008-01-05 19:44:22 +00001268 overrides *dir*, while on Windows :envvar:`TMP` is used. The specific
Georg Brandl8ec7f652007-08-15 14:28:01 +00001269 behavior of this function depends on the C library implementation; some aspects
1270 are underspecified in system documentation.
1271
1272 .. warning::
1273
1274 Use of :func:`tempnam` is vulnerable to symlink attacks; consider using
1275 :func:`tmpfile` (section :ref:`os-newstreams`) instead.
1276
Georg Brandl9af94982008-09-13 17:41:16 +00001277 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001278
1279
1280.. function:: tmpnam()
1281
1282 Return a unique path name that is reasonable for creating a temporary file.
1283 This will be an absolute path that names a potential directory entry in a common
1284 location for temporary files. Applications are responsible for properly
1285 creating and managing files created using paths returned by :func:`tmpnam`; no
1286 automatic cleanup is provided.
1287
1288 .. warning::
1289
1290 Use of :func:`tmpnam` is vulnerable to symlink attacks; consider using
1291 :func:`tmpfile` (section :ref:`os-newstreams`) instead.
1292
1293 Availability: Unix, Windows. This function probably shouldn't be used on
1294 Windows, though: Microsoft's implementation of :func:`tmpnam` always creates a
1295 name in the root directory of the current drive, and that's generally a poor
1296 location for a temp file (depending on privileges, you may not even be able to
1297 open a file using this name).
1298
1299
1300.. data:: TMP_MAX
1301
1302 The maximum number of unique names that :func:`tmpnam` will generate before
1303 reusing names.
1304
1305
1306.. function:: unlink(path)
1307
Georg Brandl5be70d42009-10-27 14:50:20 +00001308 Remove (delete) the file *path*. This is the same function as
1309 :func:`remove`; the :func:`unlink` name is its traditional Unix
1310 name. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001311
1312
1313.. function:: utime(path, times)
1314
Benjamin Peterson5b02ef32008-08-16 03:13:07 +00001315 Set the access and modified times of the file specified by *path*. If *times*
1316 is ``None``, then the file's access and modified times are set to the current
1317 time. (The effect is similar to running the Unix program :program:`touch` on
1318 the path.) Otherwise, *times* must be a 2-tuple of numbers, of the form
1319 ``(atime, mtime)`` which is used to set the access and modified times,
1320 respectively. Whether a directory can be given for *path* depends on whether
1321 the operating system implements directories as files (for example, Windows
1322 does not). Note that the exact times you set here may not be returned by a
1323 subsequent :func:`stat` call, depending on the resolution with which your
1324 operating system records access and modification times; see :func:`stat`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001325
1326 .. versionchanged:: 2.0
1327 Added support for ``None`` for *times*.
1328
Georg Brandl9af94982008-09-13 17:41:16 +00001329 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001330
1331
1332.. function:: walk(top[, topdown=True [, onerror=None[, followlinks=False]]])
1333
1334 .. index::
1335 single: directory; walking
1336 single: directory; traversal
1337
Georg Brandlf725b952008-01-05 19:44:22 +00001338 Generate the file names in a directory tree by walking the tree
1339 either top-down or bottom-up. For each directory in the tree rooted at directory
Georg Brandl8ec7f652007-08-15 14:28:01 +00001340 *top* (including *top* itself), it yields a 3-tuple ``(dirpath, dirnames,
1341 filenames)``.
1342
1343 *dirpath* is a string, the path to the directory. *dirnames* is a list of the
1344 names of the subdirectories in *dirpath* (excluding ``'.'`` and ``'..'``).
1345 *filenames* is a list of the names of the non-directory files in *dirpath*.
1346 Note that the names in the lists contain no path components. To get a full path
1347 (which begins with *top*) to a file or directory in *dirpath*, do
1348 ``os.path.join(dirpath, name)``.
1349
Georg Brandlf725b952008-01-05 19:44:22 +00001350 If optional argument *topdown* is ``True`` or not specified, the triple for a
Georg Brandl8ec7f652007-08-15 14:28:01 +00001351 directory is generated before the triples for any of its subdirectories
Georg Brandlf725b952008-01-05 19:44:22 +00001352 (directories are generated top-down). If *topdown* is ``False``, the triple for a
Georg Brandl8ec7f652007-08-15 14:28:01 +00001353 directory is generated after the triples for all of its subdirectories
Georg Brandlf725b952008-01-05 19:44:22 +00001354 (directories are generated bottom-up).
Georg Brandl8ec7f652007-08-15 14:28:01 +00001355
Georg Brandlf725b952008-01-05 19:44:22 +00001356 When *topdown* is ``True``, the caller can modify the *dirnames* list in-place
Georg Brandl8ec7f652007-08-15 14:28:01 +00001357 (perhaps using :keyword:`del` or slice assignment), and :func:`walk` will only
1358 recurse into the subdirectories whose names remain in *dirnames*; this can be
1359 used to prune the search, impose a specific order of visiting, or even to inform
1360 :func:`walk` about directories the caller creates or renames before it resumes
Georg Brandlf725b952008-01-05 19:44:22 +00001361 :func:`walk` again. Modifying *dirnames* when *topdown* is ``False`` is
Georg Brandl8ec7f652007-08-15 14:28:01 +00001362 ineffective, because in bottom-up mode the directories in *dirnames* are
1363 generated before *dirpath* itself is generated.
1364
Georg Brandlf725b952008-01-05 19:44:22 +00001365 By default errors from the :func:`listdir` call are ignored. If optional
Georg Brandl8ec7f652007-08-15 14:28:01 +00001366 argument *onerror* is specified, it should be a function; it will be called with
1367 one argument, an :exc:`OSError` instance. It can report the error to continue
1368 with the walk, or raise the exception to abort the walk. Note that the filename
1369 is available as the ``filename`` attribute of the exception object.
1370
1371 By default, :func:`walk` will not walk down into symbolic links that resolve to
Georg Brandlf725b952008-01-05 19:44:22 +00001372 directories. Set *followlinks* to ``True`` to visit directories pointed to by
Georg Brandl8ec7f652007-08-15 14:28:01 +00001373 symlinks, on systems that support them.
1374
1375 .. versionadded:: 2.6
1376 The *followlinks* parameter.
1377
1378 .. note::
1379
Georg Brandlf725b952008-01-05 19:44:22 +00001380 Be aware that setting *followlinks* to ``True`` can lead to infinite recursion if a
Georg Brandl8ec7f652007-08-15 14:28:01 +00001381 link points to a parent directory of itself. :func:`walk` does not keep track of
1382 the directories it visited already.
1383
1384 .. note::
1385
1386 If you pass a relative pathname, don't change the current working directory
1387 between resumptions of :func:`walk`. :func:`walk` never changes the current
1388 directory, and assumes that its caller doesn't either.
1389
1390 This example displays the number of bytes taken by non-directory files in each
1391 directory under the starting directory, except that it doesn't look under any
1392 CVS subdirectory::
1393
1394 import os
1395 from os.path import join, getsize
1396 for root, dirs, files in os.walk('python/Lib/email'):
1397 print root, "consumes",
1398 print sum(getsize(join(root, name)) for name in files),
1399 print "bytes in", len(files), "non-directory files"
1400 if 'CVS' in dirs:
1401 dirs.remove('CVS') # don't visit CVS directories
1402
Georg Brandlf725b952008-01-05 19:44:22 +00001403 In the next example, walking the tree bottom-up is essential: :func:`rmdir`
Georg Brandl8ec7f652007-08-15 14:28:01 +00001404 doesn't allow deleting a directory before the directory is empty::
1405
Georg Brandlf725b952008-01-05 19:44:22 +00001406 # Delete everything reachable from the directory named in "top",
Georg Brandl8ec7f652007-08-15 14:28:01 +00001407 # assuming there are no symbolic links.
1408 # CAUTION: This is dangerous! For example, if top == '/', it
1409 # could delete all your disk files.
1410 import os
1411 for root, dirs, files in os.walk(top, topdown=False):
1412 for name in files:
1413 os.remove(os.path.join(root, name))
1414 for name in dirs:
1415 os.rmdir(os.path.join(root, name))
1416
1417 .. versionadded:: 2.3
1418
1419
1420.. _os-process:
1421
1422Process Management
1423------------------
1424
1425These functions may be used to create and manage processes.
1426
1427The various :func:`exec\*` functions take a list of arguments for the new
1428program loaded into the process. In each case, the first of these arguments is
1429passed to the new program as its own name rather than as an argument a user may
1430have typed on a command line. For the C programmer, this is the ``argv[0]``
1431passed to a program's :cfunc:`main`. For example, ``os.execv('/bin/echo',
1432['foo', 'bar'])`` will only print ``bar`` on standard output; ``foo`` will seem
1433to be ignored.
1434
1435
1436.. function:: abort()
1437
1438 Generate a :const:`SIGABRT` signal to the current process. On Unix, the default
1439 behavior is to produce a core dump; on Windows, the process immediately returns
1440 an exit code of ``3``. Be aware that programs which use :func:`signal.signal`
1441 to register a handler for :const:`SIGABRT` will behave differently.
Georg Brandl9af94982008-09-13 17:41:16 +00001442 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001443
1444
1445.. function:: execl(path, arg0, arg1, ...)
1446 execle(path, arg0, arg1, ..., env)
1447 execlp(file, arg0, arg1, ...)
1448 execlpe(file, arg0, arg1, ..., env)
1449 execv(path, args)
1450 execve(path, args, env)
1451 execvp(file, args)
1452 execvpe(file, args, env)
1453
1454 These functions all execute a new program, replacing the current process; they
1455 do not return. On Unix, the new executable is loaded into the current process,
Georg Brandlf725b952008-01-05 19:44:22 +00001456 and will have the same process id as the caller. Errors will be reported as
Georg Brandl734373c2009-01-03 21:55:17 +00001457 :exc:`OSError` exceptions.
Andrew M. Kuchlingac771662008-09-28 00:15:27 +00001458
1459 The current process is replaced immediately. Open file objects and
1460 descriptors are not flushed, so if there may be data buffered
1461 on these open files, you should flush them using
1462 :func:`sys.stdout.flush` or :func:`os.fsync` before calling an
1463 :func:`exec\*` function.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001464
Georg Brandlf725b952008-01-05 19:44:22 +00001465 The "l" and "v" variants of the :func:`exec\*` functions differ in how
1466 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl8ec7f652007-08-15 14:28:01 +00001467 to work with if the number of parameters is fixed when the code is written; the
1468 individual parameters simply become additional parameters to the :func:`execl\*`
Georg Brandlf725b952008-01-05 19:44:22 +00001469 functions. The "v" variants are good when the number of parameters is
Georg Brandl8ec7f652007-08-15 14:28:01 +00001470 variable, with the arguments being passed in a list or tuple as the *args*
1471 parameter. In either case, the arguments to the child process should start with
1472 the name of the command being run, but this is not enforced.
1473
Georg Brandlf725b952008-01-05 19:44:22 +00001474 The variants which include a "p" near the end (:func:`execlp`,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001475 :func:`execlpe`, :func:`execvp`, and :func:`execvpe`) will use the
1476 :envvar:`PATH` environment variable to locate the program *file*. When the
1477 environment is being replaced (using one of the :func:`exec\*e` variants,
1478 discussed in the next paragraph), the new environment is used as the source of
1479 the :envvar:`PATH` variable. The other variants, :func:`execl`, :func:`execle`,
1480 :func:`execv`, and :func:`execve`, will not use the :envvar:`PATH` variable to
1481 locate the executable; *path* must contain an appropriate absolute or relative
1482 path.
1483
1484 For :func:`execle`, :func:`execlpe`, :func:`execve`, and :func:`execvpe` (note
Georg Brandlf725b952008-01-05 19:44:22 +00001485 that these all end in "e"), the *env* parameter must be a mapping which is
Georg Brandlfb246c42008-04-19 16:58:28 +00001486 used to define the environment variables for the new process (these are used
1487 instead of the current process' environment); the functions :func:`execl`,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001488 :func:`execlp`, :func:`execv`, and :func:`execvp` all cause the new process to
Georg Brandl734373c2009-01-03 21:55:17 +00001489 inherit the environment of the current process.
Andrew M. Kuchlingac771662008-09-28 00:15:27 +00001490
1491 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001492
1493
1494.. function:: _exit(n)
1495
1496 Exit to the system with status *n*, without calling cleanup handlers, flushing
Georg Brandl9af94982008-09-13 17:41:16 +00001497 stdio buffers, etc. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001498
1499 .. note::
1500
1501 The standard way to exit is ``sys.exit(n)``. :func:`_exit` should normally only
1502 be used in the child process after a :func:`fork`.
1503
Georg Brandlf725b952008-01-05 19:44:22 +00001504The following exit codes are defined and can be used with :func:`_exit`,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001505although they are not required. These are typically used for system programs
1506written in Python, such as a mail server's external command delivery program.
1507
1508.. note::
1509
1510 Some of these may not be available on all Unix platforms, since there is some
1511 variation. These constants are defined where they are defined by the underlying
1512 platform.
1513
1514
1515.. data:: EX_OK
1516
Georg Brandl9af94982008-09-13 17:41:16 +00001517 Exit code that means no error occurred. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001518
1519 .. versionadded:: 2.3
1520
1521
1522.. data:: EX_USAGE
1523
1524 Exit code that means the command was used incorrectly, such as when the wrong
Georg Brandl9af94982008-09-13 17:41:16 +00001525 number of arguments are given. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001526
1527 .. versionadded:: 2.3
1528
1529
1530.. data:: EX_DATAERR
1531
Georg Brandl9af94982008-09-13 17:41:16 +00001532 Exit code that means the input data was incorrect. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001533
1534 .. versionadded:: 2.3
1535
1536
1537.. data:: EX_NOINPUT
1538
1539 Exit code that means an input file did not exist or was not readable.
Georg Brandl9af94982008-09-13 17:41:16 +00001540 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001541
1542 .. versionadded:: 2.3
1543
1544
1545.. data:: EX_NOUSER
1546
Georg Brandl9af94982008-09-13 17:41:16 +00001547 Exit code that means a specified user did not exist. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001548
1549 .. versionadded:: 2.3
1550
1551
1552.. data:: EX_NOHOST
1553
Georg Brandl9af94982008-09-13 17:41:16 +00001554 Exit code that means a specified host did not exist. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001555
1556 .. versionadded:: 2.3
1557
1558
1559.. data:: EX_UNAVAILABLE
1560
1561 Exit code that means that a required service is unavailable. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00001562 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001563
1564 .. versionadded:: 2.3
1565
1566
1567.. data:: EX_SOFTWARE
1568
1569 Exit code that means an internal software error was detected. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00001570 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001571
1572 .. versionadded:: 2.3
1573
1574
1575.. data:: EX_OSERR
1576
1577 Exit code that means an operating system error was detected, such as the
Georg Brandl9af94982008-09-13 17:41:16 +00001578 inability to fork or create a pipe. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001579
1580 .. versionadded:: 2.3
1581
1582
1583.. data:: EX_OSFILE
1584
1585 Exit code that means some system file did not exist, could not be opened, or had
Georg Brandl9af94982008-09-13 17:41:16 +00001586 some other kind of error. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001587
1588 .. versionadded:: 2.3
1589
1590
1591.. data:: EX_CANTCREAT
1592
1593 Exit code that means a user specified output file could not be created.
Georg Brandl9af94982008-09-13 17:41:16 +00001594 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001595
1596 .. versionadded:: 2.3
1597
1598
1599.. data:: EX_IOERR
1600
1601 Exit code that means that an error occurred while doing I/O on some file.
Georg Brandl9af94982008-09-13 17:41:16 +00001602 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001603
1604 .. versionadded:: 2.3
1605
1606
1607.. data:: EX_TEMPFAIL
1608
1609 Exit code that means a temporary failure occurred. This indicates something
1610 that may not really be an error, such as a network connection that couldn't be
Georg Brandl9af94982008-09-13 17:41:16 +00001611 made during a retryable operation. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001612
1613 .. versionadded:: 2.3
1614
1615
1616.. data:: EX_PROTOCOL
1617
1618 Exit code that means that a protocol exchange was illegal, invalid, or not
Georg Brandl9af94982008-09-13 17:41:16 +00001619 understood. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001620
1621 .. versionadded:: 2.3
1622
1623
1624.. data:: EX_NOPERM
1625
1626 Exit code that means that there were insufficient permissions to perform the
Georg Brandl9af94982008-09-13 17:41:16 +00001627 operation (but not intended for file system problems). Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001628
1629 .. versionadded:: 2.3
1630
1631
1632.. data:: EX_CONFIG
1633
1634 Exit code that means that some kind of configuration error occurred.
Georg Brandl9af94982008-09-13 17:41:16 +00001635 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001636
1637 .. versionadded:: 2.3
1638
1639
1640.. data:: EX_NOTFOUND
1641
1642 Exit code that means something like "an entry was not found". Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00001643 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001644
1645 .. versionadded:: 2.3
1646
1647
1648.. function:: fork()
1649
Georg Brandlf725b952008-01-05 19:44:22 +00001650 Fork a child process. Return ``0`` in the child and the child's process id in the
Skip Montanaro75e51682008-03-15 02:32:49 +00001651 parent. If an error occurs :exc:`OSError` is raised.
Gregory P. Smith08067492008-09-30 20:41:13 +00001652
1653 Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have
1654 known issues when using fork() from a thread.
1655
Georg Brandl9af94982008-09-13 17:41:16 +00001656 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001657
1658
1659.. function:: forkpty()
1660
1661 Fork a child process, using a new pseudo-terminal as the child's controlling
1662 terminal. Return a pair of ``(pid, fd)``, where *pid* is ``0`` in the child, the
1663 new child's process id in the parent, and *fd* is the file descriptor of the
1664 master end of the pseudo-terminal. For a more portable approach, use the
Skip Montanaro75e51682008-03-15 02:32:49 +00001665 :mod:`pty` module. If an error occurs :exc:`OSError` is raised.
Georg Brandl9af94982008-09-13 17:41:16 +00001666 Availability: some flavors of Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001667
1668
1669.. function:: kill(pid, sig)
1670
1671 .. index::
1672 single: process; killing
1673 single: process; signalling
1674
1675 Send signal *sig* to the process *pid*. Constants for the specific signals
1676 available on the host platform are defined in the :mod:`signal` module.
Georg Brandl9af94982008-09-13 17:41:16 +00001677 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001678
1679
1680.. function:: killpg(pgid, sig)
1681
1682 .. index::
1683 single: process; killing
1684 single: process; signalling
1685
Georg Brandl9af94982008-09-13 17:41:16 +00001686 Send the signal *sig* to the process group *pgid*. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001687
1688 .. versionadded:: 2.3
1689
1690
1691.. function:: nice(increment)
1692
1693 Add *increment* to the process's "niceness". Return the new niceness.
Georg Brandl9af94982008-09-13 17:41:16 +00001694 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001695
1696
1697.. function:: plock(op)
1698
1699 Lock program segments into memory. The value of *op* (defined in
Georg Brandl9af94982008-09-13 17:41:16 +00001700 ``<sys/lock.h>``) determines which segments are locked. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001701
1702
1703.. function:: popen(...)
1704 popen2(...)
1705 popen3(...)
1706 popen4(...)
1707 :noindex:
1708
1709 Run child processes, returning opened pipes for communications. These functions
1710 are described in section :ref:`os-newstreams`.
1711
1712
1713.. function:: spawnl(mode, path, ...)
1714 spawnle(mode, path, ..., env)
1715 spawnlp(mode, file, ...)
1716 spawnlpe(mode, file, ..., env)
1717 spawnv(mode, path, args)
1718 spawnve(mode, path, args, env)
1719 spawnvp(mode, file, args)
1720 spawnvpe(mode, file, args, env)
1721
1722 Execute the program *path* in a new process.
1723
1724 (Note that the :mod:`subprocess` module provides more powerful facilities for
1725 spawning new processes and retrieving their results; using that module is
R. David Murray9f8a51c2009-06-25 17:40:52 +00001726 preferable to using these functions. Check especially the
1727 :ref:`subprocess-replacements` section.)
Georg Brandl8ec7f652007-08-15 14:28:01 +00001728
Georg Brandlf725b952008-01-05 19:44:22 +00001729 If *mode* is :const:`P_NOWAIT`, this function returns the process id of the new
Georg Brandl8ec7f652007-08-15 14:28:01 +00001730 process; if *mode* is :const:`P_WAIT`, returns the process's exit code if it
1731 exits normally, or ``-signal``, where *signal* is the signal that killed the
Georg Brandlf725b952008-01-05 19:44:22 +00001732 process. On Windows, the process id will actually be the process handle, so can
Georg Brandl8ec7f652007-08-15 14:28:01 +00001733 be used with the :func:`waitpid` function.
1734
Georg Brandlf725b952008-01-05 19:44:22 +00001735 The "l" and "v" variants of the :func:`spawn\*` functions differ in how
1736 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl8ec7f652007-08-15 14:28:01 +00001737 to work with if the number of parameters is fixed when the code is written; the
1738 individual parameters simply become additional parameters to the
Georg Brandlf725b952008-01-05 19:44:22 +00001739 :func:`spawnl\*` functions. The "v" variants are good when the number of
Georg Brandl8ec7f652007-08-15 14:28:01 +00001740 parameters is variable, with the arguments being passed in a list or tuple as
1741 the *args* parameter. In either case, the arguments to the child process must
1742 start with the name of the command being run.
1743
Georg Brandlf725b952008-01-05 19:44:22 +00001744 The variants which include a second "p" near the end (:func:`spawnlp`,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001745 :func:`spawnlpe`, :func:`spawnvp`, and :func:`spawnvpe`) will use the
1746 :envvar:`PATH` environment variable to locate the program *file*. When the
1747 environment is being replaced (using one of the :func:`spawn\*e` variants,
1748 discussed in the next paragraph), the new environment is used as the source of
1749 the :envvar:`PATH` variable. The other variants, :func:`spawnl`,
1750 :func:`spawnle`, :func:`spawnv`, and :func:`spawnve`, will not use the
1751 :envvar:`PATH` variable to locate the executable; *path* must contain an
1752 appropriate absolute or relative path.
1753
1754 For :func:`spawnle`, :func:`spawnlpe`, :func:`spawnve`, and :func:`spawnvpe`
Georg Brandlf725b952008-01-05 19:44:22 +00001755 (note that these all end in "e"), the *env* parameter must be a mapping
Georg Brandlfb246c42008-04-19 16:58:28 +00001756 which is used to define the environment variables for the new process (they are
1757 used instead of the current process' environment); the functions
Georg Brandl8ec7f652007-08-15 14:28:01 +00001758 :func:`spawnl`, :func:`spawnlp`, :func:`spawnv`, and :func:`spawnvp` all cause
Georg Brandl8943caf2009-04-05 21:11:43 +00001759 the new process to inherit the environment of the current process. Note that
1760 keys and values in the *env* dictionary must be strings; invalid keys or
1761 values will cause the function to fail, with a return value of ``127``.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001762
1763 As an example, the following calls to :func:`spawnlp` and :func:`spawnvpe` are
1764 equivalent::
1765
1766 import os
1767 os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1768
1769 L = ['cp', 'index.html', '/dev/null']
1770 os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1771
1772 Availability: Unix, Windows. :func:`spawnlp`, :func:`spawnlpe`, :func:`spawnvp`
1773 and :func:`spawnvpe` are not available on Windows.
1774
1775 .. versionadded:: 1.6
1776
1777
1778.. data:: P_NOWAIT
1779 P_NOWAITO
1780
1781 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1782 functions. If either of these values is given, the :func:`spawn\*` functions
Georg Brandlf725b952008-01-05 19:44:22 +00001783 will return as soon as the new process has been created, with the process id as
Georg Brandl9af94982008-09-13 17:41:16 +00001784 the return value. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001785
1786 .. versionadded:: 1.6
1787
1788
1789.. data:: P_WAIT
1790
1791 Possible value for the *mode* parameter to the :func:`spawn\*` family of
1792 functions. If this is given as *mode*, the :func:`spawn\*` functions will not
1793 return until the new process has run to completion and will return the exit code
1794 of the process the run is successful, or ``-signal`` if a signal kills the
Georg Brandl9af94982008-09-13 17:41:16 +00001795 process. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001796
1797 .. versionadded:: 1.6
1798
1799
1800.. data:: P_DETACH
1801 P_OVERLAY
1802
1803 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1804 functions. These are less portable than those listed above. :const:`P_DETACH`
1805 is similar to :const:`P_NOWAIT`, but the new process is detached from the
1806 console of the calling process. If :const:`P_OVERLAY` is used, the current
1807 process will be replaced; the :func:`spawn\*` function will not return.
1808 Availability: Windows.
1809
1810 .. versionadded:: 1.6
1811
1812
1813.. function:: startfile(path[, operation])
1814
1815 Start a file with its associated application.
1816
1817 When *operation* is not specified or ``'open'``, this acts like double-clicking
1818 the file in Windows Explorer, or giving the file name as an argument to the
1819 :program:`start` command from the interactive command shell: the file is opened
1820 with whatever application (if any) its extension is associated.
1821
1822 When another *operation* is given, it must be a "command verb" that specifies
1823 what should be done with the file. Common verbs documented by Microsoft are
1824 ``'print'`` and ``'edit'`` (to be used on files) as well as ``'explore'`` and
1825 ``'find'`` (to be used on directories).
1826
1827 :func:`startfile` returns as soon as the associated application is launched.
1828 There is no option to wait for the application to close, and no way to retrieve
1829 the application's exit status. The *path* parameter is relative to the current
1830 directory. If you want to use an absolute path, make sure the first character
1831 is not a slash (``'/'``); the underlying Win32 :cfunc:`ShellExecute` function
1832 doesn't work if it is. Use the :func:`os.path.normpath` function to ensure that
1833 the path is properly encoded for Win32. Availability: Windows.
1834
1835 .. versionadded:: 2.0
1836
1837 .. versionadded:: 2.5
1838 The *operation* parameter.
1839
1840
1841.. function:: system(command)
1842
1843 Execute the command (a string) in a subshell. This is implemented by calling
Georg Brandl5d2eb342009-10-27 15:08:27 +00001844 the Standard C function :cfunc:`system`, and has the same limitations.
1845 Changes to :data:`sys.stdin`, etc. are not reflected in the environment of the
1846 executed command.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001847
1848 On Unix, the return value is the exit status of the process encoded in the
1849 format specified for :func:`wait`. Note that POSIX does not specify the meaning
1850 of the return value of the C :cfunc:`system` function, so the return value of
1851 the Python function is system-dependent.
1852
1853 On Windows, the return value is that returned by the system shell after running
1854 *command*, given by the Windows environment variable :envvar:`COMSPEC`: on
1855 :program:`command.com` systems (Windows 95, 98 and ME) this is always ``0``; on
1856 :program:`cmd.exe` systems (Windows NT, 2000 and XP) this is the exit status of
1857 the command run; on systems using a non-native shell, consult your shell
1858 documentation.
1859
Georg Brandl9af94982008-09-13 17:41:16 +00001860 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001861
1862 The :mod:`subprocess` module provides more powerful facilities for spawning new
1863 processes and retrieving their results; using that module is preferable to using
Georg Brandl0ba92b22008-06-22 09:05:29 +00001864 this function. Use the :mod:`subprocess` module. Check especially the
1865 :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001866
1867
1868.. function:: times()
1869
1870 Return a 5-tuple of floating point numbers indicating accumulated (processor or
1871 other) times, in seconds. The items are: user time, system time, children's
1872 user time, children's system time, and elapsed real time since a fixed point in
1873 the past, in that order. See the Unix manual page :manpage:`times(2)` or the
Georg Brandl9af94982008-09-13 17:41:16 +00001874 corresponding Windows Platform API documentation. Availability: Unix,
Georg Brandl0a40ffb2008-02-13 07:20:22 +00001875 Windows. On Windows, only the first two items are filled, the others are zero.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001876
1877
1878.. function:: wait()
1879
1880 Wait for completion of a child process, and return a tuple containing its pid
1881 and exit status indication: a 16-bit number, whose low byte is the signal number
1882 that killed the process, and whose high byte is the exit status (if the signal
1883 number is zero); the high bit of the low byte is set if a core file was
Georg Brandl9af94982008-09-13 17:41:16 +00001884 produced. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001885
1886
1887.. function:: waitpid(pid, options)
1888
1889 The details of this function differ on Unix and Windows.
1890
1891 On Unix: Wait for completion of a child process given by process id *pid*, and
1892 return a tuple containing its process id and exit status indication (encoded as
1893 for :func:`wait`). The semantics of the call are affected by the value of the
1894 integer *options*, which should be ``0`` for normal operation.
1895
1896 If *pid* is greater than ``0``, :func:`waitpid` requests status information for
1897 that specific process. If *pid* is ``0``, the request is for the status of any
1898 child in the process group of the current process. If *pid* is ``-1``, the
1899 request pertains to any child of the current process. If *pid* is less than
1900 ``-1``, status is requested for any process in the process group ``-pid`` (the
1901 absolute value of *pid*).
1902
Gregory P. Smith59de7f52008-08-15 23:14:00 +00001903 An :exc:`OSError` is raised with the value of errno when the syscall
1904 returns -1.
1905
Georg Brandl8ec7f652007-08-15 14:28:01 +00001906 On Windows: Wait for completion of a process given by process handle *pid*, and
1907 return a tuple containing *pid*, and its exit status shifted left by 8 bits
1908 (shifting makes cross-platform use of the function easier). A *pid* less than or
1909 equal to ``0`` has no special meaning on Windows, and raises an exception. The
1910 value of integer *options* has no effect. *pid* can refer to any process whose
1911 id is known, not necessarily a child process. The :func:`spawn` functions called
1912 with :const:`P_NOWAIT` return suitable process handles.
1913
1914
1915.. function:: wait3([options])
1916
1917 Similar to :func:`waitpid`, except no process id argument is given and a
1918 3-element tuple containing the child's process id, exit status indication, and
1919 resource usage information is returned. Refer to :mod:`resource`.\
1920 :func:`getrusage` for details on resource usage information. The option
1921 argument is the same as that provided to :func:`waitpid` and :func:`wait4`.
1922 Availability: Unix.
1923
1924 .. versionadded:: 2.5
1925
1926
1927.. function:: wait4(pid, options)
1928
1929 Similar to :func:`waitpid`, except a 3-element tuple, containing the child's
1930 process id, exit status indication, and resource usage information is returned.
1931 Refer to :mod:`resource`.\ :func:`getrusage` for details on resource usage
1932 information. The arguments to :func:`wait4` are the same as those provided to
1933 :func:`waitpid`. Availability: Unix.
1934
1935 .. versionadded:: 2.5
1936
1937
1938.. data:: WNOHANG
1939
1940 The option for :func:`waitpid` to return immediately if no child process status
1941 is available immediately. The function returns ``(0, 0)`` in this case.
Georg Brandl9af94982008-09-13 17:41:16 +00001942 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001943
1944
1945.. data:: WCONTINUED
1946
1947 This option causes child processes to be reported if they have been continued
1948 from a job control stop since their status was last reported. Availability: Some
1949 Unix systems.
1950
1951 .. versionadded:: 2.3
1952
1953
1954.. data:: WUNTRACED
1955
1956 This option causes child processes to be reported if they have been stopped but
1957 their current state has not been reported since they were stopped. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00001958 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001959
1960 .. versionadded:: 2.3
1961
1962The following functions take a process status code as returned by
1963:func:`system`, :func:`wait`, or :func:`waitpid` as a parameter. They may be
1964used to determine the disposition of a process.
1965
1966
1967.. function:: WCOREDUMP(status)
1968
Georg Brandlf725b952008-01-05 19:44:22 +00001969 Return ``True`` if a core dump was generated for the process, otherwise
Georg Brandl9af94982008-09-13 17:41:16 +00001970 return ``False``. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001971
1972 .. versionadded:: 2.3
1973
1974
1975.. function:: WIFCONTINUED(status)
1976
Georg Brandlf725b952008-01-05 19:44:22 +00001977 Return ``True`` if the process has been continued from a job control stop,
1978 otherwise return ``False``. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001979
1980 .. versionadded:: 2.3
1981
1982
1983.. function:: WIFSTOPPED(status)
1984
Georg Brandlf725b952008-01-05 19:44:22 +00001985 Return ``True`` if the process has been stopped, otherwise return
Georg Brandl8ec7f652007-08-15 14:28:01 +00001986 ``False``. Availability: Unix.
1987
1988
1989.. function:: WIFSIGNALED(status)
1990
Georg Brandlf725b952008-01-05 19:44:22 +00001991 Return ``True`` if the process exited due to a signal, otherwise return
Georg Brandl9af94982008-09-13 17:41:16 +00001992 ``False``. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001993
1994
1995.. function:: WIFEXITED(status)
1996
Georg Brandlf725b952008-01-05 19:44:22 +00001997 Return ``True`` if the process exited using the :manpage:`exit(2)` system call,
Georg Brandl9af94982008-09-13 17:41:16 +00001998 otherwise return ``False``. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001999
2000
2001.. function:: WEXITSTATUS(status)
2002
2003 If ``WIFEXITED(status)`` is true, return the integer parameter to the
2004 :manpage:`exit(2)` system call. Otherwise, the return value is meaningless.
Georg Brandl9af94982008-09-13 17:41:16 +00002005 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002006
2007
2008.. function:: WSTOPSIG(status)
2009
Georg Brandl9af94982008-09-13 17:41:16 +00002010 Return the signal which caused the process to stop. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002011
2012
2013.. function:: WTERMSIG(status)
2014
Georg Brandl9af94982008-09-13 17:41:16 +00002015 Return the signal which caused the process to exit. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002016
2017
2018.. _os-path:
2019
2020Miscellaneous System Information
2021--------------------------------
2022
2023
2024.. function:: confstr(name)
2025
2026 Return string-valued system configuration values. *name* specifies the
2027 configuration value to retrieve; it may be a string which is the name of a
2028 defined system value; these names are specified in a number of standards (POSIX,
2029 Unix 95, Unix 98, and others). Some platforms define additional names as well.
2030 The names known to the host operating system are given as the keys of the
2031 ``confstr_names`` dictionary. For configuration variables not included in that
2032 mapping, passing an integer for *name* is also accepted. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00002033 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002034
2035 If the configuration value specified by *name* isn't defined, ``None`` is
2036 returned.
2037
2038 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
2039 specific value for *name* is not supported by the host system, even if it is
2040 included in ``confstr_names``, an :exc:`OSError` is raised with
2041 :const:`errno.EINVAL` for the error number.
2042
2043
2044.. data:: confstr_names
2045
2046 Dictionary mapping names accepted by :func:`confstr` to the integer values
2047 defined for those names by the host operating system. This can be used to
Georg Brandl9af94982008-09-13 17:41:16 +00002048 determine the set of names known to the system. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002049
2050
2051.. function:: getloadavg()
2052
Georg Brandl57fe0f22008-01-12 10:53:29 +00002053 Return the number of processes in the system run queue averaged over the last
2054 1, 5, and 15 minutes or raises :exc:`OSError` if the load average was
Georg Brandl6bb7bcf2008-05-30 19:12:13 +00002055 unobtainable. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002056
2057 .. versionadded:: 2.3
2058
2059
2060.. function:: sysconf(name)
2061
2062 Return integer-valued system configuration values. If the configuration value
2063 specified by *name* isn't defined, ``-1`` is returned. The comments regarding
2064 the *name* parameter for :func:`confstr` apply here as well; the dictionary that
2065 provides information on the known names is given by ``sysconf_names``.
Georg Brandl9af94982008-09-13 17:41:16 +00002066 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002067
2068
2069.. data:: sysconf_names
2070
2071 Dictionary mapping names accepted by :func:`sysconf` to the integer values
2072 defined for those names by the host operating system. This can be used to
Georg Brandl9af94982008-09-13 17:41:16 +00002073 determine the set of names known to the system. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002074
Georg Brandlf725b952008-01-05 19:44:22 +00002075The following data values are used to support path manipulation operations. These
Georg Brandl8ec7f652007-08-15 14:28:01 +00002076are defined for all platforms.
2077
2078Higher-level operations on pathnames are defined in the :mod:`os.path` module.
2079
2080
2081.. data:: curdir
2082
2083 The constant string used by the operating system to refer to the current
Georg Brandl9af94982008-09-13 17:41:16 +00002084 directory. This is ``'.'`` for Windows and POSIX. Also available via
2085 :mod:`os.path`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002086
2087
2088.. data:: pardir
2089
2090 The constant string used by the operating system to refer to the parent
Georg Brandl9af94982008-09-13 17:41:16 +00002091 directory. This is ``'..'`` for Windows and POSIX. Also available via
2092 :mod:`os.path`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002093
2094
2095.. data:: sep
2096
Georg Brandl9af94982008-09-13 17:41:16 +00002097 The character used by the operating system to separate pathname components.
2098 This is ``'/'`` for POSIX and ``'\\'`` for Windows. Note that knowing this
2099 is not sufficient to be able to parse or concatenate pathnames --- use
Georg Brandl8ec7f652007-08-15 14:28:01 +00002100 :func:`os.path.split` and :func:`os.path.join` --- but it is occasionally
2101 useful. Also available via :mod:`os.path`.
2102
2103
2104.. data:: altsep
2105
2106 An alternative character used by the operating system to separate pathname
2107 components, or ``None`` if only one separator character exists. This is set to
2108 ``'/'`` on Windows systems where ``sep`` is a backslash. Also available via
2109 :mod:`os.path`.
2110
2111
2112.. data:: extsep
2113
2114 The character which separates the base filename from the extension; for example,
2115 the ``'.'`` in :file:`os.py`. Also available via :mod:`os.path`.
2116
2117 .. versionadded:: 2.2
2118
2119
2120.. data:: pathsep
2121
2122 The character conventionally used by the operating system to separate search
2123 path components (as in :envvar:`PATH`), such as ``':'`` for POSIX or ``';'`` for
2124 Windows. Also available via :mod:`os.path`.
2125
2126
2127.. data:: defpath
2128
2129 The default search path used by :func:`exec\*p\*` and :func:`spawn\*p\*` if the
2130 environment doesn't have a ``'PATH'`` key. Also available via :mod:`os.path`.
2131
2132
2133.. data:: linesep
2134
2135 The string used to separate (or, rather, terminate) lines on the current
Georg Brandl9af94982008-09-13 17:41:16 +00002136 platform. This may be a single character, such as ``'\n'`` for POSIX, or
2137 multiple characters, for example, ``'\r\n'`` for Windows. Do not use
2138 *os.linesep* as a line terminator when writing files opened in text mode (the
2139 default); use a single ``'\n'`` instead, on all platforms.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002140
2141
2142.. data:: devnull
2143
Georg Brandl9af94982008-09-13 17:41:16 +00002144 The file path of the null device. For example: ``'/dev/null'`` for POSIX.
2145 Also available via :mod:`os.path`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002146
2147 .. versionadded:: 2.4
2148
2149
2150.. _os-miscfunc:
2151
2152Miscellaneous Functions
2153-----------------------
2154
2155
2156.. function:: urandom(n)
2157
2158 Return a string of *n* random bytes suitable for cryptographic use.
2159
2160 This function returns random bytes from an OS-specific randomness source. The
2161 returned data should be unpredictable enough for cryptographic applications,
2162 though its exact quality depends on the OS implementation. On a UNIX-like
2163 system this will query /dev/urandom, and on Windows it will use CryptGenRandom.
2164 If a randomness source is not found, :exc:`NotImplementedError` will be raised.
2165
2166 .. versionadded:: 2.4
2167