blob: 58f057acc1567ad16f3c5b29ab617eac5c223c73 [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
Benjamin Peterson662c74f2010-05-06 22:09:03 +0000144 .. versionadded:: 3.2
145
Victor Stinner84ae1182010-05-06 22:05:07 +0000146
Georg Brandl116aa622007-08-15 14:28:22 +0000147.. function:: chdir(path)
148 fchdir(fd)
149 getcwd()
150 :noindex:
151
152 These functions are described in :ref:`os-file-dir`.
153
154
Gregory P. Smithb6e8c7e2010-02-27 07:22:22 +0000155.. function:: get_exec_path(env=None)
156
157 Returns the list of directories that will be searched for a named
158 executable, similar to a shell, when launching a process.
159 *env*, when specified, should be an environment variable dictionary
160 to lookup the PATH in.
161 By default, when *env* is None, :data:`environ` is used.
162
163 .. versionadded:: 3.2
164
165
Georg Brandl116aa622007-08-15 14:28:22 +0000166.. function:: ctermid()
167
168 Return the filename corresponding to the controlling terminal of the process.
169 Availability: Unix.
170
171
172.. function:: getegid()
173
174 Return the effective group id of the current process. This corresponds to the
Christian Heimesfaf2f632008-01-06 16:59:19 +0000175 "set id" bit on the file being executed in the current process. Availability:
Georg Brandl116aa622007-08-15 14:28:22 +0000176 Unix.
177
178
179.. function:: geteuid()
180
181 .. index:: single: user; effective id
182
Christian Heimesfaf2f632008-01-06 16:59:19 +0000183 Return the current process's effective user id. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000184
185
186.. function:: getgid()
187
188 .. index:: single: process; group
189
190 Return the real group id of the current process. Availability: Unix.
191
192
193.. function:: getgroups()
194
195 Return list of supplemental group ids associated with the current process.
196 Availability: Unix.
197
198
Antoine Pitroub7572f02009-12-02 20:46:48 +0000199.. function:: initgroups(username, gid)
200
201 Call the system initgroups() to initialize the group access list with all of
202 the groups of which the specified username is a member, plus the specified
203 group id. Availability: Unix.
204
205 .. versionadded:: 3.2
206
207
Georg Brandl116aa622007-08-15 14:28:22 +0000208.. function:: getlogin()
209
210 Return the name of the user logged in on the controlling terminal of the
211 process. For most purposes, it is more useful to use the environment variable
212 :envvar:`LOGNAME` to find out who the user is, or
213 ``pwd.getpwuid(os.getuid())[0]`` to get the login name of the currently
Christian Heimesfaf2f632008-01-06 16:59:19 +0000214 effective user id. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000215
216
217.. function:: getpgid(pid)
218
219 Return the process group id of the process with process id *pid*. If *pid* is 0,
220 the process group id of the current process is returned. Availability: Unix.
221
Georg Brandl116aa622007-08-15 14:28:22 +0000222
223.. function:: getpgrp()
224
225 .. index:: single: process; group
226
227 Return the id of the current process group. Availability: Unix.
228
229
230.. function:: getpid()
231
232 .. index:: single: process; id
233
234 Return the current process id. Availability: Unix, Windows.
235
236
237.. function:: getppid()
238
239 .. index:: single: process; id of parent
240
241 Return the parent's process id. Availability: Unix.
242
Georg Brandl1b83a452009-11-28 11:12:26 +0000243
Gregory P. Smithcf02c6a2009-11-27 17:54:17 +0000244.. function:: getresuid()
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000245
246 Return a tuple (ruid, euid, suid) denoting the current process's
247 real, effective, and saved user ids. Availability: Unix.
248
Georg Brandl1b83a452009-11-28 11:12:26 +0000249 .. versionadded:: 3.2
250
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000251
Gregory P. Smithcf02c6a2009-11-27 17:54:17 +0000252.. function:: getresgid()
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000253
254 Return a tuple (rgid, egid, sgid) denoting the current process's
255 real, effective, and saved user ids. Availability: Unix.
256
Georg Brandl1b83a452009-11-28 11:12:26 +0000257 .. versionadded:: 3.2
258
Georg Brandl116aa622007-08-15 14:28:22 +0000259
260.. function:: getuid()
261
262 .. index:: single: user; id
263
Christian Heimesfaf2f632008-01-06 16:59:19 +0000264 Return the current process's user id. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000265
266
Georg Brandl18244152009-09-02 20:34:52 +0000267.. function:: getenv(key, default=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000268
Georg Brandl18244152009-09-02 20:34:52 +0000269 Return the value of the environment variable *key* if it exists, or
Victor Stinner84ae1182010-05-06 22:05:07 +0000270 *default* if it doesn't. *key*, *default* and the result are str.
271 Availability: most flavors of Unix, Windows.
272
273 On Unix, keys and values are decoded with :func:`sys.getfilesystemencoding`
274 and ``'surrogateescape'`` error handler. Use :func:`os.getenvb` if you
275 would like to use a different encoding.
276
277
278.. function:: getenvb(key, default=None)
279
280 Return the value of the environment variable *key* if it exists, or
281 *default* if it doesn't. *key*, *default* and the result are bytes.
282 Availability: most flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000283
284
Georg Brandl18244152009-09-02 20:34:52 +0000285.. function:: putenv(key, value)
Georg Brandl116aa622007-08-15 14:28:22 +0000286
287 .. index:: single: environment variables; setting
288
Georg Brandl18244152009-09-02 20:34:52 +0000289 Set the environment variable named *key* to the string *value*. Such
Georg Brandl116aa622007-08-15 14:28:22 +0000290 changes to the environment affect subprocesses started with :func:`os.system`,
291 :func:`popen` or :func:`fork` and :func:`execv`. Availability: most flavors of
292 Unix, Windows.
293
294 .. note::
295
Georg Brandlc575c902008-09-13 17:46:05 +0000296 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
297 cause memory leaks. Refer to the system documentation for putenv.
Georg Brandl116aa622007-08-15 14:28:22 +0000298
299 When :func:`putenv` is supported, assignments to items in ``os.environ`` are
300 automatically translated into corresponding calls to :func:`putenv`; however,
301 calls to :func:`putenv` don't update ``os.environ``, so it is actually
302 preferable to assign to items of ``os.environ``.
303
304
305.. function:: setegid(egid)
306
307 Set the current process's effective group id. Availability: Unix.
308
309
310.. function:: seteuid(euid)
311
312 Set the current process's effective user id. Availability: Unix.
313
314
315.. function:: setgid(gid)
316
317 Set the current process' group id. Availability: Unix.
318
319
320.. function:: setgroups(groups)
321
322 Set the list of supplemental group ids associated with the current process to
323 *groups*. *groups* must be a sequence, and each element must be an integer
Christian Heimesfaf2f632008-01-06 16:59:19 +0000324 identifying a group. This operation is typically available only to the superuser.
Georg Brandl116aa622007-08-15 14:28:22 +0000325 Availability: Unix.
326
Georg Brandl116aa622007-08-15 14:28:22 +0000327
328.. function:: setpgrp()
329
Christian Heimesfaf2f632008-01-06 16:59:19 +0000330 Call the system call :cfunc:`setpgrp` or :cfunc:`setpgrp(0, 0)` depending on
Georg Brandl116aa622007-08-15 14:28:22 +0000331 which version is implemented (if any). See the Unix manual for the semantics.
332 Availability: Unix.
333
334
335.. function:: setpgid(pid, pgrp)
336
Christian Heimesfaf2f632008-01-06 16:59:19 +0000337 Call the system call :cfunc:`setpgid` to set the process group id of the
Georg Brandl116aa622007-08-15 14:28:22 +0000338 process with id *pid* to the process group with id *pgrp*. See the Unix manual
339 for the semantics. Availability: Unix.
340
341
Georg Brandl116aa622007-08-15 14:28:22 +0000342.. function:: setregid(rgid, egid)
343
344 Set the current process's real and effective group ids. Availability: Unix.
345
Georg Brandl1b83a452009-11-28 11:12:26 +0000346
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000347.. function:: setresgid(rgid, egid, sgid)
348
349 Set the current process's real, effective, and saved group ids.
350 Availability: Unix.
351
Georg Brandl1b83a452009-11-28 11:12:26 +0000352 .. versionadded:: 3.2
353
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000354
355.. function:: setresuid(ruid, euid, suid)
356
357 Set the current process's real, effective, and saved user ids.
358 Availibility: Unix.
359
Georg Brandl1b83a452009-11-28 11:12:26 +0000360 .. versionadded:: 3.2
361
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000362
363.. function:: setreuid(ruid, euid)
364
365 Set the current process's real and effective user ids. Availability: Unix.
366
Georg Brandl116aa622007-08-15 14:28:22 +0000367
368.. function:: getsid(pid)
369
Christian Heimesfaf2f632008-01-06 16:59:19 +0000370 Call the system call :cfunc:`getsid`. See the Unix manual for the semantics.
Georg Brandl116aa622007-08-15 14:28:22 +0000371 Availability: Unix.
372
Georg Brandl116aa622007-08-15 14:28:22 +0000373
374.. function:: setsid()
375
Christian Heimesfaf2f632008-01-06 16:59:19 +0000376 Call the system call :cfunc:`setsid`. See the Unix manual for the semantics.
Georg Brandl116aa622007-08-15 14:28:22 +0000377 Availability: Unix.
378
379
380.. function:: setuid(uid)
381
382 .. index:: single: user; id, setting
383
Christian Heimesfaf2f632008-01-06 16:59:19 +0000384 Set the current process's user id. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000385
Georg Brandl116aa622007-08-15 14:28:22 +0000386
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000387.. placed in this section since it relates to errno.... a little weak
Georg Brandl116aa622007-08-15 14:28:22 +0000388.. function:: strerror(code)
389
390 Return the error message corresponding to the error code in *code*.
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000391 On platforms where :cfunc:`strerror` returns ``NULL`` when given an unknown
392 error number, :exc:`ValueError` is raised. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000393
394
395.. function:: umask(mask)
396
Christian Heimesfaf2f632008-01-06 16:59:19 +0000397 Set the current numeric umask and return the previous umask. Availability:
Georg Brandl116aa622007-08-15 14:28:22 +0000398 Unix, Windows.
399
400
401.. function:: uname()
402
403 .. index::
404 single: gethostname() (in module socket)
405 single: gethostbyaddr() (in module socket)
406
407 Return a 5-tuple containing information identifying the current operating
408 system. The tuple contains 5 strings: ``(sysname, nodename, release, version,
409 machine)``. Some systems truncate the nodename to 8 characters or to the
410 leading component; a better way to get the hostname is
411 :func:`socket.gethostname` or even
412 ``socket.gethostbyaddr(socket.gethostname())``. Availability: recent flavors of
413 Unix.
414
415
Georg Brandl18244152009-09-02 20:34:52 +0000416.. function:: unsetenv(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000417
418 .. index:: single: environment variables; deleting
419
Georg Brandl18244152009-09-02 20:34:52 +0000420 Unset (delete) the environment variable named *key*. Such changes to the
Georg Brandl116aa622007-08-15 14:28:22 +0000421 environment affect subprocesses started with :func:`os.system`, :func:`popen` or
422 :func:`fork` and :func:`execv`. Availability: most flavors of Unix, Windows.
423
424 When :func:`unsetenv` is supported, deletion of items in ``os.environ`` is
425 automatically translated into a corresponding call to :func:`unsetenv`; however,
426 calls to :func:`unsetenv` don't update ``os.environ``, so it is actually
427 preferable to delete items of ``os.environ``.
428
429
430.. _os-newstreams:
431
432File Object Creation
433--------------------
434
435These functions create new file objects. (See also :func:`open`.)
436
437
438.. function:: fdopen(fd[, mode[, bufsize]])
439
440 .. index:: single: I/O control; buffering
441
442 Return an open file object connected to the file descriptor *fd*. The *mode*
443 and *bufsize* arguments have the same meaning as the corresponding arguments to
Georg Brandlc575c902008-09-13 17:46:05 +0000444 the built-in :func:`open` function. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000445
Georg Brandl55ac8f02007-09-01 13:51:09 +0000446 When specified, the *mode* argument must start with one of the letters
447 ``'r'``, ``'w'``, or ``'a'``, otherwise a :exc:`ValueError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000448
Georg Brandl55ac8f02007-09-01 13:51:09 +0000449 On Unix, when the *mode* argument starts with ``'a'``, the *O_APPEND* flag is
450 set on the file descriptor (which the :cfunc:`fdopen` implementation already
451 does on most platforms).
Georg Brandl116aa622007-08-15 14:28:22 +0000452
453
Georg Brandl116aa622007-08-15 14:28:22 +0000454.. _os-fd-ops:
455
456File Descriptor Operations
457--------------------------
458
459These functions operate on I/O streams referenced using file descriptors.
460
461File descriptors are small integers corresponding to a file that has been opened
462by the current process. For example, standard input is usually file descriptor
4630, standard output is 1, and standard error is 2. Further files opened by a
464process will then be assigned 3, 4, 5, and so forth. The name "file descriptor"
465is slightly deceptive; on Unix platforms, sockets and pipes are also referenced
466by file descriptors.
467
Benjamin Peterson08bf91c2010-04-11 16:12:57 +0000468The :meth:`~file.fileno` method can be used to obtain the file descriptor
469associated with a file object when required. Note that using the file
470descriptor directly will bypass the file object methods, ignoring aspects such
471as internal buffering of data.
Georg Brandl116aa622007-08-15 14:28:22 +0000472
473.. function:: close(fd)
474
Georg Brandlc575c902008-09-13 17:46:05 +0000475 Close file descriptor *fd*. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000476
477 .. note::
478
479 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000480 descriptor as returned by :func:`os.open` or :func:`pipe`. To close a "file
Georg Brandl116aa622007-08-15 14:28:22 +0000481 object" returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000482 :func:`fdopen`, use its :meth:`~file.close` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000483
484
Christian Heimesfdab48e2008-01-20 09:06:41 +0000485.. function:: closerange(fd_low, fd_high)
486
487 Close all file descriptors from *fd_low* (inclusive) to *fd_high* (exclusive),
Georg Brandlc575c902008-09-13 17:46:05 +0000488 ignoring errors. Availability: Unix, Windows. Equivalent to::
Christian Heimesfdab48e2008-01-20 09:06:41 +0000489
Georg Brandlc9a5a0e2009-09-01 07:34:27 +0000490 for fd in range(fd_low, fd_high):
Christian Heimesfdab48e2008-01-20 09:06:41 +0000491 try:
492 os.close(fd)
493 except OSError:
494 pass
495
Christian Heimesfdab48e2008-01-20 09:06:41 +0000496
Georg Brandl81f11302007-12-21 08:45:42 +0000497.. function:: device_encoding(fd)
498
499 Return a string describing the encoding of the device associated with *fd*
500 if it is connected to a terminal; else return :const:`None`.
501
502
Georg Brandl116aa622007-08-15 14:28:22 +0000503.. function:: dup(fd)
504
Georg Brandlc575c902008-09-13 17:46:05 +0000505 Return a duplicate of file descriptor *fd*. Availability: Unix,
Georg Brandl116aa622007-08-15 14:28:22 +0000506 Windows.
507
508
509.. function:: dup2(fd, fd2)
510
511 Duplicate file descriptor *fd* to *fd2*, closing the latter first if necessary.
Georg Brandlc575c902008-09-13 17:46:05 +0000512 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000513
514
Christian Heimes4e30a842007-11-30 22:12:06 +0000515.. function:: fchmod(fd, mode)
516
517 Change the mode of the file given by *fd* to the numeric *mode*. See the docs
518 for :func:`chmod` for possible values of *mode*. Availability: Unix.
519
520
521.. function:: fchown(fd, uid, gid)
522
523 Change the owner and group id of the file given by *fd* to the numeric *uid*
524 and *gid*. To leave one of the ids unchanged, set it to -1.
525 Availability: Unix.
526
527
Georg Brandl116aa622007-08-15 14:28:22 +0000528.. function:: fdatasync(fd)
529
530 Force write of file with filedescriptor *fd* to disk. Does not force update of
531 metadata. Availability: Unix.
532
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000533 .. note::
534 This function is not available on MacOS.
535
Georg Brandl116aa622007-08-15 14:28:22 +0000536
537.. function:: fpathconf(fd, name)
538
539 Return system configuration information relevant to an open file. *name*
540 specifies the configuration value to retrieve; it may be a string which is the
541 name of a defined system value; these names are specified in a number of
542 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
543 additional names as well. The names known to the host operating system are
544 given in the ``pathconf_names`` dictionary. For configuration variables not
545 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandlc575c902008-09-13 17:46:05 +0000546 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000547
548 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
549 specific value for *name* is not supported by the host system, even if it is
550 included in ``pathconf_names``, an :exc:`OSError` is raised with
551 :const:`errno.EINVAL` for the error number.
552
553
554.. function:: fstat(fd)
555
556 Return status for file descriptor *fd*, like :func:`stat`. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +0000557 Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000558
559
560.. function:: fstatvfs(fd)
561
562 Return information about the filesystem containing the file associated with file
563 descriptor *fd*, like :func:`statvfs`. Availability: Unix.
564
565
566.. function:: fsync(fd)
567
568 Force write of file with filedescriptor *fd* to disk. On Unix, this calls the
569 native :cfunc:`fsync` function; on Windows, the MS :cfunc:`_commit` function.
570
571 If you're starting with a Python file object *f*, first do ``f.flush()``, and
572 then do ``os.fsync(f.fileno())``, to ensure that all internal buffers associated
Georg Brandlc575c902008-09-13 17:46:05 +0000573 with *f* are written to disk. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000574
575
576.. function:: ftruncate(fd, length)
577
578 Truncate the file corresponding to file descriptor *fd*, so that it is at most
Georg Brandlc575c902008-09-13 17:46:05 +0000579 *length* bytes in size. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000580
581
582.. function:: isatty(fd)
583
584 Return ``True`` if the file descriptor *fd* is open and connected to a
Georg Brandlc575c902008-09-13 17:46:05 +0000585 tty(-like) device, else ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000586
587
588.. function:: lseek(fd, pos, how)
589
Christian Heimesfaf2f632008-01-06 16:59:19 +0000590 Set the current position of file descriptor *fd* to position *pos*, modified
591 by *how*: :const:`SEEK_SET` or ``0`` to set the position relative to the
592 beginning of the file; :const:`SEEK_CUR` or ``1`` to set it relative to the
593 current position; :const:`os.SEEK_END` or ``2`` to set it relative to the end of
Georg Brandlc575c902008-09-13 17:46:05 +0000594 the file. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000595
596
597.. function:: open(file, flags[, mode])
598
Georg Brandlf4a41232008-05-26 17:55:52 +0000599 Open the file *file* and set various flags according to *flags* and possibly
600 its mode according to *mode*. The default *mode* is ``0o777`` (octal), and
601 the current umask value is first masked out. Return the file descriptor for
Georg Brandlc575c902008-09-13 17:46:05 +0000602 the newly opened file. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000603
604 For a description of the flag and mode values, see the C run-time documentation;
605 flag constants (like :const:`O_RDONLY` and :const:`O_WRONLY`) are defined in
606 this module too (see below).
607
608 .. note::
609
Georg Brandl502d9a52009-07-26 15:02:41 +0000610 This function is intended for low-level I/O. For normal usage, use the
611 built-in function :func:`open`, which returns a "file object" with
612 :meth:`~file.read` and :meth:`~file.write` methods (and many more). To
613 wrap a file descriptor in a "file object", use :func:`fdopen`.
Georg Brandl116aa622007-08-15 14:28:22 +0000614
615
616.. function:: openpty()
617
618 .. index:: module: pty
619
620 Open a new pseudo-terminal pair. Return a pair of file descriptors ``(master,
621 slave)`` for the pty and the tty, respectively. For a (slightly) more portable
Georg Brandlc575c902008-09-13 17:46:05 +0000622 approach, use the :mod:`pty` module. Availability: some flavors of
Georg Brandl116aa622007-08-15 14:28:22 +0000623 Unix.
624
625
626.. function:: pipe()
627
628 Create a pipe. Return a pair of file descriptors ``(r, w)`` usable for reading
Georg Brandlc575c902008-09-13 17:46:05 +0000629 and writing, respectively. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000630
631
632.. function:: read(fd, n)
633
Georg Brandlb90be692009-07-29 16:14:16 +0000634 Read at most *n* bytes from file descriptor *fd*. Return a bytestring containing the
Georg Brandl116aa622007-08-15 14:28:22 +0000635 bytes read. If the end of the file referred to by *fd* has been reached, an
Georg Brandlb90be692009-07-29 16:14:16 +0000636 empty bytes object is returned. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000637
638 .. note::
639
640 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000641 descriptor as returned by :func:`os.open` or :func:`pipe`. To read a "file object"
Georg Brandl116aa622007-08-15 14:28:22 +0000642 returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000643 :func:`fdopen`, or :data:`sys.stdin`, use its :meth:`~file.read` or
644 :meth:`~file.readline` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000645
646
647.. function:: tcgetpgrp(fd)
648
649 Return the process group associated with the terminal given by *fd* (an open
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000650 file descriptor as returned by :func:`os.open`). Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000651
652
653.. function:: tcsetpgrp(fd, pg)
654
655 Set the process group associated with the terminal given by *fd* (an open file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000656 descriptor as returned by :func:`os.open`) to *pg*. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000657
658
659.. function:: ttyname(fd)
660
661 Return a string which specifies the terminal device associated with
Georg Brandl9afde1c2007-11-01 20:32:30 +0000662 file descriptor *fd*. If *fd* is not associated with a terminal device, an
Georg Brandlc575c902008-09-13 17:46:05 +0000663 exception is raised. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000664
665
666.. function:: write(fd, str)
667
Georg Brandlb90be692009-07-29 16:14:16 +0000668 Write the bytestring in *str* to file descriptor *fd*. Return the number of
669 bytes actually written. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000670
671 .. note::
672
673 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000674 descriptor as returned by :func:`os.open` or :func:`pipe`. To write a "file
Georg Brandl116aa622007-08-15 14:28:22 +0000675 object" returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000676 :func:`fdopen`, or :data:`sys.stdout` or :data:`sys.stderr`, use its
677 :meth:`~file.write` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000678
Georg Brandlaf265f42008-12-07 15:06:20 +0000679The following constants are options for the *flags* parameter to the
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000680:func:`~os.open` function. They can be combined using the bitwise OR operator
Georg Brandlaf265f42008-12-07 15:06:20 +0000681``|``. Some of them are not available on all platforms. For descriptions of
682their availability and use, consult the :manpage:`open(2)` manual page on Unix
Doug Hellmanneb097fc2009-09-20 20:56:56 +0000683or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000684
685
686.. data:: O_RDONLY
687 O_WRONLY
688 O_RDWR
689 O_APPEND
690 O_CREAT
691 O_EXCL
692 O_TRUNC
693
Georg Brandlaf265f42008-12-07 15:06:20 +0000694 These constants are available on Unix and Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000695
696
697.. data:: O_DSYNC
698 O_RSYNC
699 O_SYNC
700 O_NDELAY
701 O_NONBLOCK
702 O_NOCTTY
703 O_SHLOCK
704 O_EXLOCK
705
Georg Brandlaf265f42008-12-07 15:06:20 +0000706 These constants are only available on Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000707
708
709.. data:: O_BINARY
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000710 O_NOINHERIT
Georg Brandl116aa622007-08-15 14:28:22 +0000711 O_SHORT_LIVED
712 O_TEMPORARY
713 O_RANDOM
714 O_SEQUENTIAL
715 O_TEXT
716
Georg Brandlaf265f42008-12-07 15:06:20 +0000717 These constants are only available on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000718
719
Alexandre Vassalottibee32532008-05-16 18:15:12 +0000720.. data:: O_ASYNC
721 O_DIRECT
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000722 O_DIRECTORY
723 O_NOFOLLOW
724 O_NOATIME
725
Georg Brandlaf265f42008-12-07 15:06:20 +0000726 These constants are GNU extensions and not present if they are not defined by
727 the C library.
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000728
729
Georg Brandl116aa622007-08-15 14:28:22 +0000730.. data:: SEEK_SET
731 SEEK_CUR
732 SEEK_END
733
734 Parameters to the :func:`lseek` function. Their values are 0, 1, and 2,
Georg Brandlc575c902008-09-13 17:46:05 +0000735 respectively. Availability: Windows, Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000736
Georg Brandl116aa622007-08-15 14:28:22 +0000737
738.. _os-file-dir:
739
740Files and Directories
741---------------------
742
Georg Brandl116aa622007-08-15 14:28:22 +0000743.. function:: access(path, mode)
744
745 Use the real uid/gid to test for access to *path*. Note that most operations
746 will use the effective uid/gid, therefore this routine can be used in a
747 suid/sgid environment to test if the invoking user has the specified access to
748 *path*. *mode* should be :const:`F_OK` to test the existence of *path*, or it
749 can be the inclusive OR of one or more of :const:`R_OK`, :const:`W_OK`, and
750 :const:`X_OK` to test permissions. Return :const:`True` if access is allowed,
751 :const:`False` if not. See the Unix man page :manpage:`access(2)` for more
Georg Brandlc575c902008-09-13 17:46:05 +0000752 information. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000753
754 .. note::
755
Georg Brandl502d9a52009-07-26 15:02:41 +0000756 Using :func:`access` to check if a user is authorized to e.g. open a file
757 before actually doing so using :func:`open` creates a security hole,
758 because the user might exploit the short time interval between checking
759 and opening the file to manipulate it.
Georg Brandl116aa622007-08-15 14:28:22 +0000760
761 .. note::
762
763 I/O operations may fail even when :func:`access` indicates that they would
764 succeed, particularly for operations on network filesystems which may have
765 permissions semantics beyond the usual POSIX permission-bit model.
766
767
768.. data:: F_OK
769
770 Value to pass as the *mode* parameter of :func:`access` to test the existence of
771 *path*.
772
773
774.. data:: R_OK
775
776 Value to include in the *mode* parameter of :func:`access` to test the
777 readability of *path*.
778
779
780.. data:: W_OK
781
782 Value to include in the *mode* parameter of :func:`access` to test the
783 writability of *path*.
784
785
786.. data:: X_OK
787
788 Value to include in the *mode* parameter of :func:`access` to determine if
789 *path* can be executed.
790
791
792.. function:: chdir(path)
793
794 .. index:: single: directory; changing
795
Georg Brandlc575c902008-09-13 17:46:05 +0000796 Change the current working directory to *path*. Availability: Unix,
Georg Brandl116aa622007-08-15 14:28:22 +0000797 Windows.
798
799
800.. function:: fchdir(fd)
801
802 Change the current working directory to the directory represented by the file
803 descriptor *fd*. The descriptor must refer to an opened directory, not an open
804 file. Availability: Unix.
805
Georg Brandl116aa622007-08-15 14:28:22 +0000806
807.. function:: getcwd()
808
Martin v. Löwis011e8422009-05-05 04:43:17 +0000809 Return a string representing the current working directory.
810 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000811
Martin v. Löwisa731b992008-10-07 06:36:31 +0000812.. function:: getcwdb()
Georg Brandl116aa622007-08-15 14:28:22 +0000813
Georg Brandl76e55382008-10-08 16:34:57 +0000814 Return a bytestring representing the current working directory.
Georg Brandlc575c902008-09-13 17:46:05 +0000815 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000816
Georg Brandl116aa622007-08-15 14:28:22 +0000817
818.. function:: chflags(path, flags)
819
820 Set the flags of *path* to the numeric *flags*. *flags* may take a combination
821 (bitwise OR) of the following values (as defined in the :mod:`stat` module):
822
823 * ``UF_NODUMP``
824 * ``UF_IMMUTABLE``
825 * ``UF_APPEND``
826 * ``UF_OPAQUE``
827 * ``UF_NOUNLINK``
828 * ``SF_ARCHIVED``
829 * ``SF_IMMUTABLE``
830 * ``SF_APPEND``
831 * ``SF_NOUNLINK``
832 * ``SF_SNAPSHOT``
833
Georg Brandlc575c902008-09-13 17:46:05 +0000834 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000835
Georg Brandl116aa622007-08-15 14:28:22 +0000836
837.. function:: chroot(path)
838
839 Change the root directory of the current process to *path*. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +0000840 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000841
Georg Brandl116aa622007-08-15 14:28:22 +0000842
843.. function:: chmod(path, mode)
844
845 Change the mode of *path* to the numeric *mode*. *mode* may take one of the
Christian Heimesfaf2f632008-01-06 16:59:19 +0000846 following values (as defined in the :mod:`stat` module) or bitwise ORed
Georg Brandl116aa622007-08-15 14:28:22 +0000847 combinations of them:
848
Alexandre Vassalottic22c6f22009-07-21 00:51:58 +0000849 * :data:`stat.S_ISUID`
850 * :data:`stat.S_ISGID`
851 * :data:`stat.S_ENFMT`
852 * :data:`stat.S_ISVTX`
853 * :data:`stat.S_IREAD`
854 * :data:`stat.S_IWRITE`
855 * :data:`stat.S_IEXEC`
856 * :data:`stat.S_IRWXU`
857 * :data:`stat.S_IRUSR`
858 * :data:`stat.S_IWUSR`
859 * :data:`stat.S_IXUSR`
860 * :data:`stat.S_IRWXG`
861 * :data:`stat.S_IRGRP`
862 * :data:`stat.S_IWGRP`
863 * :data:`stat.S_IXGRP`
864 * :data:`stat.S_IRWXO`
865 * :data:`stat.S_IROTH`
866 * :data:`stat.S_IWOTH`
867 * :data:`stat.S_IXOTH`
Georg Brandl116aa622007-08-15 14:28:22 +0000868
Georg Brandlc575c902008-09-13 17:46:05 +0000869 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000870
871 .. note::
872
873 Although Windows supports :func:`chmod`, you can only set the file's read-only
874 flag with it (via the ``stat.S_IWRITE`` and ``stat.S_IREAD``
875 constants or a corresponding integer value). All other bits are
876 ignored.
877
878
879.. function:: chown(path, uid, gid)
880
881 Change the owner and group id of *path* to the numeric *uid* and *gid*. To leave
Georg Brandlc575c902008-09-13 17:46:05 +0000882 one of the ids unchanged, set it to -1. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000883
884
885.. function:: lchflags(path, flags)
886
887 Set the flags of *path* to the numeric *flags*, like :func:`chflags`, but do not
888 follow symbolic links. Availability: Unix.
889
Georg Brandl116aa622007-08-15 14:28:22 +0000890
Christian Heimes93852662007-12-01 12:22:32 +0000891.. function:: lchmod(path, mode)
892
893 Change the mode of *path* to the numeric *mode*. If path is a symlink, this
894 affects the symlink rather than the target. See the docs for :func:`chmod`
895 for possible values of *mode*. Availability: Unix.
896
Christian Heimes93852662007-12-01 12:22:32 +0000897
Georg Brandl116aa622007-08-15 14:28:22 +0000898.. function:: lchown(path, uid, gid)
899
Christian Heimesfaf2f632008-01-06 16:59:19 +0000900 Change the owner and group id of *path* to the numeric *uid* and *gid*. This
Georg Brandlc575c902008-09-13 17:46:05 +0000901 function will not follow symbolic links. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000902
Georg Brandl116aa622007-08-15 14:28:22 +0000903
Benjamin Peterson5879d412009-03-30 14:51:56 +0000904.. function:: link(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +0000905
Benjamin Peterson5879d412009-03-30 14:51:56 +0000906 Create a hard link pointing to *source* named *link_name*. Availability:
907 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000908
909
910.. function:: listdir(path)
911
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000912 Return a list containing the names of the entries in the directory given by
913 *path*. The list is in arbitrary order. It does not include the special
914 entries ``'.'`` and ``'..'`` even if they are present in the directory.
915 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000916
Martin v. Löwis011e8422009-05-05 04:43:17 +0000917 This function can be called with a bytes or string argument, and returns
918 filenames of the same datatype.
Georg Brandl116aa622007-08-15 14:28:22 +0000919
920
921.. function:: lstat(path)
922
Guido van Rossum2cc30da2007-11-02 23:46:40 +0000923 Like :func:`stat`, but do not follow symbolic links. This is an alias for
924 :func:`stat` on platforms that do not support symbolic links, such as
925 Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000926
927
928.. function:: mkfifo(path[, mode])
929
Georg Brandlf4a41232008-05-26 17:55:52 +0000930 Create a FIFO (a named pipe) named *path* with numeric mode *mode*. The
931 default *mode* is ``0o666`` (octal). The current umask value is first masked
Georg Brandlc575c902008-09-13 17:46:05 +0000932 out from the mode. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000933
934 FIFOs are pipes that can be accessed like regular files. FIFOs exist until they
935 are deleted (for example with :func:`os.unlink`). Generally, FIFOs are used as
936 rendezvous between "client" and "server" type processes: the server opens the
937 FIFO for reading, and the client opens it for writing. Note that :func:`mkfifo`
938 doesn't open the FIFO --- it just creates the rendezvous point.
939
940
Georg Brandl18244152009-09-02 20:34:52 +0000941.. function:: mknod(filename[, mode=0o600[, device]])
Georg Brandl116aa622007-08-15 14:28:22 +0000942
943 Create a filesystem node (file, device special file or named pipe) named
Georg Brandl18244152009-09-02 20:34:52 +0000944 *filename*. *mode* specifies both the permissions to use and the type of node
945 to be created, being combined (bitwise OR) with one of ``stat.S_IFREG``,
946 ``stat.S_IFCHR``, ``stat.S_IFBLK``, and ``stat.S_IFIFO`` (those constants are
947 available in :mod:`stat`). For ``stat.S_IFCHR`` and ``stat.S_IFBLK``,
948 *device* defines the newly created device special file (probably using
Georg Brandl116aa622007-08-15 14:28:22 +0000949 :func:`os.makedev`), otherwise it is ignored.
950
Georg Brandl116aa622007-08-15 14:28:22 +0000951
952.. function:: major(device)
953
Christian Heimesfaf2f632008-01-06 16:59:19 +0000954 Extract the device major number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000955 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
956
Georg Brandl116aa622007-08-15 14:28:22 +0000957
958.. function:: minor(device)
959
Christian Heimesfaf2f632008-01-06 16:59:19 +0000960 Extract the device minor number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000961 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
962
Georg Brandl116aa622007-08-15 14:28:22 +0000963
964.. function:: makedev(major, minor)
965
Christian Heimesfaf2f632008-01-06 16:59:19 +0000966 Compose a raw device number from the major and minor device numbers.
Georg Brandl116aa622007-08-15 14:28:22 +0000967
Georg Brandl116aa622007-08-15 14:28:22 +0000968
969.. function:: mkdir(path[, mode])
970
Georg Brandlf4a41232008-05-26 17:55:52 +0000971 Create a directory named *path* with numeric mode *mode*. The default *mode*
972 is ``0o777`` (octal). On some systems, *mode* is ignored. Where it is used,
Georg Brandlc575c902008-09-13 17:46:05 +0000973 the current umask value is first masked out. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000974
Guido van Rossum2cc30da2007-11-02 23:46:40 +0000975 It is also possible to create temporary directories; see the
976 :mod:`tempfile` module's :func:`tempfile.mkdtemp` function.
977
Georg Brandl116aa622007-08-15 14:28:22 +0000978
979.. function:: makedirs(path[, mode])
980
981 .. index::
982 single: directory; creating
983 single: UNC paths; and os.makedirs()
984
985 Recursive directory creation function. Like :func:`mkdir`, but makes all
Georg Brandlf4a41232008-05-26 17:55:52 +0000986 intermediate-level directories needed to contain the leaf directory. Throws
987 an :exc:`error` exception if the leaf directory already exists or cannot be
988 created. The default *mode* is ``0o777`` (octal). On some systems, *mode*
989 is ignored. Where it is used, the current umask value is first masked out.
Georg Brandl116aa622007-08-15 14:28:22 +0000990
991 .. note::
992
993 :func:`makedirs` will become confused if the path elements to create include
Christian Heimesfaf2f632008-01-06 16:59:19 +0000994 :data:`os.pardir`.
Georg Brandl116aa622007-08-15 14:28:22 +0000995
Georg Brandl55ac8f02007-09-01 13:51:09 +0000996 This function handles UNC paths correctly.
Georg Brandl116aa622007-08-15 14:28:22 +0000997
998
999.. function:: pathconf(path, name)
1000
1001 Return system configuration information relevant to a named file. *name*
1002 specifies the configuration value to retrieve; it may be a string which is the
1003 name of a defined system value; these names are specified in a number of
1004 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
1005 additional names as well. The names known to the host operating system are
1006 given in the ``pathconf_names`` dictionary. For configuration variables not
1007 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandlc575c902008-09-13 17:46:05 +00001008 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001009
1010 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1011 specific value for *name* is not supported by the host system, even if it is
1012 included in ``pathconf_names``, an :exc:`OSError` is raised with
1013 :const:`errno.EINVAL` for the error number.
1014
1015
1016.. data:: pathconf_names
1017
1018 Dictionary mapping names accepted by :func:`pathconf` and :func:`fpathconf` to
1019 the integer values defined for those names by the host operating system. This
1020 can be used to determine the set of names known to the system. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001021 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001022
1023
1024.. function:: readlink(path)
1025
1026 Return a string representing the path to which the symbolic link points. The
1027 result may be either an absolute or relative pathname; if it is relative, it may
1028 be converted to an absolute pathname using ``os.path.join(os.path.dirname(path),
1029 result)``.
1030
Georg Brandl76e55382008-10-08 16:34:57 +00001031 If the *path* is a string object, the result will also be a string object,
1032 and the call may raise an UnicodeDecodeError. If the *path* is a bytes
1033 object, the result will be a bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +00001034
Georg Brandlc575c902008-09-13 17:46:05 +00001035 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001036
1037
1038.. function:: remove(path)
1039
Georg Brandla6053b42009-09-01 08:11:14 +00001040 Remove (delete) the file *path*. If *path* is a directory, :exc:`OSError` is
1041 raised; see :func:`rmdir` below to remove a directory. This is identical to
1042 the :func:`unlink` function documented below. On Windows, attempting to
1043 remove a file that is in use causes an exception to be raised; on Unix, the
1044 directory entry is removed but the storage allocated to the file is not made
1045 available until the original file is no longer in use. Availability: Unix,
Georg Brandl116aa622007-08-15 14:28:22 +00001046 Windows.
1047
1048
1049.. function:: removedirs(path)
1050
1051 .. index:: single: directory; deleting
1052
Christian Heimesfaf2f632008-01-06 16:59:19 +00001053 Remove directories recursively. Works like :func:`rmdir` except that, if the
Georg Brandl116aa622007-08-15 14:28:22 +00001054 leaf directory is successfully removed, :func:`removedirs` tries to
1055 successively remove every parent directory mentioned in *path* until an error
1056 is raised (which is ignored, because it generally means that a parent directory
1057 is not empty). For example, ``os.removedirs('foo/bar/baz')`` will first remove
1058 the directory ``'foo/bar/baz'``, and then remove ``'foo/bar'`` and ``'foo'`` if
1059 they are empty. Raises :exc:`OSError` if the leaf directory could not be
1060 successfully removed.
1061
Georg Brandl116aa622007-08-15 14:28:22 +00001062
1063.. function:: rename(src, dst)
1064
1065 Rename the file or directory *src* to *dst*. If *dst* is a directory,
1066 :exc:`OSError` will be raised. On Unix, if *dst* exists and is a file, it will
Christian Heimesfaf2f632008-01-06 16:59:19 +00001067 be replaced silently if the user has permission. The operation may fail on some
Georg Brandl116aa622007-08-15 14:28:22 +00001068 Unix flavors if *src* and *dst* are on different filesystems. If successful,
1069 the renaming will be an atomic operation (this is a POSIX requirement). On
1070 Windows, if *dst* already exists, :exc:`OSError` will be raised even if it is a
1071 file; there may be no way to implement an atomic rename when *dst* names an
Georg Brandlc575c902008-09-13 17:46:05 +00001072 existing file. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001073
1074
1075.. function:: renames(old, new)
1076
1077 Recursive directory or file renaming function. Works like :func:`rename`, except
1078 creation of any intermediate directories needed to make the new pathname good is
1079 attempted first. After the rename, directories corresponding to rightmost path
1080 segments of the old name will be pruned away using :func:`removedirs`.
1081
Georg Brandl116aa622007-08-15 14:28:22 +00001082 .. note::
1083
1084 This function can fail with the new directory structure made if you lack
1085 permissions needed to remove the leaf directory or file.
1086
1087
1088.. function:: rmdir(path)
1089
Georg Brandla6053b42009-09-01 08:11:14 +00001090 Remove (delete) the directory *path*. Only works when the directory is
1091 empty, otherwise, :exc:`OSError` is raised. In order to remove whole
1092 directory trees, :func:`shutil.rmtree` can be used. Availability: Unix,
1093 Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001094
1095
1096.. function:: stat(path)
1097
1098 Perform a :cfunc:`stat` system call on the given path. The return value is an
1099 object whose attributes correspond to the members of the :ctype:`stat`
1100 structure, namely: :attr:`st_mode` (protection bits), :attr:`st_ino` (inode
1101 number), :attr:`st_dev` (device), :attr:`st_nlink` (number of hard links),
Christian Heimesfaf2f632008-01-06 16:59:19 +00001102 :attr:`st_uid` (user id of owner), :attr:`st_gid` (group id of owner),
Georg Brandl116aa622007-08-15 14:28:22 +00001103 :attr:`st_size` (size of file, in bytes), :attr:`st_atime` (time of most recent
1104 access), :attr:`st_mtime` (time of most recent content modification),
1105 :attr:`st_ctime` (platform dependent; time of most recent metadata change on
1106 Unix, or the time of creation on Windows)::
1107
1108 >>> import os
1109 >>> statinfo = os.stat('somefile.txt')
1110 >>> statinfo
Georg Brandlf66df2b2010-01-16 14:41:21 +00001111 (33188, 422511, 769, 1, 1032, 100, 926, 1105022698,1105022732, 1105022732)
Georg Brandl116aa622007-08-15 14:28:22 +00001112 >>> statinfo.st_size
Georg Brandlf66df2b2010-01-16 14:41:21 +00001113 926
Georg Brandl116aa622007-08-15 14:28:22 +00001114 >>>
1115
Georg Brandl116aa622007-08-15 14:28:22 +00001116
1117 On some Unix systems (such as Linux), the following attributes may also be
1118 available: :attr:`st_blocks` (number of blocks allocated for file),
1119 :attr:`st_blksize` (filesystem blocksize), :attr:`st_rdev` (type of device if an
1120 inode device). :attr:`st_flags` (user defined flags for file).
1121
1122 On other Unix systems (such as FreeBSD), the following attributes may be
1123 available (but may be only filled out if root tries to use them): :attr:`st_gen`
1124 (file generation number), :attr:`st_birthtime` (time of file creation).
1125
1126 On Mac OS systems, the following attributes may also be available:
1127 :attr:`st_rsize`, :attr:`st_creator`, :attr:`st_type`.
1128
Georg Brandl116aa622007-08-15 14:28:22 +00001129 .. index:: module: stat
1130
1131 For backward compatibility, the return value of :func:`stat` is also accessible
1132 as a tuple of at least 10 integers giving the most important (and portable)
1133 members of the :ctype:`stat` structure, in the order :attr:`st_mode`,
1134 :attr:`st_ino`, :attr:`st_dev`, :attr:`st_nlink`, :attr:`st_uid`,
1135 :attr:`st_gid`, :attr:`st_size`, :attr:`st_atime`, :attr:`st_mtime`,
1136 :attr:`st_ctime`. More items may be added at the end by some implementations.
1137 The standard module :mod:`stat` defines functions and constants that are useful
1138 for extracting information from a :ctype:`stat` structure. (On Windows, some
1139 items are filled with dummy values.)
1140
1141 .. note::
1142
1143 The exact meaning and resolution of the :attr:`st_atime`, :attr:`st_mtime`, and
1144 :attr:`st_ctime` members depends on the operating system and the file system.
1145 For example, on Windows systems using the FAT or FAT32 file systems,
1146 :attr:`st_mtime` has 2-second resolution, and :attr:`st_atime` has only 1-day
1147 resolution. See your operating system documentation for details.
1148
Georg Brandlc575c902008-09-13 17:46:05 +00001149 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001150
Georg Brandl116aa622007-08-15 14:28:22 +00001151
1152.. function:: stat_float_times([newvalue])
1153
1154 Determine whether :class:`stat_result` represents time stamps as float objects.
1155 If *newvalue* is ``True``, future calls to :func:`stat` return floats, if it is
1156 ``False``, future calls return ints. If *newvalue* is omitted, return the
1157 current setting.
1158
1159 For compatibility with older Python versions, accessing :class:`stat_result` as
1160 a tuple always returns integers.
1161
Georg Brandl55ac8f02007-09-01 13:51:09 +00001162 Python now returns float values by default. Applications which do not work
1163 correctly with floating point time stamps can use this function to restore the
1164 old behaviour.
Georg Brandl116aa622007-08-15 14:28:22 +00001165
1166 The resolution of the timestamps (that is the smallest possible fraction)
1167 depends on the system. Some systems only support second resolution; on these
1168 systems, the fraction will always be zero.
1169
1170 It is recommended that this setting is only changed at program startup time in
1171 the *__main__* module; libraries should never change this setting. If an
1172 application uses a library that works incorrectly if floating point time stamps
1173 are processed, this application should turn the feature off until the library
1174 has been corrected.
1175
1176
1177.. function:: statvfs(path)
1178
1179 Perform a :cfunc:`statvfs` system call on the given path. The return value is
1180 an object whose attributes describe the filesystem on the given path, and
1181 correspond to the members of the :ctype:`statvfs` structure, namely:
1182 :attr:`f_bsize`, :attr:`f_frsize`, :attr:`f_blocks`, :attr:`f_bfree`,
1183 :attr:`f_bavail`, :attr:`f_files`, :attr:`f_ffree`, :attr:`f_favail`,
1184 :attr:`f_flag`, :attr:`f_namemax`. Availability: Unix.
1185
Georg Brandl116aa622007-08-15 14:28:22 +00001186
Benjamin Peterson5879d412009-03-30 14:51:56 +00001187.. function:: symlink(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +00001188
Benjamin Peterson5879d412009-03-30 14:51:56 +00001189 Create a symbolic link pointing to *source* named *link_name*. Availability:
1190 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001191
1192
Georg Brandl116aa622007-08-15 14:28:22 +00001193.. function:: unlink(path)
1194
Georg Brandla6053b42009-09-01 08:11:14 +00001195 Remove (delete) the file *path*. This is the same function as
1196 :func:`remove`; the :func:`unlink` name is its traditional Unix
1197 name. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001198
1199
1200.. function:: utime(path, times)
1201
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001202 Set the access and modified times of the file specified by *path*. If *times*
1203 is ``None``, then the file's access and modified times are set to the current
1204 time. (The effect is similar to running the Unix program :program:`touch` on
1205 the path.) Otherwise, *times* must be a 2-tuple of numbers, of the form
1206 ``(atime, mtime)`` which is used to set the access and modified times,
1207 respectively. Whether a directory can be given for *path* depends on whether
1208 the operating system implements directories as files (for example, Windows
1209 does not). Note that the exact times you set here may not be returned by a
1210 subsequent :func:`stat` call, depending on the resolution with which your
1211 operating system records access and modification times; see :func:`stat`.
Georg Brandl116aa622007-08-15 14:28:22 +00001212
Georg Brandlc575c902008-09-13 17:46:05 +00001213 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001214
1215
Georg Brandl18244152009-09-02 20:34:52 +00001216.. function:: walk(top, topdown=True, onerror=None, followlinks=False)
Georg Brandl116aa622007-08-15 14:28:22 +00001217
1218 .. index::
1219 single: directory; walking
1220 single: directory; traversal
1221
Christian Heimesfaf2f632008-01-06 16:59:19 +00001222 Generate the file names in a directory tree by walking the tree
1223 either top-down or bottom-up. For each directory in the tree rooted at directory
Georg Brandl116aa622007-08-15 14:28:22 +00001224 *top* (including *top* itself), it yields a 3-tuple ``(dirpath, dirnames,
1225 filenames)``.
1226
1227 *dirpath* is a string, the path to the directory. *dirnames* is a list of the
1228 names of the subdirectories in *dirpath* (excluding ``'.'`` and ``'..'``).
1229 *filenames* is a list of the names of the non-directory files in *dirpath*.
1230 Note that the names in the lists contain no path components. To get a full path
1231 (which begins with *top*) to a file or directory in *dirpath*, do
1232 ``os.path.join(dirpath, name)``.
1233
Christian Heimesfaf2f632008-01-06 16:59:19 +00001234 If optional argument *topdown* is ``True`` or not specified, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001235 directory is generated before the triples for any of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001236 (directories are generated top-down). If *topdown* is ``False``, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001237 directory is generated after the triples for all of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001238 (directories are generated bottom-up).
Georg Brandl116aa622007-08-15 14:28:22 +00001239
Christian Heimesfaf2f632008-01-06 16:59:19 +00001240 When *topdown* is ``True``, the caller can modify the *dirnames* list in-place
Georg Brandl116aa622007-08-15 14:28:22 +00001241 (perhaps using :keyword:`del` or slice assignment), and :func:`walk` will only
1242 recurse into the subdirectories whose names remain in *dirnames*; this can be
1243 used to prune the search, impose a specific order of visiting, or even to inform
1244 :func:`walk` about directories the caller creates or renames before it resumes
Christian Heimesfaf2f632008-01-06 16:59:19 +00001245 :func:`walk` again. Modifying *dirnames* when *topdown* is ``False`` is
Georg Brandl116aa622007-08-15 14:28:22 +00001246 ineffective, because in bottom-up mode the directories in *dirnames* are
1247 generated before *dirpath* itself is generated.
1248
Christian Heimesfaf2f632008-01-06 16:59:19 +00001249 By default errors from the :func:`listdir` call are ignored. If optional
Georg Brandl116aa622007-08-15 14:28:22 +00001250 argument *onerror* is specified, it should be a function; it will be called with
1251 one argument, an :exc:`OSError` instance. It can report the error to continue
1252 with the walk, or raise the exception to abort the walk. Note that the filename
1253 is available as the ``filename`` attribute of the exception object.
1254
1255 By default, :func:`walk` will not walk down into symbolic links that resolve to
Christian Heimesfaf2f632008-01-06 16:59:19 +00001256 directories. Set *followlinks* to ``True`` to visit directories pointed to by
Georg Brandl116aa622007-08-15 14:28:22 +00001257 symlinks, on systems that support them.
1258
Georg Brandl116aa622007-08-15 14:28:22 +00001259 .. note::
1260
Christian Heimesfaf2f632008-01-06 16:59:19 +00001261 Be aware that setting *followlinks* to ``True`` can lead to infinite recursion if a
Georg Brandl116aa622007-08-15 14:28:22 +00001262 link points to a parent directory of itself. :func:`walk` does not keep track of
1263 the directories it visited already.
1264
1265 .. note::
1266
1267 If you pass a relative pathname, don't change the current working directory
1268 between resumptions of :func:`walk`. :func:`walk` never changes the current
1269 directory, and assumes that its caller doesn't either.
1270
1271 This example displays the number of bytes taken by non-directory files in each
1272 directory under the starting directory, except that it doesn't look under any
1273 CVS subdirectory::
1274
1275 import os
1276 from os.path import join, getsize
1277 for root, dirs, files in os.walk('python/Lib/email'):
Georg Brandl6911e3c2007-09-04 07:15:32 +00001278 print(root, "consumes", end=" ")
1279 print(sum(getsize(join(root, name)) for name in files), end=" ")
1280 print("bytes in", len(files), "non-directory files")
Georg Brandl116aa622007-08-15 14:28:22 +00001281 if 'CVS' in dirs:
1282 dirs.remove('CVS') # don't visit CVS directories
1283
Christian Heimesfaf2f632008-01-06 16:59:19 +00001284 In the next example, walking the tree bottom-up is essential: :func:`rmdir`
Georg Brandl116aa622007-08-15 14:28:22 +00001285 doesn't allow deleting a directory before the directory is empty::
1286
Christian Heimesfaf2f632008-01-06 16:59:19 +00001287 # Delete everything reachable from the directory named in "top",
Georg Brandl116aa622007-08-15 14:28:22 +00001288 # assuming there are no symbolic links.
1289 # CAUTION: This is dangerous! For example, if top == '/', it
1290 # could delete all your disk files.
1291 import os
1292 for root, dirs, files in os.walk(top, topdown=False):
1293 for name in files:
1294 os.remove(os.path.join(root, name))
1295 for name in dirs:
1296 os.rmdir(os.path.join(root, name))
1297
Georg Brandl116aa622007-08-15 14:28:22 +00001298
1299.. _os-process:
1300
1301Process Management
1302------------------
1303
1304These functions may be used to create and manage processes.
1305
1306The various :func:`exec\*` functions take a list of arguments for the new
1307program loaded into the process. In each case, the first of these arguments is
1308passed to the new program as its own name rather than as an argument a user may
1309have typed on a command line. For the C programmer, this is the ``argv[0]``
1310passed to a program's :cfunc:`main`. For example, ``os.execv('/bin/echo',
1311['foo', 'bar'])`` will only print ``bar`` on standard output; ``foo`` will seem
1312to be ignored.
1313
1314
1315.. function:: abort()
1316
1317 Generate a :const:`SIGABRT` signal to the current process. On Unix, the default
1318 behavior is to produce a core dump; on Windows, the process immediately returns
1319 an exit code of ``3``. Be aware that programs which use :func:`signal.signal`
1320 to register a handler for :const:`SIGABRT` will behave differently.
Georg Brandlc575c902008-09-13 17:46:05 +00001321 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001322
1323
1324.. function:: execl(path, arg0, arg1, ...)
1325 execle(path, arg0, arg1, ..., env)
1326 execlp(file, arg0, arg1, ...)
1327 execlpe(file, arg0, arg1, ..., env)
1328 execv(path, args)
1329 execve(path, args, env)
1330 execvp(file, args)
1331 execvpe(file, args, env)
1332
1333 These functions all execute a new program, replacing the current process; they
1334 do not return. On Unix, the new executable is loaded into the current process,
Christian Heimesfaf2f632008-01-06 16:59:19 +00001335 and will have the same process id as the caller. Errors will be reported as
Georg Brandl48310cd2009-01-03 21:18:54 +00001336 :exc:`OSError` exceptions.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001337
1338 The current process is replaced immediately. Open file objects and
1339 descriptors are not flushed, so if there may be data buffered
1340 on these open files, you should flush them using
1341 :func:`sys.stdout.flush` or :func:`os.fsync` before calling an
1342 :func:`exec\*` function.
Georg Brandl116aa622007-08-15 14:28:22 +00001343
Christian Heimesfaf2f632008-01-06 16:59:19 +00001344 The "l" and "v" variants of the :func:`exec\*` functions differ in how
1345 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001346 to work with if the number of parameters is fixed when the code is written; the
1347 individual parameters simply become additional parameters to the :func:`execl\*`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001348 functions. The "v" variants are good when the number of parameters is
Georg Brandl116aa622007-08-15 14:28:22 +00001349 variable, with the arguments being passed in a list or tuple as the *args*
1350 parameter. In either case, the arguments to the child process should start with
1351 the name of the command being run, but this is not enforced.
1352
Christian Heimesfaf2f632008-01-06 16:59:19 +00001353 The variants which include a "p" near the end (:func:`execlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001354 :func:`execlpe`, :func:`execvp`, and :func:`execvpe`) will use the
1355 :envvar:`PATH` environment variable to locate the program *file*. When the
1356 environment is being replaced (using one of the :func:`exec\*e` variants,
1357 discussed in the next paragraph), the new environment is used as the source of
1358 the :envvar:`PATH` variable. The other variants, :func:`execl`, :func:`execle`,
1359 :func:`execv`, and :func:`execve`, will not use the :envvar:`PATH` variable to
1360 locate the executable; *path* must contain an appropriate absolute or relative
1361 path.
1362
1363 For :func:`execle`, :func:`execlpe`, :func:`execve`, and :func:`execvpe` (note
Christian Heimesfaf2f632008-01-06 16:59:19 +00001364 that these all end in "e"), the *env* parameter must be a mapping which is
Christian Heimesa342c012008-04-20 21:01:16 +00001365 used to define the environment variables for the new process (these are used
1366 instead of the current process' environment); the functions :func:`execl`,
Georg Brandl116aa622007-08-15 14:28:22 +00001367 :func:`execlp`, :func:`execv`, and :func:`execvp` all cause the new process to
Georg Brandl48310cd2009-01-03 21:18:54 +00001368 inherit the environment of the current process.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001369
1370 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001371
1372
1373.. function:: _exit(n)
1374
1375 Exit to the system with status *n*, without calling cleanup handlers, flushing
Georg Brandlc575c902008-09-13 17:46:05 +00001376 stdio buffers, etc. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001377
1378 .. note::
1379
1380 The standard way to exit is ``sys.exit(n)``. :func:`_exit` should normally only
1381 be used in the child process after a :func:`fork`.
1382
Christian Heimesfaf2f632008-01-06 16:59:19 +00001383The following exit codes are defined and can be used with :func:`_exit`,
Georg Brandl116aa622007-08-15 14:28:22 +00001384although they are not required. These are typically used for system programs
1385written in Python, such as a mail server's external command delivery program.
1386
1387.. note::
1388
1389 Some of these may not be available on all Unix platforms, since there is some
1390 variation. These constants are defined where they are defined by the underlying
1391 platform.
1392
1393
1394.. data:: EX_OK
1395
Georg Brandlc575c902008-09-13 17:46:05 +00001396 Exit code that means no error occurred. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001397
Georg Brandl116aa622007-08-15 14:28:22 +00001398
1399.. data:: EX_USAGE
1400
1401 Exit code that means the command was used incorrectly, such as when the wrong
Georg Brandlc575c902008-09-13 17:46:05 +00001402 number of arguments are given. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001403
Georg Brandl116aa622007-08-15 14:28:22 +00001404
1405.. data:: EX_DATAERR
1406
Georg Brandlc575c902008-09-13 17:46:05 +00001407 Exit code that means the input data was incorrect. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001408
Georg Brandl116aa622007-08-15 14:28:22 +00001409
1410.. data:: EX_NOINPUT
1411
1412 Exit code that means an input file did not exist or was not readable.
Georg Brandlc575c902008-09-13 17:46:05 +00001413 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001414
Georg Brandl116aa622007-08-15 14:28:22 +00001415
1416.. data:: EX_NOUSER
1417
Georg Brandlc575c902008-09-13 17:46:05 +00001418 Exit code that means a specified user did not exist. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001419
Georg Brandl116aa622007-08-15 14:28:22 +00001420
1421.. data:: EX_NOHOST
1422
Georg Brandlc575c902008-09-13 17:46:05 +00001423 Exit code that means a specified host did not exist. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001424
Georg Brandl116aa622007-08-15 14:28:22 +00001425
1426.. data:: EX_UNAVAILABLE
1427
1428 Exit code that means that a required service is unavailable. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001429 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001430
Georg Brandl116aa622007-08-15 14:28:22 +00001431
1432.. data:: EX_SOFTWARE
1433
1434 Exit code that means an internal software error was detected. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001435 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001436
Georg Brandl116aa622007-08-15 14:28:22 +00001437
1438.. data:: EX_OSERR
1439
1440 Exit code that means an operating system error was detected, such as the
Georg Brandlc575c902008-09-13 17:46:05 +00001441 inability to fork or create a pipe. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001442
Georg Brandl116aa622007-08-15 14:28:22 +00001443
1444.. data:: EX_OSFILE
1445
1446 Exit code that means some system file did not exist, could not be opened, or had
Georg Brandlc575c902008-09-13 17:46:05 +00001447 some other kind of error. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001448
Georg Brandl116aa622007-08-15 14:28:22 +00001449
1450.. data:: EX_CANTCREAT
1451
1452 Exit code that means a user specified output file could not be created.
Georg Brandlc575c902008-09-13 17:46:05 +00001453 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001454
Georg Brandl116aa622007-08-15 14:28:22 +00001455
1456.. data:: EX_IOERR
1457
1458 Exit code that means that an error occurred while doing I/O on some file.
Georg Brandlc575c902008-09-13 17:46:05 +00001459 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001460
Georg Brandl116aa622007-08-15 14:28:22 +00001461
1462.. data:: EX_TEMPFAIL
1463
1464 Exit code that means a temporary failure occurred. This indicates something
1465 that may not really be an error, such as a network connection that couldn't be
Georg Brandlc575c902008-09-13 17:46:05 +00001466 made during a retryable operation. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001467
Georg Brandl116aa622007-08-15 14:28:22 +00001468
1469.. data:: EX_PROTOCOL
1470
1471 Exit code that means that a protocol exchange was illegal, invalid, or not
Georg Brandlc575c902008-09-13 17:46:05 +00001472 understood. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001473
Georg Brandl116aa622007-08-15 14:28:22 +00001474
1475.. data:: EX_NOPERM
1476
1477 Exit code that means that there were insufficient permissions to perform the
Georg Brandlc575c902008-09-13 17:46:05 +00001478 operation (but not intended for file system problems). Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001479
Georg Brandl116aa622007-08-15 14:28:22 +00001480
1481.. data:: EX_CONFIG
1482
1483 Exit code that means that some kind of configuration error occurred.
Georg Brandlc575c902008-09-13 17:46:05 +00001484 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001485
Georg Brandl116aa622007-08-15 14:28:22 +00001486
1487.. data:: EX_NOTFOUND
1488
1489 Exit code that means something like "an entry was not found". Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001490 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001491
Georg Brandl116aa622007-08-15 14:28:22 +00001492
1493.. function:: fork()
1494
Christian Heimesfaf2f632008-01-06 16:59:19 +00001495 Fork a child process. Return ``0`` in the child and the child's process id in the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001496 parent. If an error occurs :exc:`OSError` is raised.
Benjamin Petersonbcd8ac32008-10-10 22:20:52 +00001497
1498 Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have
1499 known issues when using fork() from a thread.
1500
Georg Brandlc575c902008-09-13 17:46:05 +00001501 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001502
1503
1504.. function:: forkpty()
1505
1506 Fork a child process, using a new pseudo-terminal as the child's controlling
1507 terminal. Return a pair of ``(pid, fd)``, where *pid* is ``0`` in the child, the
1508 new child's process id in the parent, and *fd* is the file descriptor of the
1509 master end of the pseudo-terminal. For a more portable approach, use the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001510 :mod:`pty` module. If an error occurs :exc:`OSError` is raised.
Georg Brandlc575c902008-09-13 17:46:05 +00001511 Availability: some flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001512
1513
1514.. function:: kill(pid, sig)
1515
1516 .. index::
1517 single: process; killing
1518 single: process; signalling
1519
1520 Send signal *sig* to the process *pid*. Constants for the specific signals
1521 available on the host platform are defined in the :mod:`signal` module.
Brian Curtineb24d742010-04-12 17:16:38 +00001522
1523 Windows: The :data:`signal.CTRL_C_EVENT` and
1524 :data:`signal.CTRL_BREAK_EVENT` signals are special signals which can
1525 only be sent to console processes which share a common console window,
1526 e.g., some subprocesses. Any other value for *sig* will cause the process
1527 to be unconditionally killed by the TerminateProcess API, and the exit code
1528 will be set to *sig*. The Windows version of :func:`kill` additionally takes
1529 process handles to be killed.
Georg Brandl116aa622007-08-15 14:28:22 +00001530
Brian Curtin904bd392010-04-20 15:28:06 +00001531 .. versionadded:: 3.2 Windows support
1532
Georg Brandl116aa622007-08-15 14:28:22 +00001533
1534.. function:: killpg(pgid, sig)
1535
1536 .. index::
1537 single: process; killing
1538 single: process; signalling
1539
Georg Brandlc575c902008-09-13 17:46:05 +00001540 Send the signal *sig* to the process group *pgid*. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001541
Georg Brandl116aa622007-08-15 14:28:22 +00001542
1543.. function:: nice(increment)
1544
1545 Add *increment* to the process's "niceness". Return the new niceness.
Georg Brandlc575c902008-09-13 17:46:05 +00001546 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001547
1548
1549.. function:: plock(op)
1550
1551 Lock program segments into memory. The value of *op* (defined in
Georg Brandlc575c902008-09-13 17:46:05 +00001552 ``<sys/lock.h>``) determines which segments are locked. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001553
1554
1555.. function:: popen(...)
1556 :noindex:
1557
1558 Run child processes, returning opened pipes for communications. These functions
1559 are described in section :ref:`os-newstreams`.
1560
1561
1562.. function:: spawnl(mode, path, ...)
1563 spawnle(mode, path, ..., env)
1564 spawnlp(mode, file, ...)
1565 spawnlpe(mode, file, ..., env)
1566 spawnv(mode, path, args)
1567 spawnve(mode, path, args, env)
1568 spawnvp(mode, file, args)
1569 spawnvpe(mode, file, args, env)
1570
1571 Execute the program *path* in a new process.
1572
1573 (Note that the :mod:`subprocess` module provides more powerful facilities for
1574 spawning new processes and retrieving their results; using that module is
Benjamin Peterson87c8d872009-06-11 22:54:11 +00001575 preferable to using these functions. Check especially the
1576 :ref:`subprocess-replacements` section.)
Georg Brandl116aa622007-08-15 14:28:22 +00001577
Christian Heimesfaf2f632008-01-06 16:59:19 +00001578 If *mode* is :const:`P_NOWAIT`, this function returns the process id of the new
Georg Brandl116aa622007-08-15 14:28:22 +00001579 process; if *mode* is :const:`P_WAIT`, returns the process's exit code if it
1580 exits normally, or ``-signal``, where *signal* is the signal that killed the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001581 process. On Windows, the process id will actually be the process handle, so can
Georg Brandl116aa622007-08-15 14:28:22 +00001582 be used with the :func:`waitpid` function.
1583
Christian Heimesfaf2f632008-01-06 16:59:19 +00001584 The "l" and "v" variants of the :func:`spawn\*` functions differ in how
1585 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001586 to work with if the number of parameters is fixed when the code is written; the
1587 individual parameters simply become additional parameters to the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001588 :func:`spawnl\*` functions. The "v" variants are good when the number of
Georg Brandl116aa622007-08-15 14:28:22 +00001589 parameters is variable, with the arguments being passed in a list or tuple as
1590 the *args* parameter. In either case, the arguments to the child process must
1591 start with the name of the command being run.
1592
Christian Heimesfaf2f632008-01-06 16:59:19 +00001593 The variants which include a second "p" near the end (:func:`spawnlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001594 :func:`spawnlpe`, :func:`spawnvp`, and :func:`spawnvpe`) will use the
1595 :envvar:`PATH` environment variable to locate the program *file*. When the
1596 environment is being replaced (using one of the :func:`spawn\*e` variants,
1597 discussed in the next paragraph), the new environment is used as the source of
1598 the :envvar:`PATH` variable. The other variants, :func:`spawnl`,
1599 :func:`spawnle`, :func:`spawnv`, and :func:`spawnve`, will not use the
1600 :envvar:`PATH` variable to locate the executable; *path* must contain an
1601 appropriate absolute or relative path.
1602
1603 For :func:`spawnle`, :func:`spawnlpe`, :func:`spawnve`, and :func:`spawnvpe`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001604 (note that these all end in "e"), the *env* parameter must be a mapping
Christian Heimesa342c012008-04-20 21:01:16 +00001605 which is used to define the environment variables for the new process (they are
1606 used instead of the current process' environment); the functions
Georg Brandl116aa622007-08-15 14:28:22 +00001607 :func:`spawnl`, :func:`spawnlp`, :func:`spawnv`, and :func:`spawnvp` all cause
Benjamin Petersond23f8222009-04-05 19:13:16 +00001608 the new process to inherit the environment of the current process. Note that
1609 keys and values in the *env* dictionary must be strings; invalid keys or
1610 values will cause the function to fail, with a return value of ``127``.
Georg Brandl116aa622007-08-15 14:28:22 +00001611
1612 As an example, the following calls to :func:`spawnlp` and :func:`spawnvpe` are
1613 equivalent::
1614
1615 import os
1616 os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1617
1618 L = ['cp', 'index.html', '/dev/null']
1619 os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1620
1621 Availability: Unix, Windows. :func:`spawnlp`, :func:`spawnlpe`, :func:`spawnvp`
1622 and :func:`spawnvpe` are not available on Windows.
1623
Georg Brandl116aa622007-08-15 14:28:22 +00001624
1625.. data:: P_NOWAIT
1626 P_NOWAITO
1627
1628 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1629 functions. If either of these values is given, the :func:`spawn\*` functions
Christian Heimesfaf2f632008-01-06 16:59:19 +00001630 will return as soon as the new process has been created, with the process id as
Georg Brandlc575c902008-09-13 17:46:05 +00001631 the return value. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001632
Georg Brandl116aa622007-08-15 14:28:22 +00001633
1634.. data:: P_WAIT
1635
1636 Possible value for the *mode* parameter to the :func:`spawn\*` family of
1637 functions. If this is given as *mode*, the :func:`spawn\*` functions will not
1638 return until the new process has run to completion and will return the exit code
1639 of the process the run is successful, or ``-signal`` if a signal kills the
Georg Brandlc575c902008-09-13 17:46:05 +00001640 process. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001641
Georg Brandl116aa622007-08-15 14:28:22 +00001642
1643.. data:: P_DETACH
1644 P_OVERLAY
1645
1646 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1647 functions. These are less portable than those listed above. :const:`P_DETACH`
1648 is similar to :const:`P_NOWAIT`, but the new process is detached from the
1649 console of the calling process. If :const:`P_OVERLAY` is used, the current
1650 process will be replaced; the :func:`spawn\*` function will not return.
1651 Availability: Windows.
1652
Georg Brandl116aa622007-08-15 14:28:22 +00001653
1654.. function:: startfile(path[, operation])
1655
1656 Start a file with its associated application.
1657
1658 When *operation* is not specified or ``'open'``, this acts like double-clicking
1659 the file in Windows Explorer, or giving the file name as an argument to the
1660 :program:`start` command from the interactive command shell: the file is opened
1661 with whatever application (if any) its extension is associated.
1662
1663 When another *operation* is given, it must be a "command verb" that specifies
1664 what should be done with the file. Common verbs documented by Microsoft are
1665 ``'print'`` and ``'edit'`` (to be used on files) as well as ``'explore'`` and
1666 ``'find'`` (to be used on directories).
1667
1668 :func:`startfile` returns as soon as the associated application is launched.
1669 There is no option to wait for the application to close, and no way to retrieve
1670 the application's exit status. The *path* parameter is relative to the current
1671 directory. If you want to use an absolute path, make sure the first character
1672 is not a slash (``'/'``); the underlying Win32 :cfunc:`ShellExecute` function
1673 doesn't work if it is. Use the :func:`os.path.normpath` function to ensure that
1674 the path is properly encoded for Win32. Availability: Windows.
1675
Georg Brandl116aa622007-08-15 14:28:22 +00001676
1677.. function:: system(command)
1678
1679 Execute the command (a string) in a subshell. This is implemented by calling
Georg Brandl495f7b52009-10-27 15:28:25 +00001680 the Standard C function :cfunc:`system`, and has the same limitations.
1681 Changes to :data:`sys.stdin`, etc. are not reflected in the environment of the
1682 executed command.
Georg Brandl116aa622007-08-15 14:28:22 +00001683
1684 On Unix, the return value is the exit status of the process encoded in the
1685 format specified for :func:`wait`. Note that POSIX does not specify the meaning
1686 of the return value of the C :cfunc:`system` function, so the return value of
1687 the Python function is system-dependent.
1688
1689 On Windows, the return value is that returned by the system shell after running
1690 *command*, given by the Windows environment variable :envvar:`COMSPEC`: on
1691 :program:`command.com` systems (Windows 95, 98 and ME) this is always ``0``; on
1692 :program:`cmd.exe` systems (Windows NT, 2000 and XP) this is the exit status of
1693 the command run; on systems using a non-native shell, consult your shell
1694 documentation.
1695
Georg Brandlc575c902008-09-13 17:46:05 +00001696 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001697
1698 The :mod:`subprocess` module provides more powerful facilities for spawning new
1699 processes and retrieving their results; using that module is preferable to using
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001700 this function. Use the :mod:`subprocess` module. Check especially the
1701 :ref:`subprocess-replacements` section.
Georg Brandl116aa622007-08-15 14:28:22 +00001702
1703
1704.. function:: times()
1705
1706 Return a 5-tuple of floating point numbers indicating accumulated (processor or
1707 other) times, in seconds. The items are: user time, system time, children's
1708 user time, children's system time, and elapsed real time since a fixed point in
1709 the past, in that order. See the Unix manual page :manpage:`times(2)` or the
Georg Brandlc575c902008-09-13 17:46:05 +00001710 corresponding Windows Platform API documentation. Availability: Unix,
Christian Heimes68f5fbe2008-02-14 08:27:37 +00001711 Windows. On Windows, only the first two items are filled, the others are zero.
Georg Brandl116aa622007-08-15 14:28:22 +00001712
1713
1714.. function:: wait()
1715
1716 Wait for completion of a child process, and return a tuple containing its pid
1717 and exit status indication: a 16-bit number, whose low byte is the signal number
1718 that killed the process, and whose high byte is the exit status (if the signal
1719 number is zero); the high bit of the low byte is set if a core file was
Georg Brandlc575c902008-09-13 17:46:05 +00001720 produced. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001721
1722
1723.. function:: waitpid(pid, options)
1724
1725 The details of this function differ on Unix and Windows.
1726
1727 On Unix: Wait for completion of a child process given by process id *pid*, and
1728 return a tuple containing its process id and exit status indication (encoded as
1729 for :func:`wait`). The semantics of the call are affected by the value of the
1730 integer *options*, which should be ``0`` for normal operation.
1731
1732 If *pid* is greater than ``0``, :func:`waitpid` requests status information for
1733 that specific process. If *pid* is ``0``, the request is for the status of any
1734 child in the process group of the current process. If *pid* is ``-1``, the
1735 request pertains to any child of the current process. If *pid* is less than
1736 ``-1``, status is requested for any process in the process group ``-pid`` (the
1737 absolute value of *pid*).
1738
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001739 An :exc:`OSError` is raised with the value of errno when the syscall
1740 returns -1.
1741
Georg Brandl116aa622007-08-15 14:28:22 +00001742 On Windows: Wait for completion of a process given by process handle *pid*, and
1743 return a tuple containing *pid*, and its exit status shifted left by 8 bits
1744 (shifting makes cross-platform use of the function easier). A *pid* less than or
1745 equal to ``0`` has no special meaning on Windows, and raises an exception. The
1746 value of integer *options* has no effect. *pid* can refer to any process whose
1747 id is known, not necessarily a child process. The :func:`spawn` functions called
1748 with :const:`P_NOWAIT` return suitable process handles.
1749
1750
1751.. function:: wait3([options])
1752
1753 Similar to :func:`waitpid`, except no process id argument is given and a
1754 3-element tuple containing the child's process id, exit status indication, and
1755 resource usage information is returned. Refer to :mod:`resource`.\
1756 :func:`getrusage` for details on resource usage information. The option
1757 argument is the same as that provided to :func:`waitpid` and :func:`wait4`.
1758 Availability: Unix.
1759
Georg Brandl116aa622007-08-15 14:28:22 +00001760
1761.. function:: wait4(pid, options)
1762
1763 Similar to :func:`waitpid`, except a 3-element tuple, containing the child's
1764 process id, exit status indication, and resource usage information is returned.
1765 Refer to :mod:`resource`.\ :func:`getrusage` for details on resource usage
1766 information. The arguments to :func:`wait4` are the same as those provided to
1767 :func:`waitpid`. Availability: Unix.
1768
Georg Brandl116aa622007-08-15 14:28:22 +00001769
1770.. data:: WNOHANG
1771
1772 The option for :func:`waitpid` to return immediately if no child process status
1773 is available immediately. The function returns ``(0, 0)`` in this case.
Georg Brandlc575c902008-09-13 17:46:05 +00001774 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001775
1776
1777.. data:: WCONTINUED
1778
1779 This option causes child processes to be reported if they have been continued
1780 from a job control stop since their status was last reported. Availability: Some
1781 Unix systems.
1782
Georg Brandl116aa622007-08-15 14:28:22 +00001783
1784.. data:: WUNTRACED
1785
1786 This option causes child processes to be reported if they have been stopped but
1787 their current state has not been reported since they were stopped. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001788 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001789
Georg Brandl116aa622007-08-15 14:28:22 +00001790
1791The following functions take a process status code as returned by
1792:func:`system`, :func:`wait`, or :func:`waitpid` as a parameter. They may be
1793used to determine the disposition of a process.
1794
Georg Brandl116aa622007-08-15 14:28:22 +00001795.. function:: WCOREDUMP(status)
1796
Christian Heimesfaf2f632008-01-06 16:59:19 +00001797 Return ``True`` if a core dump was generated for the process, otherwise
Georg Brandlc575c902008-09-13 17:46:05 +00001798 return ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001799
Georg Brandl116aa622007-08-15 14:28:22 +00001800
1801.. function:: WIFCONTINUED(status)
1802
Christian Heimesfaf2f632008-01-06 16:59:19 +00001803 Return ``True`` if the process has been continued from a job control stop,
1804 otherwise return ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001805
Georg Brandl116aa622007-08-15 14:28:22 +00001806
1807.. function:: WIFSTOPPED(status)
1808
Christian Heimesfaf2f632008-01-06 16:59:19 +00001809 Return ``True`` if the process has been stopped, otherwise return
Georg Brandl116aa622007-08-15 14:28:22 +00001810 ``False``. Availability: Unix.
1811
1812
1813.. function:: WIFSIGNALED(status)
1814
Christian Heimesfaf2f632008-01-06 16:59:19 +00001815 Return ``True`` if the process exited due to a signal, otherwise return
Georg Brandlc575c902008-09-13 17:46:05 +00001816 ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001817
1818
1819.. function:: WIFEXITED(status)
1820
Christian Heimesfaf2f632008-01-06 16:59:19 +00001821 Return ``True`` if the process exited using the :manpage:`exit(2)` system call,
Georg Brandlc575c902008-09-13 17:46:05 +00001822 otherwise return ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001823
1824
1825.. function:: WEXITSTATUS(status)
1826
1827 If ``WIFEXITED(status)`` is true, return the integer parameter to the
1828 :manpage:`exit(2)` system call. Otherwise, the return value is meaningless.
Georg Brandlc575c902008-09-13 17:46:05 +00001829 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001830
1831
1832.. function:: WSTOPSIG(status)
1833
Georg Brandlc575c902008-09-13 17:46:05 +00001834 Return the signal which caused the process to stop. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001835
1836
1837.. function:: WTERMSIG(status)
1838
Georg Brandlc575c902008-09-13 17:46:05 +00001839 Return the signal which caused the process to exit. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001840
1841
1842.. _os-path:
1843
1844Miscellaneous System Information
1845--------------------------------
1846
1847
1848.. function:: confstr(name)
1849
1850 Return string-valued system configuration values. *name* specifies the
1851 configuration value to retrieve; it may be a string which is the name of a
1852 defined system value; these names are specified in a number of standards (POSIX,
1853 Unix 95, Unix 98, and others). Some platforms define additional names as well.
1854 The names known to the host operating system are given as the keys of the
1855 ``confstr_names`` dictionary. For configuration variables not included in that
1856 mapping, passing an integer for *name* is also accepted. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001857 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001858
1859 If the configuration value specified by *name* isn't defined, ``None`` is
1860 returned.
1861
1862 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1863 specific value for *name* is not supported by the host system, even if it is
1864 included in ``confstr_names``, an :exc:`OSError` is raised with
1865 :const:`errno.EINVAL` for the error number.
1866
1867
1868.. data:: confstr_names
1869
1870 Dictionary mapping names accepted by :func:`confstr` to the integer values
1871 defined for those names by the host operating system. This can be used to
Georg Brandlc575c902008-09-13 17:46:05 +00001872 determine the set of names known to the system. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001873
1874
1875.. function:: getloadavg()
1876
Christian Heimesa62da1d2008-01-12 19:39:10 +00001877 Return the number of processes in the system run queue averaged over the last
1878 1, 5, and 15 minutes or raises :exc:`OSError` if the load average was
Georg Brandlf08a9dd2008-06-10 16:57:31 +00001879 unobtainable. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001880
Georg Brandl116aa622007-08-15 14:28:22 +00001881
1882.. function:: sysconf(name)
1883
1884 Return integer-valued system configuration values. If the configuration value
1885 specified by *name* isn't defined, ``-1`` is returned. The comments regarding
1886 the *name* parameter for :func:`confstr` apply here as well; the dictionary that
1887 provides information on the known names is given by ``sysconf_names``.
Georg Brandlc575c902008-09-13 17:46:05 +00001888 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001889
1890
1891.. data:: sysconf_names
1892
1893 Dictionary mapping names accepted by :func:`sysconf` to the integer values
1894 defined for those names by the host operating system. This can be used to
Georg Brandlc575c902008-09-13 17:46:05 +00001895 determine the set of names known to the system. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001896
Christian Heimesfaf2f632008-01-06 16:59:19 +00001897The following data values are used to support path manipulation operations. These
Georg Brandl116aa622007-08-15 14:28:22 +00001898are defined for all platforms.
1899
1900Higher-level operations on pathnames are defined in the :mod:`os.path` module.
1901
1902
1903.. data:: curdir
1904
1905 The constant string used by the operating system to refer to the current
Georg Brandlc575c902008-09-13 17:46:05 +00001906 directory. This is ``'.'`` for Windows and POSIX. Also available via
1907 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00001908
1909
1910.. data:: pardir
1911
1912 The constant string used by the operating system to refer to the parent
Georg Brandlc575c902008-09-13 17:46:05 +00001913 directory. This is ``'..'`` for Windows and POSIX. Also available via
1914 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00001915
1916
1917.. data:: sep
1918
Georg Brandlc575c902008-09-13 17:46:05 +00001919 The character used by the operating system to separate pathname components.
1920 This is ``'/'`` for POSIX and ``'\\'`` for Windows. Note that knowing this
1921 is not sufficient to be able to parse or concatenate pathnames --- use
Georg Brandl116aa622007-08-15 14:28:22 +00001922 :func:`os.path.split` and :func:`os.path.join` --- but it is occasionally
1923 useful. Also available via :mod:`os.path`.
1924
1925
1926.. data:: altsep
1927
1928 An alternative character used by the operating system to separate pathname
1929 components, or ``None`` if only one separator character exists. This is set to
1930 ``'/'`` on Windows systems where ``sep`` is a backslash. Also available via
1931 :mod:`os.path`.
1932
1933
1934.. data:: extsep
1935
1936 The character which separates the base filename from the extension; for example,
1937 the ``'.'`` in :file:`os.py`. Also available via :mod:`os.path`.
1938
Georg Brandl116aa622007-08-15 14:28:22 +00001939
1940.. data:: pathsep
1941
1942 The character conventionally used by the operating system to separate search
1943 path components (as in :envvar:`PATH`), such as ``':'`` for POSIX or ``';'`` for
1944 Windows. Also available via :mod:`os.path`.
1945
1946
1947.. data:: defpath
1948
1949 The default search path used by :func:`exec\*p\*` and :func:`spawn\*p\*` if the
1950 environment doesn't have a ``'PATH'`` key. Also available via :mod:`os.path`.
1951
1952
1953.. data:: linesep
1954
1955 The string used to separate (or, rather, terminate) lines on the current
Georg Brandlc575c902008-09-13 17:46:05 +00001956 platform. This may be a single character, such as ``'\n'`` for POSIX, or
1957 multiple characters, for example, ``'\r\n'`` for Windows. Do not use
1958 *os.linesep* as a line terminator when writing files opened in text mode (the
1959 default); use a single ``'\n'`` instead, on all platforms.
Georg Brandl116aa622007-08-15 14:28:22 +00001960
1961
1962.. data:: devnull
1963
Georg Brandlc575c902008-09-13 17:46:05 +00001964 The file path of the null device. For example: ``'/dev/null'`` for POSIX.
1965 Also available via :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00001966
Georg Brandl116aa622007-08-15 14:28:22 +00001967
1968.. _os-miscfunc:
1969
1970Miscellaneous Functions
1971-----------------------
1972
1973
1974.. function:: urandom(n)
1975
1976 Return a string of *n* random bytes suitable for cryptographic use.
1977
1978 This function returns random bytes from an OS-specific randomness source. The
1979 returned data should be unpredictable enough for cryptographic applications,
1980 though its exact quality depends on the OS implementation. On a UNIX-like
1981 system this will query /dev/urandom, and on Windows it will use CryptGenRandom.
1982 If a randomness source is not found, :exc:`NotImplementedError` will be raised.