blob: 8c634447759e2089df6a8c6b3a6572fe1888a192 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`os` --- Miscellaneous operating system interfaces
2=======================================================
3
4.. module:: os
5 :synopsis: Miscellaneous operating system interfaces.
6
7
Georg Brandl57fe0f22008-01-12 10:53:29 +00008This module provides a portable way of using operating system dependent
9functionality. If you just want to read or write a file see :func:`open`, if
10you want to manipulate paths, see the :mod:`os.path` module, and if you want to
11read all the lines in all the files on the command line see the :mod:`fileinput`
12module. For creating temporary files and directories see the :mod:`tempfile`
13module, and for high-level file and directory handling see the :mod:`shutil`
14module.
Georg Brandl8ec7f652007-08-15 14:28:01 +000015
Georg Brandlc51d1f02009-12-19 18:16:31 +000016Notes on the availability of these functions:
Georg Brandl8ec7f652007-08-15 14:28:01 +000017
Georg Brandlc51d1f02009-12-19 18:16:31 +000018* The design of all built-in operating system dependent modules of Python is
19 such that as long as the same functionality is available, it uses the same
20 interface; for example, the function ``os.stat(path)`` returns stat
21 information about *path* in the same format (which happens to have originated
22 with the POSIX interface).
Georg Brandl8ec7f652007-08-15 14:28:01 +000023
Georg Brandlc51d1f02009-12-19 18:16:31 +000024* Extensions peculiar to a particular operating system are also available
25 through the :mod:`os` module, but using them is of course a threat to
26 portability.
Georg Brandl8ec7f652007-08-15 14:28:01 +000027
Georg Brandlc51d1f02009-12-19 18:16:31 +000028* An "Availability: Unix" note means that this function is commonly found on
29 Unix systems. It does not make any claims about its existence on a specific
30 operating system.
31
32* If not separately noted, all functions that claim "Availability: Unix" are
33 supported on Mac OS X, which builds on a Unix core.
Georg Brandl9af94982008-09-13 17:41:16 +000034
Benjamin Peterson328e3772010-05-06 22:49:28 +000035.. Availability notes get their own line and occur at the end of the function
36.. documentation.
37
Georg Brandl9af94982008-09-13 17:41:16 +000038.. note::
39
Georg Brandl57fe0f22008-01-12 10:53:29 +000040 All functions in this module raise :exc:`OSError` in the case of invalid or
41 inaccessible file names and paths, or other arguments that have the correct
42 type, but are not accepted by the operating system.
Georg Brandl8ec7f652007-08-15 14:28:01 +000043
Georg Brandl8ec7f652007-08-15 14:28:01 +000044
45.. exception:: error
46
Georg Brandl57fe0f22008-01-12 10:53:29 +000047 An alias for the built-in :exc:`OSError` exception.
Georg Brandl8ec7f652007-08-15 14:28:01 +000048
49
50.. data:: name
51
Georg Brandlc51d1f02009-12-19 18:16:31 +000052 The name of the operating system dependent module imported. The following
Ronald Oussoren9545a232010-05-05 19:09:31 +000053 names have currently been registered: ``'posix'``, ``'nt'``,
Georg Brandlc51d1f02009-12-19 18:16:31 +000054 ``'os2'``, ``'ce'``, ``'java'``, ``'riscos'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +000055
Antoine Pitrouea901ad2011-07-09 15:48:29 +020056 .. seealso::
57 :attr:`sys.platform` has a finer granularity. :func:`os.uname` gives
58 system-dependent version information.
59
60 The :mod:`platform` module provides detailed checks for the
61 system's identity.
62
Georg Brandl8ec7f652007-08-15 14:28:01 +000063
Georg Brandl8ec7f652007-08-15 14:28:01 +000064.. _os-procinfo:
65
66Process Parameters
67------------------
68
69These functions and data items provide information and operate on the current
70process and user.
71
72
73.. data:: environ
74
75 A mapping object representing the string environment. For example,
76 ``environ['HOME']`` is the pathname of your home directory (on some platforms),
77 and is equivalent to ``getenv("HOME")`` in C.
78
79 This mapping is captured the first time the :mod:`os` module is imported,
80 typically during Python startup as part of processing :file:`site.py`. Changes
81 to the environment made after this time are not reflected in ``os.environ``,
82 except for changes made by modifying ``os.environ`` directly.
83
84 If the platform supports the :func:`putenv` function, this mapping may be used
85 to modify the environment as well as query the environment. :func:`putenv` will
86 be called automatically when the mapping is modified.
87
88 .. note::
89
90 Calling :func:`putenv` directly does not change ``os.environ``, so it's better
91 to modify ``os.environ``.
92
93 .. note::
94
Georg Brandl9af94982008-09-13 17:41:16 +000095 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
96 cause memory leaks. Refer to the system documentation for
Sandro Tosi98ed08f2012-01-14 16:42:02 +010097 :c:func:`putenv`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000098
99 If :func:`putenv` is not provided, a modified copy of this mapping may be
100 passed to the appropriate process-creation functions to cause child processes
101 to use a modified environment.
102
Georg Brandl4a212682007-09-20 17:57:59 +0000103 If the platform supports the :func:`unsetenv` function, you can delete items in
Georg Brandl8ec7f652007-08-15 14:28:01 +0000104 this mapping to unset environment variables. :func:`unsetenv` will be called
Georg Brandl4a212682007-09-20 17:57:59 +0000105 automatically when an item is deleted from ``os.environ``, and when
Georg Brandl1a94ec22007-10-24 21:40:38 +0000106 one of the :meth:`pop` or :meth:`clear` methods is called.
Georg Brandl4a212682007-09-20 17:57:59 +0000107
108 .. versionchanged:: 2.6
Georg Brandl1a94ec22007-10-24 21:40:38 +0000109 Also unset environment variables when calling :meth:`os.environ.clear`
110 and :meth:`os.environ.pop`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000111
112
113.. function:: chdir(path)
114 fchdir(fd)
115 getcwd()
116 :noindex:
117
118 These functions are described in :ref:`os-file-dir`.
119
120
121.. function:: ctermid()
122
123 Return the filename corresponding to the controlling terminal of the process.
Benjamin Peterson328e3772010-05-06 22:49:28 +0000124
Georg Brandl8ec7f652007-08-15 14:28:01 +0000125 Availability: Unix.
126
127
128.. function:: getegid()
129
130 Return the effective group id of the current process. This corresponds to the
Benjamin Peterson328e3772010-05-06 22:49:28 +0000131 "set id" bit on the file being executed in the current process.
132
133 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000134
135
136.. function:: geteuid()
137
138 .. index:: single: user; effective id
139
Benjamin Peterson328e3772010-05-06 22:49:28 +0000140 Return the current process's effective user id.
141
142 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000143
144
145.. function:: getgid()
146
147 .. index:: single: process; group
148
Benjamin Peterson328e3772010-05-06 22:49:28 +0000149 Return the real group id of the current process.
150
151 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000152
153
154.. function:: getgroups()
155
156 Return list of supplemental group ids associated with the current process.
Benjamin Peterson328e3772010-05-06 22:49:28 +0000157
Georg Brandl8ec7f652007-08-15 14:28:01 +0000158 Availability: Unix.
159
160
Antoine Pitrou30b3b352009-12-02 20:37:54 +0000161.. function:: initgroups(username, gid)
162
163 Call the system initgroups() to initialize the group access list with all of
164 the groups of which the specified username is a member, plus the specified
Benjamin Peterson328e3772010-05-06 22:49:28 +0000165 group id.
166
167 Availability: Unix.
Antoine Pitrou30b3b352009-12-02 20:37:54 +0000168
169 .. versionadded:: 2.7
170
171
Georg Brandl8ec7f652007-08-15 14:28:01 +0000172.. function:: getlogin()
173
174 Return the name of the user logged in on the controlling terminal of the
175 process. For most purposes, it is more useful to use the environment variable
176 :envvar:`LOGNAME` to find out who the user is, or
177 ``pwd.getpwuid(os.getuid())[0]`` to get the login name of the currently
Benjamin Peterson328e3772010-05-06 22:49:28 +0000178 effective user id.
179
180 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000181
182
183.. function:: getpgid(pid)
184
185 Return the process group id of the process with process id *pid*. If *pid* is 0,
Benjamin Peterson328e3772010-05-06 22:49:28 +0000186 the process group id of the current process is returned.
187
188 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000189
190 .. versionadded:: 2.3
191
192
193.. function:: getpgrp()
194
195 .. index:: single: process; group
196
Benjamin Peterson328e3772010-05-06 22:49:28 +0000197 Return the id of the current process group.
198
199 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000200
201
202.. function:: getpid()
203
204 .. index:: single: process; id
205
Benjamin Peterson328e3772010-05-06 22:49:28 +0000206 Return the current process id.
207
208 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000209
210
211.. function:: getppid()
212
213 .. index:: single: process; id of parent
214
Benjamin Peterson328e3772010-05-06 22:49:28 +0000215 Return the parent's process id.
216
217 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000218
Georg Brandl8d8f8742009-11-28 11:11:50 +0000219
Gregory P. Smith761ae0b2009-11-27 17:51:12 +0000220.. function:: getresuid()
Martin v. Löwis50ea4562009-11-27 13:56:01 +0000221
222 Return a tuple (ruid, euid, suid) denoting the current process's
Benjamin Peterson328e3772010-05-06 22:49:28 +0000223 real, effective, and saved user ids.
224
225 Availability: Unix.
Martin v. Löwis50ea4562009-11-27 13:56:01 +0000226
Georg Brandl8d8f8742009-11-28 11:11:50 +0000227 .. versionadded:: 2.7
228
Martin v. Löwis50ea4562009-11-27 13:56:01 +0000229
Gregory P. Smith761ae0b2009-11-27 17:51:12 +0000230.. function:: getresgid()
Martin v. Löwis50ea4562009-11-27 13:56:01 +0000231
232 Return a tuple (rgid, egid, sgid) denoting the current process's
Georg Brandl21946af2010-10-06 09:28:45 +0000233 real, effective, and saved group ids.
Benjamin Peterson328e3772010-05-06 22:49:28 +0000234
235 Availability: Unix.
Martin v. Löwis50ea4562009-11-27 13:56:01 +0000236
Georg Brandl8d8f8742009-11-28 11:11:50 +0000237 .. versionadded:: 2.7
238
Georg Brandl8ec7f652007-08-15 14:28:01 +0000239
240.. function:: getuid()
241
242 .. index:: single: user; id
243
Benjamin Peterson328e3772010-05-06 22:49:28 +0000244 Return the current process's user id.
245
246 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000247
248
249.. function:: getenv(varname[, value])
250
251 Return the value of the environment variable *varname* if it exists, or *value*
Benjamin Peterson328e3772010-05-06 22:49:28 +0000252 if it doesn't. *value* defaults to ``None``.
253
254 Availability: most flavors of Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000255
256
257.. function:: putenv(varname, value)
258
259 .. index:: single: environment variables; setting
260
261 Set the environment variable named *varname* to the string *value*. Such
262 changes to the environment affect subprocesses started with :func:`os.system`,
Benjamin Peterson328e3772010-05-06 22:49:28 +0000263 :func:`popen` or :func:`fork` and :func:`execv`.
264
265 Availability: most flavors of Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000266
267 .. note::
268
Georg Brandl9af94982008-09-13 17:41:16 +0000269 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
270 cause memory leaks. Refer to the system documentation for putenv.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000271
272 When :func:`putenv` is supported, assignments to items in ``os.environ`` are
273 automatically translated into corresponding calls to :func:`putenv`; however,
274 calls to :func:`putenv` don't update ``os.environ``, so it is actually
275 preferable to assign to items of ``os.environ``.
276
277
278.. function:: setegid(egid)
279
Benjamin Peterson328e3772010-05-06 22:49:28 +0000280 Set the current process's effective group id.
281
282 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000283
284
285.. function:: seteuid(euid)
286
Benjamin Peterson328e3772010-05-06 22:49:28 +0000287 Set the current process's effective user id.
288
289 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000290
291
292.. function:: setgid(gid)
293
Benjamin Peterson328e3772010-05-06 22:49:28 +0000294 Set the current process' group id.
295
296 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000297
298
299.. function:: setgroups(groups)
300
301 Set the list of supplemental group ids associated with the current process to
302 *groups*. *groups* must be a sequence, and each element must be an integer
Georg Brandlf725b952008-01-05 19:44:22 +0000303 identifying a group. This operation is typically available only to the superuser.
Benjamin Peterson328e3772010-05-06 22:49:28 +0000304
Georg Brandl8ec7f652007-08-15 14:28:01 +0000305 Availability: Unix.
306
307 .. versionadded:: 2.2
308
309
310.. function:: setpgrp()
311
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100312 Call the system call :c:func:`setpgrp` or :c:func:`setpgrp(0, 0)` depending on
Georg Brandl8ec7f652007-08-15 14:28:01 +0000313 which version is implemented (if any). See the Unix manual for the semantics.
Benjamin Peterson328e3772010-05-06 22:49:28 +0000314
Georg Brandl8ec7f652007-08-15 14:28:01 +0000315 Availability: Unix.
316
317
318.. function:: setpgid(pid, pgrp)
319
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100320 Call the system call :c:func:`setpgid` to set the process group id of the
Georg Brandl8ec7f652007-08-15 14:28:01 +0000321 process with id *pid* to the process group with id *pgrp*. See the Unix manual
Benjamin Peterson328e3772010-05-06 22:49:28 +0000322 for the semantics.
323
324 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000325
326
Georg Brandl8ec7f652007-08-15 14:28:01 +0000327.. function:: setregid(rgid, egid)
328
Benjamin Peterson328e3772010-05-06 22:49:28 +0000329 Set the current process's real and effective group ids.
330
331 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000332
Georg Brandl8d8f8742009-11-28 11:11:50 +0000333
Martin v. Löwis50ea4562009-11-27 13:56:01 +0000334.. function:: setresgid(rgid, egid, sgid)
335
336 Set the current process's real, effective, and saved group ids.
Benjamin Peterson328e3772010-05-06 22:49:28 +0000337
Martin v. Löwis50ea4562009-11-27 13:56:01 +0000338 Availability: Unix.
339
Georg Brandl8d8f8742009-11-28 11:11:50 +0000340 .. versionadded:: 2.7
341
Martin v. Löwis50ea4562009-11-27 13:56:01 +0000342
343.. function:: setresuid(ruid, euid, suid)
344
345 Set the current process's real, effective, and saved user ids.
Benjamin Peterson328e3772010-05-06 22:49:28 +0000346
Georg Brandl09302282010-10-06 09:32:48 +0000347 Availability: Unix.
Martin v. Löwis50ea4562009-11-27 13:56:01 +0000348
Georg Brandl8d8f8742009-11-28 11:11:50 +0000349 .. versionadded:: 2.7
350
Martin v. Löwis50ea4562009-11-27 13:56:01 +0000351
352.. function:: setreuid(ruid, euid)
353
Benjamin Peterson328e3772010-05-06 22:49:28 +0000354 Set the current process's real and effective user ids.
355
356 Availability: Unix.
Martin v. Löwis50ea4562009-11-27 13:56:01 +0000357
Georg Brandl8ec7f652007-08-15 14:28:01 +0000358
359.. function:: getsid(pid)
360
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100361 Call the system call :c:func:`getsid`. See the Unix manual for the semantics.
Benjamin Peterson328e3772010-05-06 22:49:28 +0000362
Georg Brandl8ec7f652007-08-15 14:28:01 +0000363 Availability: Unix.
364
365 .. versionadded:: 2.4
366
367
368.. function:: setsid()
369
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100370 Call the system call :c:func:`setsid`. See the Unix manual for the semantics.
Benjamin Peterson328e3772010-05-06 22:49:28 +0000371
Georg Brandl8ec7f652007-08-15 14:28:01 +0000372 Availability: Unix.
373
374
375.. function:: setuid(uid)
376
377 .. index:: single: user; id, setting
378
Benjamin Peterson328e3772010-05-06 22:49:28 +0000379 Set the current process's user id.
380
381 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000382
Georg Brandl8ec7f652007-08-15 14:28:01 +0000383
Georg Brandlb19be572007-12-29 10:57:00 +0000384.. placed in this section since it relates to errno.... a little weak
Georg Brandl8ec7f652007-08-15 14:28:01 +0000385.. function:: strerror(code)
386
387 Return the error message corresponding to the error code in *code*.
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100388 On platforms where :c:func:`strerror` returns ``NULL`` when given an unknown
Benjamin Peterson328e3772010-05-06 22:49:28 +0000389 error number, :exc:`ValueError` is raised.
390
391 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000392
393
394.. function:: umask(mask)
395
Benjamin Peterson328e3772010-05-06 22:49:28 +0000396 Set the current numeric umask and return the previous umask.
397
398 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000399
400
401.. function:: uname()
402
403 .. index::
404 single: gethostname() (in module socket)
405 single: gethostbyaddr() (in module socket)
406
407 Return a 5-tuple containing information identifying the current operating
408 system. The tuple contains 5 strings: ``(sysname, nodename, release, version,
409 machine)``. Some systems truncate the nodename to 8 characters or to the
410 leading component; a better way to get the hostname is
411 :func:`socket.gethostname` or even
Benjamin Peterson328e3772010-05-06 22:49:28 +0000412 ``socket.gethostbyaddr(socket.gethostname())``.
413
414 Availability: recent flavors of Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000415
416
417.. function:: unsetenv(varname)
418
419 .. index:: single: environment variables; deleting
420
421 Unset (delete) the environment variable named *varname*. Such changes to the
422 environment affect subprocesses started with :func:`os.system`, :func:`popen` or
Benjamin Peterson328e3772010-05-06 22:49:28 +0000423 :func:`fork` and :func:`execv`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000424
425 When :func:`unsetenv` is supported, deletion of items in ``os.environ`` is
426 automatically translated into a corresponding call to :func:`unsetenv`; however,
427 calls to :func:`unsetenv` don't update ``os.environ``, so it is actually
428 preferable to delete items of ``os.environ``.
429
Benjamin Peterson328e3772010-05-06 22:49:28 +0000430 Availability: most flavors of Unix, Windows.
431
Georg Brandl8ec7f652007-08-15 14:28:01 +0000432
433.. _os-newstreams:
434
435File Object Creation
436--------------------
437
438These functions create new file objects. (See also :func:`open`.)
439
440
441.. function:: fdopen(fd[, mode[, bufsize]])
442
443 .. index:: single: I/O control; buffering
444
445 Return an open file object connected to the file descriptor *fd*. The *mode*
446 and *bufsize* arguments have the same meaning as the corresponding arguments to
Benjamin Peterson328e3772010-05-06 22:49:28 +0000447 the built-in :func:`open` function.
448
449 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000450
451 .. versionchanged:: 2.3
452 When specified, the *mode* argument must now start with one of the letters
453 ``'r'``, ``'w'``, or ``'a'``, otherwise a :exc:`ValueError` is raised.
454
455 .. versionchanged:: 2.5
456 On Unix, when the *mode* argument starts with ``'a'``, the *O_APPEND* flag is
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100457 set on the file descriptor (which the :c:func:`fdopen` implementation already
Georg Brandl8ec7f652007-08-15 14:28:01 +0000458 does on most platforms).
459
460
461.. function:: popen(command[, mode[, bufsize]])
462
463 Open a pipe to or from *command*. The return value is an open file object
464 connected to the pipe, which can be read or written depending on whether *mode*
465 is ``'r'`` (default) or ``'w'``. The *bufsize* argument has the same meaning as
466 the corresponding argument to the built-in :func:`open` function. The exit
467 status of the command (encoded in the format specified for :func:`wait`) is
Georg Brandl012408c2009-05-22 09:43:17 +0000468 available as the return value of the :meth:`~file.close` method of the file object,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000469 except that when the exit status is zero (termination without errors), ``None``
Benjamin Peterson328e3772010-05-06 22:49:28 +0000470 is returned.
471
472 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000473
474 .. deprecated:: 2.6
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000475 This function is obsolete. Use the :mod:`subprocess` module. Check
Georg Brandl0ba92b22008-06-22 09:05:29 +0000476 especially the :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000477
478 .. versionchanged:: 2.0
479 This function worked unreliably under Windows in earlier versions of Python.
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100480 This was due to the use of the :c:func:`_popen` function from the libraries
Georg Brandl8ec7f652007-08-15 14:28:01 +0000481 provided with Windows. Newer versions of Python do not use the broken
482 implementation from the Windows libraries.
483
484
485.. function:: tmpfile()
486
487 Return a new file object opened in update mode (``w+b``). The file has no
488 directory entries associated with it and will be automatically deleted once
Benjamin Peterson328e3772010-05-06 22:49:28 +0000489 there are no file descriptors for the file.
490
491 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000492
493There are a number of different :func:`popen\*` functions that provide slightly
494different ways to create subprocesses.
495
496.. deprecated:: 2.6
497 All of the :func:`popen\*` functions are obsolete. Use the :mod:`subprocess`
498 module.
499
500For each of the :func:`popen\*` variants, if *bufsize* is specified, it
501specifies the buffer size for the I/O pipes. *mode*, if provided, should be the
502string ``'b'`` or ``'t'``; on Windows this is needed to determine whether the
503file objects should be opened in binary or text mode. The default value for
504*mode* is ``'t'``.
505
506Also, for each of these variants, on Unix, *cmd* may be a sequence, in which
507case arguments will be passed directly to the program without shell intervention
508(as with :func:`os.spawnv`). If *cmd* is a string it will be passed to the shell
509(as with :func:`os.system`).
510
511These methods do not make it possible to retrieve the exit status from the child
512processes. The only way to control the input and output streams and also
513retrieve the return codes is to use the :mod:`subprocess` module; these are only
514available on Unix.
515
516For a discussion of possible deadlock conditions related to the use of these
517functions, see :ref:`popen2-flow-control`.
518
519
520.. function:: popen2(cmd[, mode[, bufsize]])
521
Georg Brandlf725b952008-01-05 19:44:22 +0000522 Execute *cmd* as a sub-process and return the file objects ``(child_stdin,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000523 child_stdout)``.
524
525 .. deprecated:: 2.6
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000526 This function is obsolete. Use the :mod:`subprocess` module. Check
Georg Brandl0ba92b22008-06-22 09:05:29 +0000527 especially the :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000528
Georg Brandl9af94982008-09-13 17:41:16 +0000529 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000530
531 .. versionadded:: 2.0
532
533
534.. function:: popen3(cmd[, mode[, bufsize]])
535
Georg Brandlf725b952008-01-05 19:44:22 +0000536 Execute *cmd* as a sub-process and return the file objects ``(child_stdin,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000537 child_stdout, child_stderr)``.
538
539 .. deprecated:: 2.6
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000540 This function is obsolete. Use the :mod:`subprocess` module. Check
Georg Brandl0ba92b22008-06-22 09:05:29 +0000541 especially the :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000542
Georg Brandl9af94982008-09-13 17:41:16 +0000543 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000544
545 .. versionadded:: 2.0
546
547
548.. function:: popen4(cmd[, mode[, bufsize]])
549
Georg Brandlf725b952008-01-05 19:44:22 +0000550 Execute *cmd* as a sub-process and return the file objects ``(child_stdin,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000551 child_stdout_and_stderr)``.
552
553 .. deprecated:: 2.6
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000554 This function is obsolete. Use the :mod:`subprocess` module. Check
Georg Brandl0ba92b22008-06-22 09:05:29 +0000555 especially the :ref:`subprocess-replacements` section.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000556
Georg Brandl9af94982008-09-13 17:41:16 +0000557 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000558
559 .. versionadded:: 2.0
560
561(Note that ``child_stdin, child_stdout, and child_stderr`` are named from the
562point of view of the child process, so *child_stdin* is the child's standard
563input.)
564
565This functionality is also available in the :mod:`popen2` module using functions
566of the same names, but the return values of those functions have a different
567order.
568
569
570.. _os-fd-ops:
571
572File Descriptor Operations
573--------------------------
574
575These functions operate on I/O streams referenced using file descriptors.
576
577File descriptors are small integers corresponding to a file that has been opened
578by the current process. For example, standard input is usually file descriptor
5790, standard output is 1, and standard error is 2. Further files opened by a
580process will then be assigned 3, 4, 5, and so forth. The name "file descriptor"
581is slightly deceptive; on Unix platforms, sockets and pipes are also referenced
582by file descriptors.
583
Georg Brandl49b91922010-04-02 08:39:09 +0000584The :meth:`~file.fileno` method can be used to obtain the file descriptor
585associated with a file object when required. Note that using the file
586descriptor directly will bypass the file object methods, ignoring aspects such
587as internal buffering of data.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000588
589.. function:: close(fd)
590
Benjamin Peterson328e3772010-05-06 22:49:28 +0000591 Close file descriptor *fd*.
592
593 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000594
595 .. note::
596
597 This function is intended for low-level I/O and must be applied to a file
Georg Brandl012408c2009-05-22 09:43:17 +0000598 descriptor as returned by :func:`os.open` or :func:`pipe`. To close a "file
Georg Brandl8ec7f652007-08-15 14:28:01 +0000599 object" returned by the built-in function :func:`open` or by :func:`popen` or
Georg Brandl012408c2009-05-22 09:43:17 +0000600 :func:`fdopen`, use its :meth:`~file.close` method.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000601
602
Georg Brandl309501a2008-01-19 20:22:13 +0000603.. function:: closerange(fd_low, fd_high)
604
605 Close all file descriptors from *fd_low* (inclusive) to *fd_high* (exclusive),
Benjamin Peterson328e3772010-05-06 22:49:28 +0000606 ignoring errors. Equivalent to::
Georg Brandl309501a2008-01-19 20:22:13 +0000607
608 for fd in xrange(fd_low, fd_high):
609 try:
610 os.close(fd)
611 except OSError:
612 pass
613
Benjamin Peterson328e3772010-05-06 22:49:28 +0000614 Availability: Unix, Windows.
615
Georg Brandl309501a2008-01-19 20:22:13 +0000616 .. versionadded:: 2.6
617
618
Georg Brandl8ec7f652007-08-15 14:28:01 +0000619.. function:: dup(fd)
620
Benjamin Peterson328e3772010-05-06 22:49:28 +0000621 Return a duplicate of file descriptor *fd*.
622
623 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000624
625
626.. function:: dup2(fd, fd2)
627
628 Duplicate file descriptor *fd* to *fd2*, closing the latter first if necessary.
Benjamin Peterson328e3772010-05-06 22:49:28 +0000629
Georg Brandl9af94982008-09-13 17:41:16 +0000630 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000631
632
Christian Heimes36281872007-11-30 21:11:28 +0000633.. function:: fchmod(fd, mode)
634
635 Change the mode of the file given by *fd* to the numeric *mode*. See the docs
Benjamin Peterson328e3772010-05-06 22:49:28 +0000636 for :func:`chmod` for possible values of *mode*.
637
638 Availability: Unix.
Christian Heimes36281872007-11-30 21:11:28 +0000639
Georg Brandl81ddc1a2007-11-30 22:04:45 +0000640 .. versionadded:: 2.6
641
Christian Heimes36281872007-11-30 21:11:28 +0000642
643.. function:: fchown(fd, uid, gid)
644
645 Change the owner and group id of the file given by *fd* to the numeric *uid*
646 and *gid*. To leave one of the ids unchanged, set it to -1.
Benjamin Peterson328e3772010-05-06 22:49:28 +0000647
Christian Heimes36281872007-11-30 21:11:28 +0000648 Availability: Unix.
649
Georg Brandl81ddc1a2007-11-30 22:04:45 +0000650 .. versionadded:: 2.6
651
Christian Heimes36281872007-11-30 21:11:28 +0000652
Georg Brandl8ec7f652007-08-15 14:28:01 +0000653.. function:: fdatasync(fd)
654
655 Force write of file with filedescriptor *fd* to disk. Does not force update of
Benjamin Peterson328e3772010-05-06 22:49:28 +0000656 metadata.
657
658 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000659
Benjamin Petersonecf3c622009-05-30 03:10:52 +0000660 .. note::
661 This function is not available on MacOS.
662
Georg Brandl8ec7f652007-08-15 14:28:01 +0000663
664.. function:: fpathconf(fd, name)
665
666 Return system configuration information relevant to an open file. *name*
667 specifies the configuration value to retrieve; it may be a string which is the
668 name of a defined system value; these names are specified in a number of
669 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
670 additional names as well. The names known to the host operating system are
671 given in the ``pathconf_names`` dictionary. For configuration variables not
672 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000673
674 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
675 specific value for *name* is not supported by the host system, even if it is
676 included in ``pathconf_names``, an :exc:`OSError` is raised with
677 :const:`errno.EINVAL` for the error number.
678
Benjamin Peterson328e3772010-05-06 22:49:28 +0000679 Availability: Unix.
680
Georg Brandl8ec7f652007-08-15 14:28:01 +0000681
682.. function:: fstat(fd)
683
R. David Murray561b96f2011-02-11 17:25:54 +0000684 Return status for file descriptor *fd*, like :func:`~os.stat`.
Benjamin Peterson328e3772010-05-06 22:49:28 +0000685
686 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000687
688
689.. function:: fstatvfs(fd)
690
691 Return information about the filesystem containing the file associated with file
Benjamin Peterson328e3772010-05-06 22:49:28 +0000692 descriptor *fd*, like :func:`statvfs`.
693
694 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000695
696
697.. function:: fsync(fd)
698
699 Force write of file with filedescriptor *fd* to disk. On Unix, this calls the
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100700 native :c:func:`fsync` function; on Windows, the MS :c:func:`_commit` function.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000701
702 If you're starting with a Python file object *f*, first do ``f.flush()``, and
703 then do ``os.fsync(f.fileno())``, to ensure that all internal buffers associated
Benjamin Peterson328e3772010-05-06 22:49:28 +0000704 with *f* are written to disk.
705
706 Availability: Unix, and Windows starting in 2.2.3.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000707
708
709.. function:: ftruncate(fd, length)
710
711 Truncate the file corresponding to file descriptor *fd*, so that it is at most
Benjamin Peterson328e3772010-05-06 22:49:28 +0000712 *length* bytes in size.
713
714 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000715
716
717.. function:: isatty(fd)
718
719 Return ``True`` if the file descriptor *fd* is open and connected to a
Benjamin Peterson328e3772010-05-06 22:49:28 +0000720 tty(-like) device, else ``False``.
721
722 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000723
724
725.. function:: lseek(fd, pos, how)
726
Georg Brandlf725b952008-01-05 19:44:22 +0000727 Set the current position of file descriptor *fd* to position *pos*, modified
728 by *how*: :const:`SEEK_SET` or ``0`` to set the position relative to the
729 beginning of the file; :const:`SEEK_CUR` or ``1`` to set it relative to the
730 current position; :const:`os.SEEK_END` or ``2`` to set it relative to the end of
Benjamin Peterson328e3772010-05-06 22:49:28 +0000731 the file.
732
733 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000734
735
Georg Brandl6c50efe2010-04-14 13:50:31 +0000736.. data:: SEEK_SET
737 SEEK_CUR
738 SEEK_END
739
740 Parameters to the :func:`lseek` function. Their values are 0, 1, and 2,
Benjamin Peterson328e3772010-05-06 22:49:28 +0000741 respectively.
742
743 Availability: Windows, Unix.
Georg Brandl6c50efe2010-04-14 13:50:31 +0000744
745 .. versionadded:: 2.5
746
747
Georg Brandl8ec7f652007-08-15 14:28:01 +0000748.. function:: open(file, flags[, mode])
749
750 Open the file *file* and set various flags according to *flags* and possibly its
751 mode according to *mode*. The default *mode* is ``0777`` (octal), and the
752 current umask value is first masked out. Return the file descriptor for the
Benjamin Peterson328e3772010-05-06 22:49:28 +0000753 newly opened file.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000754
755 For a description of the flag and mode values, see the C run-time documentation;
756 flag constants (like :const:`O_RDONLY` and :const:`O_WRONLY`) are defined in
Georg Brandl4a589c32010-04-14 19:16:38 +0000757 this module too (see :ref:`open-constants`). In particular, on Windows adding
758 :const:`O_BINARY` is needed to open files in binary mode.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000759
Benjamin Peterson328e3772010-05-06 22:49:28 +0000760 Availability: Unix, Windows.
761
Georg Brandl8ec7f652007-08-15 14:28:01 +0000762 .. note::
763
Georg Brandl9fa61bb2009-07-26 14:19:57 +0000764 This function is intended for low-level I/O. For normal usage, use the
765 built-in function :func:`open`, which returns a "file object" with
Jeroen Ruigrok van der Werven320477e2010-07-13 15:08:30 +0000766 :meth:`~file.read` and :meth:`~file.write` methods (and many more). To
Georg Brandl9fa61bb2009-07-26 14:19:57 +0000767 wrap a file descriptor in a "file object", use :func:`fdopen`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000768
769
770.. function:: openpty()
771
772 .. index:: module: pty
773
774 Open a new pseudo-terminal pair. Return a pair of file descriptors ``(master,
775 slave)`` for the pty and the tty, respectively. For a (slightly) more portable
Benjamin Peterson328e3772010-05-06 22:49:28 +0000776 approach, use the :mod:`pty` module.
777
778 Availability: some flavors of Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000779
780
781.. function:: pipe()
782
783 Create a pipe. Return a pair of file descriptors ``(r, w)`` usable for reading
Benjamin Peterson328e3772010-05-06 22:49:28 +0000784 and writing, respectively.
785
786 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000787
788
789.. function:: read(fd, n)
790
791 Read at most *n* bytes from file descriptor *fd*. Return a string containing the
792 bytes read. If the end of the file referred to by *fd* has been reached, an
Benjamin Peterson328e3772010-05-06 22:49:28 +0000793 empty string is returned.
794
795 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000796
797 .. note::
798
799 This function is intended for low-level I/O and must be applied to a file
Georg Brandl012408c2009-05-22 09:43:17 +0000800 descriptor as returned by :func:`os.open` or :func:`pipe`. To read a "file object"
Georg Brandl8ec7f652007-08-15 14:28:01 +0000801 returned by the built-in function :func:`open` or by :func:`popen` or
Georg Brandl012408c2009-05-22 09:43:17 +0000802 :func:`fdopen`, or :data:`sys.stdin`, use its :meth:`~file.read` or
803 :meth:`~file.readline` methods.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000804
805
806.. function:: tcgetpgrp(fd)
807
808 Return the process group associated with the terminal given by *fd* (an open
Benjamin Peterson328e3772010-05-06 22:49:28 +0000809 file descriptor as returned by :func:`os.open`).
810
811 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000812
813
814.. function:: tcsetpgrp(fd, pg)
815
816 Set the process group associated with the terminal given by *fd* (an open file
Benjamin Peterson328e3772010-05-06 22:49:28 +0000817 descriptor as returned by :func:`os.open`) to *pg*.
818
819 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000820
821
822.. function:: ttyname(fd)
823
824 Return a string which specifies the terminal device associated with
Georg Brandlbb75e4e2007-10-21 10:46:24 +0000825 file descriptor *fd*. If *fd* is not associated with a terminal device, an
Benjamin Peterson328e3772010-05-06 22:49:28 +0000826 exception is raised.
827
828 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000829
830
831.. function:: write(fd, str)
832
833 Write the string *str* to file descriptor *fd*. Return the number of bytes
Benjamin Peterson328e3772010-05-06 22:49:28 +0000834 actually written.
835
836 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000837
838 .. note::
839
840 This function is intended for low-level I/O and must be applied to a file
Georg Brandl012408c2009-05-22 09:43:17 +0000841 descriptor as returned by :func:`os.open` or :func:`pipe`. To write a "file
Georg Brandl8ec7f652007-08-15 14:28:01 +0000842 object" returned by the built-in function :func:`open` or by :func:`popen` or
Georg Brandl012408c2009-05-22 09:43:17 +0000843 :func:`fdopen`, or :data:`sys.stdout` or :data:`sys.stderr`, use its
844 :meth:`~file.write` method.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000845
Georg Brandl6c50efe2010-04-14 13:50:31 +0000846
847.. _open-constants:
848
849``open()`` flag constants
850~~~~~~~~~~~~~~~~~~~~~~~~~
851
Georg Brandl0c880bd2008-12-05 08:02:17 +0000852The following constants are options for the *flags* parameter to the
Georg Brandl012408c2009-05-22 09:43:17 +0000853:func:`~os.open` function. They can be combined using the bitwise OR operator
Georg Brandl0c880bd2008-12-05 08:02:17 +0000854``|``. Some of them are not available on all platforms. For descriptions of
Georg Brandle70ff4b2008-12-05 09:25:32 +0000855their availability and use, consult the :manpage:`open(2)` manual page on Unix
Doug Hellmann1d18b5b2009-09-20 20:44:13 +0000856or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000857
858
859.. data:: O_RDONLY
860 O_WRONLY
861 O_RDWR
862 O_APPEND
863 O_CREAT
864 O_EXCL
865 O_TRUNC
866
Georg Brandl0c880bd2008-12-05 08:02:17 +0000867 These constants are available on Unix and Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000868
869
870.. data:: O_DSYNC
871 O_RSYNC
872 O_SYNC
873 O_NDELAY
874 O_NONBLOCK
875 O_NOCTTY
876 O_SHLOCK
877 O_EXLOCK
878
Georg Brandl0c880bd2008-12-05 08:02:17 +0000879 These constants are only available on Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000880
881
882.. data:: O_BINARY
Georg Brandlb67da6e2007-11-24 13:56:09 +0000883 O_NOINHERIT
Georg Brandl8ec7f652007-08-15 14:28:01 +0000884 O_SHORT_LIVED
885 O_TEMPORARY
886 O_RANDOM
887 O_SEQUENTIAL
888 O_TEXT
889
Georg Brandl0c880bd2008-12-05 08:02:17 +0000890 These constants are only available on Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000891
892
Georg Brandlae6b9f32008-05-16 13:41:26 +0000893.. data:: O_ASYNC
894 O_DIRECT
Georg Brandlb67da6e2007-11-24 13:56:09 +0000895 O_DIRECTORY
896 O_NOFOLLOW
897 O_NOATIME
898
Georg Brandl0c880bd2008-12-05 08:02:17 +0000899 These constants are GNU extensions and not present if they are not defined by
900 the C library.
Georg Brandlb67da6e2007-11-24 13:56:09 +0000901
902
Georg Brandl8ec7f652007-08-15 14:28:01 +0000903.. _os-file-dir:
904
905Files and Directories
906---------------------
907
Georg Brandl8ec7f652007-08-15 14:28:01 +0000908.. function:: access(path, mode)
909
910 Use the real uid/gid to test for access to *path*. Note that most operations
911 will use the effective uid/gid, therefore this routine can be used in a
912 suid/sgid environment to test if the invoking user has the specified access to
913 *path*. *mode* should be :const:`F_OK` to test the existence of *path*, or it
914 can be the inclusive OR of one or more of :const:`R_OK`, :const:`W_OK`, and
915 :const:`X_OK` to test permissions. Return :const:`True` if access is allowed,
916 :const:`False` if not. See the Unix man page :manpage:`access(2)` for more
Benjamin Peterson328e3772010-05-06 22:49:28 +0000917 information.
918
919 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000920
921 .. note::
922
Georg Brandl9fa61bb2009-07-26 14:19:57 +0000923 Using :func:`access` to check if a user is authorized to e.g. open a file
924 before actually doing so using :func:`open` creates a security hole,
925 because the user might exploit the short time interval between checking
Benjamin Peterson30e10d82011-05-20 11:41:13 -0500926 and opening the file to manipulate it. It's preferable to use :term:`EAFP`
927 techniques. For example::
928
929 if os.access("myfile", os.R_OK):
930 with open("myfile") as fp:
931 return fp.read()
932 return "some default data"
933
934 is better written as::
935
936 try:
937 fp = open("myfile")
Benjamin Petersonce77def2011-05-20 11:49:06 -0500938 except IOError as e:
Ezio Melotti5e30fa52011-10-20 19:49:29 +0300939 if e.errno == errno.EACCES:
Benjamin Peterson30e10d82011-05-20 11:41:13 -0500940 return "some default data"
941 # Not a permission error.
942 raise
943 else:
944 with fp:
945 return fp.read()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000946
947 .. note::
948
949 I/O operations may fail even when :func:`access` indicates that they would
950 succeed, particularly for operations on network filesystems which may have
951 permissions semantics beyond the usual POSIX permission-bit model.
952
953
954.. data:: F_OK
955
956 Value to pass as the *mode* parameter of :func:`access` to test the existence of
957 *path*.
958
959
960.. data:: R_OK
961
962 Value to include in the *mode* parameter of :func:`access` to test the
963 readability of *path*.
964
965
966.. data:: W_OK
967
968 Value to include in the *mode* parameter of :func:`access` to test the
969 writability of *path*.
970
971
972.. data:: X_OK
973
974 Value to include in the *mode* parameter of :func:`access` to determine if
975 *path* can be executed.
976
977
978.. function:: chdir(path)
979
980 .. index:: single: directory; changing
981
Benjamin Peterson328e3772010-05-06 22:49:28 +0000982 Change the current working directory to *path*.
983
984 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000985
986
987.. function:: fchdir(fd)
988
989 Change the current working directory to the directory represented by the file
990 descriptor *fd*. The descriptor must refer to an opened directory, not an open
Benjamin Peterson328e3772010-05-06 22:49:28 +0000991 file.
992
993 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000994
995 .. versionadded:: 2.3
996
997
998.. function:: getcwd()
999
Benjamin Peterson328e3772010-05-06 22:49:28 +00001000 Return a string representing the current working directory.
1001
1002 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001003
1004
1005.. function:: getcwdu()
1006
1007 Return a Unicode object representing the current working directory.
Benjamin Peterson328e3772010-05-06 22:49:28 +00001008
Georg Brandl9af94982008-09-13 17:41:16 +00001009 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001010
1011 .. versionadded:: 2.3
1012
1013
1014.. function:: chflags(path, flags)
1015
1016 Set the flags of *path* to the numeric *flags*. *flags* may take a combination
1017 (bitwise OR) of the following values (as defined in the :mod:`stat` module):
1018
R David Murrayefd8bab2011-03-10 17:57:35 -05001019 * :data:`stat.UF_NODUMP`
1020 * :data:`stat.UF_IMMUTABLE`
1021 * :data:`stat.UF_APPEND`
1022 * :data:`stat.UF_OPAQUE`
1023 * :data:`stat.UF_NOUNLINK`
Ned Deily43e10542011-06-27 23:41:53 -07001024 * :data:`stat.UF_COMPRESSED`
1025 * :data:`stat.UF_HIDDEN`
R David Murrayefd8bab2011-03-10 17:57:35 -05001026 * :data:`stat.SF_ARCHIVED`
1027 * :data:`stat.SF_IMMUTABLE`
1028 * :data:`stat.SF_APPEND`
1029 * :data:`stat.SF_NOUNLINK`
1030 * :data:`stat.SF_SNAPSHOT`
Georg Brandl8ec7f652007-08-15 14:28:01 +00001031
Georg Brandl9af94982008-09-13 17:41:16 +00001032 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001033
1034 .. versionadded:: 2.6
1035
1036
1037.. function:: chroot(path)
1038
1039 Change the root directory of the current process to *path*. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00001040 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001041
1042 .. versionadded:: 2.2
1043
1044
1045.. function:: chmod(path, mode)
1046
1047 Change the mode of *path* to the numeric *mode*. *mode* may take one of the
Georg Brandlf725b952008-01-05 19:44:22 +00001048 following values (as defined in the :mod:`stat` module) or bitwise ORed
Georg Brandl8ec7f652007-08-15 14:28:01 +00001049 combinations of them:
1050
1051
R. David Murrayfbba7cd2009-07-02 18:19:20 +00001052 * :data:`stat.S_ISUID`
1053 * :data:`stat.S_ISGID`
1054 * :data:`stat.S_ENFMT`
1055 * :data:`stat.S_ISVTX`
1056 * :data:`stat.S_IREAD`
1057 * :data:`stat.S_IWRITE`
1058 * :data:`stat.S_IEXEC`
1059 * :data:`stat.S_IRWXU`
1060 * :data:`stat.S_IRUSR`
1061 * :data:`stat.S_IWUSR`
1062 * :data:`stat.S_IXUSR`
1063 * :data:`stat.S_IRWXG`
1064 * :data:`stat.S_IRGRP`
1065 * :data:`stat.S_IWGRP`
1066 * :data:`stat.S_IXGRP`
1067 * :data:`stat.S_IRWXO`
1068 * :data:`stat.S_IROTH`
1069 * :data:`stat.S_IWOTH`
1070 * :data:`stat.S_IXOTH`
Georg Brandl8ec7f652007-08-15 14:28:01 +00001071
Georg Brandl9af94982008-09-13 17:41:16 +00001072 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001073
1074 .. note::
1075
1076 Although Windows supports :func:`chmod`, you can only set the file's read-only
1077 flag with it (via the ``stat.S_IWRITE`` and ``stat.S_IREAD``
1078 constants or a corresponding integer value). All other bits are
1079 ignored.
1080
1081
1082.. function:: chown(path, uid, gid)
1083
1084 Change the owner and group id of *path* to the numeric *uid* and *gid*. To leave
Benjamin Peterson328e3772010-05-06 22:49:28 +00001085 one of the ids unchanged, set it to -1.
1086
1087 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001088
1089
1090.. function:: lchflags(path, flags)
1091
1092 Set the flags of *path* to the numeric *flags*, like :func:`chflags`, but do not
Benjamin Peterson328e3772010-05-06 22:49:28 +00001093 follow symbolic links.
1094
1095 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001096
1097 .. versionadded:: 2.6
1098
1099
Georg Brandl81ddc1a2007-11-30 22:04:45 +00001100.. function:: lchmod(path, mode)
1101
1102 Change the mode of *path* to the numeric *mode*. If path is a symlink, this
1103 affects the symlink rather than the target. See the docs for :func:`chmod`
Benjamin Peterson328e3772010-05-06 22:49:28 +00001104 for possible values of *mode*.
1105
1106 Availability: Unix.
Georg Brandl81ddc1a2007-11-30 22:04:45 +00001107
1108 .. versionadded:: 2.6
1109
1110
Georg Brandl8ec7f652007-08-15 14:28:01 +00001111.. function:: lchown(path, uid, gid)
1112
Georg Brandlf725b952008-01-05 19:44:22 +00001113 Change the owner and group id of *path* to the numeric *uid* and *gid*. This
Benjamin Peterson328e3772010-05-06 22:49:28 +00001114 function will not follow symbolic links.
1115
1116 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001117
1118 .. versionadded:: 2.3
1119
1120
Benjamin Peterson0e928582009-03-28 19:16:10 +00001121.. function:: link(source, link_name)
Georg Brandl8ec7f652007-08-15 14:28:01 +00001122
Benjamin Peterson328e3772010-05-06 22:49:28 +00001123 Create a hard link pointing to *source* named *link_name*.
1124
1125 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001126
1127
1128.. function:: listdir(path)
1129
Georg Brandl62342912008-11-24 19:56:47 +00001130 Return a list containing the names of the entries in the directory given by
1131 *path*. The list is in arbitrary order. It does not include the special
1132 entries ``'.'`` and ``'..'`` even if they are present in the
Benjamin Peterson328e3772010-05-06 22:49:28 +00001133 directory.
1134
1135 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001136
1137 .. versionchanged:: 2.3
1138 On Windows NT/2k/XP and Unix, if *path* is a Unicode object, the result will be
Georg Brandld933cc22009-05-16 11:21:29 +00001139 a list of Unicode objects. Undecodable filenames will still be returned as
1140 string objects.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001141
1142
1143.. function:: lstat(path)
1144
Sandro Tosi98ed08f2012-01-14 16:42:02 +01001145 Perform the equivalent of an :c:func:`lstat` system call on the given path.
R. David Murray561b96f2011-02-11 17:25:54 +00001146 Similar to :func:`~os.stat`, but does not follow symbolic links. On
1147 platforms that do not support symbolic links, this is an alias for
1148 :func:`~os.stat`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001149
1150
1151.. function:: mkfifo(path[, mode])
1152
1153 Create a FIFO (a named pipe) named *path* with numeric mode *mode*. The default
1154 *mode* is ``0666`` (octal). The current umask value is first masked out from
Benjamin Peterson328e3772010-05-06 22:49:28 +00001155 the mode.
1156
1157 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001158
1159 FIFOs are pipes that can be accessed like regular files. FIFOs exist until they
1160 are deleted (for example with :func:`os.unlink`). Generally, FIFOs are used as
1161 rendezvous between "client" and "server" type processes: the server opens the
1162 FIFO for reading, and the client opens it for writing. Note that :func:`mkfifo`
1163 doesn't open the FIFO --- it just creates the rendezvous point.
1164
1165
1166.. function:: mknod(filename[, mode=0600, device])
1167
1168 Create a filesystem node (file, device special file or named pipe) named
1169 *filename*. *mode* specifies both the permissions to use and the type of node to
1170 be created, being combined (bitwise OR) with one of ``stat.S_IFREG``,
1171 ``stat.S_IFCHR``, ``stat.S_IFBLK``,
1172 and ``stat.S_IFIFO`` (those constants are available in :mod:`stat`).
1173 For ``stat.S_IFCHR`` and
1174 ``stat.S_IFBLK``, *device* defines the newly created device special file (probably using
1175 :func:`os.makedev`), otherwise it is ignored.
1176
1177 .. versionadded:: 2.3
1178
1179
1180.. function:: major(device)
1181
Georg Brandlf725b952008-01-05 19:44:22 +00001182 Extract the device major number from a raw device number (usually the
Sandro Tosi98ed08f2012-01-14 16:42:02 +01001183 :attr:`st_dev` or :attr:`st_rdev` field from :c:type:`stat`).
Georg Brandl8ec7f652007-08-15 14:28:01 +00001184
1185 .. versionadded:: 2.3
1186
1187
1188.. function:: minor(device)
1189
Georg Brandlf725b952008-01-05 19:44:22 +00001190 Extract the device minor number from a raw device number (usually the
Sandro Tosi98ed08f2012-01-14 16:42:02 +01001191 :attr:`st_dev` or :attr:`st_rdev` field from :c:type:`stat`).
Georg Brandl8ec7f652007-08-15 14:28:01 +00001192
1193 .. versionadded:: 2.3
1194
1195
1196.. function:: makedev(major, minor)
1197
Georg Brandlf725b952008-01-05 19:44:22 +00001198 Compose a raw device number from the major and minor device numbers.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001199
1200 .. versionadded:: 2.3
1201
1202
1203.. function:: mkdir(path[, mode])
1204
1205 Create a directory named *path* with numeric mode *mode*. The default *mode* is
1206 ``0777`` (octal). On some systems, *mode* is ignored. Where it is used, the
Georg Brandlab776ce2010-06-12 06:28:58 +00001207 current umask value is first masked out. If the directory already exists,
1208 :exc:`OSError` is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001209
Mark Summerfieldac3d4292007-11-02 08:24:59 +00001210 It is also possible to create temporary directories; see the
1211 :mod:`tempfile` module's :func:`tempfile.mkdtemp` function.
1212
Benjamin Peterson328e3772010-05-06 22:49:28 +00001213 Availability: Unix, Windows.
1214
Georg Brandl8ec7f652007-08-15 14:28:01 +00001215
1216.. function:: makedirs(path[, mode])
1217
1218 .. index::
1219 single: directory; creating
1220 single: UNC paths; and os.makedirs()
1221
1222 Recursive directory creation function. Like :func:`mkdir`, but makes all
Éric Araujo4c8d6b62010-11-30 17:53:45 +00001223 intermediate-level directories needed to contain the leaf directory. Raises an
Georg Brandl8ec7f652007-08-15 14:28:01 +00001224 :exc:`error` exception if the leaf directory already exists or cannot be
1225 created. The default *mode* is ``0777`` (octal). On some systems, *mode* is
1226 ignored. Where it is used, the current umask value is first masked out.
1227
1228 .. note::
1229
1230 :func:`makedirs` will become confused if the path elements to create include
Georg Brandlf725b952008-01-05 19:44:22 +00001231 :data:`os.pardir`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001232
1233 .. versionadded:: 1.5.2
1234
1235 .. versionchanged:: 2.3
1236 This function now handles UNC paths correctly.
1237
1238
1239.. function:: pathconf(path, name)
1240
1241 Return system configuration information relevant to a named file. *name*
1242 specifies the configuration value to retrieve; it may be a string which is the
1243 name of a defined system value; these names are specified in a number of
1244 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
1245 additional names as well. The names known to the host operating system are
1246 given in the ``pathconf_names`` dictionary. For configuration variables not
1247 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001248
1249 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1250 specific value for *name* is not supported by the host system, even if it is
1251 included in ``pathconf_names``, an :exc:`OSError` is raised with
1252 :const:`errno.EINVAL` for the error number.
1253
Benjamin Peterson328e3772010-05-06 22:49:28 +00001254 Availability: Unix.
1255
Georg Brandl8ec7f652007-08-15 14:28:01 +00001256
1257.. data:: pathconf_names
1258
1259 Dictionary mapping names accepted by :func:`pathconf` and :func:`fpathconf` to
1260 the integer values defined for those names by the host operating system. This
1261 can be used to determine the set of names known to the system. Availability:
Georg Brandl9af94982008-09-13 17:41:16 +00001262 Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001263
1264
1265.. function:: readlink(path)
1266
1267 Return a string representing the path to which the symbolic link points. The
1268 result may be either an absolute or relative pathname; if it is relative, it may
1269 be converted to an absolute pathname using ``os.path.join(os.path.dirname(path),
1270 result)``.
1271
1272 .. versionchanged:: 2.6
1273 If the *path* is a Unicode object the result will also be a Unicode object.
1274
Georg Brandl9af94982008-09-13 17:41:16 +00001275 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001276
1277
1278.. function:: remove(path)
1279
Georg Brandl75439972009-08-24 17:24:27 +00001280 Remove (delete) the file *path*. If *path* is a directory, :exc:`OSError` is
1281 raised; see :func:`rmdir` below to remove a directory. This is identical to
1282 the :func:`unlink` function documented below. On Windows, attempting to
1283 remove a file that is in use causes an exception to be raised; on Unix, the
1284 directory entry is removed but the storage allocated to the file is not made
Benjamin Peterson328e3772010-05-06 22:49:28 +00001285 available until the original file is no longer in use.
1286
1287 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001288
1289
1290.. function:: removedirs(path)
1291
1292 .. index:: single: directory; deleting
1293
Georg Brandlf725b952008-01-05 19:44:22 +00001294 Remove directories recursively. Works like :func:`rmdir` except that, if the
Georg Brandl8ec7f652007-08-15 14:28:01 +00001295 leaf directory is successfully removed, :func:`removedirs` tries to
1296 successively remove every parent directory mentioned in *path* until an error
1297 is raised (which is ignored, because it generally means that a parent directory
1298 is not empty). For example, ``os.removedirs('foo/bar/baz')`` will first remove
1299 the directory ``'foo/bar/baz'``, and then remove ``'foo/bar'`` and ``'foo'`` if
1300 they are empty. Raises :exc:`OSError` if the leaf directory could not be
1301 successfully removed.
1302
1303 .. versionadded:: 1.5.2
1304
1305
1306.. function:: rename(src, dst)
1307
1308 Rename the file or directory *src* to *dst*. If *dst* is a directory,
1309 :exc:`OSError` will be raised. On Unix, if *dst* exists and is a file, it will
Georg Brandlf725b952008-01-05 19:44:22 +00001310 be replaced silently if the user has permission. The operation may fail on some
Georg Brandl8ec7f652007-08-15 14:28:01 +00001311 Unix flavors if *src* and *dst* are on different filesystems. If successful,
1312 the renaming will be an atomic operation (this is a POSIX requirement). On
1313 Windows, if *dst* already exists, :exc:`OSError` will be raised even if it is a
1314 file; there may be no way to implement an atomic rename when *dst* names an
Benjamin Peterson328e3772010-05-06 22:49:28 +00001315 existing file.
1316
1317 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001318
1319
1320.. function:: renames(old, new)
1321
1322 Recursive directory or file renaming function. Works like :func:`rename`, except
1323 creation of any intermediate directories needed to make the new pathname good is
1324 attempted first. After the rename, directories corresponding to rightmost path
1325 segments of the old name will be pruned away using :func:`removedirs`.
1326
1327 .. versionadded:: 1.5.2
1328
1329 .. note::
1330
1331 This function can fail with the new directory structure made if you lack
1332 permissions needed to remove the leaf directory or file.
1333
1334
1335.. function:: rmdir(path)
1336
Georg Brandl1b2695a2009-08-24 17:48:40 +00001337 Remove (delete) the directory *path*. Only works when the directory is
1338 empty, otherwise, :exc:`OSError` is raised. In order to remove whole
Benjamin Peterson328e3772010-05-06 22:49:28 +00001339 directory trees, :func:`shutil.rmtree` can be used.
1340
1341 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001342
1343
1344.. function:: stat(path)
1345
Sandro Tosi98ed08f2012-01-14 16:42:02 +01001346 Perform the equivalent of a :c:func:`stat` system call on the given path.
R. David Murray561b96f2011-02-11 17:25:54 +00001347 (This function follows symlinks; to stat a symlink use :func:`lstat`.)
Georg Brandl8ec7f652007-08-15 14:28:01 +00001348
R. David Murray561b96f2011-02-11 17:25:54 +00001349 The return value is an object whose attributes correspond to the members
Sandro Tosi98ed08f2012-01-14 16:42:02 +01001350 of the :c:type:`stat` structure, namely:
R. David Murray561b96f2011-02-11 17:25:54 +00001351
1352 * :attr:`st_mode` - protection bits,
1353 * :attr:`st_ino` - inode number,
1354 * :attr:`st_dev` - device,
1355 * :attr:`st_nlink` - number of hard links,
1356 * :attr:`st_uid` - user id of owner,
1357 * :attr:`st_gid` - group id of owner,
1358 * :attr:`st_size` - size of file, in bytes,
1359 * :attr:`st_atime` - time of most recent access,
1360 * :attr:`st_mtime` - time of most recent content modification,
1361 * :attr:`st_ctime` - platform dependent; time of most recent metadata change on
1362 Unix, or the time of creation on Windows)
Georg Brandl8ec7f652007-08-15 14:28:01 +00001363
1364 .. versionchanged:: 2.3
Georg Brandlf725b952008-01-05 19:44:22 +00001365 If :func:`stat_float_times` returns ``True``, the time values are floats, measuring
Georg Brandl8ec7f652007-08-15 14:28:01 +00001366 seconds. Fractions of a second may be reported if the system supports that. On
1367 Mac OS, the times are always floats. See :func:`stat_float_times` for further
1368 discussion.
1369
1370 On some Unix systems (such as Linux), the following attributes may also be
R. David Murray561b96f2011-02-11 17:25:54 +00001371 available:
1372
1373 * :attr:`st_blocks` - number of blocks allocated for file
1374 * :attr:`st_blksize` - filesystem blocksize
1375 * :attr:`st_rdev` - type of device if an inode device
1376 * :attr:`st_flags` - user defined flags for file
Georg Brandl8ec7f652007-08-15 14:28:01 +00001377
1378 On other Unix systems (such as FreeBSD), the following attributes may be
R. David Murray561b96f2011-02-11 17:25:54 +00001379 available (but may be only filled out if root tries to use them):
1380
1381 * :attr:`st_gen` - file generation number
1382 * :attr:`st_birthtime` - time of file creation
Georg Brandl8ec7f652007-08-15 14:28:01 +00001383
1384 On Mac OS systems, the following attributes may also be available:
Georg Brandl8ec7f652007-08-15 14:28:01 +00001385
R. David Murray561b96f2011-02-11 17:25:54 +00001386 * :attr:`st_rsize`
1387 * :attr:`st_creator`
1388 * :attr:`st_type`
Georg Brandl8ec7f652007-08-15 14:28:01 +00001389
R. David Murray561b96f2011-02-11 17:25:54 +00001390 On RISCOS systems, the following attributes are also available:
Georg Brandl8ec7f652007-08-15 14:28:01 +00001391
R. David Murray561b96f2011-02-11 17:25:54 +00001392 * :attr:`st_ftype` (file type)
1393 * :attr:`st_attrs` (attributes)
1394 * :attr:`st_obtype` (object type).
Georg Brandl8ec7f652007-08-15 14:28:01 +00001395
1396 .. note::
1397
Senthil Kumaran6f18b982011-07-04 12:50:02 -07001398 The exact meaning and resolution of the :attr:`st_atime`,
1399 :attr:`st_mtime`, and :attr:`st_ctime` attributes depend on the operating
1400 system and the file system. For example, on Windows systems using the FAT
1401 or FAT32 file systems, :attr:`st_mtime` has 2-second resolution, and
1402 :attr:`st_atime` has only 1-day resolution. See your operating system
1403 documentation for details.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001404
R. David Murray561b96f2011-02-11 17:25:54 +00001405 For backward compatibility, the return value of :func:`~os.stat` is also accessible
1406 as a tuple of at least 10 integers giving the most important (and portable)
Sandro Tosi98ed08f2012-01-14 16:42:02 +01001407 members of the :c:type:`stat` structure, in the order :attr:`st_mode`,
R. David Murray561b96f2011-02-11 17:25:54 +00001408 :attr:`st_ino`, :attr:`st_dev`, :attr:`st_nlink`, :attr:`st_uid`,
1409 :attr:`st_gid`, :attr:`st_size`, :attr:`st_atime`, :attr:`st_mtime`,
1410 :attr:`st_ctime`. More items may be added at the end by some implementations.
1411
1412 .. index:: module: stat
1413
1414 The standard module :mod:`stat` defines functions and constants that are useful
Sandro Tosi98ed08f2012-01-14 16:42:02 +01001415 for extracting information from a :c:type:`stat` structure. (On Windows, some
R. David Murray561b96f2011-02-11 17:25:54 +00001416 items are filled with dummy values.)
1417
1418 Example::
1419
1420 >>> import os
1421 >>> statinfo = os.stat('somefile.txt')
1422 >>> statinfo
1423 (33188, 422511, 769, 1, 1032, 100, 926, 1105022698,1105022732, 1105022732)
1424 >>> statinfo.st_size
1425 926
1426
Georg Brandl9af94982008-09-13 17:41:16 +00001427 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001428
1429 .. versionchanged:: 2.2
1430 Added access to values as attributes of the returned object.
1431
1432 .. versionchanged:: 2.5
Georg Brandlf725b952008-01-05 19:44:22 +00001433 Added :attr:`st_gen` and :attr:`st_birthtime`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001434
1435
1436.. function:: stat_float_times([newvalue])
1437
1438 Determine whether :class:`stat_result` represents time stamps as float objects.
R. David Murray561b96f2011-02-11 17:25:54 +00001439 If *newvalue* is ``True``, future calls to :func:`~os.stat` return floats, if it is
Georg Brandl8ec7f652007-08-15 14:28:01 +00001440 ``False``, future calls return ints. If *newvalue* is omitted, return the
1441 current setting.
1442
1443 For compatibility with older Python versions, accessing :class:`stat_result` as
1444 a tuple always returns integers.
1445
1446 .. versionchanged:: 2.5
1447 Python now returns float values by default. Applications which do not work
1448 correctly with floating point time stamps can use this function to restore the
1449 old behaviour.
1450
1451 The resolution of the timestamps (that is the smallest possible fraction)
1452 depends on the system. Some systems only support second resolution; on these
1453 systems, the fraction will always be zero.
1454
1455 It is recommended that this setting is only changed at program startup time in
1456 the *__main__* module; libraries should never change this setting. If an
1457 application uses a library that works incorrectly if floating point time stamps
1458 are processed, this application should turn the feature off until the library
1459 has been corrected.
1460
1461
1462.. function:: statvfs(path)
1463
Sandro Tosi98ed08f2012-01-14 16:42:02 +01001464 Perform a :c:func:`statvfs` system call on the given path. The return value is
Georg Brandl8ec7f652007-08-15 14:28:01 +00001465 an object whose attributes describe the filesystem on the given path, and
Sandro Tosi98ed08f2012-01-14 16:42:02 +01001466 correspond to the members of the :c:type:`statvfs` structure, namely:
Georg Brandl8ec7f652007-08-15 14:28:01 +00001467 :attr:`f_bsize`, :attr:`f_frsize`, :attr:`f_blocks`, :attr:`f_bfree`,
1468 :attr:`f_bavail`, :attr:`f_files`, :attr:`f_ffree`, :attr:`f_favail`,
Benjamin Peterson328e3772010-05-06 22:49:28 +00001469 :attr:`f_flag`, :attr:`f_namemax`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001470
1471 .. index:: module: statvfs
1472
1473 For backward compatibility, the return value is also accessible as a tuple whose
1474 values correspond to the attributes, in the order given above. The standard
1475 module :mod:`statvfs` defines constants that are useful for extracting
Sandro Tosi98ed08f2012-01-14 16:42:02 +01001476 information from a :c:type:`statvfs` structure when accessing it as a sequence;
Georg Brandl8ec7f652007-08-15 14:28:01 +00001477 this remains useful when writing code that needs to work with versions of Python
1478 that don't support accessing the fields as attributes.
1479
Benjamin Peterson328e3772010-05-06 22:49:28 +00001480 Availability: Unix.
1481
Georg Brandl8ec7f652007-08-15 14:28:01 +00001482 .. versionchanged:: 2.2
1483 Added access to values as attributes of the returned object.
1484
1485
Benjamin Peterson0e928582009-03-28 19:16:10 +00001486.. function:: symlink(source, link_name)
Georg Brandl8ec7f652007-08-15 14:28:01 +00001487
Benjamin Peterson328e3772010-05-06 22:49:28 +00001488 Create a symbolic link pointing to *source* named *link_name*.
1489
1490 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001491
1492
1493.. function:: tempnam([dir[, prefix]])
1494
1495 Return a unique path name that is reasonable for creating a temporary file.
1496 This will be an absolute path that names a potential directory entry in the
1497 directory *dir* or a common location for temporary files if *dir* is omitted or
1498 ``None``. If given and not ``None``, *prefix* is used to provide a short prefix
1499 to the filename. Applications are responsible for properly creating and
1500 managing files created using paths returned by :func:`tempnam`; no automatic
1501 cleanup is provided. On Unix, the environment variable :envvar:`TMPDIR`
Georg Brandlf725b952008-01-05 19:44:22 +00001502 overrides *dir*, while on Windows :envvar:`TMP` is used. The specific
Georg Brandl8ec7f652007-08-15 14:28:01 +00001503 behavior of this function depends on the C library implementation; some aspects
1504 are underspecified in system documentation.
1505
1506 .. warning::
1507
1508 Use of :func:`tempnam` is vulnerable to symlink attacks; consider using
1509 :func:`tmpfile` (section :ref:`os-newstreams`) instead.
1510
Georg Brandl9af94982008-09-13 17:41:16 +00001511 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001512
1513
1514.. function:: tmpnam()
1515
1516 Return a unique path name that is reasonable for creating a temporary file.
1517 This will be an absolute path that names a potential directory entry in a common
1518 location for temporary files. Applications are responsible for properly
1519 creating and managing files created using paths returned by :func:`tmpnam`; no
1520 automatic cleanup is provided.
1521
1522 .. warning::
1523
1524 Use of :func:`tmpnam` is vulnerable to symlink attacks; consider using
1525 :func:`tmpfile` (section :ref:`os-newstreams`) instead.
1526
1527 Availability: Unix, Windows. This function probably shouldn't be used on
1528 Windows, though: Microsoft's implementation of :func:`tmpnam` always creates a
1529 name in the root directory of the current drive, and that's generally a poor
1530 location for a temp file (depending on privileges, you may not even be able to
1531 open a file using this name).
1532
1533
1534.. data:: TMP_MAX
1535
1536 The maximum number of unique names that :func:`tmpnam` will generate before
1537 reusing names.
1538
1539
1540.. function:: unlink(path)
1541
Georg Brandl75439972009-08-24 17:24:27 +00001542 Remove (delete) the file *path*. This is the same function as
1543 :func:`remove`; the :func:`unlink` name is its traditional Unix
Benjamin Peterson328e3772010-05-06 22:49:28 +00001544 name.
1545
1546 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001547
1548
1549.. function:: utime(path, times)
1550
Benjamin Peterson5b02ef32008-08-16 03:13:07 +00001551 Set the access and modified times of the file specified by *path*. If *times*
1552 is ``None``, then the file's access and modified times are set to the current
1553 time. (The effect is similar to running the Unix program :program:`touch` on
1554 the path.) Otherwise, *times* must be a 2-tuple of numbers, of the form
1555 ``(atime, mtime)`` which is used to set the access and modified times,
1556 respectively. Whether a directory can be given for *path* depends on whether
1557 the operating system implements directories as files (for example, Windows
1558 does not). Note that the exact times you set here may not be returned by a
R. David Murray561b96f2011-02-11 17:25:54 +00001559 subsequent :func:`~os.stat` call, depending on the resolution with which your
1560 operating system records access and modification times; see :func:`~os.stat`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001561
1562 .. versionchanged:: 2.0
1563 Added support for ``None`` for *times*.
1564
Georg Brandl9af94982008-09-13 17:41:16 +00001565 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001566
1567
1568.. function:: walk(top[, topdown=True [, onerror=None[, followlinks=False]]])
1569
1570 .. index::
1571 single: directory; walking
1572 single: directory; traversal
1573
Georg Brandlf725b952008-01-05 19:44:22 +00001574 Generate the file names in a directory tree by walking the tree
1575 either top-down or bottom-up. For each directory in the tree rooted at directory
Georg Brandl8ec7f652007-08-15 14:28:01 +00001576 *top* (including *top* itself), it yields a 3-tuple ``(dirpath, dirnames,
1577 filenames)``.
1578
1579 *dirpath* is a string, the path to the directory. *dirnames* is a list of the
1580 names of the subdirectories in *dirpath* (excluding ``'.'`` and ``'..'``).
1581 *filenames* is a list of the names of the non-directory files in *dirpath*.
1582 Note that the names in the lists contain no path components. To get a full path
1583 (which begins with *top*) to a file or directory in *dirpath*, do
1584 ``os.path.join(dirpath, name)``.
1585
Georg Brandlf725b952008-01-05 19:44:22 +00001586 If optional argument *topdown* is ``True`` or not specified, the triple for a
Georg Brandl8ec7f652007-08-15 14:28:01 +00001587 directory is generated before the triples for any of its subdirectories
Georg Brandlf725b952008-01-05 19:44:22 +00001588 (directories are generated top-down). If *topdown* is ``False``, the triple for a
Georg Brandl8ec7f652007-08-15 14:28:01 +00001589 directory is generated after the triples for all of its subdirectories
Georg Brandlf725b952008-01-05 19:44:22 +00001590 (directories are generated bottom-up).
Georg Brandl8ec7f652007-08-15 14:28:01 +00001591
Georg Brandlf725b952008-01-05 19:44:22 +00001592 When *topdown* is ``True``, the caller can modify the *dirnames* list in-place
Georg Brandl8ec7f652007-08-15 14:28:01 +00001593 (perhaps using :keyword:`del` or slice assignment), and :func:`walk` will only
1594 recurse into the subdirectories whose names remain in *dirnames*; this can be
1595 used to prune the search, impose a specific order of visiting, or even to inform
1596 :func:`walk` about directories the caller creates or renames before it resumes
Georg Brandlf725b952008-01-05 19:44:22 +00001597 :func:`walk` again. Modifying *dirnames* when *topdown* is ``False`` is
Georg Brandl8ec7f652007-08-15 14:28:01 +00001598 ineffective, because in bottom-up mode the directories in *dirnames* are
1599 generated before *dirpath* itself is generated.
1600
Ezio Melotti086f9272011-10-18 13:02:11 +03001601 By default, errors from the :func:`listdir` call are ignored. If optional
Georg Brandl8ec7f652007-08-15 14:28:01 +00001602 argument *onerror* is specified, it should be a function; it will be called with
1603 one argument, an :exc:`OSError` instance. It can report the error to continue
1604 with the walk, or raise the exception to abort the walk. Note that the filename
1605 is available as the ``filename`` attribute of the exception object.
1606
1607 By default, :func:`walk` will not walk down into symbolic links that resolve to
Georg Brandlf725b952008-01-05 19:44:22 +00001608 directories. Set *followlinks* to ``True`` to visit directories pointed to by
Georg Brandl8ec7f652007-08-15 14:28:01 +00001609 symlinks, on systems that support them.
1610
1611 .. versionadded:: 2.6
1612 The *followlinks* parameter.
1613
1614 .. note::
1615
Georg Brandlf725b952008-01-05 19:44:22 +00001616 Be aware that setting *followlinks* to ``True`` can lead to infinite recursion if a
Georg Brandl8ec7f652007-08-15 14:28:01 +00001617 link points to a parent directory of itself. :func:`walk` does not keep track of
1618 the directories it visited already.
1619
1620 .. note::
1621
1622 If you pass a relative pathname, don't change the current working directory
1623 between resumptions of :func:`walk`. :func:`walk` never changes the current
1624 directory, and assumes that its caller doesn't either.
1625
1626 This example displays the number of bytes taken by non-directory files in each
1627 directory under the starting directory, except that it doesn't look under any
1628 CVS subdirectory::
1629
1630 import os
1631 from os.path import join, getsize
1632 for root, dirs, files in os.walk('python/Lib/email'):
1633 print root, "consumes",
1634 print sum(getsize(join(root, name)) for name in files),
1635 print "bytes in", len(files), "non-directory files"
1636 if 'CVS' in dirs:
1637 dirs.remove('CVS') # don't visit CVS directories
1638
Georg Brandlf725b952008-01-05 19:44:22 +00001639 In the next example, walking the tree bottom-up is essential: :func:`rmdir`
Georg Brandl8ec7f652007-08-15 14:28:01 +00001640 doesn't allow deleting a directory before the directory is empty::
1641
Georg Brandlf725b952008-01-05 19:44:22 +00001642 # Delete everything reachable from the directory named in "top",
Georg Brandl8ec7f652007-08-15 14:28:01 +00001643 # assuming there are no symbolic links.
1644 # CAUTION: This is dangerous! For example, if top == '/', it
1645 # could delete all your disk files.
1646 import os
1647 for root, dirs, files in os.walk(top, topdown=False):
1648 for name in files:
1649 os.remove(os.path.join(root, name))
1650 for name in dirs:
1651 os.rmdir(os.path.join(root, name))
1652
1653 .. versionadded:: 2.3
1654
1655
1656.. _os-process:
1657
1658Process Management
1659------------------
1660
1661These functions may be used to create and manage processes.
1662
1663The various :func:`exec\*` functions take a list of arguments for the new
1664program loaded into the process. In each case, the first of these arguments is
1665passed to the new program as its own name rather than as an argument a user may
1666have typed on a command line. For the C programmer, this is the ``argv[0]``
Sandro Tosi98ed08f2012-01-14 16:42:02 +01001667passed to a program's :c:func:`main`. For example, ``os.execv('/bin/echo',
Georg Brandl8ec7f652007-08-15 14:28:01 +00001668['foo', 'bar'])`` will only print ``bar`` on standard output; ``foo`` will seem
1669to be ignored.
1670
1671
1672.. function:: abort()
1673
1674 Generate a :const:`SIGABRT` signal to the current process. On Unix, the default
1675 behavior is to produce a core dump; on Windows, the process immediately returns
Victor Stinner8703be92011-07-08 02:14:55 +02001676 an exit code of ``3``. Be aware that calling this function will not call the
1677 Python signal handler registered for :const:`SIGABRT` with
1678 :func:`signal.signal`.
Benjamin Peterson328e3772010-05-06 22:49:28 +00001679
Georg Brandl9af94982008-09-13 17:41:16 +00001680 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001681
1682
1683.. function:: execl(path, arg0, arg1, ...)
1684 execle(path, arg0, arg1, ..., env)
1685 execlp(file, arg0, arg1, ...)
1686 execlpe(file, arg0, arg1, ..., env)
1687 execv(path, args)
1688 execve(path, args, env)
1689 execvp(file, args)
1690 execvpe(file, args, env)
1691
1692 These functions all execute a new program, replacing the current process; they
1693 do not return. On Unix, the new executable is loaded into the current process,
Georg Brandlf725b952008-01-05 19:44:22 +00001694 and will have the same process id as the caller. Errors will be reported as
Georg Brandlc62ef8b2009-01-03 20:55:06 +00001695 :exc:`OSError` exceptions.
Andrew M. Kuchlingac771662008-09-28 00:15:27 +00001696
1697 The current process is replaced immediately. Open file objects and
1698 descriptors are not flushed, so if there may be data buffered
1699 on these open files, you should flush them using
1700 :func:`sys.stdout.flush` or :func:`os.fsync` before calling an
1701 :func:`exec\*` function.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001702
Georg Brandlf725b952008-01-05 19:44:22 +00001703 The "l" and "v" variants of the :func:`exec\*` functions differ in how
1704 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl8ec7f652007-08-15 14:28:01 +00001705 to work with if the number of parameters is fixed when the code is written; the
1706 individual parameters simply become additional parameters to the :func:`execl\*`
Georg Brandlf725b952008-01-05 19:44:22 +00001707 functions. The "v" variants are good when the number of parameters is
Georg Brandl8ec7f652007-08-15 14:28:01 +00001708 variable, with the arguments being passed in a list or tuple as the *args*
1709 parameter. In either case, the arguments to the child process should start with
1710 the name of the command being run, but this is not enforced.
1711
Georg Brandlf725b952008-01-05 19:44:22 +00001712 The variants which include a "p" near the end (:func:`execlp`,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001713 :func:`execlpe`, :func:`execvp`, and :func:`execvpe`) will use the
1714 :envvar:`PATH` environment variable to locate the program *file*. When the
1715 environment is being replaced (using one of the :func:`exec\*e` variants,
1716 discussed in the next paragraph), the new environment is used as the source of
1717 the :envvar:`PATH` variable. The other variants, :func:`execl`, :func:`execle`,
1718 :func:`execv`, and :func:`execve`, will not use the :envvar:`PATH` variable to
1719 locate the executable; *path* must contain an appropriate absolute or relative
1720 path.
1721
1722 For :func:`execle`, :func:`execlpe`, :func:`execve`, and :func:`execvpe` (note
Georg Brandlf725b952008-01-05 19:44:22 +00001723 that these all end in "e"), the *env* parameter must be a mapping which is
Georg Brandlfb246c42008-04-19 16:58:28 +00001724 used to define the environment variables for the new process (these are used
1725 instead of the current process' environment); the functions :func:`execl`,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001726 :func:`execlp`, :func:`execv`, and :func:`execvp` all cause the new process to
Georg Brandlc62ef8b2009-01-03 20:55:06 +00001727 inherit the environment of the current process.
Andrew M. Kuchlingac771662008-09-28 00:15:27 +00001728
1729 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001730
1731
1732.. function:: _exit(n)
1733
Georg Brandlb8d0e362010-11-26 07:53:50 +00001734 Exit the process with status *n*, without calling cleanup handlers, flushing
Benjamin Peterson328e3772010-05-06 22:49:28 +00001735 stdio buffers, etc.
1736
1737 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001738
1739 .. note::
1740
Georg Brandlb8d0e362010-11-26 07:53:50 +00001741 The standard way to exit is ``sys.exit(n)``. :func:`_exit` should
1742 normally only be used in the child process after a :func:`fork`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001743
Georg Brandlf725b952008-01-05 19:44:22 +00001744The following exit codes are defined and can be used with :func:`_exit`,
Georg Brandl8ec7f652007-08-15 14:28:01 +00001745although they are not required. These are typically used for system programs
1746written in Python, such as a mail server's external command delivery program.
1747
1748.. note::
1749
1750 Some of these may not be available on all Unix platforms, since there is some
1751 variation. These constants are defined where they are defined by the underlying
1752 platform.
1753
1754
1755.. data:: EX_OK
1756
Benjamin Peterson328e3772010-05-06 22:49:28 +00001757 Exit code that means no error occurred.
1758
1759 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001760
1761 .. versionadded:: 2.3
1762
1763
1764.. data:: EX_USAGE
1765
1766 Exit code that means the command was used incorrectly, such as when the wrong
Benjamin Peterson328e3772010-05-06 22:49:28 +00001767 number of arguments are given.
1768
1769 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001770
1771 .. versionadded:: 2.3
1772
1773
1774.. data:: EX_DATAERR
1775
Benjamin Peterson328e3772010-05-06 22:49:28 +00001776 Exit code that means the input data was incorrect.
1777
1778 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001779
1780 .. versionadded:: 2.3
1781
1782
1783.. data:: EX_NOINPUT
1784
1785 Exit code that means an input file did not exist or was not readable.
Benjamin Peterson328e3772010-05-06 22:49:28 +00001786
Georg Brandl9af94982008-09-13 17:41:16 +00001787 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001788
1789 .. versionadded:: 2.3
1790
1791
1792.. data:: EX_NOUSER
1793
Benjamin Peterson328e3772010-05-06 22:49:28 +00001794 Exit code that means a specified user did not exist.
1795
1796 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001797
1798 .. versionadded:: 2.3
1799
1800
1801.. data:: EX_NOHOST
1802
Benjamin Peterson328e3772010-05-06 22:49:28 +00001803 Exit code that means a specified host did not exist.
1804
1805 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001806
1807 .. versionadded:: 2.3
1808
1809
1810.. data:: EX_UNAVAILABLE
1811
Benjamin Peterson328e3772010-05-06 22:49:28 +00001812 Exit code that means that a required service is unavailable.
1813
1814 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001815
1816 .. versionadded:: 2.3
1817
1818
1819.. data:: EX_SOFTWARE
1820
Benjamin Peterson328e3772010-05-06 22:49:28 +00001821 Exit code that means an internal software error was detected.
1822
1823 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001824
1825 .. versionadded:: 2.3
1826
1827
1828.. data:: EX_OSERR
1829
1830 Exit code that means an operating system error was detected, such as the
Benjamin Peterson328e3772010-05-06 22:49:28 +00001831 inability to fork or create a pipe.
1832
1833 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001834
1835 .. versionadded:: 2.3
1836
1837
1838.. data:: EX_OSFILE
1839
1840 Exit code that means some system file did not exist, could not be opened, or had
Benjamin Peterson328e3772010-05-06 22:49:28 +00001841 some other kind of error.
1842
1843 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001844
1845 .. versionadded:: 2.3
1846
1847
1848.. data:: EX_CANTCREAT
1849
1850 Exit code that means a user specified output file could not be created.
Benjamin Peterson328e3772010-05-06 22:49:28 +00001851
Georg Brandl9af94982008-09-13 17:41:16 +00001852 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001853
1854 .. versionadded:: 2.3
1855
1856
1857.. data:: EX_IOERR
1858
1859 Exit code that means that an error occurred while doing I/O on some file.
Benjamin Peterson328e3772010-05-06 22:49:28 +00001860
Georg Brandl9af94982008-09-13 17:41:16 +00001861 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001862
1863 .. versionadded:: 2.3
1864
1865
1866.. data:: EX_TEMPFAIL
1867
1868 Exit code that means a temporary failure occurred. This indicates something
1869 that may not really be an error, such as a network connection that couldn't be
Benjamin Peterson328e3772010-05-06 22:49:28 +00001870 made during a retryable operation.
1871
1872 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001873
1874 .. versionadded:: 2.3
1875
1876
1877.. data:: EX_PROTOCOL
1878
1879 Exit code that means that a protocol exchange was illegal, invalid, or not
Benjamin Peterson328e3772010-05-06 22:49:28 +00001880 understood.
1881
1882 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001883
1884 .. versionadded:: 2.3
1885
1886
1887.. data:: EX_NOPERM
1888
1889 Exit code that means that there were insufficient permissions to perform the
Benjamin Peterson328e3772010-05-06 22:49:28 +00001890 operation (but not intended for file system problems).
1891
1892 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001893
1894 .. versionadded:: 2.3
1895
1896
1897.. data:: EX_CONFIG
1898
1899 Exit code that means that some kind of configuration error occurred.
Benjamin Peterson328e3772010-05-06 22:49:28 +00001900
Georg Brandl9af94982008-09-13 17:41:16 +00001901 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001902
1903 .. versionadded:: 2.3
1904
1905
1906.. data:: EX_NOTFOUND
1907
Benjamin Peterson328e3772010-05-06 22:49:28 +00001908 Exit code that means something like "an entry was not found".
1909
1910 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001911
1912 .. versionadded:: 2.3
1913
1914
1915.. function:: fork()
1916
Georg Brandlf725b952008-01-05 19:44:22 +00001917 Fork a child process. Return ``0`` in the child and the child's process id in the
Skip Montanaro75e51682008-03-15 02:32:49 +00001918 parent. If an error occurs :exc:`OSError` is raised.
Gregory P. Smith08067492008-09-30 20:41:13 +00001919
1920 Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have
1921 known issues when using fork() from a thread.
1922
Georg Brandl9af94982008-09-13 17:41:16 +00001923 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001924
1925
1926.. function:: forkpty()
1927
1928 Fork a child process, using a new pseudo-terminal as the child's controlling
1929 terminal. Return a pair of ``(pid, fd)``, where *pid* is ``0`` in the child, the
1930 new child's process id in the parent, and *fd* is the file descriptor of the
1931 master end of the pseudo-terminal. For a more portable approach, use the
Skip Montanaro75e51682008-03-15 02:32:49 +00001932 :mod:`pty` module. If an error occurs :exc:`OSError` is raised.
Benjamin Peterson328e3772010-05-06 22:49:28 +00001933
Georg Brandl9af94982008-09-13 17:41:16 +00001934 Availability: some flavors of Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001935
1936
1937.. function:: kill(pid, sig)
1938
1939 .. index::
1940 single: process; killing
1941 single: process; signalling
1942
1943 Send signal *sig* to the process *pid*. Constants for the specific signals
1944 available on the host platform are defined in the :mod:`signal` module.
Brian Curtine5aa8862010-04-02 23:26:06 +00001945
1946 Windows: The :data:`signal.CTRL_C_EVENT` and
1947 :data:`signal.CTRL_BREAK_EVENT` signals are special signals which can
1948 only be sent to console processes which share a common console window,
1949 e.g., some subprocesses. Any other value for *sig* will cause the process
1950 to be unconditionally killed by the TerminateProcess API, and the exit code
1951 will be set to *sig*. The Windows version of :func:`kill` additionally takes
1952 process handles to be killed.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001953
Brian Curtin1f8dd362010-04-20 15:23:18 +00001954 .. versionadded:: 2.7 Windows support
1955
Georg Brandl8ec7f652007-08-15 14:28:01 +00001956
1957.. function:: killpg(pgid, sig)
1958
1959 .. index::
1960 single: process; killing
1961 single: process; signalling
1962
Benjamin Peterson328e3772010-05-06 22:49:28 +00001963 Send the signal *sig* to the process group *pgid*.
1964
1965 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001966
1967 .. versionadded:: 2.3
1968
1969
1970.. function:: nice(increment)
1971
1972 Add *increment* to the process's "niceness". Return the new niceness.
Benjamin Peterson328e3772010-05-06 22:49:28 +00001973
Georg Brandl9af94982008-09-13 17:41:16 +00001974 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001975
1976
1977.. function:: plock(op)
1978
1979 Lock program segments into memory. The value of *op* (defined in
Benjamin Peterson328e3772010-05-06 22:49:28 +00001980 ``<sys/lock.h>``) determines which segments are locked.
1981
1982 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001983
1984
1985.. function:: popen(...)
1986 popen2(...)
1987 popen3(...)
1988 popen4(...)
1989 :noindex:
1990
1991 Run child processes, returning opened pipes for communications. These functions
1992 are described in section :ref:`os-newstreams`.
1993
1994
1995.. function:: spawnl(mode, path, ...)
1996 spawnle(mode, path, ..., env)
1997 spawnlp(mode, file, ...)
1998 spawnlpe(mode, file, ..., env)
1999 spawnv(mode, path, args)
2000 spawnve(mode, path, args, env)
2001 spawnvp(mode, file, args)
2002 spawnvpe(mode, file, args, env)
2003
2004 Execute the program *path* in a new process.
2005
2006 (Note that the :mod:`subprocess` module provides more powerful facilities for
2007 spawning new processes and retrieving their results; using that module is
R. David Murrayccb9d4b2009-06-09 00:44:22 +00002008 preferable to using these functions. Check especially the
2009 :ref:`subprocess-replacements` section.)
Georg Brandl8ec7f652007-08-15 14:28:01 +00002010
Georg Brandlf725b952008-01-05 19:44:22 +00002011 If *mode* is :const:`P_NOWAIT`, this function returns the process id of the new
Georg Brandl8ec7f652007-08-15 14:28:01 +00002012 process; if *mode* is :const:`P_WAIT`, returns the process's exit code if it
2013 exits normally, or ``-signal``, where *signal* is the signal that killed the
Georg Brandlf725b952008-01-05 19:44:22 +00002014 process. On Windows, the process id will actually be the process handle, so can
Georg Brandl8ec7f652007-08-15 14:28:01 +00002015 be used with the :func:`waitpid` function.
2016
Georg Brandlf725b952008-01-05 19:44:22 +00002017 The "l" and "v" variants of the :func:`spawn\*` functions differ in how
2018 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl8ec7f652007-08-15 14:28:01 +00002019 to work with if the number of parameters is fixed when the code is written; the
2020 individual parameters simply become additional parameters to the
Georg Brandlf725b952008-01-05 19:44:22 +00002021 :func:`spawnl\*` functions. The "v" variants are good when the number of
Georg Brandl8ec7f652007-08-15 14:28:01 +00002022 parameters is variable, with the arguments being passed in a list or tuple as
2023 the *args* parameter. In either case, the arguments to the child process must
2024 start with the name of the command being run.
2025
Georg Brandlf725b952008-01-05 19:44:22 +00002026 The variants which include a second "p" near the end (:func:`spawnlp`,
Georg Brandl8ec7f652007-08-15 14:28:01 +00002027 :func:`spawnlpe`, :func:`spawnvp`, and :func:`spawnvpe`) will use the
2028 :envvar:`PATH` environment variable to locate the program *file*. When the
2029 environment is being replaced (using one of the :func:`spawn\*e` variants,
2030 discussed in the next paragraph), the new environment is used as the source of
2031 the :envvar:`PATH` variable. The other variants, :func:`spawnl`,
2032 :func:`spawnle`, :func:`spawnv`, and :func:`spawnve`, will not use the
2033 :envvar:`PATH` variable to locate the executable; *path* must contain an
2034 appropriate absolute or relative path.
2035
2036 For :func:`spawnle`, :func:`spawnlpe`, :func:`spawnve`, and :func:`spawnvpe`
Georg Brandlf725b952008-01-05 19:44:22 +00002037 (note that these all end in "e"), the *env* parameter must be a mapping
Georg Brandlfb246c42008-04-19 16:58:28 +00002038 which is used to define the environment variables for the new process (they are
2039 used instead of the current process' environment); the functions
Georg Brandl8ec7f652007-08-15 14:28:01 +00002040 :func:`spawnl`, :func:`spawnlp`, :func:`spawnv`, and :func:`spawnvp` all cause
Georg Brandl22717df2009-03-31 18:26:55 +00002041 the new process to inherit the environment of the current process. Note that
2042 keys and values in the *env* dictionary must be strings; invalid keys or
2043 values will cause the function to fail, with a return value of ``127``.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002044
2045 As an example, the following calls to :func:`spawnlp` and :func:`spawnvpe` are
2046 equivalent::
2047
2048 import os
2049 os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
2050
2051 L = ['cp', 'index.html', '/dev/null']
2052 os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
2053
2054 Availability: Unix, Windows. :func:`spawnlp`, :func:`spawnlpe`, :func:`spawnvp`
Antoine Pitrou711cb582011-07-19 01:26:58 +02002055 and :func:`spawnvpe` are not available on Windows. :func:`spawnle` and
2056 :func:`spawnve` are not thread-safe on Windows; we advise you to use the
2057 :mod:`subprocess` module instead.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002058
2059 .. versionadded:: 1.6
2060
2061
2062.. data:: P_NOWAIT
2063 P_NOWAITO
2064
2065 Possible values for the *mode* parameter to the :func:`spawn\*` family of
2066 functions. If either of these values is given, the :func:`spawn\*` functions
Georg Brandlf725b952008-01-05 19:44:22 +00002067 will return as soon as the new process has been created, with the process id as
Benjamin Peterson328e3772010-05-06 22:49:28 +00002068 the return value.
2069
2070 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002071
2072 .. versionadded:: 1.6
2073
2074
2075.. data:: P_WAIT
2076
2077 Possible value for the *mode* parameter to the :func:`spawn\*` family of
2078 functions. If this is given as *mode*, the :func:`spawn\*` functions will not
2079 return until the new process has run to completion and will return the exit code
2080 of the process the run is successful, or ``-signal`` if a signal kills the
Benjamin Peterson328e3772010-05-06 22:49:28 +00002081 process.
2082
2083 Availability: Unix, Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002084
2085 .. versionadded:: 1.6
2086
2087
2088.. data:: P_DETACH
2089 P_OVERLAY
2090
2091 Possible values for the *mode* parameter to the :func:`spawn\*` family of
2092 functions. These are less portable than those listed above. :const:`P_DETACH`
2093 is similar to :const:`P_NOWAIT`, but the new process is detached from the
2094 console of the calling process. If :const:`P_OVERLAY` is used, the current
2095 process will be replaced; the :func:`spawn\*` function will not return.
Benjamin Peterson328e3772010-05-06 22:49:28 +00002096
Georg Brandl8ec7f652007-08-15 14:28:01 +00002097 Availability: Windows.
2098
2099 .. versionadded:: 1.6
2100
2101
2102.. function:: startfile(path[, operation])
2103
2104 Start a file with its associated application.
2105
2106 When *operation* is not specified or ``'open'``, this acts like double-clicking
2107 the file in Windows Explorer, or giving the file name as an argument to the
2108 :program:`start` command from the interactive command shell: the file is opened
2109 with whatever application (if any) its extension is associated.
2110
2111 When another *operation* is given, it must be a "command verb" that specifies
2112 what should be done with the file. Common verbs documented by Microsoft are
2113 ``'print'`` and ``'edit'`` (to be used on files) as well as ``'explore'`` and
2114 ``'find'`` (to be used on directories).
2115
2116 :func:`startfile` returns as soon as the associated application is launched.
2117 There is no option to wait for the application to close, and no way to retrieve
2118 the application's exit status. The *path* parameter is relative to the current
2119 directory. If you want to use an absolute path, make sure the first character
Sandro Tosi98ed08f2012-01-14 16:42:02 +01002120 is not a slash (``'/'``); the underlying Win32 :c:func:`ShellExecute` function
Georg Brandl8ec7f652007-08-15 14:28:01 +00002121 doesn't work if it is. Use the :func:`os.path.normpath` function to ensure that
Benjamin Peterson328e3772010-05-06 22:49:28 +00002122 the path is properly encoded for Win32.
2123
2124 Availability: Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002125
2126 .. versionadded:: 2.0
2127
2128 .. versionadded:: 2.5
2129 The *operation* parameter.
2130
2131
2132.. function:: system(command)
2133
2134 Execute the command (a string) in a subshell. This is implemented by calling
Sandro Tosi98ed08f2012-01-14 16:42:02 +01002135 the Standard C function :c:func:`system`, and has the same limitations.
Georg Brandl11abfe62009-10-18 07:58:12 +00002136 Changes to :data:`sys.stdin`, etc. are not reflected in the environment of the
Georg Brandl647e9d22009-10-14 15:57:46 +00002137 executed command.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002138
2139 On Unix, the return value is the exit status of the process encoded in the
2140 format specified for :func:`wait`. Note that POSIX does not specify the meaning
Sandro Tosi98ed08f2012-01-14 16:42:02 +01002141 of the return value of the C :c:func:`system` function, so the return value of
Georg Brandl8ec7f652007-08-15 14:28:01 +00002142 the Python function is system-dependent.
2143
2144 On Windows, the return value is that returned by the system shell after running
2145 *command*, given by the Windows environment variable :envvar:`COMSPEC`: on
2146 :program:`command.com` systems (Windows 95, 98 and ME) this is always ``0``; on
2147 :program:`cmd.exe` systems (Windows NT, 2000 and XP) this is the exit status of
2148 the command run; on systems using a non-native shell, consult your shell
2149 documentation.
2150
Georg Brandl8ec7f652007-08-15 14:28:01 +00002151 The :mod:`subprocess` module provides more powerful facilities for spawning new
2152 processes and retrieving their results; using that module is preferable to using
Andrew M. Kuchlingfdf94c52010-07-26 13:42:35 +00002153 this function. See the
2154 :ref:`subprocess-replacements` section in the :mod:`subprocess` documentation
2155 for some helpful recipes.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002156
Benjamin Peterson328e3772010-05-06 22:49:28 +00002157 Availability: Unix, Windows.
2158
Georg Brandl8ec7f652007-08-15 14:28:01 +00002159
2160.. function:: times()
2161
Benjamin Peterson328e3772010-05-06 22:49:28 +00002162 Return a 5-tuple of floating point numbers indicating accumulated (processor
2163 or other) times, in seconds. The items are: user time, system time,
2164 children's user time, children's system time, and elapsed real time since a
2165 fixed point in the past, in that order. See the Unix manual page
2166 :manpage:`times(2)` or the corresponding Windows Platform API documentation.
2167 On Windows, only the first two items are filled, the others are zero.
2168
2169 Availability: Unix, Windows
Georg Brandl8ec7f652007-08-15 14:28:01 +00002170
2171
2172.. function:: wait()
2173
2174 Wait for completion of a child process, and return a tuple containing its pid
2175 and exit status indication: a 16-bit number, whose low byte is the signal number
2176 that killed the process, and whose high byte is the exit status (if the signal
2177 number is zero); the high bit of the low byte is set if a core file was
Benjamin Peterson328e3772010-05-06 22:49:28 +00002178 produced.
2179
2180 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002181
2182
2183.. function:: waitpid(pid, options)
2184
2185 The details of this function differ on Unix and Windows.
2186
2187 On Unix: Wait for completion of a child process given by process id *pid*, and
2188 return a tuple containing its process id and exit status indication (encoded as
2189 for :func:`wait`). The semantics of the call are affected by the value of the
2190 integer *options*, which should be ``0`` for normal operation.
2191
2192 If *pid* is greater than ``0``, :func:`waitpid` requests status information for
2193 that specific process. If *pid* is ``0``, the request is for the status of any
2194 child in the process group of the current process. If *pid* is ``-1``, the
2195 request pertains to any child of the current process. If *pid* is less than
2196 ``-1``, status is requested for any process in the process group ``-pid`` (the
2197 absolute value of *pid*).
2198
Gregory P. Smith59de7f52008-08-15 23:14:00 +00002199 An :exc:`OSError` is raised with the value of errno when the syscall
2200 returns -1.
2201
Georg Brandl8ec7f652007-08-15 14:28:01 +00002202 On Windows: Wait for completion of a process given by process handle *pid*, and
2203 return a tuple containing *pid*, and its exit status shifted left by 8 bits
2204 (shifting makes cross-platform use of the function easier). A *pid* less than or
2205 equal to ``0`` has no special meaning on Windows, and raises an exception. The
2206 value of integer *options* has no effect. *pid* can refer to any process whose
2207 id is known, not necessarily a child process. The :func:`spawn` functions called
2208 with :const:`P_NOWAIT` return suitable process handles.
2209
2210
2211.. function:: wait3([options])
2212
2213 Similar to :func:`waitpid`, except no process id argument is given and a
2214 3-element tuple containing the child's process id, exit status indication, and
2215 resource usage information is returned. Refer to :mod:`resource`.\
2216 :func:`getrusage` for details on resource usage information. The option
2217 argument is the same as that provided to :func:`waitpid` and :func:`wait4`.
Benjamin Peterson328e3772010-05-06 22:49:28 +00002218
Georg Brandl8ec7f652007-08-15 14:28:01 +00002219 Availability: Unix.
2220
2221 .. versionadded:: 2.5
2222
2223
2224.. function:: wait4(pid, options)
2225
2226 Similar to :func:`waitpid`, except a 3-element tuple, containing the child's
2227 process id, exit status indication, and resource usage information is returned.
2228 Refer to :mod:`resource`.\ :func:`getrusage` for details on resource usage
2229 information. The arguments to :func:`wait4` are the same as those provided to
Benjamin Peterson328e3772010-05-06 22:49:28 +00002230 :func:`waitpid`.
2231
2232 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002233
2234 .. versionadded:: 2.5
2235
2236
2237.. data:: WNOHANG
2238
2239 The option for :func:`waitpid` to return immediately if no child process status
2240 is available immediately. The function returns ``(0, 0)`` in this case.
Benjamin Peterson328e3772010-05-06 22:49:28 +00002241
Georg Brandl9af94982008-09-13 17:41:16 +00002242 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002243
2244
2245.. data:: WCONTINUED
2246
2247 This option causes child processes to be reported if they have been continued
Benjamin Peterson328e3772010-05-06 22:49:28 +00002248 from a job control stop since their status was last reported.
2249
2250 Availability: Some Unix systems.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002251
2252 .. versionadded:: 2.3
2253
2254
2255.. data:: WUNTRACED
2256
2257 This option causes child processes to be reported if they have been stopped but
Benjamin Peterson328e3772010-05-06 22:49:28 +00002258 their current state has not been reported since they were stopped.
2259
2260 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002261
2262 .. versionadded:: 2.3
2263
2264The following functions take a process status code as returned by
2265:func:`system`, :func:`wait`, or :func:`waitpid` as a parameter. They may be
2266used to determine the disposition of a process.
2267
2268
2269.. function:: WCOREDUMP(status)
2270
Georg Brandlf725b952008-01-05 19:44:22 +00002271 Return ``True`` if a core dump was generated for the process, otherwise
Benjamin Peterson328e3772010-05-06 22:49:28 +00002272 return ``False``.
2273
2274 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002275
2276 .. versionadded:: 2.3
2277
2278
2279.. function:: WIFCONTINUED(status)
2280
Georg Brandlf725b952008-01-05 19:44:22 +00002281 Return ``True`` if the process has been continued from a job control stop,
Benjamin Peterson328e3772010-05-06 22:49:28 +00002282 otherwise return ``False``.
2283
2284 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002285
2286 .. versionadded:: 2.3
2287
2288
2289.. function:: WIFSTOPPED(status)
2290
Georg Brandlf725b952008-01-05 19:44:22 +00002291 Return ``True`` if the process has been stopped, otherwise return
Benjamin Peterson328e3772010-05-06 22:49:28 +00002292 ``False``.
2293
2294 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002295
2296
2297.. function:: WIFSIGNALED(status)
2298
Georg Brandlf725b952008-01-05 19:44:22 +00002299 Return ``True`` if the process exited due to a signal, otherwise return
Benjamin Peterson328e3772010-05-06 22:49:28 +00002300 ``False``.
2301
2302 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002303
2304
2305.. function:: WIFEXITED(status)
2306
Georg Brandlf725b952008-01-05 19:44:22 +00002307 Return ``True`` if the process exited using the :manpage:`exit(2)` system call,
Benjamin Peterson328e3772010-05-06 22:49:28 +00002308 otherwise return ``False``.
2309
2310 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002311
2312
2313.. function:: WEXITSTATUS(status)
2314
2315 If ``WIFEXITED(status)`` is true, return the integer parameter to the
2316 :manpage:`exit(2)` system call. Otherwise, the return value is meaningless.
Benjamin Peterson328e3772010-05-06 22:49:28 +00002317
Georg Brandl9af94982008-09-13 17:41:16 +00002318 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002319
2320
2321.. function:: WSTOPSIG(status)
2322
Benjamin Peterson328e3772010-05-06 22:49:28 +00002323 Return the signal which caused the process to stop.
2324
2325 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002326
2327
2328.. function:: WTERMSIG(status)
2329
Benjamin Peterson328e3772010-05-06 22:49:28 +00002330 Return the signal which caused the process to exit.
2331
2332 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002333
2334
2335.. _os-path:
2336
2337Miscellaneous System Information
2338--------------------------------
2339
2340
2341.. function:: confstr(name)
2342
2343 Return string-valued system configuration values. *name* specifies the
2344 configuration value to retrieve; it may be a string which is the name of a
2345 defined system value; these names are specified in a number of standards (POSIX,
2346 Unix 95, Unix 98, and others). Some platforms define additional names as well.
2347 The names known to the host operating system are given as the keys of the
2348 ``confstr_names`` dictionary. For configuration variables not included in that
Benjamin Peterson328e3772010-05-06 22:49:28 +00002349 mapping, passing an integer for *name* is also accepted.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002350
2351 If the configuration value specified by *name* isn't defined, ``None`` is
2352 returned.
2353
2354 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
2355 specific value for *name* is not supported by the host system, even if it is
2356 included in ``confstr_names``, an :exc:`OSError` is raised with
2357 :const:`errno.EINVAL` for the error number.
2358
Benjamin Peterson328e3772010-05-06 22:49:28 +00002359 Availability: Unix
2360
Georg Brandl8ec7f652007-08-15 14:28:01 +00002361
2362.. data:: confstr_names
2363
2364 Dictionary mapping names accepted by :func:`confstr` to the integer values
2365 defined for those names by the host operating system. This can be used to
Benjamin Peterson328e3772010-05-06 22:49:28 +00002366 determine the set of names known to the system.
2367
2368 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002369
2370
2371.. function:: getloadavg()
2372
Georg Brandl57fe0f22008-01-12 10:53:29 +00002373 Return the number of processes in the system run queue averaged over the last
2374 1, 5, and 15 minutes or raises :exc:`OSError` if the load average was
Benjamin Peterson328e3772010-05-06 22:49:28 +00002375 unobtainable.
2376
2377 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002378
2379 .. versionadded:: 2.3
2380
2381
2382.. function:: sysconf(name)
2383
2384 Return integer-valued system configuration values. If the configuration value
2385 specified by *name* isn't defined, ``-1`` is returned. The comments regarding
2386 the *name* parameter for :func:`confstr` apply here as well; the dictionary that
2387 provides information on the known names is given by ``sysconf_names``.
Benjamin Peterson328e3772010-05-06 22:49:28 +00002388
Georg Brandl9af94982008-09-13 17:41:16 +00002389 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002390
2391
2392.. data:: sysconf_names
2393
2394 Dictionary mapping names accepted by :func:`sysconf` to the integer values
2395 defined for those names by the host operating system. This can be used to
Benjamin Peterson328e3772010-05-06 22:49:28 +00002396 determine the set of names known to the system.
2397
2398 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002399
Georg Brandlf725b952008-01-05 19:44:22 +00002400The following data values are used to support path manipulation operations. These
Georg Brandl8ec7f652007-08-15 14:28:01 +00002401are defined for all platforms.
2402
2403Higher-level operations on pathnames are defined in the :mod:`os.path` module.
2404
2405
2406.. data:: curdir
2407
2408 The constant string used by the operating system to refer to the current
Georg Brandl9af94982008-09-13 17:41:16 +00002409 directory. This is ``'.'`` for Windows and POSIX. Also available via
2410 :mod:`os.path`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002411
2412
2413.. data:: pardir
2414
2415 The constant string used by the operating system to refer to the parent
Georg Brandl9af94982008-09-13 17:41:16 +00002416 directory. This is ``'..'`` for Windows and POSIX. Also available via
2417 :mod:`os.path`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002418
2419
2420.. data:: sep
2421
Georg Brandl9af94982008-09-13 17:41:16 +00002422 The character used by the operating system to separate pathname components.
2423 This is ``'/'`` for POSIX and ``'\\'`` for Windows. Note that knowing this
2424 is not sufficient to be able to parse or concatenate pathnames --- use
Georg Brandl8ec7f652007-08-15 14:28:01 +00002425 :func:`os.path.split` and :func:`os.path.join` --- but it is occasionally
2426 useful. Also available via :mod:`os.path`.
2427
2428
2429.. data:: altsep
2430
2431 An alternative character used by the operating system to separate pathname
2432 components, or ``None`` if only one separator character exists. This is set to
2433 ``'/'`` on Windows systems where ``sep`` is a backslash. Also available via
2434 :mod:`os.path`.
2435
2436
2437.. data:: extsep
2438
2439 The character which separates the base filename from the extension; for example,
2440 the ``'.'`` in :file:`os.py`. Also available via :mod:`os.path`.
2441
2442 .. versionadded:: 2.2
2443
2444
2445.. data:: pathsep
2446
2447 The character conventionally used by the operating system to separate search
2448 path components (as in :envvar:`PATH`), such as ``':'`` for POSIX or ``';'`` for
2449 Windows. Also available via :mod:`os.path`.
2450
2451
2452.. data:: defpath
2453
2454 The default search path used by :func:`exec\*p\*` and :func:`spawn\*p\*` if the
2455 environment doesn't have a ``'PATH'`` key. Also available via :mod:`os.path`.
2456
2457
2458.. data:: linesep
2459
2460 The string used to separate (or, rather, terminate) lines on the current
Georg Brandl9af94982008-09-13 17:41:16 +00002461 platform. This may be a single character, such as ``'\n'`` for POSIX, or
2462 multiple characters, for example, ``'\r\n'`` for Windows. Do not use
2463 *os.linesep* as a line terminator when writing files opened in text mode (the
2464 default); use a single ``'\n'`` instead, on all platforms.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002465
2466
2467.. data:: devnull
2468
Georg Brandlfa0fdb82010-05-21 22:03:29 +00002469 The file path of the null device. For example: ``'/dev/null'`` for
2470 POSIX, ``'nul'`` for Windows. Also available via :mod:`os.path`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002471
2472 .. versionadded:: 2.4
2473
2474
2475.. _os-miscfunc:
2476
2477Miscellaneous Functions
2478-----------------------
2479
2480
2481.. function:: urandom(n)
2482
2483 Return a string of *n* random bytes suitable for cryptographic use.
2484
2485 This function returns random bytes from an OS-specific randomness source. The
2486 returned data should be unpredictable enough for cryptographic applications,
2487 though its exact quality depends on the OS implementation. On a UNIX-like
2488 system this will query /dev/urandom, and on Windows it will use CryptGenRandom.
2489 If a randomness source is not found, :exc:`NotImplementedError` will be raised.
2490
2491 .. versionadded:: 2.4
2492