blob: 6ad4785e2094221ed5bd968f2bc6990b92037ca0 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`os` --- Miscellaneous operating system interfaces
2=======================================================
3
4.. module:: os
5 :synopsis: Miscellaneous operating system interfaces.
6
7
Christian Heimesa62da1d2008-01-12 19:39:10 +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 Brandl116aa622007-08-15 14:28:22 +000015
Benjamin Peterson1baf4652009-12-31 03:11:23 +000016Notes on the availability of these functions:
Georg Brandl116aa622007-08-15 14:28:22 +000017
Benjamin Peterson1baf4652009-12-31 03:11:23 +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 Brandl116aa622007-08-15 14:28:22 +000023
Benjamin Peterson1baf4652009-12-31 03:11:23 +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 Brandl116aa622007-08-15 14:28:22 +000027
Benjamin Peterson1baf4652009-12-31 03:11:23 +000028* All functions accepting path or file names accept both bytes and string
29 objects, and result in an object of the same type, if a path or file name is
30 returned.
Georg Brandl76e55382008-10-08 16:34:57 +000031
32.. note::
33
Georg Brandlc575c902008-09-13 17:46:05 +000034 If not separately noted, all functions that claim "Availability: Unix" are
35 supported on Mac OS X, which builds on a Unix core.
36
Benjamin Peterson1baf4652009-12-31 03:11:23 +000037* An "Availability: Unix" note means that this function is commonly found on
38 Unix systems. It does not make any claims about its existence on a specific
39 operating system.
40
41* If not separately noted, all functions that claim "Availability: Unix" are
42 supported on Mac OS X, which builds on a Unix core.
43
Georg Brandlc575c902008-09-13 17:46:05 +000044.. note::
45
Christian Heimesa62da1d2008-01-12 19:39:10 +000046 All functions in this module raise :exc:`OSError` in the case of invalid or
47 inaccessible file names and paths, or other arguments that have the correct
48 type, but are not accepted by the operating system.
Georg Brandl116aa622007-08-15 14:28:22 +000049
Georg Brandl116aa622007-08-15 14:28:22 +000050.. exception:: error
51
Christian Heimesa62da1d2008-01-12 19:39:10 +000052 An alias for the built-in :exc:`OSError` exception.
Georg Brandl116aa622007-08-15 14:28:22 +000053
54
55.. data:: name
56
Benjamin Peterson1baf4652009-12-31 03:11:23 +000057 The name of the operating system dependent module imported. The following
58 names have currently been registered: ``'posix'``, ``'nt'``, ``'mac'``,
59 ``'os2'``, ``'ce'``, ``'java'``.
Georg Brandl116aa622007-08-15 14:28:22 +000060
61
Martin v. Löwis011e8422009-05-05 04:43:17 +000062.. _os-filenames:
63
64File Names, Command Line Arguments, and Environment Variables
65-------------------------------------------------------------
66
67In Python, file names, command line arguments, and environment
68variables are represented using the string type. On some systems,
69decoding these strings to and from bytes is necessary before passing
70them to the operating system. Python uses the file system encoding to
71perform this conversion (see :func:`sys.getfilesystemencoding`).
72
73.. versionchanged:: 3.1
74 On some systems, conversion using the file system encoding may
Martin v. Löwis43c57782009-05-10 08:15:24 +000075 fail. In this case, Python uses the ``surrogateescape`` encoding
76 error handler, which means that undecodable bytes are replaced by a
Martin v. Löwis011e8422009-05-05 04:43:17 +000077 Unicode character U+DCxx on decoding, and these are again
78 translated to the original byte on encoding.
79
80
81The file system encoding must guarantee to successfully decode all
82bytes below 128. If the file system encoding fails to provide this
83guarantee, API functions may raise UnicodeErrors.
84
85
Georg Brandl116aa622007-08-15 14:28:22 +000086.. _os-procinfo:
87
88Process Parameters
89------------------
90
91These functions and data items provide information and operate on the current
92process and user.
93
94
95.. data:: environ
96
97 A mapping object representing the string environment. For example,
98 ``environ['HOME']`` is the pathname of your home directory (on some platforms),
99 and is equivalent to ``getenv("HOME")`` in C.
100
101 This mapping is captured the first time the :mod:`os` module is imported,
102 typically during Python startup as part of processing :file:`site.py`. Changes
103 to the environment made after this time are not reflected in ``os.environ``,
104 except for changes made by modifying ``os.environ`` directly.
105
106 If the platform supports the :func:`putenv` function, this mapping may be used
107 to modify the environment as well as query the environment. :func:`putenv` will
108 be called automatically when the mapping is modified.
109
Victor Stinner84ae1182010-05-06 22:05:07 +0000110 On Unix, keys and values use :func:`sys.getfilesystemencoding` and
111 ``'surrogateescape'`` error handler. Use :data:`environb` if you would like
112 to use a different encoding.
113
Georg Brandl116aa622007-08-15 14:28:22 +0000114 .. note::
115
116 Calling :func:`putenv` directly does not change ``os.environ``, so it's better
117 to modify ``os.environ``.
118
119 .. note::
120
Georg Brandlc575c902008-09-13 17:46:05 +0000121 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
122 cause memory leaks. Refer to the system documentation for
123 :cfunc:`putenv`.
Georg Brandl116aa622007-08-15 14:28:22 +0000124
125 If :func:`putenv` is not provided, a modified copy of this mapping may be
126 passed to the appropriate process-creation functions to cause child processes
127 to use a modified environment.
128
Georg Brandl9afde1c2007-11-01 20:32:30 +0000129 If the platform supports the :func:`unsetenv` function, you can delete items in
Georg Brandl116aa622007-08-15 14:28:22 +0000130 this mapping to unset environment variables. :func:`unsetenv` will be called
Georg Brandl9afde1c2007-11-01 20:32:30 +0000131 automatically when an item is deleted from ``os.environ``, and when
132 one of the :meth:`pop` or :meth:`clear` methods is called.
133
Georg Brandl116aa622007-08-15 14:28:22 +0000134
Victor Stinner84ae1182010-05-06 22:05:07 +0000135.. data:: environb
136
137 Bytes version of :data:`environ`: a mapping object representing the
138 environment as byte strings. :data:`environ` and :data:`environb` are
139 synchronized (modify :data:`environb` updates :data:`environ`, and vice
140 versa).
141
142 Availability: Unix.
143
144
Georg Brandl116aa622007-08-15 14:28:22 +0000145.. function:: chdir(path)
146 fchdir(fd)
147 getcwd()
148 :noindex:
149
150 These functions are described in :ref:`os-file-dir`.
151
152
Gregory P. Smithb6e8c7e2010-02-27 07:22:22 +0000153.. function:: get_exec_path(env=None)
154
155 Returns the list of directories that will be searched for a named
156 executable, similar to a shell, when launching a process.
157 *env*, when specified, should be an environment variable dictionary
158 to lookup the PATH in.
159 By default, when *env* is None, :data:`environ` is used.
160
161 .. versionadded:: 3.2
162
163
Georg Brandl116aa622007-08-15 14:28:22 +0000164.. function:: ctermid()
165
166 Return the filename corresponding to the controlling terminal of the process.
167 Availability: Unix.
168
169
170.. function:: getegid()
171
172 Return the effective group id of the current process. This corresponds to the
Christian Heimesfaf2f632008-01-06 16:59:19 +0000173 "set id" bit on the file being executed in the current process. Availability:
Georg Brandl116aa622007-08-15 14:28:22 +0000174 Unix.
175
176
177.. function:: geteuid()
178
179 .. index:: single: user; effective id
180
Christian Heimesfaf2f632008-01-06 16:59:19 +0000181 Return the current process's effective user id. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000182
183
184.. function:: getgid()
185
186 .. index:: single: process; group
187
188 Return the real group id of the current process. Availability: Unix.
189
190
191.. function:: getgroups()
192
193 Return list of supplemental group ids associated with the current process.
194 Availability: Unix.
195
196
Antoine Pitroub7572f02009-12-02 20:46:48 +0000197.. function:: initgroups(username, gid)
198
199 Call the system initgroups() to initialize the group access list with all of
200 the groups of which the specified username is a member, plus the specified
201 group id. Availability: Unix.
202
203 .. versionadded:: 3.2
204
205
Georg Brandl116aa622007-08-15 14:28:22 +0000206.. function:: getlogin()
207
208 Return the name of the user logged in on the controlling terminal of the
209 process. For most purposes, it is more useful to use the environment variable
210 :envvar:`LOGNAME` to find out who the user is, or
211 ``pwd.getpwuid(os.getuid())[0]`` to get the login name of the currently
Christian Heimesfaf2f632008-01-06 16:59:19 +0000212 effective user id. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000213
214
215.. function:: getpgid(pid)
216
217 Return the process group id of the process with process id *pid*. If *pid* is 0,
218 the process group id of the current process is returned. Availability: Unix.
219
Georg Brandl116aa622007-08-15 14:28:22 +0000220
221.. function:: getpgrp()
222
223 .. index:: single: process; group
224
225 Return the id of the current process group. Availability: Unix.
226
227
228.. function:: getpid()
229
230 .. index:: single: process; id
231
232 Return the current process id. Availability: Unix, Windows.
233
234
235.. function:: getppid()
236
237 .. index:: single: process; id of parent
238
239 Return the parent's process id. Availability: Unix.
240
Georg Brandl1b83a452009-11-28 11:12:26 +0000241
Gregory P. Smithcf02c6a2009-11-27 17:54:17 +0000242.. function:: getresuid()
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000243
244 Return a tuple (ruid, euid, suid) denoting the current process's
245 real, effective, and saved user ids. Availability: Unix.
246
Georg Brandl1b83a452009-11-28 11:12:26 +0000247 .. versionadded:: 3.2
248
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000249
Gregory P. Smithcf02c6a2009-11-27 17:54:17 +0000250.. function:: getresgid()
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000251
252 Return a tuple (rgid, egid, sgid) denoting the current process's
253 real, effective, and saved user ids. Availability: Unix.
254
Georg Brandl1b83a452009-11-28 11:12:26 +0000255 .. versionadded:: 3.2
256
Georg Brandl116aa622007-08-15 14:28:22 +0000257
258.. function:: getuid()
259
260 .. index:: single: user; id
261
Christian Heimesfaf2f632008-01-06 16:59:19 +0000262 Return the current process's user id. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000263
264
Georg Brandl18244152009-09-02 20:34:52 +0000265.. function:: getenv(key, default=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000266
Georg Brandl18244152009-09-02 20:34:52 +0000267 Return the value of the environment variable *key* if it exists, or
Victor Stinner84ae1182010-05-06 22:05:07 +0000268 *default* if it doesn't. *key*, *default* and the result are str.
269 Availability: most flavors of Unix, Windows.
270
271 On Unix, keys and values are decoded with :func:`sys.getfilesystemencoding`
272 and ``'surrogateescape'`` error handler. Use :func:`os.getenvb` if you
273 would like to use a different encoding.
274
275
276.. function:: getenvb(key, default=None)
277
278 Return the value of the environment variable *key* if it exists, or
279 *default* if it doesn't. *key*, *default* and the result are bytes.
280 Availability: most flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000281
282
Georg Brandl18244152009-09-02 20:34:52 +0000283.. function:: putenv(key, value)
Georg Brandl116aa622007-08-15 14:28:22 +0000284
285 .. index:: single: environment variables; setting
286
Georg Brandl18244152009-09-02 20:34:52 +0000287 Set the environment variable named *key* to the string *value*. Such
Georg Brandl116aa622007-08-15 14:28:22 +0000288 changes to the environment affect subprocesses started with :func:`os.system`,
289 :func:`popen` or :func:`fork` and :func:`execv`. Availability: most flavors of
290 Unix, Windows.
291
292 .. note::
293
Georg Brandlc575c902008-09-13 17:46:05 +0000294 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
295 cause memory leaks. Refer to the system documentation for putenv.
Georg Brandl116aa622007-08-15 14:28:22 +0000296
297 When :func:`putenv` is supported, assignments to items in ``os.environ`` are
298 automatically translated into corresponding calls to :func:`putenv`; however,
299 calls to :func:`putenv` don't update ``os.environ``, so it is actually
300 preferable to assign to items of ``os.environ``.
301
302
303.. function:: setegid(egid)
304
305 Set the current process's effective group id. Availability: Unix.
306
307
308.. function:: seteuid(euid)
309
310 Set the current process's effective user id. Availability: Unix.
311
312
313.. function:: setgid(gid)
314
315 Set the current process' group id. Availability: Unix.
316
317
318.. function:: setgroups(groups)
319
320 Set the list of supplemental group ids associated with the current process to
321 *groups*. *groups* must be a sequence, and each element must be an integer
Christian Heimesfaf2f632008-01-06 16:59:19 +0000322 identifying a group. This operation is typically available only to the superuser.
Georg Brandl116aa622007-08-15 14:28:22 +0000323 Availability: Unix.
324
Georg Brandl116aa622007-08-15 14:28:22 +0000325
326.. function:: setpgrp()
327
Christian Heimesfaf2f632008-01-06 16:59:19 +0000328 Call the system call :cfunc:`setpgrp` or :cfunc:`setpgrp(0, 0)` depending on
Georg Brandl116aa622007-08-15 14:28:22 +0000329 which version is implemented (if any). See the Unix manual for the semantics.
330 Availability: Unix.
331
332
333.. function:: setpgid(pid, pgrp)
334
Christian Heimesfaf2f632008-01-06 16:59:19 +0000335 Call the system call :cfunc:`setpgid` to set the process group id of the
Georg Brandl116aa622007-08-15 14:28:22 +0000336 process with id *pid* to the process group with id *pgrp*. See the Unix manual
337 for the semantics. Availability: Unix.
338
339
Georg Brandl116aa622007-08-15 14:28:22 +0000340.. function:: setregid(rgid, egid)
341
342 Set the current process's real and effective group ids. Availability: Unix.
343
Georg Brandl1b83a452009-11-28 11:12:26 +0000344
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000345.. function:: setresgid(rgid, egid, sgid)
346
347 Set the current process's real, effective, and saved group ids.
348 Availability: Unix.
349
Georg Brandl1b83a452009-11-28 11:12:26 +0000350 .. versionadded:: 3.2
351
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000352
353.. function:: setresuid(ruid, euid, suid)
354
355 Set the current process's real, effective, and saved user ids.
356 Availibility: Unix.
357
Georg Brandl1b83a452009-11-28 11:12:26 +0000358 .. versionadded:: 3.2
359
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000360
361.. function:: setreuid(ruid, euid)
362
363 Set the current process's real and effective user ids. Availability: Unix.
364
Georg Brandl116aa622007-08-15 14:28:22 +0000365
366.. function:: getsid(pid)
367
Christian Heimesfaf2f632008-01-06 16:59:19 +0000368 Call the system call :cfunc:`getsid`. See the Unix manual for the semantics.
Georg Brandl116aa622007-08-15 14:28:22 +0000369 Availability: Unix.
370
Georg Brandl116aa622007-08-15 14:28:22 +0000371
372.. function:: setsid()
373
Christian Heimesfaf2f632008-01-06 16:59:19 +0000374 Call the system call :cfunc:`setsid`. See the Unix manual for the semantics.
Georg Brandl116aa622007-08-15 14:28:22 +0000375 Availability: Unix.
376
377
378.. function:: setuid(uid)
379
380 .. index:: single: user; id, setting
381
Christian Heimesfaf2f632008-01-06 16:59:19 +0000382 Set the current process's user id. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000383
Georg Brandl116aa622007-08-15 14:28:22 +0000384
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000385.. placed in this section since it relates to errno.... a little weak
Georg Brandl116aa622007-08-15 14:28:22 +0000386.. function:: strerror(code)
387
388 Return the error message corresponding to the error code in *code*.
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000389 On platforms where :cfunc:`strerror` returns ``NULL`` when given an unknown
390 error number, :exc:`ValueError` is raised. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000391
392
393.. function:: umask(mask)
394
Christian Heimesfaf2f632008-01-06 16:59:19 +0000395 Set the current numeric umask and return the previous umask. Availability:
Georg Brandl116aa622007-08-15 14:28:22 +0000396 Unix, Windows.
397
398
399.. function:: uname()
400
401 .. index::
402 single: gethostname() (in module socket)
403 single: gethostbyaddr() (in module socket)
404
405 Return a 5-tuple containing information identifying the current operating
406 system. The tuple contains 5 strings: ``(sysname, nodename, release, version,
407 machine)``. Some systems truncate the nodename to 8 characters or to the
408 leading component; a better way to get the hostname is
409 :func:`socket.gethostname` or even
410 ``socket.gethostbyaddr(socket.gethostname())``. Availability: recent flavors of
411 Unix.
412
413
Georg Brandl18244152009-09-02 20:34:52 +0000414.. function:: unsetenv(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000415
416 .. index:: single: environment variables; deleting
417
Georg Brandl18244152009-09-02 20:34:52 +0000418 Unset (delete) the environment variable named *key*. Such changes to the
Georg Brandl116aa622007-08-15 14:28:22 +0000419 environment affect subprocesses started with :func:`os.system`, :func:`popen` or
420 :func:`fork` and :func:`execv`. Availability: most flavors of Unix, Windows.
421
422 When :func:`unsetenv` is supported, deletion of items in ``os.environ`` is
423 automatically translated into a corresponding call to :func:`unsetenv`; however,
424 calls to :func:`unsetenv` don't update ``os.environ``, so it is actually
425 preferable to delete items of ``os.environ``.
426
427
428.. _os-newstreams:
429
430File Object Creation
431--------------------
432
433These functions create new file objects. (See also :func:`open`.)
434
435
436.. function:: fdopen(fd[, mode[, bufsize]])
437
438 .. index:: single: I/O control; buffering
439
440 Return an open file object connected to the file descriptor *fd*. The *mode*
441 and *bufsize* arguments have the same meaning as the corresponding arguments to
Georg Brandlc575c902008-09-13 17:46:05 +0000442 the built-in :func:`open` function. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000443
Georg Brandl55ac8f02007-09-01 13:51:09 +0000444 When specified, the *mode* argument must start with one of the letters
445 ``'r'``, ``'w'``, or ``'a'``, otherwise a :exc:`ValueError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000446
Georg Brandl55ac8f02007-09-01 13:51:09 +0000447 On Unix, when the *mode* argument starts with ``'a'``, the *O_APPEND* flag is
448 set on the file descriptor (which the :cfunc:`fdopen` implementation already
449 does on most platforms).
Georg Brandl116aa622007-08-15 14:28:22 +0000450
451
Georg Brandl116aa622007-08-15 14:28:22 +0000452.. _os-fd-ops:
453
454File Descriptor Operations
455--------------------------
456
457These functions operate on I/O streams referenced using file descriptors.
458
459File descriptors are small integers corresponding to a file that has been opened
460by the current process. For example, standard input is usually file descriptor
4610, standard output is 1, and standard error is 2. Further files opened by a
462process will then be assigned 3, 4, 5, and so forth. The name "file descriptor"
463is slightly deceptive; on Unix platforms, sockets and pipes are also referenced
464by file descriptors.
465
Benjamin Peterson08bf91c2010-04-11 16:12:57 +0000466The :meth:`~file.fileno` method can be used to obtain the file descriptor
467associated with a file object when required. Note that using the file
468descriptor directly will bypass the file object methods, ignoring aspects such
469as internal buffering of data.
Georg Brandl116aa622007-08-15 14:28:22 +0000470
471.. function:: close(fd)
472
Georg Brandlc575c902008-09-13 17:46:05 +0000473 Close file descriptor *fd*. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000474
475 .. note::
476
477 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000478 descriptor as returned by :func:`os.open` or :func:`pipe`. To close a "file
Georg Brandl116aa622007-08-15 14:28:22 +0000479 object" returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000480 :func:`fdopen`, use its :meth:`~file.close` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000481
482
Christian Heimesfdab48e2008-01-20 09:06:41 +0000483.. function:: closerange(fd_low, fd_high)
484
485 Close all file descriptors from *fd_low* (inclusive) to *fd_high* (exclusive),
Georg Brandlc575c902008-09-13 17:46:05 +0000486 ignoring errors. Availability: Unix, Windows. Equivalent to::
Christian Heimesfdab48e2008-01-20 09:06:41 +0000487
Georg Brandlc9a5a0e2009-09-01 07:34:27 +0000488 for fd in range(fd_low, fd_high):
Christian Heimesfdab48e2008-01-20 09:06:41 +0000489 try:
490 os.close(fd)
491 except OSError:
492 pass
493
Christian Heimesfdab48e2008-01-20 09:06:41 +0000494
Georg Brandl81f11302007-12-21 08:45:42 +0000495.. function:: device_encoding(fd)
496
497 Return a string describing the encoding of the device associated with *fd*
498 if it is connected to a terminal; else return :const:`None`.
499
500
Georg Brandl116aa622007-08-15 14:28:22 +0000501.. function:: dup(fd)
502
Georg Brandlc575c902008-09-13 17:46:05 +0000503 Return a duplicate of file descriptor *fd*. Availability: Unix,
Georg Brandl116aa622007-08-15 14:28:22 +0000504 Windows.
505
506
507.. function:: dup2(fd, fd2)
508
509 Duplicate file descriptor *fd* to *fd2*, closing the latter first if necessary.
Georg Brandlc575c902008-09-13 17:46:05 +0000510 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000511
512
Christian Heimes4e30a842007-11-30 22:12:06 +0000513.. function:: fchmod(fd, mode)
514
515 Change the mode of the file given by *fd* to the numeric *mode*. See the docs
516 for :func:`chmod` for possible values of *mode*. Availability: Unix.
517
518
519.. function:: fchown(fd, uid, gid)
520
521 Change the owner and group id of the file given by *fd* to the numeric *uid*
522 and *gid*. To leave one of the ids unchanged, set it to -1.
523 Availability: Unix.
524
525
Georg Brandl116aa622007-08-15 14:28:22 +0000526.. function:: fdatasync(fd)
527
528 Force write of file with filedescriptor *fd* to disk. Does not force update of
529 metadata. Availability: Unix.
530
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000531 .. note::
532 This function is not available on MacOS.
533
Georg Brandl116aa622007-08-15 14:28:22 +0000534
535.. function:: fpathconf(fd, name)
536
537 Return system configuration information relevant to an open file. *name*
538 specifies the configuration value to retrieve; it may be a string which is the
539 name of a defined system value; these names are specified in a number of
540 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
541 additional names as well. The names known to the host operating system are
542 given in the ``pathconf_names`` dictionary. For configuration variables not
543 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandlc575c902008-09-13 17:46:05 +0000544 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000545
546 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
547 specific value for *name* is not supported by the host system, even if it is
548 included in ``pathconf_names``, an :exc:`OSError` is raised with
549 :const:`errno.EINVAL` for the error number.
550
551
552.. function:: fstat(fd)
553
554 Return status for file descriptor *fd*, like :func:`stat`. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +0000555 Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000556
557
558.. function:: fstatvfs(fd)
559
560 Return information about the filesystem containing the file associated with file
561 descriptor *fd*, like :func:`statvfs`. Availability: Unix.
562
563
564.. function:: fsync(fd)
565
566 Force write of file with filedescriptor *fd* to disk. On Unix, this calls the
567 native :cfunc:`fsync` function; on Windows, the MS :cfunc:`_commit` function.
568
569 If you're starting with a Python file object *f*, first do ``f.flush()``, and
570 then do ``os.fsync(f.fileno())``, to ensure that all internal buffers associated
Georg Brandlc575c902008-09-13 17:46:05 +0000571 with *f* are written to disk. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000572
573
574.. function:: ftruncate(fd, length)
575
576 Truncate the file corresponding to file descriptor *fd*, so that it is at most
Georg Brandlc575c902008-09-13 17:46:05 +0000577 *length* bytes in size. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000578
579
580.. function:: isatty(fd)
581
582 Return ``True`` if the file descriptor *fd* is open and connected to a
Georg Brandlc575c902008-09-13 17:46:05 +0000583 tty(-like) device, else ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000584
585
586.. function:: lseek(fd, pos, how)
587
Christian Heimesfaf2f632008-01-06 16:59:19 +0000588 Set the current position of file descriptor *fd* to position *pos*, modified
589 by *how*: :const:`SEEK_SET` or ``0`` to set the position relative to the
590 beginning of the file; :const:`SEEK_CUR` or ``1`` to set it relative to the
591 current position; :const:`os.SEEK_END` or ``2`` to set it relative to the end of
Georg Brandlc575c902008-09-13 17:46:05 +0000592 the file. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000593
594
595.. function:: open(file, flags[, mode])
596
Georg Brandlf4a41232008-05-26 17:55:52 +0000597 Open the file *file* and set various flags according to *flags* and possibly
598 its mode according to *mode*. The default *mode* is ``0o777`` (octal), and
599 the current umask value is first masked out. Return the file descriptor for
Georg Brandlc575c902008-09-13 17:46:05 +0000600 the newly opened file. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000601
602 For a description of the flag and mode values, see the C run-time documentation;
603 flag constants (like :const:`O_RDONLY` and :const:`O_WRONLY`) are defined in
604 this module too (see below).
605
606 .. note::
607
Georg Brandl502d9a52009-07-26 15:02:41 +0000608 This function is intended for low-level I/O. For normal usage, use the
609 built-in function :func:`open`, which returns a "file object" with
610 :meth:`~file.read` and :meth:`~file.write` methods (and many more). To
611 wrap a file descriptor in a "file object", use :func:`fdopen`.
Georg Brandl116aa622007-08-15 14:28:22 +0000612
613
614.. function:: openpty()
615
616 .. index:: module: pty
617
618 Open a new pseudo-terminal pair. Return a pair of file descriptors ``(master,
619 slave)`` for the pty and the tty, respectively. For a (slightly) more portable
Georg Brandlc575c902008-09-13 17:46:05 +0000620 approach, use the :mod:`pty` module. Availability: some flavors of
Georg Brandl116aa622007-08-15 14:28:22 +0000621 Unix.
622
623
624.. function:: pipe()
625
626 Create a pipe. Return a pair of file descriptors ``(r, w)`` usable for reading
Georg Brandlc575c902008-09-13 17:46:05 +0000627 and writing, respectively. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000628
629
630.. function:: read(fd, n)
631
Georg Brandlb90be692009-07-29 16:14:16 +0000632 Read at most *n* bytes from file descriptor *fd*. Return a bytestring containing the
Georg Brandl116aa622007-08-15 14:28:22 +0000633 bytes read. If the end of the file referred to by *fd* has been reached, an
Georg Brandlb90be692009-07-29 16:14:16 +0000634 empty bytes object is returned. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000635
636 .. note::
637
638 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000639 descriptor as returned by :func:`os.open` or :func:`pipe`. To read a "file object"
Georg Brandl116aa622007-08-15 14:28:22 +0000640 returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000641 :func:`fdopen`, or :data:`sys.stdin`, use its :meth:`~file.read` or
642 :meth:`~file.readline` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000643
644
645.. function:: tcgetpgrp(fd)
646
647 Return the process group associated with the terminal given by *fd* (an open
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000648 file descriptor as returned by :func:`os.open`). Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000649
650
651.. function:: tcsetpgrp(fd, pg)
652
653 Set the process group associated with the terminal given by *fd* (an open file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000654 descriptor as returned by :func:`os.open`) to *pg*. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000655
656
657.. function:: ttyname(fd)
658
659 Return a string which specifies the terminal device associated with
Georg Brandl9afde1c2007-11-01 20:32:30 +0000660 file descriptor *fd*. If *fd* is not associated with a terminal device, an
Georg Brandlc575c902008-09-13 17:46:05 +0000661 exception is raised. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000662
663
664.. function:: write(fd, str)
665
Georg Brandlb90be692009-07-29 16:14:16 +0000666 Write the bytestring in *str* to file descriptor *fd*. Return the number of
667 bytes actually written. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000668
669 .. note::
670
671 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000672 descriptor as returned by :func:`os.open` or :func:`pipe`. To write a "file
Georg Brandl116aa622007-08-15 14:28:22 +0000673 object" returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000674 :func:`fdopen`, or :data:`sys.stdout` or :data:`sys.stderr`, use its
675 :meth:`~file.write` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000676
Georg Brandlaf265f42008-12-07 15:06:20 +0000677The following constants are options for the *flags* parameter to the
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000678:func:`~os.open` function. They can be combined using the bitwise OR operator
Georg Brandlaf265f42008-12-07 15:06:20 +0000679``|``. Some of them are not available on all platforms. For descriptions of
680their availability and use, consult the :manpage:`open(2)` manual page on Unix
Doug Hellmanneb097fc2009-09-20 20:56:56 +0000681or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000682
683
684.. data:: O_RDONLY
685 O_WRONLY
686 O_RDWR
687 O_APPEND
688 O_CREAT
689 O_EXCL
690 O_TRUNC
691
Georg Brandlaf265f42008-12-07 15:06:20 +0000692 These constants are available on Unix and Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000693
694
695.. data:: O_DSYNC
696 O_RSYNC
697 O_SYNC
698 O_NDELAY
699 O_NONBLOCK
700 O_NOCTTY
701 O_SHLOCK
702 O_EXLOCK
703
Georg Brandlaf265f42008-12-07 15:06:20 +0000704 These constants are only available on Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000705
706
707.. data:: O_BINARY
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000708 O_NOINHERIT
Georg Brandl116aa622007-08-15 14:28:22 +0000709 O_SHORT_LIVED
710 O_TEMPORARY
711 O_RANDOM
712 O_SEQUENTIAL
713 O_TEXT
714
Georg Brandlaf265f42008-12-07 15:06:20 +0000715 These constants are only available on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000716
717
Alexandre Vassalottibee32532008-05-16 18:15:12 +0000718.. data:: O_ASYNC
719 O_DIRECT
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000720 O_DIRECTORY
721 O_NOFOLLOW
722 O_NOATIME
723
Georg Brandlaf265f42008-12-07 15:06:20 +0000724 These constants are GNU extensions and not present if they are not defined by
725 the C library.
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000726
727
Georg Brandl116aa622007-08-15 14:28:22 +0000728.. data:: SEEK_SET
729 SEEK_CUR
730 SEEK_END
731
732 Parameters to the :func:`lseek` function. Their values are 0, 1, and 2,
Georg Brandlc575c902008-09-13 17:46:05 +0000733 respectively. Availability: Windows, Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000734
Georg Brandl116aa622007-08-15 14:28:22 +0000735
736.. _os-file-dir:
737
738Files and Directories
739---------------------
740
Georg Brandl116aa622007-08-15 14:28:22 +0000741.. function:: access(path, mode)
742
743 Use the real uid/gid to test for access to *path*. Note that most operations
744 will use the effective uid/gid, therefore this routine can be used in a
745 suid/sgid environment to test if the invoking user has the specified access to
746 *path*. *mode* should be :const:`F_OK` to test the existence of *path*, or it
747 can be the inclusive OR of one or more of :const:`R_OK`, :const:`W_OK`, and
748 :const:`X_OK` to test permissions. Return :const:`True` if access is allowed,
749 :const:`False` if not. See the Unix man page :manpage:`access(2)` for more
Georg Brandlc575c902008-09-13 17:46:05 +0000750 information. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000751
752 .. note::
753
Georg Brandl502d9a52009-07-26 15:02:41 +0000754 Using :func:`access` to check if a user is authorized to e.g. open a file
755 before actually doing so using :func:`open` creates a security hole,
756 because the user might exploit the short time interval between checking
757 and opening the file to manipulate it.
Georg Brandl116aa622007-08-15 14:28:22 +0000758
759 .. note::
760
761 I/O operations may fail even when :func:`access` indicates that they would
762 succeed, particularly for operations on network filesystems which may have
763 permissions semantics beyond the usual POSIX permission-bit model.
764
765
766.. data:: F_OK
767
768 Value to pass as the *mode* parameter of :func:`access` to test the existence of
769 *path*.
770
771
772.. data:: R_OK
773
774 Value to include in the *mode* parameter of :func:`access` to test the
775 readability of *path*.
776
777
778.. data:: W_OK
779
780 Value to include in the *mode* parameter of :func:`access` to test the
781 writability of *path*.
782
783
784.. data:: X_OK
785
786 Value to include in the *mode* parameter of :func:`access` to determine if
787 *path* can be executed.
788
789
790.. function:: chdir(path)
791
792 .. index:: single: directory; changing
793
Georg Brandlc575c902008-09-13 17:46:05 +0000794 Change the current working directory to *path*. Availability: Unix,
Georg Brandl116aa622007-08-15 14:28:22 +0000795 Windows.
796
797
798.. function:: fchdir(fd)
799
800 Change the current working directory to the directory represented by the file
801 descriptor *fd*. The descriptor must refer to an opened directory, not an open
802 file. Availability: Unix.
803
Georg Brandl116aa622007-08-15 14:28:22 +0000804
805.. function:: getcwd()
806
Martin v. Löwis011e8422009-05-05 04:43:17 +0000807 Return a string representing the current working directory.
808 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000809
Martin v. Löwisa731b992008-10-07 06:36:31 +0000810.. function:: getcwdb()
Georg Brandl116aa622007-08-15 14:28:22 +0000811
Georg Brandl76e55382008-10-08 16:34:57 +0000812 Return a bytestring representing the current working directory.
Georg Brandlc575c902008-09-13 17:46:05 +0000813 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000814
Georg Brandl116aa622007-08-15 14:28:22 +0000815
816.. function:: chflags(path, flags)
817
818 Set the flags of *path* to the numeric *flags*. *flags* may take a combination
819 (bitwise OR) of the following values (as defined in the :mod:`stat` module):
820
821 * ``UF_NODUMP``
822 * ``UF_IMMUTABLE``
823 * ``UF_APPEND``
824 * ``UF_OPAQUE``
825 * ``UF_NOUNLINK``
826 * ``SF_ARCHIVED``
827 * ``SF_IMMUTABLE``
828 * ``SF_APPEND``
829 * ``SF_NOUNLINK``
830 * ``SF_SNAPSHOT``
831
Georg Brandlc575c902008-09-13 17:46:05 +0000832 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000833
Georg Brandl116aa622007-08-15 14:28:22 +0000834
835.. function:: chroot(path)
836
837 Change the root directory of the current process to *path*. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +0000838 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000839
Georg Brandl116aa622007-08-15 14:28:22 +0000840
841.. function:: chmod(path, mode)
842
843 Change the mode of *path* to the numeric *mode*. *mode* may take one of the
Christian Heimesfaf2f632008-01-06 16:59:19 +0000844 following values (as defined in the :mod:`stat` module) or bitwise ORed
Georg Brandl116aa622007-08-15 14:28:22 +0000845 combinations of them:
846
Alexandre Vassalottic22c6f22009-07-21 00:51:58 +0000847 * :data:`stat.S_ISUID`
848 * :data:`stat.S_ISGID`
849 * :data:`stat.S_ENFMT`
850 * :data:`stat.S_ISVTX`
851 * :data:`stat.S_IREAD`
852 * :data:`stat.S_IWRITE`
853 * :data:`stat.S_IEXEC`
854 * :data:`stat.S_IRWXU`
855 * :data:`stat.S_IRUSR`
856 * :data:`stat.S_IWUSR`
857 * :data:`stat.S_IXUSR`
858 * :data:`stat.S_IRWXG`
859 * :data:`stat.S_IRGRP`
860 * :data:`stat.S_IWGRP`
861 * :data:`stat.S_IXGRP`
862 * :data:`stat.S_IRWXO`
863 * :data:`stat.S_IROTH`
864 * :data:`stat.S_IWOTH`
865 * :data:`stat.S_IXOTH`
Georg Brandl116aa622007-08-15 14:28:22 +0000866
Georg Brandlc575c902008-09-13 17:46:05 +0000867 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000868
869 .. note::
870
871 Although Windows supports :func:`chmod`, you can only set the file's read-only
872 flag with it (via the ``stat.S_IWRITE`` and ``stat.S_IREAD``
873 constants or a corresponding integer value). All other bits are
874 ignored.
875
876
877.. function:: chown(path, uid, gid)
878
879 Change the owner and group id of *path* to the numeric *uid* and *gid*. To leave
Georg Brandlc575c902008-09-13 17:46:05 +0000880 one of the ids unchanged, set it to -1. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000881
882
883.. function:: lchflags(path, flags)
884
885 Set the flags of *path* to the numeric *flags*, like :func:`chflags`, but do not
886 follow symbolic links. Availability: Unix.
887
Georg Brandl116aa622007-08-15 14:28:22 +0000888
Christian Heimes93852662007-12-01 12:22:32 +0000889.. function:: lchmod(path, mode)
890
891 Change the mode of *path* to the numeric *mode*. If path is a symlink, this
892 affects the symlink rather than the target. See the docs for :func:`chmod`
893 for possible values of *mode*. Availability: Unix.
894
Christian Heimes93852662007-12-01 12:22:32 +0000895
Georg Brandl116aa622007-08-15 14:28:22 +0000896.. function:: lchown(path, uid, gid)
897
Christian Heimesfaf2f632008-01-06 16:59:19 +0000898 Change the owner and group id of *path* to the numeric *uid* and *gid*. This
Georg Brandlc575c902008-09-13 17:46:05 +0000899 function will not follow symbolic links. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000900
Georg Brandl116aa622007-08-15 14:28:22 +0000901
Benjamin Peterson5879d412009-03-30 14:51:56 +0000902.. function:: link(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +0000903
Benjamin Peterson5879d412009-03-30 14:51:56 +0000904 Create a hard link pointing to *source* named *link_name*. Availability:
905 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000906
907
908.. function:: listdir(path)
909
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000910 Return a list containing the names of the entries in the directory given by
911 *path*. The list is in arbitrary order. It does not include the special
912 entries ``'.'`` and ``'..'`` even if they are present in the directory.
913 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000914
Martin v. Löwis011e8422009-05-05 04:43:17 +0000915 This function can be called with a bytes or string argument, and returns
916 filenames of the same datatype.
Georg Brandl116aa622007-08-15 14:28:22 +0000917
918
919.. function:: lstat(path)
920
Guido van Rossum2cc30da2007-11-02 23:46:40 +0000921 Like :func:`stat`, but do not follow symbolic links. This is an alias for
922 :func:`stat` on platforms that do not support symbolic links, such as
923 Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000924
925
926.. function:: mkfifo(path[, mode])
927
Georg Brandlf4a41232008-05-26 17:55:52 +0000928 Create a FIFO (a named pipe) named *path* with numeric mode *mode*. The
929 default *mode* is ``0o666`` (octal). The current umask value is first masked
Georg Brandlc575c902008-09-13 17:46:05 +0000930 out from the mode. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000931
932 FIFOs are pipes that can be accessed like regular files. FIFOs exist until they
933 are deleted (for example with :func:`os.unlink`). Generally, FIFOs are used as
934 rendezvous between "client" and "server" type processes: the server opens the
935 FIFO for reading, and the client opens it for writing. Note that :func:`mkfifo`
936 doesn't open the FIFO --- it just creates the rendezvous point.
937
938
Georg Brandl18244152009-09-02 20:34:52 +0000939.. function:: mknod(filename[, mode=0o600[, device]])
Georg Brandl116aa622007-08-15 14:28:22 +0000940
941 Create a filesystem node (file, device special file or named pipe) named
Georg Brandl18244152009-09-02 20:34:52 +0000942 *filename*. *mode* specifies both the permissions to use and the type of node
943 to be created, being combined (bitwise OR) with one of ``stat.S_IFREG``,
944 ``stat.S_IFCHR``, ``stat.S_IFBLK``, and ``stat.S_IFIFO`` (those constants are
945 available in :mod:`stat`). For ``stat.S_IFCHR`` and ``stat.S_IFBLK``,
946 *device* defines the newly created device special file (probably using
Georg Brandl116aa622007-08-15 14:28:22 +0000947 :func:`os.makedev`), otherwise it is ignored.
948
Georg Brandl116aa622007-08-15 14:28:22 +0000949
950.. function:: major(device)
951
Christian Heimesfaf2f632008-01-06 16:59:19 +0000952 Extract the device major number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000953 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
954
Georg Brandl116aa622007-08-15 14:28:22 +0000955
956.. function:: minor(device)
957
Christian Heimesfaf2f632008-01-06 16:59:19 +0000958 Extract the device minor number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000959 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
960
Georg Brandl116aa622007-08-15 14:28:22 +0000961
962.. function:: makedev(major, minor)
963
Christian Heimesfaf2f632008-01-06 16:59:19 +0000964 Compose a raw device number from the major and minor device numbers.
Georg Brandl116aa622007-08-15 14:28:22 +0000965
Georg Brandl116aa622007-08-15 14:28:22 +0000966
967.. function:: mkdir(path[, mode])
968
Georg Brandlf4a41232008-05-26 17:55:52 +0000969 Create a directory named *path* with numeric mode *mode*. The default *mode*
970 is ``0o777`` (octal). On some systems, *mode* is ignored. Where it is used,
Georg Brandlc575c902008-09-13 17:46:05 +0000971 the current umask value is first masked out. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000972
Guido van Rossum2cc30da2007-11-02 23:46:40 +0000973 It is also possible to create temporary directories; see the
974 :mod:`tempfile` module's :func:`tempfile.mkdtemp` function.
975
Georg Brandl116aa622007-08-15 14:28:22 +0000976
977.. function:: makedirs(path[, mode])
978
979 .. index::
980 single: directory; creating
981 single: UNC paths; and os.makedirs()
982
983 Recursive directory creation function. Like :func:`mkdir`, but makes all
Georg Brandlf4a41232008-05-26 17:55:52 +0000984 intermediate-level directories needed to contain the leaf directory. Throws
985 an :exc:`error` exception if the leaf directory already exists or cannot be
986 created. The default *mode* is ``0o777`` (octal). On some systems, *mode*
987 is ignored. Where it is used, the current umask value is first masked out.
Georg Brandl116aa622007-08-15 14:28:22 +0000988
989 .. note::
990
991 :func:`makedirs` will become confused if the path elements to create include
Christian Heimesfaf2f632008-01-06 16:59:19 +0000992 :data:`os.pardir`.
Georg Brandl116aa622007-08-15 14:28:22 +0000993
Georg Brandl55ac8f02007-09-01 13:51:09 +0000994 This function handles UNC paths correctly.
Georg Brandl116aa622007-08-15 14:28:22 +0000995
996
997.. function:: pathconf(path, name)
998
999 Return system configuration information relevant to a named file. *name*
1000 specifies the configuration value to retrieve; it may be a string which is the
1001 name of a defined system value; these names are specified in a number of
1002 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
1003 additional names as well. The names known to the host operating system are
1004 given in the ``pathconf_names`` dictionary. For configuration variables not
1005 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandlc575c902008-09-13 17:46:05 +00001006 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001007
1008 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1009 specific value for *name* is not supported by the host system, even if it is
1010 included in ``pathconf_names``, an :exc:`OSError` is raised with
1011 :const:`errno.EINVAL` for the error number.
1012
1013
1014.. data:: pathconf_names
1015
1016 Dictionary mapping names accepted by :func:`pathconf` and :func:`fpathconf` to
1017 the integer values defined for those names by the host operating system. This
1018 can be used to determine the set of names known to the system. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001019 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001020
1021
1022.. function:: readlink(path)
1023
1024 Return a string representing the path to which the symbolic link points. The
1025 result may be either an absolute or relative pathname; if it is relative, it may
1026 be converted to an absolute pathname using ``os.path.join(os.path.dirname(path),
1027 result)``.
1028
Georg Brandl76e55382008-10-08 16:34:57 +00001029 If the *path* is a string object, the result will also be a string object,
1030 and the call may raise an UnicodeDecodeError. If the *path* is a bytes
1031 object, the result will be a bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +00001032
Georg Brandlc575c902008-09-13 17:46:05 +00001033 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001034
1035
1036.. function:: remove(path)
1037
Georg Brandla6053b42009-09-01 08:11:14 +00001038 Remove (delete) the file *path*. If *path* is a directory, :exc:`OSError` is
1039 raised; see :func:`rmdir` below to remove a directory. This is identical to
1040 the :func:`unlink` function documented below. On Windows, attempting to
1041 remove a file that is in use causes an exception to be raised; on Unix, the
1042 directory entry is removed but the storage allocated to the file is not made
1043 available until the original file is no longer in use. Availability: Unix,
Georg Brandl116aa622007-08-15 14:28:22 +00001044 Windows.
1045
1046
1047.. function:: removedirs(path)
1048
1049 .. index:: single: directory; deleting
1050
Christian Heimesfaf2f632008-01-06 16:59:19 +00001051 Remove directories recursively. Works like :func:`rmdir` except that, if the
Georg Brandl116aa622007-08-15 14:28:22 +00001052 leaf directory is successfully removed, :func:`removedirs` tries to
1053 successively remove every parent directory mentioned in *path* until an error
1054 is raised (which is ignored, because it generally means that a parent directory
1055 is not empty). For example, ``os.removedirs('foo/bar/baz')`` will first remove
1056 the directory ``'foo/bar/baz'``, and then remove ``'foo/bar'`` and ``'foo'`` if
1057 they are empty. Raises :exc:`OSError` if the leaf directory could not be
1058 successfully removed.
1059
Georg Brandl116aa622007-08-15 14:28:22 +00001060
1061.. function:: rename(src, dst)
1062
1063 Rename the file or directory *src* to *dst*. If *dst* is a directory,
1064 :exc:`OSError` will be raised. On Unix, if *dst* exists and is a file, it will
Christian Heimesfaf2f632008-01-06 16:59:19 +00001065 be replaced silently if the user has permission. The operation may fail on some
Georg Brandl116aa622007-08-15 14:28:22 +00001066 Unix flavors if *src* and *dst* are on different filesystems. If successful,
1067 the renaming will be an atomic operation (this is a POSIX requirement). On
1068 Windows, if *dst* already exists, :exc:`OSError` will be raised even if it is a
1069 file; there may be no way to implement an atomic rename when *dst* names an
Georg Brandlc575c902008-09-13 17:46:05 +00001070 existing file. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001071
1072
1073.. function:: renames(old, new)
1074
1075 Recursive directory or file renaming function. Works like :func:`rename`, except
1076 creation of any intermediate directories needed to make the new pathname good is
1077 attempted first. After the rename, directories corresponding to rightmost path
1078 segments of the old name will be pruned away using :func:`removedirs`.
1079
Georg Brandl116aa622007-08-15 14:28:22 +00001080 .. note::
1081
1082 This function can fail with the new directory structure made if you lack
1083 permissions needed to remove the leaf directory or file.
1084
1085
1086.. function:: rmdir(path)
1087
Georg Brandla6053b42009-09-01 08:11:14 +00001088 Remove (delete) the directory *path*. Only works when the directory is
1089 empty, otherwise, :exc:`OSError` is raised. In order to remove whole
1090 directory trees, :func:`shutil.rmtree` can be used. Availability: Unix,
1091 Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001092
1093
1094.. function:: stat(path)
1095
1096 Perform a :cfunc:`stat` system call on the given path. The return value is an
1097 object whose attributes correspond to the members of the :ctype:`stat`
1098 structure, namely: :attr:`st_mode` (protection bits), :attr:`st_ino` (inode
1099 number), :attr:`st_dev` (device), :attr:`st_nlink` (number of hard links),
Christian Heimesfaf2f632008-01-06 16:59:19 +00001100 :attr:`st_uid` (user id of owner), :attr:`st_gid` (group id of owner),
Georg Brandl116aa622007-08-15 14:28:22 +00001101 :attr:`st_size` (size of file, in bytes), :attr:`st_atime` (time of most recent
1102 access), :attr:`st_mtime` (time of most recent content modification),
1103 :attr:`st_ctime` (platform dependent; time of most recent metadata change on
1104 Unix, or the time of creation on Windows)::
1105
1106 >>> import os
1107 >>> statinfo = os.stat('somefile.txt')
1108 >>> statinfo
Georg Brandlf66df2b2010-01-16 14:41:21 +00001109 (33188, 422511, 769, 1, 1032, 100, 926, 1105022698,1105022732, 1105022732)
Georg Brandl116aa622007-08-15 14:28:22 +00001110 >>> statinfo.st_size
Georg Brandlf66df2b2010-01-16 14:41:21 +00001111 926
Georg Brandl116aa622007-08-15 14:28:22 +00001112 >>>
1113
Georg Brandl116aa622007-08-15 14:28:22 +00001114
1115 On some Unix systems (such as Linux), the following attributes may also be
1116 available: :attr:`st_blocks` (number of blocks allocated for file),
1117 :attr:`st_blksize` (filesystem blocksize), :attr:`st_rdev` (type of device if an
1118 inode device). :attr:`st_flags` (user defined flags for file).
1119
1120 On other Unix systems (such as FreeBSD), the following attributes may be
1121 available (but may be only filled out if root tries to use them): :attr:`st_gen`
1122 (file generation number), :attr:`st_birthtime` (time of file creation).
1123
1124 On Mac OS systems, the following attributes may also be available:
1125 :attr:`st_rsize`, :attr:`st_creator`, :attr:`st_type`.
1126
Georg Brandl116aa622007-08-15 14:28:22 +00001127 .. index:: module: stat
1128
1129 For backward compatibility, the return value of :func:`stat` is also accessible
1130 as a tuple of at least 10 integers giving the most important (and portable)
1131 members of the :ctype:`stat` structure, in the order :attr:`st_mode`,
1132 :attr:`st_ino`, :attr:`st_dev`, :attr:`st_nlink`, :attr:`st_uid`,
1133 :attr:`st_gid`, :attr:`st_size`, :attr:`st_atime`, :attr:`st_mtime`,
1134 :attr:`st_ctime`. More items may be added at the end by some implementations.
1135 The standard module :mod:`stat` defines functions and constants that are useful
1136 for extracting information from a :ctype:`stat` structure. (On Windows, some
1137 items are filled with dummy values.)
1138
1139 .. note::
1140
1141 The exact meaning and resolution of the :attr:`st_atime`, :attr:`st_mtime`, and
1142 :attr:`st_ctime` members depends on the operating system and the file system.
1143 For example, on Windows systems using the FAT or FAT32 file systems,
1144 :attr:`st_mtime` has 2-second resolution, and :attr:`st_atime` has only 1-day
1145 resolution. See your operating system documentation for details.
1146
Georg Brandlc575c902008-09-13 17:46:05 +00001147 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001148
Georg Brandl116aa622007-08-15 14:28:22 +00001149
1150.. function:: stat_float_times([newvalue])
1151
1152 Determine whether :class:`stat_result` represents time stamps as float objects.
1153 If *newvalue* is ``True``, future calls to :func:`stat` return floats, if it is
1154 ``False``, future calls return ints. If *newvalue* is omitted, return the
1155 current setting.
1156
1157 For compatibility with older Python versions, accessing :class:`stat_result` as
1158 a tuple always returns integers.
1159
Georg Brandl55ac8f02007-09-01 13:51:09 +00001160 Python now returns float values by default. Applications which do not work
1161 correctly with floating point time stamps can use this function to restore the
1162 old behaviour.
Georg Brandl116aa622007-08-15 14:28:22 +00001163
1164 The resolution of the timestamps (that is the smallest possible fraction)
1165 depends on the system. Some systems only support second resolution; on these
1166 systems, the fraction will always be zero.
1167
1168 It is recommended that this setting is only changed at program startup time in
1169 the *__main__* module; libraries should never change this setting. If an
1170 application uses a library that works incorrectly if floating point time stamps
1171 are processed, this application should turn the feature off until the library
1172 has been corrected.
1173
1174
1175.. function:: statvfs(path)
1176
1177 Perform a :cfunc:`statvfs` system call on the given path. The return value is
1178 an object whose attributes describe the filesystem on the given path, and
1179 correspond to the members of the :ctype:`statvfs` structure, namely:
1180 :attr:`f_bsize`, :attr:`f_frsize`, :attr:`f_blocks`, :attr:`f_bfree`,
1181 :attr:`f_bavail`, :attr:`f_files`, :attr:`f_ffree`, :attr:`f_favail`,
1182 :attr:`f_flag`, :attr:`f_namemax`. Availability: Unix.
1183
Georg Brandl116aa622007-08-15 14:28:22 +00001184
Benjamin Peterson5879d412009-03-30 14:51:56 +00001185.. function:: symlink(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +00001186
Benjamin Peterson5879d412009-03-30 14:51:56 +00001187 Create a symbolic link pointing to *source* named *link_name*. Availability:
1188 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001189
1190
Georg Brandl116aa622007-08-15 14:28:22 +00001191.. function:: unlink(path)
1192
Georg Brandla6053b42009-09-01 08:11:14 +00001193 Remove (delete) the file *path*. This is the same function as
1194 :func:`remove`; the :func:`unlink` name is its traditional Unix
1195 name. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001196
1197
1198.. function:: utime(path, times)
1199
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001200 Set the access and modified times of the file specified by *path*. If *times*
1201 is ``None``, then the file's access and modified times are set to the current
1202 time. (The effect is similar to running the Unix program :program:`touch` on
1203 the path.) Otherwise, *times* must be a 2-tuple of numbers, of the form
1204 ``(atime, mtime)`` which is used to set the access and modified times,
1205 respectively. Whether a directory can be given for *path* depends on whether
1206 the operating system implements directories as files (for example, Windows
1207 does not). Note that the exact times you set here may not be returned by a
1208 subsequent :func:`stat` call, depending on the resolution with which your
1209 operating system records access and modification times; see :func:`stat`.
Georg Brandl116aa622007-08-15 14:28:22 +00001210
Georg Brandlc575c902008-09-13 17:46:05 +00001211 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001212
1213
Georg Brandl18244152009-09-02 20:34:52 +00001214.. function:: walk(top, topdown=True, onerror=None, followlinks=False)
Georg Brandl116aa622007-08-15 14:28:22 +00001215
1216 .. index::
1217 single: directory; walking
1218 single: directory; traversal
1219
Christian Heimesfaf2f632008-01-06 16:59:19 +00001220 Generate the file names in a directory tree by walking the tree
1221 either top-down or bottom-up. For each directory in the tree rooted at directory
Georg Brandl116aa622007-08-15 14:28:22 +00001222 *top* (including *top* itself), it yields a 3-tuple ``(dirpath, dirnames,
1223 filenames)``.
1224
1225 *dirpath* is a string, the path to the directory. *dirnames* is a list of the
1226 names of the subdirectories in *dirpath* (excluding ``'.'`` and ``'..'``).
1227 *filenames* is a list of the names of the non-directory files in *dirpath*.
1228 Note that the names in the lists contain no path components. To get a full path
1229 (which begins with *top*) to a file or directory in *dirpath*, do
1230 ``os.path.join(dirpath, name)``.
1231
Christian Heimesfaf2f632008-01-06 16:59:19 +00001232 If optional argument *topdown* is ``True`` or not specified, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001233 directory is generated before the triples for any of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001234 (directories are generated top-down). If *topdown* is ``False``, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001235 directory is generated after the triples for all of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001236 (directories are generated bottom-up).
Georg Brandl116aa622007-08-15 14:28:22 +00001237
Christian Heimesfaf2f632008-01-06 16:59:19 +00001238 When *topdown* is ``True``, the caller can modify the *dirnames* list in-place
Georg Brandl116aa622007-08-15 14:28:22 +00001239 (perhaps using :keyword:`del` or slice assignment), and :func:`walk` will only
1240 recurse into the subdirectories whose names remain in *dirnames*; this can be
1241 used to prune the search, impose a specific order of visiting, or even to inform
1242 :func:`walk` about directories the caller creates or renames before it resumes
Christian Heimesfaf2f632008-01-06 16:59:19 +00001243 :func:`walk` again. Modifying *dirnames* when *topdown* is ``False`` is
Georg Brandl116aa622007-08-15 14:28:22 +00001244 ineffective, because in bottom-up mode the directories in *dirnames* are
1245 generated before *dirpath* itself is generated.
1246
Christian Heimesfaf2f632008-01-06 16:59:19 +00001247 By default errors from the :func:`listdir` call are ignored. If optional
Georg Brandl116aa622007-08-15 14:28:22 +00001248 argument *onerror* is specified, it should be a function; it will be called with
1249 one argument, an :exc:`OSError` instance. It can report the error to continue
1250 with the walk, or raise the exception to abort the walk. Note that the filename
1251 is available as the ``filename`` attribute of the exception object.
1252
1253 By default, :func:`walk` will not walk down into symbolic links that resolve to
Christian Heimesfaf2f632008-01-06 16:59:19 +00001254 directories. Set *followlinks* to ``True`` to visit directories pointed to by
Georg Brandl116aa622007-08-15 14:28:22 +00001255 symlinks, on systems that support them.
1256
Georg Brandl116aa622007-08-15 14:28:22 +00001257 .. note::
1258
Christian Heimesfaf2f632008-01-06 16:59:19 +00001259 Be aware that setting *followlinks* to ``True`` can lead to infinite recursion if a
Georg Brandl116aa622007-08-15 14:28:22 +00001260 link points to a parent directory of itself. :func:`walk` does not keep track of
1261 the directories it visited already.
1262
1263 .. note::
1264
1265 If you pass a relative pathname, don't change the current working directory
1266 between resumptions of :func:`walk`. :func:`walk` never changes the current
1267 directory, and assumes that its caller doesn't either.
1268
1269 This example displays the number of bytes taken by non-directory files in each
1270 directory under the starting directory, except that it doesn't look under any
1271 CVS subdirectory::
1272
1273 import os
1274 from os.path import join, getsize
1275 for root, dirs, files in os.walk('python/Lib/email'):
Georg Brandl6911e3c2007-09-04 07:15:32 +00001276 print(root, "consumes", end=" ")
1277 print(sum(getsize(join(root, name)) for name in files), end=" ")
1278 print("bytes in", len(files), "non-directory files")
Georg Brandl116aa622007-08-15 14:28:22 +00001279 if 'CVS' in dirs:
1280 dirs.remove('CVS') # don't visit CVS directories
1281
Christian Heimesfaf2f632008-01-06 16:59:19 +00001282 In the next example, walking the tree bottom-up is essential: :func:`rmdir`
Georg Brandl116aa622007-08-15 14:28:22 +00001283 doesn't allow deleting a directory before the directory is empty::
1284
Christian Heimesfaf2f632008-01-06 16:59:19 +00001285 # Delete everything reachable from the directory named in "top",
Georg Brandl116aa622007-08-15 14:28:22 +00001286 # assuming there are no symbolic links.
1287 # CAUTION: This is dangerous! For example, if top == '/', it
1288 # could delete all your disk files.
1289 import os
1290 for root, dirs, files in os.walk(top, topdown=False):
1291 for name in files:
1292 os.remove(os.path.join(root, name))
1293 for name in dirs:
1294 os.rmdir(os.path.join(root, name))
1295
Georg Brandl116aa622007-08-15 14:28:22 +00001296
1297.. _os-process:
1298
1299Process Management
1300------------------
1301
1302These functions may be used to create and manage processes.
1303
1304The various :func:`exec\*` functions take a list of arguments for the new
1305program loaded into the process. In each case, the first of these arguments is
1306passed to the new program as its own name rather than as an argument a user may
1307have typed on a command line. For the C programmer, this is the ``argv[0]``
1308passed to a program's :cfunc:`main`. For example, ``os.execv('/bin/echo',
1309['foo', 'bar'])`` will only print ``bar`` on standard output; ``foo`` will seem
1310to be ignored.
1311
1312
1313.. function:: abort()
1314
1315 Generate a :const:`SIGABRT` signal to the current process. On Unix, the default
1316 behavior is to produce a core dump; on Windows, the process immediately returns
1317 an exit code of ``3``. Be aware that programs which use :func:`signal.signal`
1318 to register a handler for :const:`SIGABRT` will behave differently.
Georg Brandlc575c902008-09-13 17:46:05 +00001319 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001320
1321
1322.. function:: execl(path, arg0, arg1, ...)
1323 execle(path, arg0, arg1, ..., env)
1324 execlp(file, arg0, arg1, ...)
1325 execlpe(file, arg0, arg1, ..., env)
1326 execv(path, args)
1327 execve(path, args, env)
1328 execvp(file, args)
1329 execvpe(file, args, env)
1330
1331 These functions all execute a new program, replacing the current process; they
1332 do not return. On Unix, the new executable is loaded into the current process,
Christian Heimesfaf2f632008-01-06 16:59:19 +00001333 and will have the same process id as the caller. Errors will be reported as
Georg Brandl48310cd2009-01-03 21:18:54 +00001334 :exc:`OSError` exceptions.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001335
1336 The current process is replaced immediately. Open file objects and
1337 descriptors are not flushed, so if there may be data buffered
1338 on these open files, you should flush them using
1339 :func:`sys.stdout.flush` or :func:`os.fsync` before calling an
1340 :func:`exec\*` function.
Georg Brandl116aa622007-08-15 14:28:22 +00001341
Christian Heimesfaf2f632008-01-06 16:59:19 +00001342 The "l" and "v" variants of the :func:`exec\*` functions differ in how
1343 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001344 to work with if the number of parameters is fixed when the code is written; the
1345 individual parameters simply become additional parameters to the :func:`execl\*`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001346 functions. The "v" variants are good when the number of parameters is
Georg Brandl116aa622007-08-15 14:28:22 +00001347 variable, with the arguments being passed in a list or tuple as the *args*
1348 parameter. In either case, the arguments to the child process should start with
1349 the name of the command being run, but this is not enforced.
1350
Christian Heimesfaf2f632008-01-06 16:59:19 +00001351 The variants which include a "p" near the end (:func:`execlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001352 :func:`execlpe`, :func:`execvp`, and :func:`execvpe`) will use the
1353 :envvar:`PATH` environment variable to locate the program *file*. When the
1354 environment is being replaced (using one of the :func:`exec\*e` variants,
1355 discussed in the next paragraph), the new environment is used as the source of
1356 the :envvar:`PATH` variable. The other variants, :func:`execl`, :func:`execle`,
1357 :func:`execv`, and :func:`execve`, will not use the :envvar:`PATH` variable to
1358 locate the executable; *path* must contain an appropriate absolute or relative
1359 path.
1360
1361 For :func:`execle`, :func:`execlpe`, :func:`execve`, and :func:`execvpe` (note
Christian Heimesfaf2f632008-01-06 16:59:19 +00001362 that these all end in "e"), the *env* parameter must be a mapping which is
Christian Heimesa342c012008-04-20 21:01:16 +00001363 used to define the environment variables for the new process (these are used
1364 instead of the current process' environment); the functions :func:`execl`,
Georg Brandl116aa622007-08-15 14:28:22 +00001365 :func:`execlp`, :func:`execv`, and :func:`execvp` all cause the new process to
Georg Brandl48310cd2009-01-03 21:18:54 +00001366 inherit the environment of the current process.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001367
1368 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001369
1370
1371.. function:: _exit(n)
1372
1373 Exit to the system with status *n*, without calling cleanup handlers, flushing
Georg Brandlc575c902008-09-13 17:46:05 +00001374 stdio buffers, etc. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001375
1376 .. note::
1377
1378 The standard way to exit is ``sys.exit(n)``. :func:`_exit` should normally only
1379 be used in the child process after a :func:`fork`.
1380
Christian Heimesfaf2f632008-01-06 16:59:19 +00001381The following exit codes are defined and can be used with :func:`_exit`,
Georg Brandl116aa622007-08-15 14:28:22 +00001382although they are not required. These are typically used for system programs
1383written in Python, such as a mail server's external command delivery program.
1384
1385.. note::
1386
1387 Some of these may not be available on all Unix platforms, since there is some
1388 variation. These constants are defined where they are defined by the underlying
1389 platform.
1390
1391
1392.. data:: EX_OK
1393
Georg Brandlc575c902008-09-13 17:46:05 +00001394 Exit code that means no error occurred. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001395
Georg Brandl116aa622007-08-15 14:28:22 +00001396
1397.. data:: EX_USAGE
1398
1399 Exit code that means the command was used incorrectly, such as when the wrong
Georg Brandlc575c902008-09-13 17:46:05 +00001400 number of arguments are given. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001401
Georg Brandl116aa622007-08-15 14:28:22 +00001402
1403.. data:: EX_DATAERR
1404
Georg Brandlc575c902008-09-13 17:46:05 +00001405 Exit code that means the input data was incorrect. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001406
Georg Brandl116aa622007-08-15 14:28:22 +00001407
1408.. data:: EX_NOINPUT
1409
1410 Exit code that means an input file did not exist or was not readable.
Georg Brandlc575c902008-09-13 17:46:05 +00001411 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001412
Georg Brandl116aa622007-08-15 14:28:22 +00001413
1414.. data:: EX_NOUSER
1415
Georg Brandlc575c902008-09-13 17:46:05 +00001416 Exit code that means a specified user did not exist. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001417
Georg Brandl116aa622007-08-15 14:28:22 +00001418
1419.. data:: EX_NOHOST
1420
Georg Brandlc575c902008-09-13 17:46:05 +00001421 Exit code that means a specified host did not exist. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001422
Georg Brandl116aa622007-08-15 14:28:22 +00001423
1424.. data:: EX_UNAVAILABLE
1425
1426 Exit code that means that a required service is unavailable. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001427 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001428
Georg Brandl116aa622007-08-15 14:28:22 +00001429
1430.. data:: EX_SOFTWARE
1431
1432 Exit code that means an internal software error was detected. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001433 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001434
Georg Brandl116aa622007-08-15 14:28:22 +00001435
1436.. data:: EX_OSERR
1437
1438 Exit code that means an operating system error was detected, such as the
Georg Brandlc575c902008-09-13 17:46:05 +00001439 inability to fork or create a pipe. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001440
Georg Brandl116aa622007-08-15 14:28:22 +00001441
1442.. data:: EX_OSFILE
1443
1444 Exit code that means some system file did not exist, could not be opened, or had
Georg Brandlc575c902008-09-13 17:46:05 +00001445 some other kind of error. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001446
Georg Brandl116aa622007-08-15 14:28:22 +00001447
1448.. data:: EX_CANTCREAT
1449
1450 Exit code that means a user specified output file could not be created.
Georg Brandlc575c902008-09-13 17:46:05 +00001451 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001452
Georg Brandl116aa622007-08-15 14:28:22 +00001453
1454.. data:: EX_IOERR
1455
1456 Exit code that means that an error occurred while doing I/O on some file.
Georg Brandlc575c902008-09-13 17:46:05 +00001457 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001458
Georg Brandl116aa622007-08-15 14:28:22 +00001459
1460.. data:: EX_TEMPFAIL
1461
1462 Exit code that means a temporary failure occurred. This indicates something
1463 that may not really be an error, such as a network connection that couldn't be
Georg Brandlc575c902008-09-13 17:46:05 +00001464 made during a retryable operation. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001465
Georg Brandl116aa622007-08-15 14:28:22 +00001466
1467.. data:: EX_PROTOCOL
1468
1469 Exit code that means that a protocol exchange was illegal, invalid, or not
Georg Brandlc575c902008-09-13 17:46:05 +00001470 understood. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001471
Georg Brandl116aa622007-08-15 14:28:22 +00001472
1473.. data:: EX_NOPERM
1474
1475 Exit code that means that there were insufficient permissions to perform the
Georg Brandlc575c902008-09-13 17:46:05 +00001476 operation (but not intended for file system problems). Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001477
Georg Brandl116aa622007-08-15 14:28:22 +00001478
1479.. data:: EX_CONFIG
1480
1481 Exit code that means that some kind of configuration error occurred.
Georg Brandlc575c902008-09-13 17:46:05 +00001482 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001483
Georg Brandl116aa622007-08-15 14:28:22 +00001484
1485.. data:: EX_NOTFOUND
1486
1487 Exit code that means something like "an entry was not found". Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001488 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001489
Georg Brandl116aa622007-08-15 14:28:22 +00001490
1491.. function:: fork()
1492
Christian Heimesfaf2f632008-01-06 16:59:19 +00001493 Fork a child process. Return ``0`` in the child and the child's process id in the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001494 parent. If an error occurs :exc:`OSError` is raised.
Benjamin Petersonbcd8ac32008-10-10 22:20:52 +00001495
1496 Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have
1497 known issues when using fork() from a thread.
1498
Georg Brandlc575c902008-09-13 17:46:05 +00001499 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001500
1501
1502.. function:: forkpty()
1503
1504 Fork a child process, using a new pseudo-terminal as the child's controlling
1505 terminal. Return a pair of ``(pid, fd)``, where *pid* is ``0`` in the child, the
1506 new child's process id in the parent, and *fd* is the file descriptor of the
1507 master end of the pseudo-terminal. For a more portable approach, use the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001508 :mod:`pty` module. If an error occurs :exc:`OSError` is raised.
Georg Brandlc575c902008-09-13 17:46:05 +00001509 Availability: some flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001510
1511
1512.. function:: kill(pid, sig)
1513
1514 .. index::
1515 single: process; killing
1516 single: process; signalling
1517
1518 Send signal *sig* to the process *pid*. Constants for the specific signals
1519 available on the host platform are defined in the :mod:`signal` module.
Brian Curtineb24d742010-04-12 17:16:38 +00001520
1521 Windows: The :data:`signal.CTRL_C_EVENT` and
1522 :data:`signal.CTRL_BREAK_EVENT` signals are special signals which can
1523 only be sent to console processes which share a common console window,
1524 e.g., some subprocesses. Any other value for *sig* will cause the process
1525 to be unconditionally killed by the TerminateProcess API, and the exit code
1526 will be set to *sig*. The Windows version of :func:`kill` additionally takes
1527 process handles to be killed.
Georg Brandl116aa622007-08-15 14:28:22 +00001528
Brian Curtin904bd392010-04-20 15:28:06 +00001529 .. versionadded:: 3.2 Windows support
1530
Georg Brandl116aa622007-08-15 14:28:22 +00001531
1532.. function:: killpg(pgid, sig)
1533
1534 .. index::
1535 single: process; killing
1536 single: process; signalling
1537
Georg Brandlc575c902008-09-13 17:46:05 +00001538 Send the signal *sig* to the process group *pgid*. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001539
Georg Brandl116aa622007-08-15 14:28:22 +00001540
1541.. function:: nice(increment)
1542
1543 Add *increment* to the process's "niceness". Return the new niceness.
Georg Brandlc575c902008-09-13 17:46:05 +00001544 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001545
1546
1547.. function:: plock(op)
1548
1549 Lock program segments into memory. The value of *op* (defined in
Georg Brandlc575c902008-09-13 17:46:05 +00001550 ``<sys/lock.h>``) determines which segments are locked. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001551
1552
1553.. function:: popen(...)
1554 :noindex:
1555
1556 Run child processes, returning opened pipes for communications. These functions
1557 are described in section :ref:`os-newstreams`.
1558
1559
1560.. function:: spawnl(mode, path, ...)
1561 spawnle(mode, path, ..., env)
1562 spawnlp(mode, file, ...)
1563 spawnlpe(mode, file, ..., env)
1564 spawnv(mode, path, args)
1565 spawnve(mode, path, args, env)
1566 spawnvp(mode, file, args)
1567 spawnvpe(mode, file, args, env)
1568
1569 Execute the program *path* in a new process.
1570
1571 (Note that the :mod:`subprocess` module provides more powerful facilities for
1572 spawning new processes and retrieving their results; using that module is
Benjamin Peterson87c8d872009-06-11 22:54:11 +00001573 preferable to using these functions. Check especially the
1574 :ref:`subprocess-replacements` section.)
Georg Brandl116aa622007-08-15 14:28:22 +00001575
Christian Heimesfaf2f632008-01-06 16:59:19 +00001576 If *mode* is :const:`P_NOWAIT`, this function returns the process id of the new
Georg Brandl116aa622007-08-15 14:28:22 +00001577 process; if *mode* is :const:`P_WAIT`, returns the process's exit code if it
1578 exits normally, or ``-signal``, where *signal* is the signal that killed the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001579 process. On Windows, the process id will actually be the process handle, so can
Georg Brandl116aa622007-08-15 14:28:22 +00001580 be used with the :func:`waitpid` function.
1581
Christian Heimesfaf2f632008-01-06 16:59:19 +00001582 The "l" and "v" variants of the :func:`spawn\*` functions differ in how
1583 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001584 to work with if the number of parameters is fixed when the code is written; the
1585 individual parameters simply become additional parameters to the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001586 :func:`spawnl\*` functions. The "v" variants are good when the number of
Georg Brandl116aa622007-08-15 14:28:22 +00001587 parameters is variable, with the arguments being passed in a list or tuple as
1588 the *args* parameter. In either case, the arguments to the child process must
1589 start with the name of the command being run.
1590
Christian Heimesfaf2f632008-01-06 16:59:19 +00001591 The variants which include a second "p" near the end (:func:`spawnlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001592 :func:`spawnlpe`, :func:`spawnvp`, and :func:`spawnvpe`) will use the
1593 :envvar:`PATH` environment variable to locate the program *file*. When the
1594 environment is being replaced (using one of the :func:`spawn\*e` variants,
1595 discussed in the next paragraph), the new environment is used as the source of
1596 the :envvar:`PATH` variable. The other variants, :func:`spawnl`,
1597 :func:`spawnle`, :func:`spawnv`, and :func:`spawnve`, will not use the
1598 :envvar:`PATH` variable to locate the executable; *path* must contain an
1599 appropriate absolute or relative path.
1600
1601 For :func:`spawnle`, :func:`spawnlpe`, :func:`spawnve`, and :func:`spawnvpe`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001602 (note that these all end in "e"), the *env* parameter must be a mapping
Christian Heimesa342c012008-04-20 21:01:16 +00001603 which is used to define the environment variables for the new process (they are
1604 used instead of the current process' environment); the functions
Georg Brandl116aa622007-08-15 14:28:22 +00001605 :func:`spawnl`, :func:`spawnlp`, :func:`spawnv`, and :func:`spawnvp` all cause
Benjamin Petersond23f8222009-04-05 19:13:16 +00001606 the new process to inherit the environment of the current process. Note that
1607 keys and values in the *env* dictionary must be strings; invalid keys or
1608 values will cause the function to fail, with a return value of ``127``.
Georg Brandl116aa622007-08-15 14:28:22 +00001609
1610 As an example, the following calls to :func:`spawnlp` and :func:`spawnvpe` are
1611 equivalent::
1612
1613 import os
1614 os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1615
1616 L = ['cp', 'index.html', '/dev/null']
1617 os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1618
1619 Availability: Unix, Windows. :func:`spawnlp`, :func:`spawnlpe`, :func:`spawnvp`
1620 and :func:`spawnvpe` are not available on Windows.
1621
Georg Brandl116aa622007-08-15 14:28:22 +00001622
1623.. data:: P_NOWAIT
1624 P_NOWAITO
1625
1626 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1627 functions. If either of these values is given, the :func:`spawn\*` functions
Christian Heimesfaf2f632008-01-06 16:59:19 +00001628 will return as soon as the new process has been created, with the process id as
Georg Brandlc575c902008-09-13 17:46:05 +00001629 the return value. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001630
Georg Brandl116aa622007-08-15 14:28:22 +00001631
1632.. data:: P_WAIT
1633
1634 Possible value for the *mode* parameter to the :func:`spawn\*` family of
1635 functions. If this is given as *mode*, the :func:`spawn\*` functions will not
1636 return until the new process has run to completion and will return the exit code
1637 of the process the run is successful, or ``-signal`` if a signal kills the
Georg Brandlc575c902008-09-13 17:46:05 +00001638 process. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001639
Georg Brandl116aa622007-08-15 14:28:22 +00001640
1641.. data:: P_DETACH
1642 P_OVERLAY
1643
1644 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1645 functions. These are less portable than those listed above. :const:`P_DETACH`
1646 is similar to :const:`P_NOWAIT`, but the new process is detached from the
1647 console of the calling process. If :const:`P_OVERLAY` is used, the current
1648 process will be replaced; the :func:`spawn\*` function will not return.
1649 Availability: Windows.
1650
Georg Brandl116aa622007-08-15 14:28:22 +00001651
1652.. function:: startfile(path[, operation])
1653
1654 Start a file with its associated application.
1655
1656 When *operation* is not specified or ``'open'``, this acts like double-clicking
1657 the file in Windows Explorer, or giving the file name as an argument to the
1658 :program:`start` command from the interactive command shell: the file is opened
1659 with whatever application (if any) its extension is associated.
1660
1661 When another *operation* is given, it must be a "command verb" that specifies
1662 what should be done with the file. Common verbs documented by Microsoft are
1663 ``'print'`` and ``'edit'`` (to be used on files) as well as ``'explore'`` and
1664 ``'find'`` (to be used on directories).
1665
1666 :func:`startfile` returns as soon as the associated application is launched.
1667 There is no option to wait for the application to close, and no way to retrieve
1668 the application's exit status. The *path* parameter is relative to the current
1669 directory. If you want to use an absolute path, make sure the first character
1670 is not a slash (``'/'``); the underlying Win32 :cfunc:`ShellExecute` function
1671 doesn't work if it is. Use the :func:`os.path.normpath` function to ensure that
1672 the path is properly encoded for Win32. Availability: Windows.
1673
Georg Brandl116aa622007-08-15 14:28:22 +00001674
1675.. function:: system(command)
1676
1677 Execute the command (a string) in a subshell. This is implemented by calling
Georg Brandl495f7b52009-10-27 15:28:25 +00001678 the Standard C function :cfunc:`system`, and has the same limitations.
1679 Changes to :data:`sys.stdin`, etc. are not reflected in the environment of the
1680 executed command.
Georg Brandl116aa622007-08-15 14:28:22 +00001681
1682 On Unix, the return value is the exit status of the process encoded in the
1683 format specified for :func:`wait`. Note that POSIX does not specify the meaning
1684 of the return value of the C :cfunc:`system` function, so the return value of
1685 the Python function is system-dependent.
1686
1687 On Windows, the return value is that returned by the system shell after running
1688 *command*, given by the Windows environment variable :envvar:`COMSPEC`: on
1689 :program:`command.com` systems (Windows 95, 98 and ME) this is always ``0``; on
1690 :program:`cmd.exe` systems (Windows NT, 2000 and XP) this is the exit status of
1691 the command run; on systems using a non-native shell, consult your shell
1692 documentation.
1693
Georg Brandlc575c902008-09-13 17:46:05 +00001694 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001695
1696 The :mod:`subprocess` module provides more powerful facilities for spawning new
1697 processes and retrieving their results; using that module is preferable to using
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001698 this function. Use the :mod:`subprocess` module. Check especially the
1699 :ref:`subprocess-replacements` section.
Georg Brandl116aa622007-08-15 14:28:22 +00001700
1701
1702.. function:: times()
1703
1704 Return a 5-tuple of floating point numbers indicating accumulated (processor or
1705 other) times, in seconds. The items are: user time, system time, children's
1706 user time, children's system time, and elapsed real time since a fixed point in
1707 the past, in that order. See the Unix manual page :manpage:`times(2)` or the
Georg Brandlc575c902008-09-13 17:46:05 +00001708 corresponding Windows Platform API documentation. Availability: Unix,
Christian Heimes68f5fbe2008-02-14 08:27:37 +00001709 Windows. On Windows, only the first two items are filled, the others are zero.
Georg Brandl116aa622007-08-15 14:28:22 +00001710
1711
1712.. function:: wait()
1713
1714 Wait for completion of a child process, and return a tuple containing its pid
1715 and exit status indication: a 16-bit number, whose low byte is the signal number
1716 that killed the process, and whose high byte is the exit status (if the signal
1717 number is zero); the high bit of the low byte is set if a core file was
Georg Brandlc575c902008-09-13 17:46:05 +00001718 produced. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001719
1720
1721.. function:: waitpid(pid, options)
1722
1723 The details of this function differ on Unix and Windows.
1724
1725 On Unix: Wait for completion of a child process given by process id *pid*, and
1726 return a tuple containing its process id and exit status indication (encoded as
1727 for :func:`wait`). The semantics of the call are affected by the value of the
1728 integer *options*, which should be ``0`` for normal operation.
1729
1730 If *pid* is greater than ``0``, :func:`waitpid` requests status information for
1731 that specific process. If *pid* is ``0``, the request is for the status of any
1732 child in the process group of the current process. If *pid* is ``-1``, the
1733 request pertains to any child of the current process. If *pid* is less than
1734 ``-1``, status is requested for any process in the process group ``-pid`` (the
1735 absolute value of *pid*).
1736
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001737 An :exc:`OSError` is raised with the value of errno when the syscall
1738 returns -1.
1739
Georg Brandl116aa622007-08-15 14:28:22 +00001740 On Windows: Wait for completion of a process given by process handle *pid*, and
1741 return a tuple containing *pid*, and its exit status shifted left by 8 bits
1742 (shifting makes cross-platform use of the function easier). A *pid* less than or
1743 equal to ``0`` has no special meaning on Windows, and raises an exception. The
1744 value of integer *options* has no effect. *pid* can refer to any process whose
1745 id is known, not necessarily a child process. The :func:`spawn` functions called
1746 with :const:`P_NOWAIT` return suitable process handles.
1747
1748
1749.. function:: wait3([options])
1750
1751 Similar to :func:`waitpid`, except no process id argument is given and a
1752 3-element tuple containing the child's process id, exit status indication, and
1753 resource usage information is returned. Refer to :mod:`resource`.\
1754 :func:`getrusage` for details on resource usage information. The option
1755 argument is the same as that provided to :func:`waitpid` and :func:`wait4`.
1756 Availability: Unix.
1757
Georg Brandl116aa622007-08-15 14:28:22 +00001758
1759.. function:: wait4(pid, options)
1760
1761 Similar to :func:`waitpid`, except a 3-element tuple, containing the child's
1762 process id, exit status indication, and resource usage information is returned.
1763 Refer to :mod:`resource`.\ :func:`getrusage` for details on resource usage
1764 information. The arguments to :func:`wait4` are the same as those provided to
1765 :func:`waitpid`. Availability: Unix.
1766
Georg Brandl116aa622007-08-15 14:28:22 +00001767
1768.. data:: WNOHANG
1769
1770 The option for :func:`waitpid` to return immediately if no child process status
1771 is available immediately. The function returns ``(0, 0)`` in this case.
Georg Brandlc575c902008-09-13 17:46:05 +00001772 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001773
1774
1775.. data:: WCONTINUED
1776
1777 This option causes child processes to be reported if they have been continued
1778 from a job control stop since their status was last reported. Availability: Some
1779 Unix systems.
1780
Georg Brandl116aa622007-08-15 14:28:22 +00001781
1782.. data:: WUNTRACED
1783
1784 This option causes child processes to be reported if they have been stopped but
1785 their current state has not been reported since they were stopped. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001786 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001787
Georg Brandl116aa622007-08-15 14:28:22 +00001788
1789The following functions take a process status code as returned by
1790:func:`system`, :func:`wait`, or :func:`waitpid` as a parameter. They may be
1791used to determine the disposition of a process.
1792
Georg Brandl116aa622007-08-15 14:28:22 +00001793.. function:: WCOREDUMP(status)
1794
Christian Heimesfaf2f632008-01-06 16:59:19 +00001795 Return ``True`` if a core dump was generated for the process, otherwise
Georg Brandlc575c902008-09-13 17:46:05 +00001796 return ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001797
Georg Brandl116aa622007-08-15 14:28:22 +00001798
1799.. function:: WIFCONTINUED(status)
1800
Christian Heimesfaf2f632008-01-06 16:59:19 +00001801 Return ``True`` if the process has been continued from a job control stop,
1802 otherwise return ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001803
Georg Brandl116aa622007-08-15 14:28:22 +00001804
1805.. function:: WIFSTOPPED(status)
1806
Christian Heimesfaf2f632008-01-06 16:59:19 +00001807 Return ``True`` if the process has been stopped, otherwise return
Georg Brandl116aa622007-08-15 14:28:22 +00001808 ``False``. Availability: Unix.
1809
1810
1811.. function:: WIFSIGNALED(status)
1812
Christian Heimesfaf2f632008-01-06 16:59:19 +00001813 Return ``True`` if the process exited due to a signal, otherwise return
Georg Brandlc575c902008-09-13 17:46:05 +00001814 ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001815
1816
1817.. function:: WIFEXITED(status)
1818
Christian Heimesfaf2f632008-01-06 16:59:19 +00001819 Return ``True`` if the process exited using the :manpage:`exit(2)` system call,
Georg Brandlc575c902008-09-13 17:46:05 +00001820 otherwise return ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001821
1822
1823.. function:: WEXITSTATUS(status)
1824
1825 If ``WIFEXITED(status)`` is true, return the integer parameter to the
1826 :manpage:`exit(2)` system call. Otherwise, the return value is meaningless.
Georg Brandlc575c902008-09-13 17:46:05 +00001827 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001828
1829
1830.. function:: WSTOPSIG(status)
1831
Georg Brandlc575c902008-09-13 17:46:05 +00001832 Return the signal which caused the process to stop. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001833
1834
1835.. function:: WTERMSIG(status)
1836
Georg Brandlc575c902008-09-13 17:46:05 +00001837 Return the signal which caused the process to exit. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001838
1839
1840.. _os-path:
1841
1842Miscellaneous System Information
1843--------------------------------
1844
1845
1846.. function:: confstr(name)
1847
1848 Return string-valued system configuration values. *name* specifies the
1849 configuration value to retrieve; it may be a string which is the name of a
1850 defined system value; these names are specified in a number of standards (POSIX,
1851 Unix 95, Unix 98, and others). Some platforms define additional names as well.
1852 The names known to the host operating system are given as the keys of the
1853 ``confstr_names`` dictionary. For configuration variables not included in that
1854 mapping, passing an integer for *name* is also accepted. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001855 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001856
1857 If the configuration value specified by *name* isn't defined, ``None`` is
1858 returned.
1859
1860 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1861 specific value for *name* is not supported by the host system, even if it is
1862 included in ``confstr_names``, an :exc:`OSError` is raised with
1863 :const:`errno.EINVAL` for the error number.
1864
1865
1866.. data:: confstr_names
1867
1868 Dictionary mapping names accepted by :func:`confstr` to the integer values
1869 defined for those names by the host operating system. This can be used to
Georg Brandlc575c902008-09-13 17:46:05 +00001870 determine the set of names known to the system. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001871
1872
1873.. function:: getloadavg()
1874
Christian Heimesa62da1d2008-01-12 19:39:10 +00001875 Return the number of processes in the system run queue averaged over the last
1876 1, 5, and 15 minutes or raises :exc:`OSError` if the load average was
Georg Brandlf08a9dd2008-06-10 16:57:31 +00001877 unobtainable. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001878
Georg Brandl116aa622007-08-15 14:28:22 +00001879
1880.. function:: sysconf(name)
1881
1882 Return integer-valued system configuration values. If the configuration value
1883 specified by *name* isn't defined, ``-1`` is returned. The comments regarding
1884 the *name* parameter for :func:`confstr` apply here as well; the dictionary that
1885 provides information on the known names is given by ``sysconf_names``.
Georg Brandlc575c902008-09-13 17:46:05 +00001886 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001887
1888
1889.. data:: sysconf_names
1890
1891 Dictionary mapping names accepted by :func:`sysconf` to the integer values
1892 defined for those names by the host operating system. This can be used to
Georg Brandlc575c902008-09-13 17:46:05 +00001893 determine the set of names known to the system. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001894
Christian Heimesfaf2f632008-01-06 16:59:19 +00001895The following data values are used to support path manipulation operations. These
Georg Brandl116aa622007-08-15 14:28:22 +00001896are defined for all platforms.
1897
1898Higher-level operations on pathnames are defined in the :mod:`os.path` module.
1899
1900
1901.. data:: curdir
1902
1903 The constant string used by the operating system to refer to the current
Georg Brandlc575c902008-09-13 17:46:05 +00001904 directory. This is ``'.'`` for Windows and POSIX. Also available via
1905 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00001906
1907
1908.. data:: pardir
1909
1910 The constant string used by the operating system to refer to the parent
Georg Brandlc575c902008-09-13 17:46:05 +00001911 directory. This is ``'..'`` for Windows and POSIX. Also available via
1912 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00001913
1914
1915.. data:: sep
1916
Georg Brandlc575c902008-09-13 17:46:05 +00001917 The character used by the operating system to separate pathname components.
1918 This is ``'/'`` for POSIX and ``'\\'`` for Windows. Note that knowing this
1919 is not sufficient to be able to parse or concatenate pathnames --- use
Georg Brandl116aa622007-08-15 14:28:22 +00001920 :func:`os.path.split` and :func:`os.path.join` --- but it is occasionally
1921 useful. Also available via :mod:`os.path`.
1922
1923
1924.. data:: altsep
1925
1926 An alternative character used by the operating system to separate pathname
1927 components, or ``None`` if only one separator character exists. This is set to
1928 ``'/'`` on Windows systems where ``sep`` is a backslash. Also available via
1929 :mod:`os.path`.
1930
1931
1932.. data:: extsep
1933
1934 The character which separates the base filename from the extension; for example,
1935 the ``'.'`` in :file:`os.py`. Also available via :mod:`os.path`.
1936
Georg Brandl116aa622007-08-15 14:28:22 +00001937
1938.. data:: pathsep
1939
1940 The character conventionally used by the operating system to separate search
1941 path components (as in :envvar:`PATH`), such as ``':'`` for POSIX or ``';'`` for
1942 Windows. Also available via :mod:`os.path`.
1943
1944
1945.. data:: defpath
1946
1947 The default search path used by :func:`exec\*p\*` and :func:`spawn\*p\*` if the
1948 environment doesn't have a ``'PATH'`` key. Also available via :mod:`os.path`.
1949
1950
1951.. data:: linesep
1952
1953 The string used to separate (or, rather, terminate) lines on the current
Georg Brandlc575c902008-09-13 17:46:05 +00001954 platform. This may be a single character, such as ``'\n'`` for POSIX, or
1955 multiple characters, for example, ``'\r\n'`` for Windows. Do not use
1956 *os.linesep* as a line terminator when writing files opened in text mode (the
1957 default); use a single ``'\n'`` instead, on all platforms.
Georg Brandl116aa622007-08-15 14:28:22 +00001958
1959
1960.. data:: devnull
1961
Georg Brandlc575c902008-09-13 17:46:05 +00001962 The file path of the null device. For example: ``'/dev/null'`` for POSIX.
1963 Also available via :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00001964
Georg Brandl116aa622007-08-15 14:28:22 +00001965
1966.. _os-miscfunc:
1967
1968Miscellaneous Functions
1969-----------------------
1970
1971
1972.. function:: urandom(n)
1973
1974 Return a string of *n* random bytes suitable for cryptographic use.
1975
1976 This function returns random bytes from an OS-specific randomness source. The
1977 returned data should be unpredictable enough for cryptographic applications,
1978 though its exact quality depends on the OS implementation. On a UNIX-like
1979 system this will query /dev/urandom, and on Windows it will use CryptGenRandom.
1980 If a randomness source is not found, :exc:`NotImplementedError` will be raised.