blob: 061f1ce408f824c47767a4467bf9151dba12167e [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`os` --- Miscellaneous operating system interfaces
2=======================================================
3
4.. module:: os
5 :synopsis: Miscellaneous operating system interfaces.
6
7
Georg Brandl57fe0f22008-01-12 10:53:29 +00008This module provides a portable way of using operating system dependent
9functionality. If you just want to read or write a file see :func:`open`, if
10you want to manipulate paths, see the :mod:`os.path` module, and if you want to
11read all the lines in all the files on the command line see the :mod:`fileinput`
12module. For creating temporary files and directories see the :mod:`tempfile`
13module, and for high-level file and directory handling see the :mod:`shutil`
14module.
Georg Brandl8ec7f652007-08-15 14:28:01 +000015
Georg Brandl57fe0f22008-01-12 10:53:29 +000016The design of all built-in operating system dependent modules of Python is such
17that as long as the same functionality is available, it uses the same interface;
18for example, the function ``os.stat(path)`` returns stat information about
19*path* in the same format (which happens to have originated with the POSIX
Georg Brandl8ec7f652007-08-15 14:28:01 +000020interface).
21
22Extensions peculiar to a particular operating system are also available through
23the :mod:`os` module, but using them is of course a threat to portability!
24
Georg Brandl57fe0f22008-01-12 10:53:29 +000025.. note::
Georg Brandl8ec7f652007-08-15 14:28:01 +000026
Georg Brandl9af94982008-09-13 17:41:16 +000027 If not separately noted, all functions that claim "Availability: Unix" are
28 supported on Mac OS X, which builds on a Unix core.
29
30.. note::
31
Georg Brandl57fe0f22008-01-12 10:53:29 +000032 All functions in this module raise :exc:`OSError` in the case of invalid or
33 inaccessible file names and paths, or other arguments that have the correct
34 type, but are not accepted by the operating system.
Georg Brandl8ec7f652007-08-15 14:28:01 +000035
Georg Brandl8ec7f652007-08-15 14:28:01 +000036
37.. exception:: error
38
Georg Brandl57fe0f22008-01-12 10:53:29 +000039 An alias for the built-in :exc:`OSError` exception.
Georg Brandl8ec7f652007-08-15 14:28:01 +000040
41
42.. data:: name
43
44 The name of the operating system dependent module imported. The following names
45 have currently been registered: ``'posix'``, ``'nt'``, ``'mac'``, ``'os2'``,
46 ``'ce'``, ``'java'``, ``'riscos'``.
47
48
Georg Brandl8ec7f652007-08-15 14:28:01 +000049.. _os-procinfo:
50
51Process Parameters
52------------------
53
54These functions and data items provide information and operate on the current
55process and user.
56
57
58.. data:: environ
59
60 A mapping object representing the string environment. For example,
61 ``environ['HOME']`` is the pathname of your home directory (on some platforms),
62 and is equivalent to ``getenv("HOME")`` in C.
63
64 This mapping is captured the first time the :mod:`os` module is imported,
65 typically during Python startup as part of processing :file:`site.py`. Changes
66 to the environment made after this time are not reflected in ``os.environ``,
67 except for changes made by modifying ``os.environ`` directly.
68
69 If the platform supports the :func:`putenv` function, this mapping may be used
70 to modify the environment as well as query the environment. :func:`putenv` will
71 be called automatically when the mapping is modified.
72
73 .. note::
74
75 Calling :func:`putenv` directly does not change ``os.environ``, so it's better
76 to modify ``os.environ``.
77
78 .. note::
79
Georg Brandl9af94982008-09-13 17:41:16 +000080 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
81 cause memory leaks. Refer to the system documentation for
82 :cfunc:`putenv`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000083
84 If :func:`putenv` is not provided, a modified copy of this mapping may be
85 passed to the appropriate process-creation functions to cause child processes
86 to use a modified environment.
87
Georg Brandl4a212682007-09-20 17:57:59 +000088 If the platform supports the :func:`unsetenv` function, you can delete items in
Georg Brandl8ec7f652007-08-15 14:28:01 +000089 this mapping to unset environment variables. :func:`unsetenv` will be called
Georg Brandl4a212682007-09-20 17:57:59 +000090 automatically when an item is deleted from ``os.environ``, and when
Georg Brandl1a94ec22007-10-24 21:40:38 +000091 one of the :meth:`pop` or :meth:`clear` methods is called.
Georg Brandl4a212682007-09-20 17:57:59 +000092
93 .. versionchanged:: 2.6
Georg Brandl1a94ec22007-10-24 21:40:38 +000094 Also unset environment variables when calling :meth:`os.environ.clear`
95 and :meth:`os.environ.pop`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000096
97
98.. function:: chdir(path)
99 fchdir(fd)
100 getcwd()
101 :noindex:
102
103 These functions are described in :ref:`os-file-dir`.
104
105
106.. function:: ctermid()
107
108 Return the filename corresponding to the controlling terminal of the process.
109 Availability: Unix.
110
111
112.. function:: getegid()
113
114 Return the effective group id of the current process. This corresponds to the
Georg Brandlf725b952008-01-05 19:44:22 +0000115 "set id" bit on the file being executed in the current process. Availability:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000116 Unix.
117
118
119.. function:: geteuid()
120
121 .. index:: single: user; effective id
122
Georg Brandlf725b952008-01-05 19:44:22 +0000123 Return the current process's effective user id. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000124
125
126.. function:: getgid()
127
128 .. index:: single: process; group
129
130 Return the real group id of the current process. Availability: Unix.
131
132
133.. function:: getgroups()
134
135 Return list of supplemental group ids associated with the current process.
136 Availability: Unix.
137
138
139.. function:: getlogin()
140
141 Return the name of the user logged in on the controlling terminal of the
142 process. For most purposes, it is more useful to use the environment variable
143 :envvar:`LOGNAME` to find out who the user is, or
144 ``pwd.getpwuid(os.getuid())[0]`` to get the login name of the currently
Georg Brandlf725b952008-01-05 19:44:22 +0000145 effective user id. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000146
147
148.. function:: getpgid(pid)
149
150 Return the process group id of the process with process id *pid*. If *pid* is 0,
151 the process group id of the current process is returned. Availability: Unix.
152
153 .. versionadded:: 2.3
154
155
156.. function:: getpgrp()
157
158 .. index:: single: process; group
159
160 Return the id of the current process group. Availability: Unix.
161
162
163.. function:: getpid()
164
165 .. index:: single: process; id
166
167 Return the current process id. Availability: Unix, Windows.
168
169
170.. function:: getppid()
171
172 .. index:: single: process; id of parent
173
174 Return the parent's process id. Availability: Unix.
175
176
177.. function:: getuid()
178
179 .. index:: single: user; id
180
Georg Brandlf725b952008-01-05 19:44:22 +0000181 Return the current process's user id. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000182
183
184.. function:: getenv(varname[, value])
185
186 Return the value of the environment variable *varname* if it exists, or *value*
187 if it doesn't. *value* defaults to ``None``. Availability: most flavors of
188 Unix, Windows.
189
190
191.. function:: putenv(varname, value)
192
193 .. index:: single: environment variables; setting
194
195 Set the environment variable named *varname* to the string *value*. Such
196 changes to the environment affect subprocesses started with :func:`os.system`,
197 :func:`popen` or :func:`fork` and :func:`execv`. Availability: most flavors of
198 Unix, Windows.
199
200 .. note::
201
Georg Brandl9af94982008-09-13 17:41:16 +0000202 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
203 cause memory leaks. Refer to the system documentation for putenv.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000204
205 When :func:`putenv` is supported, assignments to items in ``os.environ`` are
206 automatically translated into corresponding calls to :func:`putenv`; however,
207 calls to :func:`putenv` don't update ``os.environ``, so it is actually
208 preferable to assign to items of ``os.environ``.
209
210
211.. function:: setegid(egid)
212
213 Set the current process's effective group id. Availability: Unix.
214
215
216.. function:: seteuid(euid)
217
218 Set the current process's effective user id. Availability: Unix.
219
220
221.. function:: setgid(gid)
222
223 Set the current process' group id. Availability: Unix.
224
225
226.. function:: setgroups(groups)
227
228 Set the list of supplemental group ids associated with the current process to
229 *groups*. *groups* must be a sequence, and each element must be an integer
Georg Brandlf725b952008-01-05 19:44:22 +0000230 identifying a group. This operation is typically available only to the superuser.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000231 Availability: Unix.
232
233 .. versionadded:: 2.2
234
235
236.. function:: setpgrp()
237
Georg Brandlf725b952008-01-05 19:44:22 +0000238 Call the system call :cfunc:`setpgrp` or :cfunc:`setpgrp(0, 0)` depending on
Georg Brandl8ec7f652007-08-15 14:28:01 +0000239 which version is implemented (if any). See the Unix manual for the semantics.
240 Availability: Unix.
241
242
243.. function:: setpgid(pid, pgrp)
244
Georg Brandlf725b952008-01-05 19:44:22 +0000245 Call the system call :cfunc:`setpgid` to set the process group id of the
Georg Brandl8ec7f652007-08-15 14:28:01 +0000246 process with id *pid* to the process group with id *pgrp*. See the Unix manual
247 for the semantics. Availability: Unix.
248
249
250.. function:: setreuid(ruid, euid)
251
252 Set the current process's real and effective user ids. Availability: Unix.
253
254
255.. function:: setregid(rgid, egid)
256
257 Set the current process's real and effective group ids. Availability: Unix.
258
259
260.. function:: getsid(pid)
261
Georg Brandlf725b952008-01-05 19:44:22 +0000262 Call the system call :cfunc:`getsid`. See the Unix manual for the semantics.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000263 Availability: Unix.
264
265 .. versionadded:: 2.4
266
267
268.. function:: setsid()
269
Georg Brandlf725b952008-01-05 19:44:22 +0000270 Call the system call :cfunc:`setsid`. See the Unix manual for the semantics.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000271 Availability: Unix.
272
273
274.. function:: setuid(uid)
275
276 .. index:: single: user; id, setting
277
Georg Brandlf725b952008-01-05 19:44:22 +0000278 Set the current process's user id. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000279
Georg Brandl8ec7f652007-08-15 14:28:01 +0000280
Georg Brandlb19be572007-12-29 10:57:00 +0000281.. placed in this section since it relates to errno.... a little weak
Georg Brandl8ec7f652007-08-15 14:28:01 +0000282.. function:: strerror(code)
283
284 Return the error message corresponding to the error code in *code*.
Georg Brandl3fc974f2008-05-11 21:16:37 +0000285 On platforms where :cfunc:`strerror` returns ``NULL`` when given an unknown
286 error number, :exc:`ValueError` is raised. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000287
288
289.. function:: umask(mask)
290
Georg Brandlf725b952008-01-05 19:44:22 +0000291 Set the current numeric umask and return the previous umask. Availability:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000292 Unix, Windows.
293
294
295.. function:: uname()
296
297 .. index::
298 single: gethostname() (in module socket)
299 single: gethostbyaddr() (in module socket)
300
301 Return a 5-tuple containing information identifying the current operating
302 system. The tuple contains 5 strings: ``(sysname, nodename, release, version,
303 machine)``. Some systems truncate the nodename to 8 characters or to the
304 leading component; a better way to get the hostname is
305 :func:`socket.gethostname` or even
306 ``socket.gethostbyaddr(socket.gethostname())``. Availability: recent flavors of
307 Unix.
308
309
310.. function:: unsetenv(varname)
311
312 .. index:: single: environment variables; deleting
313
314 Unset (delete) the environment variable named *varname*. Such changes to the
315 environment affect subprocesses started with :func:`os.system`, :func:`popen` or
316 :func:`fork` and :func:`execv`. Availability: most flavors of Unix, Windows.
317
318 When :func:`unsetenv` is supported, deletion of items in ``os.environ`` is
319 automatically translated into a corresponding call to :func:`unsetenv`; however,
320 calls to :func:`unsetenv` don't update ``os.environ``, so it is actually
321 preferable to delete items of ``os.environ``.
322
323
324.. _os-newstreams:
325
326File Object Creation
327--------------------
328
329These functions create new file objects. (See also :func:`open`.)
330
331
332.. function:: fdopen(fd[, mode[, bufsize]])
333
334 .. index:: single: I/O control; buffering
335
336 Return an open file object connected to the file descriptor *fd*. The *mode*
337 and *bufsize* arguments have the same meaning as the corresponding arguments to
Georg Brandl9af94982008-09-13 17:41:16 +0000338 the built-in :func:`open` function. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000339
340 .. versionchanged:: 2.3
341 When specified, the *mode* argument must now start with one of the letters
342 ``'r'``, ``'w'``, or ``'a'``, otherwise a :exc:`ValueError` is raised.
343
344 .. versionchanged:: 2.5
345 On Unix, when the *mode* argument starts with ``'a'``, the *O_APPEND* flag is
346 set on the file descriptor (which the :cfunc:`fdopen` implementation already
347 does on most platforms).
348
349
350.. function:: popen(command[, mode[, bufsize]])
351
352 Open a pipe to or from *command*. The return value is an open file object
353 connected to the pipe, which can be read or written depending on whether *mode*
354 is ``'r'`` (default) or ``'w'``. The *bufsize* argument has the same meaning as
355 the corresponding argument to the built-in :func:`open` function. The exit
356 status of the command (encoded in the format specified for :func:`wait`) is
357 available as the return value of the :meth:`close` method of the file object,
358 except that when the exit status is zero (termination without errors), ``None``
Georg Brandl9af94982008-09-13 17:41:16 +0000359 is returned. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000360
361 .. deprecated:: 2.6
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000362 This function is obsolete. Use the :mod:`subprocess` module. Check
Georg Brandl0ba92b22008-06-22 09:05:29 +0000363 especially the :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000364
365 .. versionchanged:: 2.0
366 This function worked unreliably under Windows in earlier versions of Python.
367 This was due to the use of the :cfunc:`_popen` function from the libraries
368 provided with Windows. Newer versions of Python do not use the broken
369 implementation from the Windows libraries.
370
371
372.. function:: tmpfile()
373
374 Return a new file object opened in update mode (``w+b``). The file has no
375 directory entries associated with it and will be automatically deleted once
Georg Brandl9af94982008-09-13 17:41:16 +0000376 there are no file descriptors for the file. Availability: Unix,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000377 Windows.
378
379There are a number of different :func:`popen\*` functions that provide slightly
380different ways to create subprocesses.
381
382.. deprecated:: 2.6
383 All of the :func:`popen\*` functions are obsolete. Use the :mod:`subprocess`
384 module.
385
386For each of the :func:`popen\*` variants, if *bufsize* is specified, it
387specifies the buffer size for the I/O pipes. *mode*, if provided, should be the
388string ``'b'`` or ``'t'``; on Windows this is needed to determine whether the
389file objects should be opened in binary or text mode. The default value for
390*mode* is ``'t'``.
391
392Also, for each of these variants, on Unix, *cmd* may be a sequence, in which
393case arguments will be passed directly to the program without shell intervention
394(as with :func:`os.spawnv`). If *cmd* is a string it will be passed to the shell
395(as with :func:`os.system`).
396
397These methods do not make it possible to retrieve the exit status from the child
398processes. The only way to control the input and output streams and also
399retrieve the return codes is to use the :mod:`subprocess` module; these are only
400available on Unix.
401
402For a discussion of possible deadlock conditions related to the use of these
403functions, see :ref:`popen2-flow-control`.
404
405
406.. function:: popen2(cmd[, mode[, bufsize]])
407
Georg Brandlf725b952008-01-05 19:44:22 +0000408 Execute *cmd* as a sub-process and return the file objects ``(child_stdin,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000409 child_stdout)``.
410
411 .. deprecated:: 2.6
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000412 This function is obsolete. Use the :mod:`subprocess` module. Check
Georg Brandl0ba92b22008-06-22 09:05:29 +0000413 especially the :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000414
Georg Brandl9af94982008-09-13 17:41:16 +0000415 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000416
417 .. versionadded:: 2.0
418
419
420.. function:: popen3(cmd[, mode[, bufsize]])
421
Georg Brandlf725b952008-01-05 19:44:22 +0000422 Execute *cmd* as a sub-process and return the file objects ``(child_stdin,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000423 child_stdout, child_stderr)``.
424
425 .. deprecated:: 2.6
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000426 This function is obsolete. Use the :mod:`subprocess` module. Check
Georg Brandl0ba92b22008-06-22 09:05:29 +0000427 especially the :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000428
Georg Brandl9af94982008-09-13 17:41:16 +0000429 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000430
431 .. versionadded:: 2.0
432
433
434.. function:: popen4(cmd[, mode[, bufsize]])
435
Georg Brandlf725b952008-01-05 19:44:22 +0000436 Execute *cmd* as a sub-process and return the file objects ``(child_stdin,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000437 child_stdout_and_stderr)``.
438
439 .. deprecated:: 2.6
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000440 This function is obsolete. Use the :mod:`subprocess` module. Check
Georg Brandl0ba92b22008-06-22 09:05:29 +0000441 especially the :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000442
Georg Brandl9af94982008-09-13 17:41:16 +0000443 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000444
445 .. versionadded:: 2.0
446
447(Note that ``child_stdin, child_stdout, and child_stderr`` are named from the
448point of view of the child process, so *child_stdin* is the child's standard
449input.)
450
451This functionality is also available in the :mod:`popen2` module using functions
452of the same names, but the return values of those functions have a different
453order.
454
455
456.. _os-fd-ops:
457
458File Descriptor Operations
459--------------------------
460
461These functions operate on I/O streams referenced using file descriptors.
462
463File descriptors are small integers corresponding to a file that has been opened
464by the current process. For example, standard input is usually file descriptor
4650, standard output is 1, and standard error is 2. Further files opened by a
466process will then be assigned 3, 4, 5, and so forth. The name "file descriptor"
467is slightly deceptive; on Unix platforms, sockets and pipes are also referenced
468by file descriptors.
469
470
471.. function:: close(fd)
472
Georg Brandl9af94982008-09-13 17:41:16 +0000473 Close file descriptor *fd*. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000474
475 .. note::
476
477 This function is intended for low-level I/O and must be applied to a file
478 descriptor as returned by :func:`open` or :func:`pipe`. To close a "file
479 object" returned by the built-in function :func:`open` or by :func:`popen` or
480 :func:`fdopen`, use its :meth:`close` method.
481
482
Georg Brandl309501a2008-01-19 20:22:13 +0000483.. function:: closerange(fd_low, fd_high)
484
485 Close all file descriptors from *fd_low* (inclusive) to *fd_high* (exclusive),
Georg Brandl9af94982008-09-13 17:41:16 +0000486 ignoring errors. Availability: Unix, Windows. Equivalent to::
Georg Brandl309501a2008-01-19 20:22:13 +0000487
488 for fd in xrange(fd_low, fd_high):
489 try:
490 os.close(fd)
491 except OSError:
492 pass
493
494 .. versionadded:: 2.6
495
496
Georg Brandl8ec7f652007-08-15 14:28:01 +0000497.. function:: dup(fd)
498
Georg Brandl9af94982008-09-13 17:41:16 +0000499 Return a duplicate of file descriptor *fd*. Availability: Unix,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000500 Windows.
501
502
503.. function:: dup2(fd, fd2)
504
505 Duplicate file descriptor *fd* to *fd2*, closing the latter first if necessary.
Georg Brandl9af94982008-09-13 17:41:16 +0000506 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000507
508
Christian Heimes36281872007-11-30 21:11:28 +0000509.. function:: fchmod(fd, mode)
510
511 Change the mode of the file given by *fd* to the numeric *mode*. See the docs
512 for :func:`chmod` for possible values of *mode*. Availability: Unix.
513
Georg Brandl81ddc1a2007-11-30 22:04:45 +0000514 .. versionadded:: 2.6
515
Christian Heimes36281872007-11-30 21:11:28 +0000516
517.. function:: fchown(fd, uid, gid)
518
519 Change the owner and group id of the file given by *fd* to the numeric *uid*
520 and *gid*. To leave one of the ids unchanged, set it to -1.
521 Availability: Unix.
522
Georg Brandl81ddc1a2007-11-30 22:04:45 +0000523 .. versionadded:: 2.6
524
Christian Heimes36281872007-11-30 21:11:28 +0000525
Georg Brandl8ec7f652007-08-15 14:28:01 +0000526.. function:: fdatasync(fd)
527
528 Force write of file with filedescriptor *fd* to disk. Does not force update of
529 metadata. Availability: Unix.
530
531
532.. function:: fpathconf(fd, name)
533
534 Return system configuration information relevant to an open file. *name*
535 specifies the configuration value to retrieve; it may be a string which is the
536 name of a defined system value; these names are specified in a number of
537 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
538 additional names as well. The names known to the host operating system are
539 given in the ``pathconf_names`` dictionary. For configuration variables not
540 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl9af94982008-09-13 17:41:16 +0000541 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000542
543 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
544 specific value for *name* is not supported by the host system, even if it is
545 included in ``pathconf_names``, an :exc:`OSError` is raised with
546 :const:`errno.EINVAL` for the error number.
547
548
549.. function:: fstat(fd)
550
551 Return status for file descriptor *fd*, like :func:`stat`. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +0000552 Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000553
554
555.. function:: fstatvfs(fd)
556
557 Return information about the filesystem containing the file associated with file
558 descriptor *fd*, like :func:`statvfs`. Availability: Unix.
559
560
561.. function:: fsync(fd)
562
563 Force write of file with filedescriptor *fd* to disk. On Unix, this calls the
564 native :cfunc:`fsync` function; on Windows, the MS :cfunc:`_commit` function.
565
566 If you're starting with a Python file object *f*, first do ``f.flush()``, and
567 then do ``os.fsync(f.fileno())``, to ensure that all internal buffers associated
Georg Brandl9af94982008-09-13 17:41:16 +0000568 with *f* are written to disk. Availability: Unix, and Windows
Georg Brandl8ec7f652007-08-15 14:28:01 +0000569 starting in 2.2.3.
570
571
572.. function:: ftruncate(fd, length)
573
574 Truncate the file corresponding to file descriptor *fd*, so that it is at most
Georg Brandl9af94982008-09-13 17:41:16 +0000575 *length* bytes in size. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000576
577
578.. function:: isatty(fd)
579
580 Return ``True`` if the file descriptor *fd* is open and connected to a
Georg Brandl9af94982008-09-13 17:41:16 +0000581 tty(-like) device, else ``False``. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000582
583
584.. function:: lseek(fd, pos, how)
585
Georg Brandlf725b952008-01-05 19:44:22 +0000586 Set the current position of file descriptor *fd* to position *pos*, modified
587 by *how*: :const:`SEEK_SET` or ``0`` to set the position relative to the
588 beginning of the file; :const:`SEEK_CUR` or ``1`` to set it relative to the
589 current position; :const:`os.SEEK_END` or ``2`` to set it relative to the end of
Georg Brandl9af94982008-09-13 17:41:16 +0000590 the file. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000591
592
593.. function:: open(file, flags[, mode])
594
595 Open the file *file* and set various flags according to *flags* and possibly its
596 mode according to *mode*. The default *mode* is ``0777`` (octal), and the
597 current umask value is first masked out. Return the file descriptor for the
Georg Brandl9af94982008-09-13 17:41:16 +0000598 newly opened file. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000599
600 For a description of the flag and mode values, see the C run-time documentation;
601 flag constants (like :const:`O_RDONLY` and :const:`O_WRONLY`) are defined in
602 this module too (see below).
603
604 .. note::
605
606 This function is intended for low-level I/O. For normal usage, use the built-in
607 function :func:`open`, which returns a "file object" with :meth:`read` and
608 :meth:`write` methods (and many more). To wrap a file descriptor in a "file
609 object", use :func:`fdopen`.
610
611
612.. function:: openpty()
613
614 .. index:: module: pty
615
616 Open a new pseudo-terminal pair. Return a pair of file descriptors ``(master,
617 slave)`` for the pty and the tty, respectively. For a (slightly) more portable
Georg Brandl9af94982008-09-13 17:41:16 +0000618 approach, use the :mod:`pty` module. Availability: some flavors of
Georg Brandl8ec7f652007-08-15 14:28:01 +0000619 Unix.
620
621
622.. function:: pipe()
623
624 Create a pipe. Return a pair of file descriptors ``(r, w)`` usable for reading
Georg Brandl9af94982008-09-13 17:41:16 +0000625 and writing, respectively. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000626
627
628.. function:: read(fd, n)
629
630 Read at most *n* bytes from file descriptor *fd*. Return a string containing the
631 bytes read. If the end of the file referred to by *fd* has been reached, an
Georg Brandl9af94982008-09-13 17:41:16 +0000632 empty string is returned. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000633
634 .. note::
635
636 This function is intended for low-level I/O and must be applied to a file
637 descriptor as returned by :func:`open` or :func:`pipe`. To read a "file object"
638 returned by the built-in function :func:`open` or by :func:`popen` or
Georg Brandlf725b952008-01-05 19:44:22 +0000639 :func:`fdopen`, or :data:`sys.stdin`, use its :meth:`read` or :meth:`readline`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000640 methods.
641
642
643.. function:: tcgetpgrp(fd)
644
645 Return the process group associated with the terminal given by *fd* (an open
Georg Brandl9af94982008-09-13 17:41:16 +0000646 file descriptor as returned by :func:`open`). Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000647
648
649.. function:: tcsetpgrp(fd, pg)
650
651 Set the process group associated with the terminal given by *fd* (an open file
Georg Brandl9af94982008-09-13 17:41:16 +0000652 descriptor as returned by :func:`open`) to *pg*. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000653
654
655.. function:: ttyname(fd)
656
657 Return a string which specifies the terminal device associated with
Georg Brandlbb75e4e2007-10-21 10:46:24 +0000658 file descriptor *fd*. If *fd* is not associated with a terminal device, an
Georg Brandl9af94982008-09-13 17:41:16 +0000659 exception is raised. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000660
661
662.. function:: write(fd, str)
663
664 Write the string *str* to file descriptor *fd*. Return the number of bytes
Georg Brandl9af94982008-09-13 17:41:16 +0000665 actually written. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000666
667 .. note::
668
669 This function is intended for low-level I/O and must be applied to a file
670 descriptor as returned by :func:`open` or :func:`pipe`. To write a "file
671 object" returned by the built-in function :func:`open` or by :func:`popen` or
Georg Brandlf725b952008-01-05 19:44:22 +0000672 :func:`fdopen`, or :data:`sys.stdout` or :data:`sys.stderr`, use its :meth:`write`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000673 method.
674
Georg Brandl0c880bd2008-12-05 08:02:17 +0000675The following constants are options for the *flags* parameter to the
676:func:`open` function. They can be combined using the bitwise OR operator
677``|``. Some of them are not available on all platforms. For descriptions of
Georg Brandle70ff4b2008-12-05 09:25:32 +0000678their availability and use, consult the :manpage:`open(2)` manual page on Unix
679or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>` on Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000680
681
682.. data:: O_RDONLY
683 O_WRONLY
684 O_RDWR
685 O_APPEND
686 O_CREAT
687 O_EXCL
688 O_TRUNC
689
Georg Brandl0c880bd2008-12-05 08:02:17 +0000690 These constants are available on Unix and Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000691
692
693.. data:: O_DSYNC
694 O_RSYNC
695 O_SYNC
696 O_NDELAY
697 O_NONBLOCK
698 O_NOCTTY
699 O_SHLOCK
700 O_EXLOCK
701
Georg Brandl0c880bd2008-12-05 08:02:17 +0000702 These constants are only available on Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000703
704
705.. data:: O_BINARY
Georg Brandlb67da6e2007-11-24 13:56:09 +0000706 O_NOINHERIT
Georg Brandl8ec7f652007-08-15 14:28:01 +0000707 O_SHORT_LIVED
708 O_TEMPORARY
709 O_RANDOM
710 O_SEQUENTIAL
711 O_TEXT
712
Georg Brandl0c880bd2008-12-05 08:02:17 +0000713 These constants are only available on Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000714
715
Georg Brandlae6b9f32008-05-16 13:41:26 +0000716.. data:: O_ASYNC
717 O_DIRECT
Georg Brandlb67da6e2007-11-24 13:56:09 +0000718 O_DIRECTORY
719 O_NOFOLLOW
720 O_NOATIME
721
Georg Brandl0c880bd2008-12-05 08:02:17 +0000722 These constants are GNU extensions and not present if they are not defined by
723 the C library.
Georg Brandlb67da6e2007-11-24 13:56:09 +0000724
725
Georg Brandl8ec7f652007-08-15 14:28:01 +0000726.. data:: SEEK_SET
727 SEEK_CUR
728 SEEK_END
729
730 Parameters to the :func:`lseek` function. Their values are 0, 1, and 2,
Georg Brandl9af94982008-09-13 17:41:16 +0000731 respectively. Availability: Windows, Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000732
733 .. versionadded:: 2.5
734
735
736.. _os-file-dir:
737
738Files and Directories
739---------------------
740
Georg Brandl8ec7f652007-08-15 14:28:01 +0000741.. function:: access(path, mode)
742
743 Use the real uid/gid to test for access to *path*. Note that most operations
744 will use the effective uid/gid, therefore this routine can be used in a
745 suid/sgid environment to test if the invoking user has the specified access to
746 *path*. *mode* should be :const:`F_OK` to test the existence of *path*, or it
747 can be the inclusive OR of one or more of :const:`R_OK`, :const:`W_OK`, and
748 :const:`X_OK` to test permissions. Return :const:`True` if access is allowed,
749 :const:`False` if not. See the Unix man page :manpage:`access(2)` for more
Georg Brandl9af94982008-09-13 17:41:16 +0000750 information. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000751
752 .. note::
753
754 Using :func:`access` to check if a user is authorized to e.g. open a file before
755 actually doing so using :func:`open` creates a security hole, because the user
756 might exploit the short time interval between checking and opening the file to
757 manipulate it.
758
759 .. note::
760
761 I/O operations may fail even when :func:`access` indicates that they would
762 succeed, particularly for operations on network filesystems which may have
763 permissions semantics beyond the usual POSIX permission-bit model.
764
765
766.. data:: F_OK
767
768 Value to pass as the *mode* parameter of :func:`access` to test the existence of
769 *path*.
770
771
772.. data:: R_OK
773
774 Value to include in the *mode* parameter of :func:`access` to test the
775 readability of *path*.
776
777
778.. data:: W_OK
779
780 Value to include in the *mode* parameter of :func:`access` to test the
781 writability of *path*.
782
783
784.. data:: X_OK
785
786 Value to include in the *mode* parameter of :func:`access` to determine if
787 *path* can be executed.
788
789
790.. function:: chdir(path)
791
792 .. index:: single: directory; changing
793
Georg Brandl9af94982008-09-13 17:41:16 +0000794 Change the current working directory to *path*. Availability: Unix,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000795 Windows.
796
797
798.. function:: fchdir(fd)
799
800 Change the current working directory to the directory represented by the file
801 descriptor *fd*. The descriptor must refer to an opened directory, not an open
802 file. Availability: Unix.
803
804 .. versionadded:: 2.3
805
806
807.. function:: getcwd()
808
809 Return a string representing the current working directory. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +0000810 Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000811
812
813.. function:: getcwdu()
814
815 Return a Unicode object representing the current working directory.
Georg Brandl9af94982008-09-13 17:41:16 +0000816 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000817
818 .. versionadded:: 2.3
819
820
821.. function:: chflags(path, flags)
822
823 Set the flags of *path* to the numeric *flags*. *flags* may take a combination
824 (bitwise OR) of the following values (as defined in the :mod:`stat` module):
825
826 * ``UF_NODUMP``
827 * ``UF_IMMUTABLE``
828 * ``UF_APPEND``
829 * ``UF_OPAQUE``
830 * ``UF_NOUNLINK``
831 * ``SF_ARCHIVED``
832 * ``SF_IMMUTABLE``
833 * ``SF_APPEND``
834 * ``SF_NOUNLINK``
835 * ``SF_SNAPSHOT``
836
Georg Brandl9af94982008-09-13 17:41:16 +0000837 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000838
839 .. versionadded:: 2.6
840
841
842.. function:: chroot(path)
843
844 Change the root directory of the current process to *path*. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +0000845 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000846
847 .. versionadded:: 2.2
848
849
850.. function:: chmod(path, mode)
851
852 Change the mode of *path* to the numeric *mode*. *mode* may take one of the
Georg Brandlf725b952008-01-05 19:44:22 +0000853 following values (as defined in the :mod:`stat` module) or bitwise ORed
Georg Brandl8ec7f652007-08-15 14:28:01 +0000854 combinations of them:
855
856
857 * ``stat.S_ISUID``
858 * ``stat.S_ISGID``
859 * ``stat.S_ENFMT``
860 * ``stat.S_ISVTX``
861 * ``stat.S_IREAD``
862 * ``stat.S_IWRITE``
863 * ``stat.S_IEXEC``
864 * ``stat.S_IRWXU``
865 * ``stat.S_IRUSR``
866 * ``stat.S_IWUSR``
867 * ``stat.S_IXUSR``
868 * ``stat.S_IRWXG``
869 * ``stat.S_IRGRP``
870 * ``stat.S_IWGRP``
871 * ``stat.S_IXGRP``
872 * ``stat.S_IRWXO``
873 * ``stat.S_IROTH``
874 * ``stat.S_IWOTH``
875 * ``stat.S_IXOTH``
876
Georg Brandl9af94982008-09-13 17:41:16 +0000877 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000878
879 .. note::
880
881 Although Windows supports :func:`chmod`, you can only set the file's read-only
882 flag with it (via the ``stat.S_IWRITE`` and ``stat.S_IREAD``
883 constants or a corresponding integer value). All other bits are
884 ignored.
885
886
887.. function:: chown(path, uid, gid)
888
889 Change the owner and group id of *path* to the numeric *uid* and *gid*. To leave
Georg Brandl9af94982008-09-13 17:41:16 +0000890 one of the ids unchanged, set it to -1. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000891
892
893.. function:: lchflags(path, flags)
894
895 Set the flags of *path* to the numeric *flags*, like :func:`chflags`, but do not
896 follow symbolic links. Availability: Unix.
897
898 .. versionadded:: 2.6
899
900
Georg Brandl81ddc1a2007-11-30 22:04:45 +0000901.. function:: lchmod(path, mode)
902
903 Change the mode of *path* to the numeric *mode*. If path is a symlink, this
904 affects the symlink rather than the target. See the docs for :func:`chmod`
905 for possible values of *mode*. Availability: Unix.
906
907 .. versionadded:: 2.6
908
909
Georg Brandl8ec7f652007-08-15 14:28:01 +0000910.. function:: lchown(path, uid, gid)
911
Georg Brandlf725b952008-01-05 19:44:22 +0000912 Change the owner and group id of *path* to the numeric *uid* and *gid*. This
Georg Brandl9af94982008-09-13 17:41:16 +0000913 function will not follow symbolic links. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000914
915 .. versionadded:: 2.3
916
917
Benjamin Peterson0e928582009-03-28 19:16:10 +0000918.. function:: link(source, link_name)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000919
Benjamin Peterson0e928582009-03-28 19:16:10 +0000920 Create a hard link pointing to *source* named *link_name*. Availability:
921 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000922
923
924.. function:: listdir(path)
925
Georg Brandl62342912008-11-24 19:56:47 +0000926 Return a list containing the names of the entries in the directory given by
927 *path*. The list is in arbitrary order. It does not include the special
928 entries ``'.'`` and ``'..'`` even if they are present in the
929 directory. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000930
931 .. versionchanged:: 2.3
932 On Windows NT/2k/XP and Unix, if *path* is a Unicode object, the result will be
933 a list of Unicode objects.
934
935
936.. function:: lstat(path)
937
Georg Brandl03b15c62007-11-01 17:19:33 +0000938 Like :func:`stat`, but do not follow symbolic links. This is an alias for
939 :func:`stat` on platforms that do not support symbolic links, such as
940 Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000941
942
943.. function:: mkfifo(path[, mode])
944
945 Create a FIFO (a named pipe) named *path* with numeric mode *mode*. The default
946 *mode* is ``0666`` (octal). The current umask value is first masked out from
Georg Brandl9af94982008-09-13 17:41:16 +0000947 the mode. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000948
949 FIFOs are pipes that can be accessed like regular files. FIFOs exist until they
950 are deleted (for example with :func:`os.unlink`). Generally, FIFOs are used as
951 rendezvous between "client" and "server" type processes: the server opens the
952 FIFO for reading, and the client opens it for writing. Note that :func:`mkfifo`
953 doesn't open the FIFO --- it just creates the rendezvous point.
954
955
956.. function:: mknod(filename[, mode=0600, device])
957
958 Create a filesystem node (file, device special file or named pipe) named
959 *filename*. *mode* specifies both the permissions to use and the type of node to
960 be created, being combined (bitwise OR) with one of ``stat.S_IFREG``,
961 ``stat.S_IFCHR``, ``stat.S_IFBLK``,
962 and ``stat.S_IFIFO`` (those constants are available in :mod:`stat`).
963 For ``stat.S_IFCHR`` and
964 ``stat.S_IFBLK``, *device* defines the newly created device special file (probably using
965 :func:`os.makedev`), otherwise it is ignored.
966
967 .. versionadded:: 2.3
968
969
970.. function:: major(device)
971
Georg Brandlf725b952008-01-05 19:44:22 +0000972 Extract the device major number from a raw device number (usually the
Georg Brandl8ec7f652007-08-15 14:28:01 +0000973 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
974
975 .. versionadded:: 2.3
976
977
978.. function:: minor(device)
979
Georg Brandlf725b952008-01-05 19:44:22 +0000980 Extract the device minor number from a raw device number (usually the
Georg Brandl8ec7f652007-08-15 14:28:01 +0000981 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
982
983 .. versionadded:: 2.3
984
985
986.. function:: makedev(major, minor)
987
Georg Brandlf725b952008-01-05 19:44:22 +0000988 Compose a raw device number from the major and minor device numbers.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000989
990 .. versionadded:: 2.3
991
992
993.. function:: mkdir(path[, mode])
994
995 Create a directory named *path* with numeric mode *mode*. The default *mode* is
996 ``0777`` (octal). On some systems, *mode* is ignored. Where it is used, the
Georg Brandl9af94982008-09-13 17:41:16 +0000997 current umask value is first masked out. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000998
Mark Summerfieldac3d4292007-11-02 08:24:59 +0000999 It is also possible to create temporary directories; see the
1000 :mod:`tempfile` module's :func:`tempfile.mkdtemp` function.
1001
Georg Brandl8ec7f652007-08-15 14:28:01 +00001002
1003.. function:: makedirs(path[, mode])
1004
1005 .. index::
1006 single: directory; creating
1007 single: UNC paths; and os.makedirs()
1008
1009 Recursive directory creation function. Like :func:`mkdir`, but makes all
1010 intermediate-level directories needed to contain the leaf directory. Throws an
1011 :exc:`error` exception if the leaf directory already exists or cannot be
1012 created. The default *mode* is ``0777`` (octal). On some systems, *mode* is
1013 ignored. Where it is used, the current umask value is first masked out.
1014
1015 .. note::
1016
1017 :func:`makedirs` will become confused if the path elements to create include
Georg Brandlf725b952008-01-05 19:44:22 +00001018 :data:`os.pardir`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001019
1020 .. versionadded:: 1.5.2
1021
1022 .. versionchanged:: 2.3
1023 This function now handles UNC paths correctly.
1024
1025
1026.. function:: pathconf(path, name)
1027
1028 Return system configuration information relevant to a named file. *name*
1029 specifies the configuration value to retrieve; it may be a string which is the
1030 name of a defined system value; these names are specified in a number of
1031 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
1032 additional names as well. The names known to the host operating system are
1033 given in the ``pathconf_names`` dictionary. For configuration variables not
1034 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl9af94982008-09-13 17:41:16 +00001035 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001036
1037 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1038 specific value for *name* is not supported by the host system, even if it is
1039 included in ``pathconf_names``, an :exc:`OSError` is raised with
1040 :const:`errno.EINVAL` for the error number.
1041
1042
1043.. data:: pathconf_names
1044
1045 Dictionary mapping names accepted by :func:`pathconf` and :func:`fpathconf` to
1046 the integer values defined for those names by the host operating system. This
1047 can be used to determine the set of names known to the system. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00001048 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001049
1050
1051.. function:: readlink(path)
1052
1053 Return a string representing the path to which the symbolic link points. The
1054 result may be either an absolute or relative pathname; if it is relative, it may
1055 be converted to an absolute pathname using ``os.path.join(os.path.dirname(path),
1056 result)``.
1057
1058 .. versionchanged:: 2.6
1059 If the *path* is a Unicode object the result will also be a Unicode object.
1060
Georg Brandl9af94982008-09-13 17:41:16 +00001061 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001062
1063
1064.. function:: remove(path)
1065
1066 Remove the file *path*. If *path* is a directory, :exc:`OSError` is raised; see
1067 :func:`rmdir` below to remove a directory. This is identical to the
1068 :func:`unlink` function documented below. On Windows, attempting to remove a
1069 file that is in use causes an exception to be raised; on Unix, the directory
1070 entry is removed but the storage allocated to the file is not made available
Georg Brandl9af94982008-09-13 17:41:16 +00001071 until the original file is no longer in use. Availability: Unix,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001072 Windows.
1073
1074
1075.. function:: removedirs(path)
1076
1077 .. index:: single: directory; deleting
1078
Georg Brandlf725b952008-01-05 19:44:22 +00001079 Remove directories recursively. Works like :func:`rmdir` except that, if the
Georg Brandl8ec7f652007-08-15 14:28:01 +00001080 leaf directory is successfully removed, :func:`removedirs` tries to
1081 successively remove every parent directory mentioned in *path* until an error
1082 is raised (which is ignored, because it generally means that a parent directory
1083 is not empty). For example, ``os.removedirs('foo/bar/baz')`` will first remove
1084 the directory ``'foo/bar/baz'``, and then remove ``'foo/bar'`` and ``'foo'`` if
1085 they are empty. Raises :exc:`OSError` if the leaf directory could not be
1086 successfully removed.
1087
1088 .. versionadded:: 1.5.2
1089
1090
1091.. function:: rename(src, dst)
1092
1093 Rename the file or directory *src* to *dst*. If *dst* is a directory,
1094 :exc:`OSError` will be raised. On Unix, if *dst* exists and is a file, it will
Georg Brandlf725b952008-01-05 19:44:22 +00001095 be replaced silently if the user has permission. The operation may fail on some
Georg Brandl8ec7f652007-08-15 14:28:01 +00001096 Unix flavors if *src* and *dst* are on different filesystems. If successful,
1097 the renaming will be an atomic operation (this is a POSIX requirement). On
1098 Windows, if *dst* already exists, :exc:`OSError` will be raised even if it is a
1099 file; there may be no way to implement an atomic rename when *dst* names an
Georg Brandl9af94982008-09-13 17:41:16 +00001100 existing file. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001101
1102
1103.. function:: renames(old, new)
1104
1105 Recursive directory or file renaming function. Works like :func:`rename`, except
1106 creation of any intermediate directories needed to make the new pathname good is
1107 attempted first. After the rename, directories corresponding to rightmost path
1108 segments of the old name will be pruned away using :func:`removedirs`.
1109
1110 .. versionadded:: 1.5.2
1111
1112 .. note::
1113
1114 This function can fail with the new directory structure made if you lack
1115 permissions needed to remove the leaf directory or file.
1116
1117
1118.. function:: rmdir(path)
1119
Georg Brandl9af94982008-09-13 17:41:16 +00001120 Remove the directory *path*. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001121
1122
1123.. function:: stat(path)
1124
1125 Perform a :cfunc:`stat` system call on the given path. The return value is an
1126 object whose attributes correspond to the members of the :ctype:`stat`
1127 structure, namely: :attr:`st_mode` (protection bits), :attr:`st_ino` (inode
1128 number), :attr:`st_dev` (device), :attr:`st_nlink` (number of hard links),
Georg Brandlf725b952008-01-05 19:44:22 +00001129 :attr:`st_uid` (user id of owner), :attr:`st_gid` (group id of owner),
Georg Brandl8ec7f652007-08-15 14:28:01 +00001130 :attr:`st_size` (size of file, in bytes), :attr:`st_atime` (time of most recent
1131 access), :attr:`st_mtime` (time of most recent content modification),
1132 :attr:`st_ctime` (platform dependent; time of most recent metadata change on
1133 Unix, or the time of creation on Windows)::
1134
1135 >>> import os
1136 >>> statinfo = os.stat('somefile.txt')
1137 >>> statinfo
1138 (33188, 422511L, 769L, 1, 1032, 100, 926L, 1105022698,1105022732, 1105022732)
1139 >>> statinfo.st_size
1140 926L
1141 >>>
1142
1143 .. versionchanged:: 2.3
Georg Brandlf725b952008-01-05 19:44:22 +00001144 If :func:`stat_float_times` returns ``True``, the time values are floats, measuring
Georg Brandl8ec7f652007-08-15 14:28:01 +00001145 seconds. Fractions of a second may be reported if the system supports that. On
1146 Mac OS, the times are always floats. See :func:`stat_float_times` for further
1147 discussion.
1148
1149 On some Unix systems (such as Linux), the following attributes may also be
1150 available: :attr:`st_blocks` (number of blocks allocated for file),
1151 :attr:`st_blksize` (filesystem blocksize), :attr:`st_rdev` (type of device if an
1152 inode device). :attr:`st_flags` (user defined flags for file).
1153
1154 On other Unix systems (such as FreeBSD), the following attributes may be
1155 available (but may be only filled out if root tries to use them): :attr:`st_gen`
1156 (file generation number), :attr:`st_birthtime` (time of file creation).
1157
1158 On Mac OS systems, the following attributes may also be available:
1159 :attr:`st_rsize`, :attr:`st_creator`, :attr:`st_type`.
1160
1161 On RISCOS systems, the following attributes are also available: :attr:`st_ftype`
1162 (file type), :attr:`st_attrs` (attributes), :attr:`st_obtype` (object type).
1163
1164 .. index:: module: stat
1165
1166 For backward compatibility, the return value of :func:`stat` is also accessible
1167 as a tuple of at least 10 integers giving the most important (and portable)
1168 members of the :ctype:`stat` structure, in the order :attr:`st_mode`,
1169 :attr:`st_ino`, :attr:`st_dev`, :attr:`st_nlink`, :attr:`st_uid`,
1170 :attr:`st_gid`, :attr:`st_size`, :attr:`st_atime`, :attr:`st_mtime`,
1171 :attr:`st_ctime`. More items may be added at the end by some implementations.
1172 The standard module :mod:`stat` defines functions and constants that are useful
1173 for extracting information from a :ctype:`stat` structure. (On Windows, some
1174 items are filled with dummy values.)
1175
1176 .. note::
1177
1178 The exact meaning and resolution of the :attr:`st_atime`, :attr:`st_mtime`, and
1179 :attr:`st_ctime` members depends on the operating system and the file system.
1180 For example, on Windows systems using the FAT or FAT32 file systems,
1181 :attr:`st_mtime` has 2-second resolution, and :attr:`st_atime` has only 1-day
1182 resolution. See your operating system documentation for details.
1183
Georg Brandl9af94982008-09-13 17:41:16 +00001184 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001185
1186 .. versionchanged:: 2.2
1187 Added access to values as attributes of the returned object.
1188
1189 .. versionchanged:: 2.5
Georg Brandlf725b952008-01-05 19:44:22 +00001190 Added :attr:`st_gen` and :attr:`st_birthtime`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001191
1192
1193.. function:: stat_float_times([newvalue])
1194
1195 Determine whether :class:`stat_result` represents time stamps as float objects.
1196 If *newvalue* is ``True``, future calls to :func:`stat` return floats, if it is
1197 ``False``, future calls return ints. If *newvalue* is omitted, return the
1198 current setting.
1199
1200 For compatibility with older Python versions, accessing :class:`stat_result` as
1201 a tuple always returns integers.
1202
1203 .. versionchanged:: 2.5
1204 Python now returns float values by default. Applications which do not work
1205 correctly with floating point time stamps can use this function to restore the
1206 old behaviour.
1207
1208 The resolution of the timestamps (that is the smallest possible fraction)
1209 depends on the system. Some systems only support second resolution; on these
1210 systems, the fraction will always be zero.
1211
1212 It is recommended that this setting is only changed at program startup time in
1213 the *__main__* module; libraries should never change this setting. If an
1214 application uses a library that works incorrectly if floating point time stamps
1215 are processed, this application should turn the feature off until the library
1216 has been corrected.
1217
1218
1219.. function:: statvfs(path)
1220
1221 Perform a :cfunc:`statvfs` system call on the given path. The return value is
1222 an object whose attributes describe the filesystem on the given path, and
1223 correspond to the members of the :ctype:`statvfs` structure, namely:
1224 :attr:`f_bsize`, :attr:`f_frsize`, :attr:`f_blocks`, :attr:`f_bfree`,
1225 :attr:`f_bavail`, :attr:`f_files`, :attr:`f_ffree`, :attr:`f_favail`,
1226 :attr:`f_flag`, :attr:`f_namemax`. Availability: Unix.
1227
1228 .. index:: module: statvfs
1229
1230 For backward compatibility, the return value is also accessible as a tuple whose
1231 values correspond to the attributes, in the order given above. The standard
1232 module :mod:`statvfs` defines constants that are useful for extracting
1233 information from a :ctype:`statvfs` structure when accessing it as a sequence;
1234 this remains useful when writing code that needs to work with versions of Python
1235 that don't support accessing the fields as attributes.
1236
1237 .. versionchanged:: 2.2
1238 Added access to values as attributes of the returned object.
1239
1240
Benjamin Peterson0e928582009-03-28 19:16:10 +00001241.. function:: symlink(source, link_name)
Georg Brandl8ec7f652007-08-15 14:28:01 +00001242
Benjamin Peterson0e928582009-03-28 19:16:10 +00001243 Create a symbolic link pointing to *source* named *link_name*. Availability:
1244 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001245
1246
1247.. function:: tempnam([dir[, prefix]])
1248
1249 Return a unique path name that is reasonable for creating a temporary file.
1250 This will be an absolute path that names a potential directory entry in the
1251 directory *dir* or a common location for temporary files if *dir* is omitted or
1252 ``None``. If given and not ``None``, *prefix* is used to provide a short prefix
1253 to the filename. Applications are responsible for properly creating and
1254 managing files created using paths returned by :func:`tempnam`; no automatic
1255 cleanup is provided. On Unix, the environment variable :envvar:`TMPDIR`
Georg Brandlf725b952008-01-05 19:44:22 +00001256 overrides *dir*, while on Windows :envvar:`TMP` is used. The specific
Georg Brandl8ec7f652007-08-15 14:28:01 +00001257 behavior of this function depends on the C library implementation; some aspects
1258 are underspecified in system documentation.
1259
1260 .. warning::
1261
1262 Use of :func:`tempnam` is vulnerable to symlink attacks; consider using
1263 :func:`tmpfile` (section :ref:`os-newstreams`) instead.
1264
Georg Brandl9af94982008-09-13 17:41:16 +00001265 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001266
1267
1268.. function:: tmpnam()
1269
1270 Return a unique path name that is reasonable for creating a temporary file.
1271 This will be an absolute path that names a potential directory entry in a common
1272 location for temporary files. Applications are responsible for properly
1273 creating and managing files created using paths returned by :func:`tmpnam`; no
1274 automatic cleanup is provided.
1275
1276 .. warning::
1277
1278 Use of :func:`tmpnam` is vulnerable to symlink attacks; consider using
1279 :func:`tmpfile` (section :ref:`os-newstreams`) instead.
1280
1281 Availability: Unix, Windows. This function probably shouldn't be used on
1282 Windows, though: Microsoft's implementation of :func:`tmpnam` always creates a
1283 name in the root directory of the current drive, and that's generally a poor
1284 location for a temp file (depending on privileges, you may not even be able to
1285 open a file using this name).
1286
1287
1288.. data:: TMP_MAX
1289
1290 The maximum number of unique names that :func:`tmpnam` will generate before
1291 reusing names.
1292
1293
1294.. function:: unlink(path)
1295
1296 Remove the file *path*. This is the same function as :func:`remove`; the
Georg Brandl9af94982008-09-13 17:41:16 +00001297 :func:`unlink` name is its traditional Unix name. Availability: Unix,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001298 Windows.
1299
1300
1301.. function:: utime(path, times)
1302
Benjamin Peterson5b02ef32008-08-16 03:13:07 +00001303 Set the access and modified times of the file specified by *path*. If *times*
1304 is ``None``, then the file's access and modified times are set to the current
1305 time. (The effect is similar to running the Unix program :program:`touch` on
1306 the path.) Otherwise, *times* must be a 2-tuple of numbers, of the form
1307 ``(atime, mtime)`` which is used to set the access and modified times,
1308 respectively. Whether a directory can be given for *path* depends on whether
1309 the operating system implements directories as files (for example, Windows
1310 does not). Note that the exact times you set here may not be returned by a
1311 subsequent :func:`stat` call, depending on the resolution with which your
1312 operating system records access and modification times; see :func:`stat`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001313
1314 .. versionchanged:: 2.0
1315 Added support for ``None`` for *times*.
1316
Georg Brandl9af94982008-09-13 17:41:16 +00001317 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001318
1319
1320.. function:: walk(top[, topdown=True [, onerror=None[, followlinks=False]]])
1321
1322 .. index::
1323 single: directory; walking
1324 single: directory; traversal
1325
Georg Brandlf725b952008-01-05 19:44:22 +00001326 Generate the file names in a directory tree by walking the tree
1327 either top-down or bottom-up. For each directory in the tree rooted at directory
Georg Brandl8ec7f652007-08-15 14:28:01 +00001328 *top* (including *top* itself), it yields a 3-tuple ``(dirpath, dirnames,
1329 filenames)``.
1330
1331 *dirpath* is a string, the path to the directory. *dirnames* is a list of the
1332 names of the subdirectories in *dirpath* (excluding ``'.'`` and ``'..'``).
1333 *filenames* is a list of the names of the non-directory files in *dirpath*.
1334 Note that the names in the lists contain no path components. To get a full path
1335 (which begins with *top*) to a file or directory in *dirpath*, do
1336 ``os.path.join(dirpath, name)``.
1337
Georg Brandlf725b952008-01-05 19:44:22 +00001338 If optional argument *topdown* is ``True`` or not specified, the triple for a
Georg Brandl8ec7f652007-08-15 14:28:01 +00001339 directory is generated before the triples for any of its subdirectories
Georg Brandlf725b952008-01-05 19:44:22 +00001340 (directories are generated top-down). If *topdown* is ``False``, the triple for a
Georg Brandl8ec7f652007-08-15 14:28:01 +00001341 directory is generated after the triples for all of its subdirectories
Georg Brandlf725b952008-01-05 19:44:22 +00001342 (directories are generated bottom-up).
Georg Brandl8ec7f652007-08-15 14:28:01 +00001343
Georg Brandlf725b952008-01-05 19:44:22 +00001344 When *topdown* is ``True``, the caller can modify the *dirnames* list in-place
Georg Brandl8ec7f652007-08-15 14:28:01 +00001345 (perhaps using :keyword:`del` or slice assignment), and :func:`walk` will only
1346 recurse into the subdirectories whose names remain in *dirnames*; this can be
1347 used to prune the search, impose a specific order of visiting, or even to inform
1348 :func:`walk` about directories the caller creates or renames before it resumes
Georg Brandlf725b952008-01-05 19:44:22 +00001349 :func:`walk` again. Modifying *dirnames* when *topdown* is ``False`` is
Georg Brandl8ec7f652007-08-15 14:28:01 +00001350 ineffective, because in bottom-up mode the directories in *dirnames* are
1351 generated before *dirpath* itself is generated.
1352
Georg Brandlf725b952008-01-05 19:44:22 +00001353 By default errors from the :func:`listdir` call are ignored. If optional
Georg Brandl8ec7f652007-08-15 14:28:01 +00001354 argument *onerror* is specified, it should be a function; it will be called with
1355 one argument, an :exc:`OSError` instance. It can report the error to continue
1356 with the walk, or raise the exception to abort the walk. Note that the filename
1357 is available as the ``filename`` attribute of the exception object.
1358
1359 By default, :func:`walk` will not walk down into symbolic links that resolve to
Georg Brandlf725b952008-01-05 19:44:22 +00001360 directories. Set *followlinks* to ``True`` to visit directories pointed to by
Georg Brandl8ec7f652007-08-15 14:28:01 +00001361 symlinks, on systems that support them.
1362
1363 .. versionadded:: 2.6
1364 The *followlinks* parameter.
1365
1366 .. note::
1367
Georg Brandlf725b952008-01-05 19:44:22 +00001368 Be aware that setting *followlinks* to ``True`` can lead to infinite recursion if a
Georg Brandl8ec7f652007-08-15 14:28:01 +00001369 link points to a parent directory of itself. :func:`walk` does not keep track of
1370 the directories it visited already.
1371
1372 .. note::
1373
1374 If you pass a relative pathname, don't change the current working directory
1375 between resumptions of :func:`walk`. :func:`walk` never changes the current
1376 directory, and assumes that its caller doesn't either.
1377
1378 This example displays the number of bytes taken by non-directory files in each
1379 directory under the starting directory, except that it doesn't look under any
1380 CVS subdirectory::
1381
1382 import os
1383 from os.path import join, getsize
1384 for root, dirs, files in os.walk('python/Lib/email'):
1385 print root, "consumes",
1386 print sum(getsize(join(root, name)) for name in files),
1387 print "bytes in", len(files), "non-directory files"
1388 if 'CVS' in dirs:
1389 dirs.remove('CVS') # don't visit CVS directories
1390
Georg Brandlf725b952008-01-05 19:44:22 +00001391 In the next example, walking the tree bottom-up is essential: :func:`rmdir`
Georg Brandl8ec7f652007-08-15 14:28:01 +00001392 doesn't allow deleting a directory before the directory is empty::
1393
Georg Brandlf725b952008-01-05 19:44:22 +00001394 # Delete everything reachable from the directory named in "top",
Georg Brandl8ec7f652007-08-15 14:28:01 +00001395 # assuming there are no symbolic links.
1396 # CAUTION: This is dangerous! For example, if top == '/', it
1397 # could delete all your disk files.
1398 import os
1399 for root, dirs, files in os.walk(top, topdown=False):
1400 for name in files:
1401 os.remove(os.path.join(root, name))
1402 for name in dirs:
1403 os.rmdir(os.path.join(root, name))
1404
1405 .. versionadded:: 2.3
1406
1407
1408.. _os-process:
1409
1410Process Management
1411------------------
1412
1413These functions may be used to create and manage processes.
1414
1415The various :func:`exec\*` functions take a list of arguments for the new
1416program loaded into the process. In each case, the first of these arguments is
1417passed to the new program as its own name rather than as an argument a user may
1418have typed on a command line. For the C programmer, this is the ``argv[0]``
1419passed to a program's :cfunc:`main`. For example, ``os.execv('/bin/echo',
1420['foo', 'bar'])`` will only print ``bar`` on standard output; ``foo`` will seem
1421to be ignored.
1422
1423
1424.. function:: abort()
1425
1426 Generate a :const:`SIGABRT` signal to the current process. On Unix, the default
1427 behavior is to produce a core dump; on Windows, the process immediately returns
1428 an exit code of ``3``. Be aware that programs which use :func:`signal.signal`
1429 to register a handler for :const:`SIGABRT` will behave differently.
Georg Brandl9af94982008-09-13 17:41:16 +00001430 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001431
1432
1433.. function:: execl(path, arg0, arg1, ...)
1434 execle(path, arg0, arg1, ..., env)
1435 execlp(file, arg0, arg1, ...)
1436 execlpe(file, arg0, arg1, ..., env)
1437 execv(path, args)
1438 execve(path, args, env)
1439 execvp(file, args)
1440 execvpe(file, args, env)
1441
1442 These functions all execute a new program, replacing the current process; they
1443 do not return. On Unix, the new executable is loaded into the current process,
Georg Brandlf725b952008-01-05 19:44:22 +00001444 and will have the same process id as the caller. Errors will be reported as
Georg Brandlc62ef8b2009-01-03 20:55:06 +00001445 :exc:`OSError` exceptions.
Andrew M. Kuchlingac771662008-09-28 00:15:27 +00001446
1447 The current process is replaced immediately. Open file objects and
1448 descriptors are not flushed, so if there may be data buffered
1449 on these open files, you should flush them using
1450 :func:`sys.stdout.flush` or :func:`os.fsync` before calling an
1451 :func:`exec\*` function.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001452
Georg Brandlf725b952008-01-05 19:44:22 +00001453 The "l" and "v" variants of the :func:`exec\*` functions differ in how
1454 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl8ec7f652007-08-15 14:28:01 +00001455 to work with if the number of parameters is fixed when the code is written; the
1456 individual parameters simply become additional parameters to the :func:`execl\*`
Georg Brandlf725b952008-01-05 19:44:22 +00001457 functions. The "v" variants are good when the number of parameters is
Georg Brandl8ec7f652007-08-15 14:28:01 +00001458 variable, with the arguments being passed in a list or tuple as the *args*
1459 parameter. In either case, the arguments to the child process should start with
1460 the name of the command being run, but this is not enforced.
1461
Georg Brandlf725b952008-01-05 19:44:22 +00001462 The variants which include a "p" near the end (:func:`execlp`,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001463 :func:`execlpe`, :func:`execvp`, and :func:`execvpe`) will use the
1464 :envvar:`PATH` environment variable to locate the program *file*. When the
1465 environment is being replaced (using one of the :func:`exec\*e` variants,
1466 discussed in the next paragraph), the new environment is used as the source of
1467 the :envvar:`PATH` variable. The other variants, :func:`execl`, :func:`execle`,
1468 :func:`execv`, and :func:`execve`, will not use the :envvar:`PATH` variable to
1469 locate the executable; *path* must contain an appropriate absolute or relative
1470 path.
1471
1472 For :func:`execle`, :func:`execlpe`, :func:`execve`, and :func:`execvpe` (note
Georg Brandlf725b952008-01-05 19:44:22 +00001473 that these all end in "e"), the *env* parameter must be a mapping which is
Georg Brandlfb246c42008-04-19 16:58:28 +00001474 used to define the environment variables for the new process (these are used
1475 instead of the current process' environment); the functions :func:`execl`,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001476 :func:`execlp`, :func:`execv`, and :func:`execvp` all cause the new process to
Georg Brandlc62ef8b2009-01-03 20:55:06 +00001477 inherit the environment of the current process.
Andrew M. Kuchlingac771662008-09-28 00:15:27 +00001478
1479 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001480
1481
1482.. function:: _exit(n)
1483
1484 Exit to the system with status *n*, without calling cleanup handlers, flushing
Georg Brandl9af94982008-09-13 17:41:16 +00001485 stdio buffers, etc. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001486
1487 .. note::
1488
1489 The standard way to exit is ``sys.exit(n)``. :func:`_exit` should normally only
1490 be used in the child process after a :func:`fork`.
1491
Georg Brandlf725b952008-01-05 19:44:22 +00001492The following exit codes are defined and can be used with :func:`_exit`,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001493although they are not required. These are typically used for system programs
1494written in Python, such as a mail server's external command delivery program.
1495
1496.. note::
1497
1498 Some of these may not be available on all Unix platforms, since there is some
1499 variation. These constants are defined where they are defined by the underlying
1500 platform.
1501
1502
1503.. data:: EX_OK
1504
Georg Brandl9af94982008-09-13 17:41:16 +00001505 Exit code that means no error occurred. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001506
1507 .. versionadded:: 2.3
1508
1509
1510.. data:: EX_USAGE
1511
1512 Exit code that means the command was used incorrectly, such as when the wrong
Georg Brandl9af94982008-09-13 17:41:16 +00001513 number of arguments are given. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001514
1515 .. versionadded:: 2.3
1516
1517
1518.. data:: EX_DATAERR
1519
Georg Brandl9af94982008-09-13 17:41:16 +00001520 Exit code that means the input data was incorrect. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001521
1522 .. versionadded:: 2.3
1523
1524
1525.. data:: EX_NOINPUT
1526
1527 Exit code that means an input file did not exist or was not readable.
Georg Brandl9af94982008-09-13 17:41:16 +00001528 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001529
1530 .. versionadded:: 2.3
1531
1532
1533.. data:: EX_NOUSER
1534
Georg Brandl9af94982008-09-13 17:41:16 +00001535 Exit code that means a specified user did not exist. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001536
1537 .. versionadded:: 2.3
1538
1539
1540.. data:: EX_NOHOST
1541
Georg Brandl9af94982008-09-13 17:41:16 +00001542 Exit code that means a specified host did not exist. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001543
1544 .. versionadded:: 2.3
1545
1546
1547.. data:: EX_UNAVAILABLE
1548
1549 Exit code that means that a required service is unavailable. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00001550 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001551
1552 .. versionadded:: 2.3
1553
1554
1555.. data:: EX_SOFTWARE
1556
1557 Exit code that means an internal software error was detected. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00001558 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001559
1560 .. versionadded:: 2.3
1561
1562
1563.. data:: EX_OSERR
1564
1565 Exit code that means an operating system error was detected, such as the
Georg Brandl9af94982008-09-13 17:41:16 +00001566 inability to fork or create a pipe. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001567
1568 .. versionadded:: 2.3
1569
1570
1571.. data:: EX_OSFILE
1572
1573 Exit code that means some system file did not exist, could not be opened, or had
Georg Brandl9af94982008-09-13 17:41:16 +00001574 some other kind of error. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001575
1576 .. versionadded:: 2.3
1577
1578
1579.. data:: EX_CANTCREAT
1580
1581 Exit code that means a user specified output file could not be created.
Georg Brandl9af94982008-09-13 17:41:16 +00001582 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001583
1584 .. versionadded:: 2.3
1585
1586
1587.. data:: EX_IOERR
1588
1589 Exit code that means that an error occurred while doing I/O on some file.
Georg Brandl9af94982008-09-13 17:41:16 +00001590 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001591
1592 .. versionadded:: 2.3
1593
1594
1595.. data:: EX_TEMPFAIL
1596
1597 Exit code that means a temporary failure occurred. This indicates something
1598 that may not really be an error, such as a network connection that couldn't be
Georg Brandl9af94982008-09-13 17:41:16 +00001599 made during a retryable operation. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001600
1601 .. versionadded:: 2.3
1602
1603
1604.. data:: EX_PROTOCOL
1605
1606 Exit code that means that a protocol exchange was illegal, invalid, or not
Georg Brandl9af94982008-09-13 17:41:16 +00001607 understood. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001608
1609 .. versionadded:: 2.3
1610
1611
1612.. data:: EX_NOPERM
1613
1614 Exit code that means that there were insufficient permissions to perform the
Georg Brandl9af94982008-09-13 17:41:16 +00001615 operation (but not intended for file system problems). Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001616
1617 .. versionadded:: 2.3
1618
1619
1620.. data:: EX_CONFIG
1621
1622 Exit code that means that some kind of configuration error occurred.
Georg Brandl9af94982008-09-13 17:41:16 +00001623 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001624
1625 .. versionadded:: 2.3
1626
1627
1628.. data:: EX_NOTFOUND
1629
1630 Exit code that means something like "an entry was not found". Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00001631 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001632
1633 .. versionadded:: 2.3
1634
1635
1636.. function:: fork()
1637
Georg Brandlf725b952008-01-05 19:44:22 +00001638 Fork a child process. Return ``0`` in the child and the child's process id in the
Skip Montanaro75e51682008-03-15 02:32:49 +00001639 parent. If an error occurs :exc:`OSError` is raised.
Gregory P. Smith08067492008-09-30 20:41:13 +00001640
1641 Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have
1642 known issues when using fork() from a thread.
1643
Georg Brandl9af94982008-09-13 17:41:16 +00001644 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001645
1646
1647.. function:: forkpty()
1648
1649 Fork a child process, using a new pseudo-terminal as the child's controlling
1650 terminal. Return a pair of ``(pid, fd)``, where *pid* is ``0`` in the child, the
1651 new child's process id in the parent, and *fd* is the file descriptor of the
1652 master end of the pseudo-terminal. For a more portable approach, use the
Skip Montanaro75e51682008-03-15 02:32:49 +00001653 :mod:`pty` module. If an error occurs :exc:`OSError` is raised.
Georg Brandl9af94982008-09-13 17:41:16 +00001654 Availability: some flavors of Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001655
1656
1657.. function:: kill(pid, sig)
1658
1659 .. index::
1660 single: process; killing
1661 single: process; signalling
1662
1663 Send signal *sig* to the process *pid*. Constants for the specific signals
1664 available on the host platform are defined in the :mod:`signal` module.
Georg Brandl9af94982008-09-13 17:41:16 +00001665 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001666
1667
1668.. function:: killpg(pgid, sig)
1669
1670 .. index::
1671 single: process; killing
1672 single: process; signalling
1673
Georg Brandl9af94982008-09-13 17:41:16 +00001674 Send the signal *sig* to the process group *pgid*. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001675
1676 .. versionadded:: 2.3
1677
1678
1679.. function:: nice(increment)
1680
1681 Add *increment* to the process's "niceness". Return the new niceness.
Georg Brandl9af94982008-09-13 17:41:16 +00001682 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001683
1684
1685.. function:: plock(op)
1686
1687 Lock program segments into memory. The value of *op* (defined in
Georg Brandl9af94982008-09-13 17:41:16 +00001688 ``<sys/lock.h>``) determines which segments are locked. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001689
1690
1691.. function:: popen(...)
1692 popen2(...)
1693 popen3(...)
1694 popen4(...)
1695 :noindex:
1696
1697 Run child processes, returning opened pipes for communications. These functions
1698 are described in section :ref:`os-newstreams`.
1699
1700
1701.. function:: spawnl(mode, path, ...)
1702 spawnle(mode, path, ..., env)
1703 spawnlp(mode, file, ...)
1704 spawnlpe(mode, file, ..., env)
1705 spawnv(mode, path, args)
1706 spawnve(mode, path, args, env)
1707 spawnvp(mode, file, args)
1708 spawnvpe(mode, file, args, env)
1709
1710 Execute the program *path* in a new process.
1711
1712 (Note that the :mod:`subprocess` module provides more powerful facilities for
1713 spawning new processes and retrieving their results; using that module is
Georg Brandlc62ef8b2009-01-03 20:55:06 +00001714 preferable to using these functions. Check specially the *Replacing Older
Facundo Batista74a6ba82008-06-21 19:48:19 +00001715 Functions with the subprocess Module* section in that documentation page.)
Georg Brandl8ec7f652007-08-15 14:28:01 +00001716
Georg Brandlf725b952008-01-05 19:44:22 +00001717 If *mode* is :const:`P_NOWAIT`, this function returns the process id of the new
Georg Brandl8ec7f652007-08-15 14:28:01 +00001718 process; if *mode* is :const:`P_WAIT`, returns the process's exit code if it
1719 exits normally, or ``-signal``, where *signal* is the signal that killed the
Georg Brandlf725b952008-01-05 19:44:22 +00001720 process. On Windows, the process id will actually be the process handle, so can
Georg Brandl8ec7f652007-08-15 14:28:01 +00001721 be used with the :func:`waitpid` function.
1722
Georg Brandlf725b952008-01-05 19:44:22 +00001723 The "l" and "v" variants of the :func:`spawn\*` functions differ in how
1724 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl8ec7f652007-08-15 14:28:01 +00001725 to work with if the number of parameters is fixed when the code is written; the
1726 individual parameters simply become additional parameters to the
Georg Brandlf725b952008-01-05 19:44:22 +00001727 :func:`spawnl\*` functions. The "v" variants are good when the number of
Georg Brandl8ec7f652007-08-15 14:28:01 +00001728 parameters is variable, with the arguments being passed in a list or tuple as
1729 the *args* parameter. In either case, the arguments to the child process must
1730 start with the name of the command being run.
1731
Georg Brandlf725b952008-01-05 19:44:22 +00001732 The variants which include a second "p" near the end (:func:`spawnlp`,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001733 :func:`spawnlpe`, :func:`spawnvp`, and :func:`spawnvpe`) will use the
1734 :envvar:`PATH` environment variable to locate the program *file*. When the
1735 environment is being replaced (using one of the :func:`spawn\*e` variants,
1736 discussed in the next paragraph), the new environment is used as the source of
1737 the :envvar:`PATH` variable. The other variants, :func:`spawnl`,
1738 :func:`spawnle`, :func:`spawnv`, and :func:`spawnve`, will not use the
1739 :envvar:`PATH` variable to locate the executable; *path* must contain an
1740 appropriate absolute or relative path.
1741
1742 For :func:`spawnle`, :func:`spawnlpe`, :func:`spawnve`, and :func:`spawnvpe`
Georg Brandlf725b952008-01-05 19:44:22 +00001743 (note that these all end in "e"), the *env* parameter must be a mapping
Georg Brandlfb246c42008-04-19 16:58:28 +00001744 which is used to define the environment variables for the new process (they are
1745 used instead of the current process' environment); the functions
Georg Brandl8ec7f652007-08-15 14:28:01 +00001746 :func:`spawnl`, :func:`spawnlp`, :func:`spawnv`, and :func:`spawnvp` all cause
Georg Brandl22717df2009-03-31 18:26:55 +00001747 the new process to inherit the environment of the current process. Note that
1748 keys and values in the *env* dictionary must be strings; invalid keys or
1749 values will cause the function to fail, with a return value of ``127``.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001750
1751 As an example, the following calls to :func:`spawnlp` and :func:`spawnvpe` are
1752 equivalent::
1753
1754 import os
1755 os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1756
1757 L = ['cp', 'index.html', '/dev/null']
1758 os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1759
1760 Availability: Unix, Windows. :func:`spawnlp`, :func:`spawnlpe`, :func:`spawnvp`
1761 and :func:`spawnvpe` are not available on Windows.
1762
1763 .. versionadded:: 1.6
1764
1765
1766.. data:: P_NOWAIT
1767 P_NOWAITO
1768
1769 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1770 functions. If either of these values is given, the :func:`spawn\*` functions
Georg Brandlf725b952008-01-05 19:44:22 +00001771 will return as soon as the new process has been created, with the process id as
Georg Brandl9af94982008-09-13 17:41:16 +00001772 the return value. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001773
1774 .. versionadded:: 1.6
1775
1776
1777.. data:: P_WAIT
1778
1779 Possible value for the *mode* parameter to the :func:`spawn\*` family of
1780 functions. If this is given as *mode*, the :func:`spawn\*` functions will not
1781 return until the new process has run to completion and will return the exit code
1782 of the process the run is successful, or ``-signal`` if a signal kills the
Georg Brandl9af94982008-09-13 17:41:16 +00001783 process. Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001784
1785 .. versionadded:: 1.6
1786
1787
1788.. data:: P_DETACH
1789 P_OVERLAY
1790
1791 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1792 functions. These are less portable than those listed above. :const:`P_DETACH`
1793 is similar to :const:`P_NOWAIT`, but the new process is detached from the
1794 console of the calling process. If :const:`P_OVERLAY` is used, the current
1795 process will be replaced; the :func:`spawn\*` function will not return.
1796 Availability: Windows.
1797
1798 .. versionadded:: 1.6
1799
1800
1801.. function:: startfile(path[, operation])
1802
1803 Start a file with its associated application.
1804
1805 When *operation* is not specified or ``'open'``, this acts like double-clicking
1806 the file in Windows Explorer, or giving the file name as an argument to the
1807 :program:`start` command from the interactive command shell: the file is opened
1808 with whatever application (if any) its extension is associated.
1809
1810 When another *operation* is given, it must be a "command verb" that specifies
1811 what should be done with the file. Common verbs documented by Microsoft are
1812 ``'print'`` and ``'edit'`` (to be used on files) as well as ``'explore'`` and
1813 ``'find'`` (to be used on directories).
1814
1815 :func:`startfile` returns as soon as the associated application is launched.
1816 There is no option to wait for the application to close, and no way to retrieve
1817 the application's exit status. The *path* parameter is relative to the current
1818 directory. If you want to use an absolute path, make sure the first character
1819 is not a slash (``'/'``); the underlying Win32 :cfunc:`ShellExecute` function
1820 doesn't work if it is. Use the :func:`os.path.normpath` function to ensure that
1821 the path is properly encoded for Win32. Availability: Windows.
1822
1823 .. versionadded:: 2.0
1824
1825 .. versionadded:: 2.5
1826 The *operation* parameter.
1827
1828
1829.. function:: system(command)
1830
1831 Execute the command (a string) in a subshell. This is implemented by calling
1832 the Standard C function :cfunc:`system`, and has the same limitations. Changes
Georg Brandlf725b952008-01-05 19:44:22 +00001833 to :data:`os.environ`, :data:`sys.stdin`, etc. are not reflected in the
1834 environment of the executed command.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001835
1836 On Unix, the return value is the exit status of the process encoded in the
1837 format specified for :func:`wait`. Note that POSIX does not specify the meaning
1838 of the return value of the C :cfunc:`system` function, so the return value of
1839 the Python function is system-dependent.
1840
1841 On Windows, the return value is that returned by the system shell after running
1842 *command*, given by the Windows environment variable :envvar:`COMSPEC`: on
1843 :program:`command.com` systems (Windows 95, 98 and ME) this is always ``0``; on
1844 :program:`cmd.exe` systems (Windows NT, 2000 and XP) this is the exit status of
1845 the command run; on systems using a non-native shell, consult your shell
1846 documentation.
1847
Georg Brandl9af94982008-09-13 17:41:16 +00001848 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001849
1850 The :mod:`subprocess` module provides more powerful facilities for spawning new
1851 processes and retrieving their results; using that module is preferable to using
Georg Brandl0ba92b22008-06-22 09:05:29 +00001852 this function. Use the :mod:`subprocess` module. Check especially the
1853 :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001854
1855
1856.. function:: times()
1857
1858 Return a 5-tuple of floating point numbers indicating accumulated (processor or
1859 other) times, in seconds. The items are: user time, system time, children's
1860 user time, children's system time, and elapsed real time since a fixed point in
1861 the past, in that order. See the Unix manual page :manpage:`times(2)` or the
Georg Brandl9af94982008-09-13 17:41:16 +00001862 corresponding Windows Platform API documentation. Availability: Unix,
Georg Brandl0a40ffb2008-02-13 07:20:22 +00001863 Windows. On Windows, only the first two items are filled, the others are zero.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001864
1865
1866.. function:: wait()
1867
1868 Wait for completion of a child process, and return a tuple containing its pid
1869 and exit status indication: a 16-bit number, whose low byte is the signal number
1870 that killed the process, and whose high byte is the exit status (if the signal
1871 number is zero); the high bit of the low byte is set if a core file was
Georg Brandl9af94982008-09-13 17:41:16 +00001872 produced. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001873
1874
1875.. function:: waitpid(pid, options)
1876
1877 The details of this function differ on Unix and Windows.
1878
1879 On Unix: Wait for completion of a child process given by process id *pid*, and
1880 return a tuple containing its process id and exit status indication (encoded as
1881 for :func:`wait`). The semantics of the call are affected by the value of the
1882 integer *options*, which should be ``0`` for normal operation.
1883
1884 If *pid* is greater than ``0``, :func:`waitpid` requests status information for
1885 that specific process. If *pid* is ``0``, the request is for the status of any
1886 child in the process group of the current process. If *pid* is ``-1``, the
1887 request pertains to any child of the current process. If *pid* is less than
1888 ``-1``, status is requested for any process in the process group ``-pid`` (the
1889 absolute value of *pid*).
1890
Gregory P. Smith59de7f52008-08-15 23:14:00 +00001891 An :exc:`OSError` is raised with the value of errno when the syscall
1892 returns -1.
1893
Georg Brandl8ec7f652007-08-15 14:28:01 +00001894 On Windows: Wait for completion of a process given by process handle *pid*, and
1895 return a tuple containing *pid*, and its exit status shifted left by 8 bits
1896 (shifting makes cross-platform use of the function easier). A *pid* less than or
1897 equal to ``0`` has no special meaning on Windows, and raises an exception. The
1898 value of integer *options* has no effect. *pid* can refer to any process whose
1899 id is known, not necessarily a child process. The :func:`spawn` functions called
1900 with :const:`P_NOWAIT` return suitable process handles.
1901
1902
1903.. function:: wait3([options])
1904
1905 Similar to :func:`waitpid`, except no process id argument is given and a
1906 3-element tuple containing the child's process id, exit status indication, and
1907 resource usage information is returned. Refer to :mod:`resource`.\
1908 :func:`getrusage` for details on resource usage information. The option
1909 argument is the same as that provided to :func:`waitpid` and :func:`wait4`.
1910 Availability: Unix.
1911
1912 .. versionadded:: 2.5
1913
1914
1915.. function:: wait4(pid, options)
1916
1917 Similar to :func:`waitpid`, except a 3-element tuple, containing the child's
1918 process id, exit status indication, and resource usage information is returned.
1919 Refer to :mod:`resource`.\ :func:`getrusage` for details on resource usage
1920 information. The arguments to :func:`wait4` are the same as those provided to
1921 :func:`waitpid`. Availability: Unix.
1922
1923 .. versionadded:: 2.5
1924
1925
1926.. data:: WNOHANG
1927
1928 The option for :func:`waitpid` to return immediately if no child process status
1929 is available immediately. The function returns ``(0, 0)`` in this case.
Georg Brandl9af94982008-09-13 17:41:16 +00001930 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001931
1932
1933.. data:: WCONTINUED
1934
1935 This option causes child processes to be reported if they have been continued
1936 from a job control stop since their status was last reported. Availability: Some
1937 Unix systems.
1938
1939 .. versionadded:: 2.3
1940
1941
1942.. data:: WUNTRACED
1943
1944 This option causes child processes to be reported if they have been stopped but
1945 their current state has not been reported since they were stopped. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00001946 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001947
1948 .. versionadded:: 2.3
1949
1950The following functions take a process status code as returned by
1951:func:`system`, :func:`wait`, or :func:`waitpid` as a parameter. They may be
1952used to determine the disposition of a process.
1953
1954
1955.. function:: WCOREDUMP(status)
1956
Georg Brandlf725b952008-01-05 19:44:22 +00001957 Return ``True`` if a core dump was generated for the process, otherwise
Georg Brandl9af94982008-09-13 17:41:16 +00001958 return ``False``. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001959
1960 .. versionadded:: 2.3
1961
1962
1963.. function:: WIFCONTINUED(status)
1964
Georg Brandlf725b952008-01-05 19:44:22 +00001965 Return ``True`` if the process has been continued from a job control stop,
1966 otherwise return ``False``. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001967
1968 .. versionadded:: 2.3
1969
1970
1971.. function:: WIFSTOPPED(status)
1972
Georg Brandlf725b952008-01-05 19:44:22 +00001973 Return ``True`` if the process has been stopped, otherwise return
Georg Brandl8ec7f652007-08-15 14:28:01 +00001974 ``False``. Availability: Unix.
1975
1976
1977.. function:: WIFSIGNALED(status)
1978
Georg Brandlf725b952008-01-05 19:44:22 +00001979 Return ``True`` if the process exited due to a signal, otherwise return
Georg Brandl9af94982008-09-13 17:41:16 +00001980 ``False``. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001981
1982
1983.. function:: WIFEXITED(status)
1984
Georg Brandlf725b952008-01-05 19:44:22 +00001985 Return ``True`` if the process exited using the :manpage:`exit(2)` system call,
Georg Brandl9af94982008-09-13 17:41:16 +00001986 otherwise return ``False``. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001987
1988
1989.. function:: WEXITSTATUS(status)
1990
1991 If ``WIFEXITED(status)`` is true, return the integer parameter to the
1992 :manpage:`exit(2)` system call. Otherwise, the return value is meaningless.
Georg Brandl9af94982008-09-13 17:41:16 +00001993 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001994
1995
1996.. function:: WSTOPSIG(status)
1997
Georg Brandl9af94982008-09-13 17:41:16 +00001998 Return the signal which caused the process to stop. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001999
2000
2001.. function:: WTERMSIG(status)
2002
Georg Brandl9af94982008-09-13 17:41:16 +00002003 Return the signal which caused the process to exit. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002004
2005
2006.. _os-path:
2007
2008Miscellaneous System Information
2009--------------------------------
2010
2011
2012.. function:: confstr(name)
2013
2014 Return string-valued system configuration values. *name* specifies the
2015 configuration value to retrieve; it may be a string which is the name of a
2016 defined system value; these names are specified in a number of standards (POSIX,
2017 Unix 95, Unix 98, and others). Some platforms define additional names as well.
2018 The names known to the host operating system are given as the keys of the
2019 ``confstr_names`` dictionary. For configuration variables not included in that
2020 mapping, passing an integer for *name* is also accepted. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00002021 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002022
2023 If the configuration value specified by *name* isn't defined, ``None`` is
2024 returned.
2025
2026 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
2027 specific value for *name* is not supported by the host system, even if it is
2028 included in ``confstr_names``, an :exc:`OSError` is raised with
2029 :const:`errno.EINVAL` for the error number.
2030
2031
2032.. data:: confstr_names
2033
2034 Dictionary mapping names accepted by :func:`confstr` to the integer values
2035 defined for those names by the host operating system. This can be used to
Georg Brandl9af94982008-09-13 17:41:16 +00002036 determine the set of names known to the system. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002037
2038
2039.. function:: getloadavg()
2040
Georg Brandl57fe0f22008-01-12 10:53:29 +00002041 Return the number of processes in the system run queue averaged over the last
2042 1, 5, and 15 minutes or raises :exc:`OSError` if the load average was
Georg Brandl6bb7bcf2008-05-30 19:12:13 +00002043 unobtainable. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002044
2045 .. versionadded:: 2.3
2046
2047
2048.. function:: sysconf(name)
2049
2050 Return integer-valued system configuration values. If the configuration value
2051 specified by *name* isn't defined, ``-1`` is returned. The comments regarding
2052 the *name* parameter for :func:`confstr` apply here as well; the dictionary that
2053 provides information on the known names is given by ``sysconf_names``.
Georg Brandl9af94982008-09-13 17:41:16 +00002054 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002055
2056
2057.. data:: sysconf_names
2058
2059 Dictionary mapping names accepted by :func:`sysconf` to the integer values
2060 defined for those names by the host operating system. This can be used to
Georg Brandl9af94982008-09-13 17:41:16 +00002061 determine the set of names known to the system. Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002062
Georg Brandlf725b952008-01-05 19:44:22 +00002063The following data values are used to support path manipulation operations. These
Georg Brandl8ec7f652007-08-15 14:28:01 +00002064are defined for all platforms.
2065
2066Higher-level operations on pathnames are defined in the :mod:`os.path` module.
2067
2068
2069.. data:: curdir
2070
2071 The constant string used by the operating system to refer to the current
Georg Brandl9af94982008-09-13 17:41:16 +00002072 directory. This is ``'.'`` for Windows and POSIX. Also available via
2073 :mod:`os.path`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002074
2075
2076.. data:: pardir
2077
2078 The constant string used by the operating system to refer to the parent
Georg Brandl9af94982008-09-13 17:41:16 +00002079 directory. This is ``'..'`` for Windows and POSIX. Also available via
2080 :mod:`os.path`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002081
2082
2083.. data:: sep
2084
Georg Brandl9af94982008-09-13 17:41:16 +00002085 The character used by the operating system to separate pathname components.
2086 This is ``'/'`` for POSIX and ``'\\'`` for Windows. Note that knowing this
2087 is not sufficient to be able to parse or concatenate pathnames --- use
Georg Brandl8ec7f652007-08-15 14:28:01 +00002088 :func:`os.path.split` and :func:`os.path.join` --- but it is occasionally
2089 useful. Also available via :mod:`os.path`.
2090
2091
2092.. data:: altsep
2093
2094 An alternative character used by the operating system to separate pathname
2095 components, or ``None`` if only one separator character exists. This is set to
2096 ``'/'`` on Windows systems where ``sep`` is a backslash. Also available via
2097 :mod:`os.path`.
2098
2099
2100.. data:: extsep
2101
2102 The character which separates the base filename from the extension; for example,
2103 the ``'.'`` in :file:`os.py`. Also available via :mod:`os.path`.
2104
2105 .. versionadded:: 2.2
2106
2107
2108.. data:: pathsep
2109
2110 The character conventionally used by the operating system to separate search
2111 path components (as in :envvar:`PATH`), such as ``':'`` for POSIX or ``';'`` for
2112 Windows. Also available via :mod:`os.path`.
2113
2114
2115.. data:: defpath
2116
2117 The default search path used by :func:`exec\*p\*` and :func:`spawn\*p\*` if the
2118 environment doesn't have a ``'PATH'`` key. Also available via :mod:`os.path`.
2119
2120
2121.. data:: linesep
2122
2123 The string used to separate (or, rather, terminate) lines on the current
Georg Brandl9af94982008-09-13 17:41:16 +00002124 platform. This may be a single character, such as ``'\n'`` for POSIX, or
2125 multiple characters, for example, ``'\r\n'`` for Windows. Do not use
2126 *os.linesep* as a line terminator when writing files opened in text mode (the
2127 default); use a single ``'\n'`` instead, on all platforms.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002128
2129
2130.. data:: devnull
2131
Georg Brandl9af94982008-09-13 17:41:16 +00002132 The file path of the null device. For example: ``'/dev/null'`` for POSIX.
2133 Also available via :mod:`os.path`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002134
2135 .. versionadded:: 2.4
2136
2137
2138.. _os-miscfunc:
2139
2140Miscellaneous Functions
2141-----------------------
2142
2143
2144.. function:: urandom(n)
2145
2146 Return a string of *n* random bytes suitable for cryptographic use.
2147
2148 This function returns random bytes from an OS-specific randomness source. The
2149 returned data should be unpredictable enough for cryptographic applications,
2150 though its exact quality depends on the OS implementation. On a UNIX-like
2151 system this will query /dev/urandom, and on Windows it will use CryptGenRandom.
2152 If a randomness source is not found, :exc:`NotImplementedError` will be raised.
2153
2154 .. versionadded:: 2.4
2155