blob: 4b2f8ec84ee70182ca90198a8a9e064e143f4998 [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.
Benjamin Peterson0d6fe512010-05-06 22:13:11 +0000282
Victor Stinner84ae1182010-05-06 22:05:07 +0000283 Availability: most flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000284
Benjamin Peterson0d6fe512010-05-06 22:13:11 +0000285 .. versionadded:: 3.2
286
Georg Brandl116aa622007-08-15 14:28:22 +0000287
Georg Brandl18244152009-09-02 20:34:52 +0000288.. function:: putenv(key, value)
Georg Brandl116aa622007-08-15 14:28:22 +0000289
290 .. index:: single: environment variables; setting
291
Georg Brandl18244152009-09-02 20:34:52 +0000292 Set the environment variable named *key* to the string *value*. Such
Georg Brandl116aa622007-08-15 14:28:22 +0000293 changes to the environment affect subprocesses started with :func:`os.system`,
294 :func:`popen` or :func:`fork` and :func:`execv`. Availability: most flavors of
295 Unix, Windows.
296
297 .. note::
298
Georg Brandlc575c902008-09-13 17:46:05 +0000299 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
300 cause memory leaks. Refer to the system documentation for putenv.
Georg Brandl116aa622007-08-15 14:28:22 +0000301
302 When :func:`putenv` is supported, assignments to items in ``os.environ`` are
303 automatically translated into corresponding calls to :func:`putenv`; however,
304 calls to :func:`putenv` don't update ``os.environ``, so it is actually
305 preferable to assign to items of ``os.environ``.
306
307
308.. function:: setegid(egid)
309
310 Set the current process's effective group id. Availability: Unix.
311
312
313.. function:: seteuid(euid)
314
315 Set the current process's effective user id. Availability: Unix.
316
317
318.. function:: setgid(gid)
319
320 Set the current process' group id. Availability: Unix.
321
322
323.. function:: setgroups(groups)
324
325 Set the list of supplemental group ids associated with the current process to
326 *groups*. *groups* must be a sequence, and each element must be an integer
Christian Heimesfaf2f632008-01-06 16:59:19 +0000327 identifying a group. This operation is typically available only to the superuser.
Georg Brandl116aa622007-08-15 14:28:22 +0000328 Availability: Unix.
329
Georg Brandl116aa622007-08-15 14:28:22 +0000330
331.. function:: setpgrp()
332
Christian Heimesfaf2f632008-01-06 16:59:19 +0000333 Call the system call :cfunc:`setpgrp` or :cfunc:`setpgrp(0, 0)` depending on
Georg Brandl116aa622007-08-15 14:28:22 +0000334 which version is implemented (if any). See the Unix manual for the semantics.
335 Availability: Unix.
336
337
338.. function:: setpgid(pid, pgrp)
339
Christian Heimesfaf2f632008-01-06 16:59:19 +0000340 Call the system call :cfunc:`setpgid` to set the process group id of the
Georg Brandl116aa622007-08-15 14:28:22 +0000341 process with id *pid* to the process group with id *pgrp*. See the Unix manual
342 for the semantics. Availability: Unix.
343
344
Georg Brandl116aa622007-08-15 14:28:22 +0000345.. function:: setregid(rgid, egid)
346
347 Set the current process's real and effective group ids. Availability: Unix.
348
Georg Brandl1b83a452009-11-28 11:12:26 +0000349
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000350.. function:: setresgid(rgid, egid, sgid)
351
352 Set the current process's real, effective, and saved group ids.
353 Availability: Unix.
354
Georg Brandl1b83a452009-11-28 11:12:26 +0000355 .. versionadded:: 3.2
356
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000357
358.. function:: setresuid(ruid, euid, suid)
359
360 Set the current process's real, effective, and saved user ids.
361 Availibility: Unix.
362
Georg Brandl1b83a452009-11-28 11:12:26 +0000363 .. versionadded:: 3.2
364
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000365
366.. function:: setreuid(ruid, euid)
367
368 Set the current process's real and effective user ids. Availability: Unix.
369
Georg Brandl116aa622007-08-15 14:28:22 +0000370
371.. function:: getsid(pid)
372
Christian Heimesfaf2f632008-01-06 16:59:19 +0000373 Call the system call :cfunc:`getsid`. See the Unix manual for the semantics.
Georg Brandl116aa622007-08-15 14:28:22 +0000374 Availability: Unix.
375
Georg Brandl116aa622007-08-15 14:28:22 +0000376
377.. function:: setsid()
378
Christian Heimesfaf2f632008-01-06 16:59:19 +0000379 Call the system call :cfunc:`setsid`. See the Unix manual for the semantics.
Georg Brandl116aa622007-08-15 14:28:22 +0000380 Availability: Unix.
381
382
383.. function:: setuid(uid)
384
385 .. index:: single: user; id, setting
386
Christian Heimesfaf2f632008-01-06 16:59:19 +0000387 Set the current process's user id. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000388
Georg Brandl116aa622007-08-15 14:28:22 +0000389
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000390.. placed in this section since it relates to errno.... a little weak
Georg Brandl116aa622007-08-15 14:28:22 +0000391.. function:: strerror(code)
392
393 Return the error message corresponding to the error code in *code*.
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000394 On platforms where :cfunc:`strerror` returns ``NULL`` when given an unknown
395 error number, :exc:`ValueError` is raised. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000396
397
398.. function:: umask(mask)
399
Christian Heimesfaf2f632008-01-06 16:59:19 +0000400 Set the current numeric umask and return the previous umask. Availability:
Georg Brandl116aa622007-08-15 14:28:22 +0000401 Unix, Windows.
402
403
404.. function:: uname()
405
406 .. index::
407 single: gethostname() (in module socket)
408 single: gethostbyaddr() (in module socket)
409
410 Return a 5-tuple containing information identifying the current operating
411 system. The tuple contains 5 strings: ``(sysname, nodename, release, version,
412 machine)``. Some systems truncate the nodename to 8 characters or to the
413 leading component; a better way to get the hostname is
414 :func:`socket.gethostname` or even
415 ``socket.gethostbyaddr(socket.gethostname())``. Availability: recent flavors of
416 Unix.
417
418
Georg Brandl18244152009-09-02 20:34:52 +0000419.. function:: unsetenv(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000420
421 .. index:: single: environment variables; deleting
422
Georg Brandl18244152009-09-02 20:34:52 +0000423 Unset (delete) the environment variable named *key*. Such changes to the
Georg Brandl116aa622007-08-15 14:28:22 +0000424 environment affect subprocesses started with :func:`os.system`, :func:`popen` or
425 :func:`fork` and :func:`execv`. Availability: most flavors of Unix, Windows.
426
427 When :func:`unsetenv` is supported, deletion of items in ``os.environ`` is
428 automatically translated into a corresponding call to :func:`unsetenv`; however,
429 calls to :func:`unsetenv` don't update ``os.environ``, so it is actually
430 preferable to delete items of ``os.environ``.
431
432
433.. _os-newstreams:
434
435File Object Creation
436--------------------
437
438These functions create new file objects. (See also :func:`open`.)
439
440
441.. function:: fdopen(fd[, mode[, bufsize]])
442
443 .. index:: single: I/O control; buffering
444
445 Return an open file object connected to the file descriptor *fd*. The *mode*
446 and *bufsize* arguments have the same meaning as the corresponding arguments to
Georg Brandlc575c902008-09-13 17:46:05 +0000447 the built-in :func:`open` function. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000448
Georg Brandl55ac8f02007-09-01 13:51:09 +0000449 When specified, the *mode* argument must start with one of the letters
450 ``'r'``, ``'w'``, or ``'a'``, otherwise a :exc:`ValueError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000451
Georg Brandl55ac8f02007-09-01 13:51:09 +0000452 On Unix, when the *mode* argument starts with ``'a'``, the *O_APPEND* flag is
453 set on the file descriptor (which the :cfunc:`fdopen` implementation already
454 does on most platforms).
Georg Brandl116aa622007-08-15 14:28:22 +0000455
456
Georg Brandl116aa622007-08-15 14:28:22 +0000457.. _os-fd-ops:
458
459File Descriptor Operations
460--------------------------
461
462These functions operate on I/O streams referenced using file descriptors.
463
464File descriptors are small integers corresponding to a file that has been opened
465by the current process. For example, standard input is usually file descriptor
4660, standard output is 1, and standard error is 2. Further files opened by a
467process will then be assigned 3, 4, 5, and so forth. The name "file descriptor"
468is slightly deceptive; on Unix platforms, sockets and pipes are also referenced
469by file descriptors.
470
Benjamin Peterson08bf91c2010-04-11 16:12:57 +0000471The :meth:`~file.fileno` method can be used to obtain the file descriptor
472associated with a file object when required. Note that using the file
473descriptor directly will bypass the file object methods, ignoring aspects such
474as internal buffering of data.
Georg Brandl116aa622007-08-15 14:28:22 +0000475
476.. function:: close(fd)
477
Georg Brandlc575c902008-09-13 17:46:05 +0000478 Close file descriptor *fd*. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000479
480 .. note::
481
482 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000483 descriptor as returned by :func:`os.open` or :func:`pipe`. To close a "file
Georg Brandl116aa622007-08-15 14:28:22 +0000484 object" returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000485 :func:`fdopen`, use its :meth:`~file.close` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000486
487
Christian Heimesfdab48e2008-01-20 09:06:41 +0000488.. function:: closerange(fd_low, fd_high)
489
490 Close all file descriptors from *fd_low* (inclusive) to *fd_high* (exclusive),
Georg Brandlc575c902008-09-13 17:46:05 +0000491 ignoring errors. Availability: Unix, Windows. Equivalent to::
Christian Heimesfdab48e2008-01-20 09:06:41 +0000492
Georg Brandlc9a5a0e2009-09-01 07:34:27 +0000493 for fd in range(fd_low, fd_high):
Christian Heimesfdab48e2008-01-20 09:06:41 +0000494 try:
495 os.close(fd)
496 except OSError:
497 pass
498
Christian Heimesfdab48e2008-01-20 09:06:41 +0000499
Georg Brandl81f11302007-12-21 08:45:42 +0000500.. function:: device_encoding(fd)
501
502 Return a string describing the encoding of the device associated with *fd*
503 if it is connected to a terminal; else return :const:`None`.
504
505
Georg Brandl116aa622007-08-15 14:28:22 +0000506.. function:: dup(fd)
507
Georg Brandlc575c902008-09-13 17:46:05 +0000508 Return a duplicate of file descriptor *fd*. Availability: Unix,
Georg Brandl116aa622007-08-15 14:28:22 +0000509 Windows.
510
511
512.. function:: dup2(fd, fd2)
513
514 Duplicate file descriptor *fd* to *fd2*, closing the latter first if necessary.
Georg Brandlc575c902008-09-13 17:46:05 +0000515 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000516
517
Christian Heimes4e30a842007-11-30 22:12:06 +0000518.. function:: fchmod(fd, mode)
519
520 Change the mode of the file given by *fd* to the numeric *mode*. See the docs
521 for :func:`chmod` for possible values of *mode*. Availability: Unix.
522
523
524.. function:: fchown(fd, uid, gid)
525
526 Change the owner and group id of the file given by *fd* to the numeric *uid*
527 and *gid*. To leave one of the ids unchanged, set it to -1.
528 Availability: Unix.
529
530
Georg Brandl116aa622007-08-15 14:28:22 +0000531.. function:: fdatasync(fd)
532
533 Force write of file with filedescriptor *fd* to disk. Does not force update of
534 metadata. Availability: Unix.
535
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000536 .. note::
537 This function is not available on MacOS.
538
Georg Brandl116aa622007-08-15 14:28:22 +0000539
540.. function:: fpathconf(fd, name)
541
542 Return system configuration information relevant to an open file. *name*
543 specifies the configuration value to retrieve; it may be a string which is the
544 name of a defined system value; these names are specified in a number of
545 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
546 additional names as well. The names known to the host operating system are
547 given in the ``pathconf_names`` dictionary. For configuration variables not
548 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandlc575c902008-09-13 17:46:05 +0000549 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000550
551 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
552 specific value for *name* is not supported by the host system, even if it is
553 included in ``pathconf_names``, an :exc:`OSError` is raised with
554 :const:`errno.EINVAL` for the error number.
555
556
557.. function:: fstat(fd)
558
559 Return status for file descriptor *fd*, like :func:`stat`. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +0000560 Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000561
562
563.. function:: fstatvfs(fd)
564
565 Return information about the filesystem containing the file associated with file
566 descriptor *fd*, like :func:`statvfs`. Availability: Unix.
567
568
569.. function:: fsync(fd)
570
571 Force write of file with filedescriptor *fd* to disk. On Unix, this calls the
572 native :cfunc:`fsync` function; on Windows, the MS :cfunc:`_commit` function.
573
574 If you're starting with a Python file object *f*, first do ``f.flush()``, and
575 then do ``os.fsync(f.fileno())``, to ensure that all internal buffers associated
Georg Brandlc575c902008-09-13 17:46:05 +0000576 with *f* are written to disk. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000577
578
579.. function:: ftruncate(fd, length)
580
581 Truncate the file corresponding to file descriptor *fd*, so that it is at most
Georg Brandlc575c902008-09-13 17:46:05 +0000582 *length* bytes in size. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000583
584
585.. function:: isatty(fd)
586
587 Return ``True`` if the file descriptor *fd* is open and connected to a
Georg Brandlc575c902008-09-13 17:46:05 +0000588 tty(-like) device, else ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000589
590
591.. function:: lseek(fd, pos, how)
592
Christian Heimesfaf2f632008-01-06 16:59:19 +0000593 Set the current position of file descriptor *fd* to position *pos*, modified
594 by *how*: :const:`SEEK_SET` or ``0`` to set the position relative to the
595 beginning of the file; :const:`SEEK_CUR` or ``1`` to set it relative to the
596 current position; :const:`os.SEEK_END` or ``2`` to set it relative to the end of
Georg Brandlc575c902008-09-13 17:46:05 +0000597 the file. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000598
599
600.. function:: open(file, flags[, mode])
601
Georg Brandlf4a41232008-05-26 17:55:52 +0000602 Open the file *file* and set various flags according to *flags* and possibly
603 its mode according to *mode*. The default *mode* is ``0o777`` (octal), and
604 the current umask value is first masked out. Return the file descriptor for
Georg Brandlc575c902008-09-13 17:46:05 +0000605 the newly opened file. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000606
607 For a description of the flag and mode values, see the C run-time documentation;
608 flag constants (like :const:`O_RDONLY` and :const:`O_WRONLY`) are defined in
609 this module too (see below).
610
611 .. note::
612
Georg Brandl502d9a52009-07-26 15:02:41 +0000613 This function is intended for low-level I/O. For normal usage, use the
614 built-in function :func:`open`, which returns a "file object" with
615 :meth:`~file.read` and :meth:`~file.write` methods (and many more). To
616 wrap a file descriptor in a "file object", use :func:`fdopen`.
Georg Brandl116aa622007-08-15 14:28:22 +0000617
618
619.. function:: openpty()
620
621 .. index:: module: pty
622
623 Open a new pseudo-terminal pair. Return a pair of file descriptors ``(master,
624 slave)`` for the pty and the tty, respectively. For a (slightly) more portable
Georg Brandlc575c902008-09-13 17:46:05 +0000625 approach, use the :mod:`pty` module. Availability: some flavors of
Georg Brandl116aa622007-08-15 14:28:22 +0000626 Unix.
627
628
629.. function:: pipe()
630
631 Create a pipe. Return a pair of file descriptors ``(r, w)`` usable for reading
Georg Brandlc575c902008-09-13 17:46:05 +0000632 and writing, respectively. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000633
634
635.. function:: read(fd, n)
636
Georg Brandlb90be692009-07-29 16:14:16 +0000637 Read at most *n* bytes from file descriptor *fd*. Return a bytestring containing the
Georg Brandl116aa622007-08-15 14:28:22 +0000638 bytes read. If the end of the file referred to by *fd* has been reached, an
Georg Brandlb90be692009-07-29 16:14:16 +0000639 empty bytes object is returned. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000640
641 .. note::
642
643 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000644 descriptor as returned by :func:`os.open` or :func:`pipe`. To read a "file object"
Georg Brandl116aa622007-08-15 14:28:22 +0000645 returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000646 :func:`fdopen`, or :data:`sys.stdin`, use its :meth:`~file.read` or
647 :meth:`~file.readline` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000648
649
650.. function:: tcgetpgrp(fd)
651
652 Return the process group associated with the terminal given by *fd* (an open
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000653 file descriptor as returned by :func:`os.open`). Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000654
655
656.. function:: tcsetpgrp(fd, pg)
657
658 Set the process group associated with the terminal given by *fd* (an open file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000659 descriptor as returned by :func:`os.open`) to *pg*. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000660
661
662.. function:: ttyname(fd)
663
664 Return a string which specifies the terminal device associated with
Georg Brandl9afde1c2007-11-01 20:32:30 +0000665 file descriptor *fd*. If *fd* is not associated with a terminal device, an
Georg Brandlc575c902008-09-13 17:46:05 +0000666 exception is raised. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000667
668
669.. function:: write(fd, str)
670
Georg Brandlb90be692009-07-29 16:14:16 +0000671 Write the bytestring in *str* to file descriptor *fd*. Return the number of
672 bytes actually written. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000673
674 .. note::
675
676 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000677 descriptor as returned by :func:`os.open` or :func:`pipe`. To write a "file
Georg Brandl116aa622007-08-15 14:28:22 +0000678 object" returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000679 :func:`fdopen`, or :data:`sys.stdout` or :data:`sys.stderr`, use its
680 :meth:`~file.write` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000681
Georg Brandlaf265f42008-12-07 15:06:20 +0000682The following constants are options for the *flags* parameter to the
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000683:func:`~os.open` function. They can be combined using the bitwise OR operator
Georg Brandlaf265f42008-12-07 15:06:20 +0000684``|``. Some of them are not available on all platforms. For descriptions of
685their availability and use, consult the :manpage:`open(2)` manual page on Unix
Doug Hellmanneb097fc2009-09-20 20:56:56 +0000686or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000687
688
689.. data:: O_RDONLY
690 O_WRONLY
691 O_RDWR
692 O_APPEND
693 O_CREAT
694 O_EXCL
695 O_TRUNC
696
Georg Brandlaf265f42008-12-07 15:06:20 +0000697 These constants are available on Unix and Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000698
699
700.. data:: O_DSYNC
701 O_RSYNC
702 O_SYNC
703 O_NDELAY
704 O_NONBLOCK
705 O_NOCTTY
706 O_SHLOCK
707 O_EXLOCK
708
Georg Brandlaf265f42008-12-07 15:06:20 +0000709 These constants are only available on Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000710
711
712.. data:: O_BINARY
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000713 O_NOINHERIT
Georg Brandl116aa622007-08-15 14:28:22 +0000714 O_SHORT_LIVED
715 O_TEMPORARY
716 O_RANDOM
717 O_SEQUENTIAL
718 O_TEXT
719
Georg Brandlaf265f42008-12-07 15:06:20 +0000720 These constants are only available on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000721
722
Alexandre Vassalottibee32532008-05-16 18:15:12 +0000723.. data:: O_ASYNC
724 O_DIRECT
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000725 O_DIRECTORY
726 O_NOFOLLOW
727 O_NOATIME
728
Georg Brandlaf265f42008-12-07 15:06:20 +0000729 These constants are GNU extensions and not present if they are not defined by
730 the C library.
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000731
732
Georg Brandl116aa622007-08-15 14:28:22 +0000733.. data:: SEEK_SET
734 SEEK_CUR
735 SEEK_END
736
737 Parameters to the :func:`lseek` function. Their values are 0, 1, and 2,
Georg Brandlc575c902008-09-13 17:46:05 +0000738 respectively. Availability: Windows, Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000739
Georg Brandl116aa622007-08-15 14:28:22 +0000740
741.. _os-file-dir:
742
743Files and Directories
744---------------------
745
Georg Brandl116aa622007-08-15 14:28:22 +0000746.. function:: access(path, mode)
747
748 Use the real uid/gid to test for access to *path*. Note that most operations
749 will use the effective uid/gid, therefore this routine can be used in a
750 suid/sgid environment to test if the invoking user has the specified access to
751 *path*. *mode* should be :const:`F_OK` to test the existence of *path*, or it
752 can be the inclusive OR of one or more of :const:`R_OK`, :const:`W_OK`, and
753 :const:`X_OK` to test permissions. Return :const:`True` if access is allowed,
754 :const:`False` if not. See the Unix man page :manpage:`access(2)` for more
Georg Brandlc575c902008-09-13 17:46:05 +0000755 information. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000756
757 .. note::
758
Georg Brandl502d9a52009-07-26 15:02:41 +0000759 Using :func:`access` to check if a user is authorized to e.g. open a file
760 before actually doing so using :func:`open` creates a security hole,
761 because the user might exploit the short time interval between checking
762 and opening the file to manipulate it.
Georg Brandl116aa622007-08-15 14:28:22 +0000763
764 .. note::
765
766 I/O operations may fail even when :func:`access` indicates that they would
767 succeed, particularly for operations on network filesystems which may have
768 permissions semantics beyond the usual POSIX permission-bit model.
769
770
771.. data:: F_OK
772
773 Value to pass as the *mode* parameter of :func:`access` to test the existence of
774 *path*.
775
776
777.. data:: R_OK
778
779 Value to include in the *mode* parameter of :func:`access` to test the
780 readability of *path*.
781
782
783.. data:: W_OK
784
785 Value to include in the *mode* parameter of :func:`access` to test the
786 writability of *path*.
787
788
789.. data:: X_OK
790
791 Value to include in the *mode* parameter of :func:`access` to determine if
792 *path* can be executed.
793
794
795.. function:: chdir(path)
796
797 .. index:: single: directory; changing
798
Georg Brandlc575c902008-09-13 17:46:05 +0000799 Change the current working directory to *path*. Availability: Unix,
Georg Brandl116aa622007-08-15 14:28:22 +0000800 Windows.
801
802
803.. function:: fchdir(fd)
804
805 Change the current working directory to the directory represented by the file
806 descriptor *fd*. The descriptor must refer to an opened directory, not an open
807 file. Availability: Unix.
808
Georg Brandl116aa622007-08-15 14:28:22 +0000809
810.. function:: getcwd()
811
Martin v. Löwis011e8422009-05-05 04:43:17 +0000812 Return a string representing the current working directory.
813 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000814
Martin v. Löwisa731b992008-10-07 06:36:31 +0000815.. function:: getcwdb()
Georg Brandl116aa622007-08-15 14:28:22 +0000816
Georg Brandl76e55382008-10-08 16:34:57 +0000817 Return a bytestring representing the current working directory.
Georg Brandlc575c902008-09-13 17:46:05 +0000818 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000819
Georg Brandl116aa622007-08-15 14:28:22 +0000820
821.. function:: chflags(path, flags)
822
823 Set the flags of *path* to the numeric *flags*. *flags* may take a combination
824 (bitwise OR) of the following values (as defined in the :mod:`stat` module):
825
826 * ``UF_NODUMP``
827 * ``UF_IMMUTABLE``
828 * ``UF_APPEND``
829 * ``UF_OPAQUE``
830 * ``UF_NOUNLINK``
831 * ``SF_ARCHIVED``
832 * ``SF_IMMUTABLE``
833 * ``SF_APPEND``
834 * ``SF_NOUNLINK``
835 * ``SF_SNAPSHOT``
836
Georg Brandlc575c902008-09-13 17:46:05 +0000837 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000838
Georg Brandl116aa622007-08-15 14:28:22 +0000839
840.. function:: chroot(path)
841
842 Change the root directory of the current process to *path*. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +0000843 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000844
Georg Brandl116aa622007-08-15 14:28:22 +0000845
846.. function:: chmod(path, mode)
847
848 Change the mode of *path* to the numeric *mode*. *mode* may take one of the
Christian Heimesfaf2f632008-01-06 16:59:19 +0000849 following values (as defined in the :mod:`stat` module) or bitwise ORed
Georg Brandl116aa622007-08-15 14:28:22 +0000850 combinations of them:
851
Alexandre Vassalottic22c6f22009-07-21 00:51:58 +0000852 * :data:`stat.S_ISUID`
853 * :data:`stat.S_ISGID`
854 * :data:`stat.S_ENFMT`
855 * :data:`stat.S_ISVTX`
856 * :data:`stat.S_IREAD`
857 * :data:`stat.S_IWRITE`
858 * :data:`stat.S_IEXEC`
859 * :data:`stat.S_IRWXU`
860 * :data:`stat.S_IRUSR`
861 * :data:`stat.S_IWUSR`
862 * :data:`stat.S_IXUSR`
863 * :data:`stat.S_IRWXG`
864 * :data:`stat.S_IRGRP`
865 * :data:`stat.S_IWGRP`
866 * :data:`stat.S_IXGRP`
867 * :data:`stat.S_IRWXO`
868 * :data:`stat.S_IROTH`
869 * :data:`stat.S_IWOTH`
870 * :data:`stat.S_IXOTH`
Georg Brandl116aa622007-08-15 14:28:22 +0000871
Georg Brandlc575c902008-09-13 17:46:05 +0000872 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000873
874 .. note::
875
876 Although Windows supports :func:`chmod`, you can only set the file's read-only
877 flag with it (via the ``stat.S_IWRITE`` and ``stat.S_IREAD``
878 constants or a corresponding integer value). All other bits are
879 ignored.
880
881
882.. function:: chown(path, uid, gid)
883
884 Change the owner and group id of *path* to the numeric *uid* and *gid*. To leave
Georg Brandlc575c902008-09-13 17:46:05 +0000885 one of the ids unchanged, set it to -1. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000886
887
888.. function:: lchflags(path, flags)
889
890 Set the flags of *path* to the numeric *flags*, like :func:`chflags`, but do not
891 follow symbolic links. Availability: Unix.
892
Georg Brandl116aa622007-08-15 14:28:22 +0000893
Christian Heimes93852662007-12-01 12:22:32 +0000894.. function:: lchmod(path, mode)
895
896 Change the mode of *path* to the numeric *mode*. If path is a symlink, this
897 affects the symlink rather than the target. See the docs for :func:`chmod`
898 for possible values of *mode*. Availability: Unix.
899
Christian Heimes93852662007-12-01 12:22:32 +0000900
Georg Brandl116aa622007-08-15 14:28:22 +0000901.. function:: lchown(path, uid, gid)
902
Christian Heimesfaf2f632008-01-06 16:59:19 +0000903 Change the owner and group id of *path* to the numeric *uid* and *gid*. This
Georg Brandlc575c902008-09-13 17:46:05 +0000904 function will not follow symbolic links. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000905
Georg Brandl116aa622007-08-15 14:28:22 +0000906
Benjamin Peterson5879d412009-03-30 14:51:56 +0000907.. function:: link(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +0000908
Benjamin Peterson5879d412009-03-30 14:51:56 +0000909 Create a hard link pointing to *source* named *link_name*. Availability:
910 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000911
912
913.. function:: listdir(path)
914
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000915 Return a list containing the names of the entries in the directory given by
916 *path*. The list is in arbitrary order. It does not include the special
917 entries ``'.'`` and ``'..'`` even if they are present in the directory.
918 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000919
Martin v. Löwis011e8422009-05-05 04:43:17 +0000920 This function can be called with a bytes or string argument, and returns
921 filenames of the same datatype.
Georg Brandl116aa622007-08-15 14:28:22 +0000922
923
924.. function:: lstat(path)
925
Guido van Rossum2cc30da2007-11-02 23:46:40 +0000926 Like :func:`stat`, but do not follow symbolic links. This is an alias for
927 :func:`stat` on platforms that do not support symbolic links, such as
928 Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000929
930
931.. function:: mkfifo(path[, mode])
932
Georg Brandlf4a41232008-05-26 17:55:52 +0000933 Create a FIFO (a named pipe) named *path* with numeric mode *mode*. The
934 default *mode* is ``0o666`` (octal). The current umask value is first masked
Georg Brandlc575c902008-09-13 17:46:05 +0000935 out from the mode. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000936
937 FIFOs are pipes that can be accessed like regular files. FIFOs exist until they
938 are deleted (for example with :func:`os.unlink`). Generally, FIFOs are used as
939 rendezvous between "client" and "server" type processes: the server opens the
940 FIFO for reading, and the client opens it for writing. Note that :func:`mkfifo`
941 doesn't open the FIFO --- it just creates the rendezvous point.
942
943
Georg Brandl18244152009-09-02 20:34:52 +0000944.. function:: mknod(filename[, mode=0o600[, device]])
Georg Brandl116aa622007-08-15 14:28:22 +0000945
946 Create a filesystem node (file, device special file or named pipe) named
Georg Brandl18244152009-09-02 20:34:52 +0000947 *filename*. *mode* specifies both the permissions to use and the type of node
948 to be created, being combined (bitwise OR) with one of ``stat.S_IFREG``,
949 ``stat.S_IFCHR``, ``stat.S_IFBLK``, and ``stat.S_IFIFO`` (those constants are
950 available in :mod:`stat`). For ``stat.S_IFCHR`` and ``stat.S_IFBLK``,
951 *device* defines the newly created device special file (probably using
Georg Brandl116aa622007-08-15 14:28:22 +0000952 :func:`os.makedev`), otherwise it is ignored.
953
Georg Brandl116aa622007-08-15 14:28:22 +0000954
955.. function:: major(device)
956
Christian Heimesfaf2f632008-01-06 16:59:19 +0000957 Extract the device major number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000958 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
959
Georg Brandl116aa622007-08-15 14:28:22 +0000960
961.. function:: minor(device)
962
Christian Heimesfaf2f632008-01-06 16:59:19 +0000963 Extract the device minor number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000964 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
965
Georg Brandl116aa622007-08-15 14:28:22 +0000966
967.. function:: makedev(major, minor)
968
Christian Heimesfaf2f632008-01-06 16:59:19 +0000969 Compose a raw device number from the major and minor device numbers.
Georg Brandl116aa622007-08-15 14:28:22 +0000970
Georg Brandl116aa622007-08-15 14:28:22 +0000971
972.. function:: mkdir(path[, mode])
973
Georg Brandlf4a41232008-05-26 17:55:52 +0000974 Create a directory named *path* with numeric mode *mode*. The default *mode*
975 is ``0o777`` (octal). On some systems, *mode* is ignored. Where it is used,
Georg Brandlc575c902008-09-13 17:46:05 +0000976 the current umask value is first masked out. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000977
Guido van Rossum2cc30da2007-11-02 23:46:40 +0000978 It is also possible to create temporary directories; see the
979 :mod:`tempfile` module's :func:`tempfile.mkdtemp` function.
980
Georg Brandl116aa622007-08-15 14:28:22 +0000981
982.. function:: makedirs(path[, mode])
983
984 .. index::
985 single: directory; creating
986 single: UNC paths; and os.makedirs()
987
988 Recursive directory creation function. Like :func:`mkdir`, but makes all
Georg Brandlf4a41232008-05-26 17:55:52 +0000989 intermediate-level directories needed to contain the leaf directory. Throws
990 an :exc:`error` exception if the leaf directory already exists or cannot be
991 created. The default *mode* is ``0o777`` (octal). On some systems, *mode*
992 is ignored. Where it is used, the current umask value is first masked out.
Georg Brandl116aa622007-08-15 14:28:22 +0000993
994 .. note::
995
996 :func:`makedirs` will become confused if the path elements to create include
Christian Heimesfaf2f632008-01-06 16:59:19 +0000997 :data:`os.pardir`.
Georg Brandl116aa622007-08-15 14:28:22 +0000998
Georg Brandl55ac8f02007-09-01 13:51:09 +0000999 This function handles UNC paths correctly.
Georg Brandl116aa622007-08-15 14:28:22 +00001000
1001
1002.. function:: pathconf(path, name)
1003
1004 Return system configuration information relevant to a named file. *name*
1005 specifies the configuration value to retrieve; it may be a string which is the
1006 name of a defined system value; these names are specified in a number of
1007 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
1008 additional names as well. The names known to the host operating system are
1009 given in the ``pathconf_names`` dictionary. For configuration variables not
1010 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandlc575c902008-09-13 17:46:05 +00001011 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001012
1013 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1014 specific value for *name* is not supported by the host system, even if it is
1015 included in ``pathconf_names``, an :exc:`OSError` is raised with
1016 :const:`errno.EINVAL` for the error number.
1017
1018
1019.. data:: pathconf_names
1020
1021 Dictionary mapping names accepted by :func:`pathconf` and :func:`fpathconf` to
1022 the integer values defined for those names by the host operating system. This
1023 can be used to determine the set of names known to the system. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001024 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001025
1026
1027.. function:: readlink(path)
1028
1029 Return a string representing the path to which the symbolic link points. The
1030 result may be either an absolute or relative pathname; if it is relative, it may
1031 be converted to an absolute pathname using ``os.path.join(os.path.dirname(path),
1032 result)``.
1033
Georg Brandl76e55382008-10-08 16:34:57 +00001034 If the *path* is a string object, the result will also be a string object,
1035 and the call may raise an UnicodeDecodeError. If the *path* is a bytes
1036 object, the result will be a bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +00001037
Georg Brandlc575c902008-09-13 17:46:05 +00001038 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001039
1040
1041.. function:: remove(path)
1042
Georg Brandla6053b42009-09-01 08:11:14 +00001043 Remove (delete) the file *path*. If *path* is a directory, :exc:`OSError` is
1044 raised; see :func:`rmdir` below to remove a directory. This is identical to
1045 the :func:`unlink` function documented below. On Windows, attempting to
1046 remove a file that is in use causes an exception to be raised; on Unix, the
1047 directory entry is removed but the storage allocated to the file is not made
1048 available until the original file is no longer in use. Availability: Unix,
Georg Brandl116aa622007-08-15 14:28:22 +00001049 Windows.
1050
1051
1052.. function:: removedirs(path)
1053
1054 .. index:: single: directory; deleting
1055
Christian Heimesfaf2f632008-01-06 16:59:19 +00001056 Remove directories recursively. Works like :func:`rmdir` except that, if the
Georg Brandl116aa622007-08-15 14:28:22 +00001057 leaf directory is successfully removed, :func:`removedirs` tries to
1058 successively remove every parent directory mentioned in *path* until an error
1059 is raised (which is ignored, because it generally means that a parent directory
1060 is not empty). For example, ``os.removedirs('foo/bar/baz')`` will first remove
1061 the directory ``'foo/bar/baz'``, and then remove ``'foo/bar'`` and ``'foo'`` if
1062 they are empty. Raises :exc:`OSError` if the leaf directory could not be
1063 successfully removed.
1064
Georg Brandl116aa622007-08-15 14:28:22 +00001065
1066.. function:: rename(src, dst)
1067
1068 Rename the file or directory *src* to *dst*. If *dst* is a directory,
1069 :exc:`OSError` will be raised. On Unix, if *dst* exists and is a file, it will
Christian Heimesfaf2f632008-01-06 16:59:19 +00001070 be replaced silently if the user has permission. The operation may fail on some
Georg Brandl116aa622007-08-15 14:28:22 +00001071 Unix flavors if *src* and *dst* are on different filesystems. If successful,
1072 the renaming will be an atomic operation (this is a POSIX requirement). On
1073 Windows, if *dst* already exists, :exc:`OSError` will be raised even if it is a
1074 file; there may be no way to implement an atomic rename when *dst* names an
Georg Brandlc575c902008-09-13 17:46:05 +00001075 existing file. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001076
1077
1078.. function:: renames(old, new)
1079
1080 Recursive directory or file renaming function. Works like :func:`rename`, except
1081 creation of any intermediate directories needed to make the new pathname good is
1082 attempted first. After the rename, directories corresponding to rightmost path
1083 segments of the old name will be pruned away using :func:`removedirs`.
1084
Georg Brandl116aa622007-08-15 14:28:22 +00001085 .. note::
1086
1087 This function can fail with the new directory structure made if you lack
1088 permissions needed to remove the leaf directory or file.
1089
1090
1091.. function:: rmdir(path)
1092
Georg Brandla6053b42009-09-01 08:11:14 +00001093 Remove (delete) the directory *path*. Only works when the directory is
1094 empty, otherwise, :exc:`OSError` is raised. In order to remove whole
1095 directory trees, :func:`shutil.rmtree` can be used. Availability: Unix,
1096 Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001097
1098
1099.. function:: stat(path)
1100
1101 Perform a :cfunc:`stat` system call on the given path. The return value is an
1102 object whose attributes correspond to the members of the :ctype:`stat`
1103 structure, namely: :attr:`st_mode` (protection bits), :attr:`st_ino` (inode
1104 number), :attr:`st_dev` (device), :attr:`st_nlink` (number of hard links),
Christian Heimesfaf2f632008-01-06 16:59:19 +00001105 :attr:`st_uid` (user id of owner), :attr:`st_gid` (group id of owner),
Georg Brandl116aa622007-08-15 14:28:22 +00001106 :attr:`st_size` (size of file, in bytes), :attr:`st_atime` (time of most recent
1107 access), :attr:`st_mtime` (time of most recent content modification),
1108 :attr:`st_ctime` (platform dependent; time of most recent metadata change on
1109 Unix, or the time of creation on Windows)::
1110
1111 >>> import os
1112 >>> statinfo = os.stat('somefile.txt')
1113 >>> statinfo
Georg Brandlf66df2b2010-01-16 14:41:21 +00001114 (33188, 422511, 769, 1, 1032, 100, 926, 1105022698,1105022732, 1105022732)
Georg Brandl116aa622007-08-15 14:28:22 +00001115 >>> statinfo.st_size
Georg Brandlf66df2b2010-01-16 14:41:21 +00001116 926
Georg Brandl116aa622007-08-15 14:28:22 +00001117 >>>
1118
Georg Brandl116aa622007-08-15 14:28:22 +00001119
1120 On some Unix systems (such as Linux), the following attributes may also be
1121 available: :attr:`st_blocks` (number of blocks allocated for file),
1122 :attr:`st_blksize` (filesystem blocksize), :attr:`st_rdev` (type of device if an
1123 inode device). :attr:`st_flags` (user defined flags for file).
1124
1125 On other Unix systems (such as FreeBSD), the following attributes may be
1126 available (but may be only filled out if root tries to use them): :attr:`st_gen`
1127 (file generation number), :attr:`st_birthtime` (time of file creation).
1128
1129 On Mac OS systems, the following attributes may also be available:
1130 :attr:`st_rsize`, :attr:`st_creator`, :attr:`st_type`.
1131
Georg Brandl116aa622007-08-15 14:28:22 +00001132 .. index:: module: stat
1133
1134 For backward compatibility, the return value of :func:`stat` is also accessible
1135 as a tuple of at least 10 integers giving the most important (and portable)
1136 members of the :ctype:`stat` structure, in the order :attr:`st_mode`,
1137 :attr:`st_ino`, :attr:`st_dev`, :attr:`st_nlink`, :attr:`st_uid`,
1138 :attr:`st_gid`, :attr:`st_size`, :attr:`st_atime`, :attr:`st_mtime`,
1139 :attr:`st_ctime`. More items may be added at the end by some implementations.
1140 The standard module :mod:`stat` defines functions and constants that are useful
1141 for extracting information from a :ctype:`stat` structure. (On Windows, some
1142 items are filled with dummy values.)
1143
1144 .. note::
1145
1146 The exact meaning and resolution of the :attr:`st_atime`, :attr:`st_mtime`, and
1147 :attr:`st_ctime` members depends on the operating system and the file system.
1148 For example, on Windows systems using the FAT or FAT32 file systems,
1149 :attr:`st_mtime` has 2-second resolution, and :attr:`st_atime` has only 1-day
1150 resolution. See your operating system documentation for details.
1151
Georg Brandlc575c902008-09-13 17:46:05 +00001152 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001153
Georg Brandl116aa622007-08-15 14:28:22 +00001154
1155.. function:: stat_float_times([newvalue])
1156
1157 Determine whether :class:`stat_result` represents time stamps as float objects.
1158 If *newvalue* is ``True``, future calls to :func:`stat` return floats, if it is
1159 ``False``, future calls return ints. If *newvalue* is omitted, return the
1160 current setting.
1161
1162 For compatibility with older Python versions, accessing :class:`stat_result` as
1163 a tuple always returns integers.
1164
Georg Brandl55ac8f02007-09-01 13:51:09 +00001165 Python now returns float values by default. Applications which do not work
1166 correctly with floating point time stamps can use this function to restore the
1167 old behaviour.
Georg Brandl116aa622007-08-15 14:28:22 +00001168
1169 The resolution of the timestamps (that is the smallest possible fraction)
1170 depends on the system. Some systems only support second resolution; on these
1171 systems, the fraction will always be zero.
1172
1173 It is recommended that this setting is only changed at program startup time in
1174 the *__main__* module; libraries should never change this setting. If an
1175 application uses a library that works incorrectly if floating point time stamps
1176 are processed, this application should turn the feature off until the library
1177 has been corrected.
1178
1179
1180.. function:: statvfs(path)
1181
1182 Perform a :cfunc:`statvfs` system call on the given path. The return value is
1183 an object whose attributes describe the filesystem on the given path, and
1184 correspond to the members of the :ctype:`statvfs` structure, namely:
1185 :attr:`f_bsize`, :attr:`f_frsize`, :attr:`f_blocks`, :attr:`f_bfree`,
1186 :attr:`f_bavail`, :attr:`f_files`, :attr:`f_ffree`, :attr:`f_favail`,
1187 :attr:`f_flag`, :attr:`f_namemax`. Availability: Unix.
1188
Georg Brandl116aa622007-08-15 14:28:22 +00001189
Benjamin Peterson5879d412009-03-30 14:51:56 +00001190.. function:: symlink(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +00001191
Benjamin Peterson5879d412009-03-30 14:51:56 +00001192 Create a symbolic link pointing to *source* named *link_name*. Availability:
1193 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001194
1195
Georg Brandl116aa622007-08-15 14:28:22 +00001196.. function:: unlink(path)
1197
Georg Brandla6053b42009-09-01 08:11:14 +00001198 Remove (delete) the file *path*. This is the same function as
1199 :func:`remove`; the :func:`unlink` name is its traditional Unix
1200 name. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001201
1202
1203.. function:: utime(path, times)
1204
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001205 Set the access and modified times of the file specified by *path*. If *times*
1206 is ``None``, then the file's access and modified times are set to the current
1207 time. (The effect is similar to running the Unix program :program:`touch` on
1208 the path.) Otherwise, *times* must be a 2-tuple of numbers, of the form
1209 ``(atime, mtime)`` which is used to set the access and modified times,
1210 respectively. Whether a directory can be given for *path* depends on whether
1211 the operating system implements directories as files (for example, Windows
1212 does not). Note that the exact times you set here may not be returned by a
1213 subsequent :func:`stat` call, depending on the resolution with which your
1214 operating system records access and modification times; see :func:`stat`.
Georg Brandl116aa622007-08-15 14:28:22 +00001215
Georg Brandlc575c902008-09-13 17:46:05 +00001216 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001217
1218
Georg Brandl18244152009-09-02 20:34:52 +00001219.. function:: walk(top, topdown=True, onerror=None, followlinks=False)
Georg Brandl116aa622007-08-15 14:28:22 +00001220
1221 .. index::
1222 single: directory; walking
1223 single: directory; traversal
1224
Christian Heimesfaf2f632008-01-06 16:59:19 +00001225 Generate the file names in a directory tree by walking the tree
1226 either top-down or bottom-up. For each directory in the tree rooted at directory
Georg Brandl116aa622007-08-15 14:28:22 +00001227 *top* (including *top* itself), it yields a 3-tuple ``(dirpath, dirnames,
1228 filenames)``.
1229
1230 *dirpath* is a string, the path to the directory. *dirnames* is a list of the
1231 names of the subdirectories in *dirpath* (excluding ``'.'`` and ``'..'``).
1232 *filenames* is a list of the names of the non-directory files in *dirpath*.
1233 Note that the names in the lists contain no path components. To get a full path
1234 (which begins with *top*) to a file or directory in *dirpath*, do
1235 ``os.path.join(dirpath, name)``.
1236
Christian Heimesfaf2f632008-01-06 16:59:19 +00001237 If optional argument *topdown* is ``True`` or not specified, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001238 directory is generated before the triples for any of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001239 (directories are generated top-down). If *topdown* is ``False``, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001240 directory is generated after the triples for all of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001241 (directories are generated bottom-up).
Georg Brandl116aa622007-08-15 14:28:22 +00001242
Christian Heimesfaf2f632008-01-06 16:59:19 +00001243 When *topdown* is ``True``, the caller can modify the *dirnames* list in-place
Georg Brandl116aa622007-08-15 14:28:22 +00001244 (perhaps using :keyword:`del` or slice assignment), and :func:`walk` will only
1245 recurse into the subdirectories whose names remain in *dirnames*; this can be
1246 used to prune the search, impose a specific order of visiting, or even to inform
1247 :func:`walk` about directories the caller creates or renames before it resumes
Christian Heimesfaf2f632008-01-06 16:59:19 +00001248 :func:`walk` again. Modifying *dirnames* when *topdown* is ``False`` is
Georg Brandl116aa622007-08-15 14:28:22 +00001249 ineffective, because in bottom-up mode the directories in *dirnames* are
1250 generated before *dirpath* itself is generated.
1251
Christian Heimesfaf2f632008-01-06 16:59:19 +00001252 By default errors from the :func:`listdir` call are ignored. If optional
Georg Brandl116aa622007-08-15 14:28:22 +00001253 argument *onerror* is specified, it should be a function; it will be called with
1254 one argument, an :exc:`OSError` instance. It can report the error to continue
1255 with the walk, or raise the exception to abort the walk. Note that the filename
1256 is available as the ``filename`` attribute of the exception object.
1257
1258 By default, :func:`walk` will not walk down into symbolic links that resolve to
Christian Heimesfaf2f632008-01-06 16:59:19 +00001259 directories. Set *followlinks* to ``True`` to visit directories pointed to by
Georg Brandl116aa622007-08-15 14:28:22 +00001260 symlinks, on systems that support them.
1261
Georg Brandl116aa622007-08-15 14:28:22 +00001262 .. note::
1263
Christian Heimesfaf2f632008-01-06 16:59:19 +00001264 Be aware that setting *followlinks* to ``True`` can lead to infinite recursion if a
Georg Brandl116aa622007-08-15 14:28:22 +00001265 link points to a parent directory of itself. :func:`walk` does not keep track of
1266 the directories it visited already.
1267
1268 .. note::
1269
1270 If you pass a relative pathname, don't change the current working directory
1271 between resumptions of :func:`walk`. :func:`walk` never changes the current
1272 directory, and assumes that its caller doesn't either.
1273
1274 This example displays the number of bytes taken by non-directory files in each
1275 directory under the starting directory, except that it doesn't look under any
1276 CVS subdirectory::
1277
1278 import os
1279 from os.path import join, getsize
1280 for root, dirs, files in os.walk('python/Lib/email'):
Georg Brandl6911e3c2007-09-04 07:15:32 +00001281 print(root, "consumes", end=" ")
1282 print(sum(getsize(join(root, name)) for name in files), end=" ")
1283 print("bytes in", len(files), "non-directory files")
Georg Brandl116aa622007-08-15 14:28:22 +00001284 if 'CVS' in dirs:
1285 dirs.remove('CVS') # don't visit CVS directories
1286
Christian Heimesfaf2f632008-01-06 16:59:19 +00001287 In the next example, walking the tree bottom-up is essential: :func:`rmdir`
Georg Brandl116aa622007-08-15 14:28:22 +00001288 doesn't allow deleting a directory before the directory is empty::
1289
Christian Heimesfaf2f632008-01-06 16:59:19 +00001290 # Delete everything reachable from the directory named in "top",
Georg Brandl116aa622007-08-15 14:28:22 +00001291 # assuming there are no symbolic links.
1292 # CAUTION: This is dangerous! For example, if top == '/', it
1293 # could delete all your disk files.
1294 import os
1295 for root, dirs, files in os.walk(top, topdown=False):
1296 for name in files:
1297 os.remove(os.path.join(root, name))
1298 for name in dirs:
1299 os.rmdir(os.path.join(root, name))
1300
Georg Brandl116aa622007-08-15 14:28:22 +00001301
1302.. _os-process:
1303
1304Process Management
1305------------------
1306
1307These functions may be used to create and manage processes.
1308
1309The various :func:`exec\*` functions take a list of arguments for the new
1310program loaded into the process. In each case, the first of these arguments is
1311passed to the new program as its own name rather than as an argument a user may
1312have typed on a command line. For the C programmer, this is the ``argv[0]``
1313passed to a program's :cfunc:`main`. For example, ``os.execv('/bin/echo',
1314['foo', 'bar'])`` will only print ``bar`` on standard output; ``foo`` will seem
1315to be ignored.
1316
1317
1318.. function:: abort()
1319
1320 Generate a :const:`SIGABRT` signal to the current process. On Unix, the default
1321 behavior is to produce a core dump; on Windows, the process immediately returns
1322 an exit code of ``3``. Be aware that programs which use :func:`signal.signal`
1323 to register a handler for :const:`SIGABRT` will behave differently.
Georg Brandlc575c902008-09-13 17:46:05 +00001324 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001325
1326
1327.. function:: execl(path, arg0, arg1, ...)
1328 execle(path, arg0, arg1, ..., env)
1329 execlp(file, arg0, arg1, ...)
1330 execlpe(file, arg0, arg1, ..., env)
1331 execv(path, args)
1332 execve(path, args, env)
1333 execvp(file, args)
1334 execvpe(file, args, env)
1335
1336 These functions all execute a new program, replacing the current process; they
1337 do not return. On Unix, the new executable is loaded into the current process,
Christian Heimesfaf2f632008-01-06 16:59:19 +00001338 and will have the same process id as the caller. Errors will be reported as
Georg Brandl48310cd2009-01-03 21:18:54 +00001339 :exc:`OSError` exceptions.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001340
1341 The current process is replaced immediately. Open file objects and
1342 descriptors are not flushed, so if there may be data buffered
1343 on these open files, you should flush them using
1344 :func:`sys.stdout.flush` or :func:`os.fsync` before calling an
1345 :func:`exec\*` function.
Georg Brandl116aa622007-08-15 14:28:22 +00001346
Christian Heimesfaf2f632008-01-06 16:59:19 +00001347 The "l" and "v" variants of the :func:`exec\*` functions differ in how
1348 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001349 to work with if the number of parameters is fixed when the code is written; the
1350 individual parameters simply become additional parameters to the :func:`execl\*`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001351 functions. The "v" variants are good when the number of parameters is
Georg Brandl116aa622007-08-15 14:28:22 +00001352 variable, with the arguments being passed in a list or tuple as the *args*
1353 parameter. In either case, the arguments to the child process should start with
1354 the name of the command being run, but this is not enforced.
1355
Christian Heimesfaf2f632008-01-06 16:59:19 +00001356 The variants which include a "p" near the end (:func:`execlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001357 :func:`execlpe`, :func:`execvp`, and :func:`execvpe`) will use the
1358 :envvar:`PATH` environment variable to locate the program *file*. When the
1359 environment is being replaced (using one of the :func:`exec\*e` variants,
1360 discussed in the next paragraph), the new environment is used as the source of
1361 the :envvar:`PATH` variable. The other variants, :func:`execl`, :func:`execle`,
1362 :func:`execv`, and :func:`execve`, will not use the :envvar:`PATH` variable to
1363 locate the executable; *path* must contain an appropriate absolute or relative
1364 path.
1365
1366 For :func:`execle`, :func:`execlpe`, :func:`execve`, and :func:`execvpe` (note
Christian Heimesfaf2f632008-01-06 16:59:19 +00001367 that these all end in "e"), the *env* parameter must be a mapping which is
Christian Heimesa342c012008-04-20 21:01:16 +00001368 used to define the environment variables for the new process (these are used
1369 instead of the current process' environment); the functions :func:`execl`,
Georg Brandl116aa622007-08-15 14:28:22 +00001370 :func:`execlp`, :func:`execv`, and :func:`execvp` all cause the new process to
Georg Brandl48310cd2009-01-03 21:18:54 +00001371 inherit the environment of the current process.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001372
1373 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001374
1375
1376.. function:: _exit(n)
1377
1378 Exit to the system with status *n*, without calling cleanup handlers, flushing
Georg Brandlc575c902008-09-13 17:46:05 +00001379 stdio buffers, etc. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001380
1381 .. note::
1382
1383 The standard way to exit is ``sys.exit(n)``. :func:`_exit` should normally only
1384 be used in the child process after a :func:`fork`.
1385
Christian Heimesfaf2f632008-01-06 16:59:19 +00001386The following exit codes are defined and can be used with :func:`_exit`,
Georg Brandl116aa622007-08-15 14:28:22 +00001387although they are not required. These are typically used for system programs
1388written in Python, such as a mail server's external command delivery program.
1389
1390.. note::
1391
1392 Some of these may not be available on all Unix platforms, since there is some
1393 variation. These constants are defined where they are defined by the underlying
1394 platform.
1395
1396
1397.. data:: EX_OK
1398
Georg Brandlc575c902008-09-13 17:46:05 +00001399 Exit code that means no error occurred. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001400
Georg Brandl116aa622007-08-15 14:28:22 +00001401
1402.. data:: EX_USAGE
1403
1404 Exit code that means the command was used incorrectly, such as when the wrong
Georg Brandlc575c902008-09-13 17:46:05 +00001405 number of arguments are given. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001406
Georg Brandl116aa622007-08-15 14:28:22 +00001407
1408.. data:: EX_DATAERR
1409
Georg Brandlc575c902008-09-13 17:46:05 +00001410 Exit code that means the input data was incorrect. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001411
Georg Brandl116aa622007-08-15 14:28:22 +00001412
1413.. data:: EX_NOINPUT
1414
1415 Exit code that means an input file did not exist or was not readable.
Georg Brandlc575c902008-09-13 17:46:05 +00001416 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001417
Georg Brandl116aa622007-08-15 14:28:22 +00001418
1419.. data:: EX_NOUSER
1420
Georg Brandlc575c902008-09-13 17:46:05 +00001421 Exit code that means a specified user did not exist. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001422
Georg Brandl116aa622007-08-15 14:28:22 +00001423
1424.. data:: EX_NOHOST
1425
Georg Brandlc575c902008-09-13 17:46:05 +00001426 Exit code that means a specified host did not exist. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001427
Georg Brandl116aa622007-08-15 14:28:22 +00001428
1429.. data:: EX_UNAVAILABLE
1430
1431 Exit code that means that a required service is unavailable. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001432 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001433
Georg Brandl116aa622007-08-15 14:28:22 +00001434
1435.. data:: EX_SOFTWARE
1436
1437 Exit code that means an internal software error was detected. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001438 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001439
Georg Brandl116aa622007-08-15 14:28:22 +00001440
1441.. data:: EX_OSERR
1442
1443 Exit code that means an operating system error was detected, such as the
Georg Brandlc575c902008-09-13 17:46:05 +00001444 inability to fork or create a pipe. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001445
Georg Brandl116aa622007-08-15 14:28:22 +00001446
1447.. data:: EX_OSFILE
1448
1449 Exit code that means some system file did not exist, could not be opened, or had
Georg Brandlc575c902008-09-13 17:46:05 +00001450 some other kind of error. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001451
Georg Brandl116aa622007-08-15 14:28:22 +00001452
1453.. data:: EX_CANTCREAT
1454
1455 Exit code that means a user specified output file could not be created.
Georg Brandlc575c902008-09-13 17:46:05 +00001456 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001457
Georg Brandl116aa622007-08-15 14:28:22 +00001458
1459.. data:: EX_IOERR
1460
1461 Exit code that means that an error occurred while doing I/O on some file.
Georg Brandlc575c902008-09-13 17:46:05 +00001462 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001463
Georg Brandl116aa622007-08-15 14:28:22 +00001464
1465.. data:: EX_TEMPFAIL
1466
1467 Exit code that means a temporary failure occurred. This indicates something
1468 that may not really be an error, such as a network connection that couldn't be
Georg Brandlc575c902008-09-13 17:46:05 +00001469 made during a retryable operation. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001470
Georg Brandl116aa622007-08-15 14:28:22 +00001471
1472.. data:: EX_PROTOCOL
1473
1474 Exit code that means that a protocol exchange was illegal, invalid, or not
Georg Brandlc575c902008-09-13 17:46:05 +00001475 understood. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001476
Georg Brandl116aa622007-08-15 14:28:22 +00001477
1478.. data:: EX_NOPERM
1479
1480 Exit code that means that there were insufficient permissions to perform the
Georg Brandlc575c902008-09-13 17:46:05 +00001481 operation (but not intended for file system problems). Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001482
Georg Brandl116aa622007-08-15 14:28:22 +00001483
1484.. data:: EX_CONFIG
1485
1486 Exit code that means that some kind of configuration error occurred.
Georg Brandlc575c902008-09-13 17:46:05 +00001487 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001488
Georg Brandl116aa622007-08-15 14:28:22 +00001489
1490.. data:: EX_NOTFOUND
1491
1492 Exit code that means something like "an entry was not found". Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001493 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001494
Georg Brandl116aa622007-08-15 14:28:22 +00001495
1496.. function:: fork()
1497
Christian Heimesfaf2f632008-01-06 16:59:19 +00001498 Fork a child process. Return ``0`` in the child and the child's process id in the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001499 parent. If an error occurs :exc:`OSError` is raised.
Benjamin Petersonbcd8ac32008-10-10 22:20:52 +00001500
1501 Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have
1502 known issues when using fork() from a thread.
1503
Georg Brandlc575c902008-09-13 17:46:05 +00001504 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001505
1506
1507.. function:: forkpty()
1508
1509 Fork a child process, using a new pseudo-terminal as the child's controlling
1510 terminal. Return a pair of ``(pid, fd)``, where *pid* is ``0`` in the child, the
1511 new child's process id in the parent, and *fd* is the file descriptor of the
1512 master end of the pseudo-terminal. For a more portable approach, use the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001513 :mod:`pty` module. If an error occurs :exc:`OSError` is raised.
Georg Brandlc575c902008-09-13 17:46:05 +00001514 Availability: some flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001515
1516
1517.. function:: kill(pid, sig)
1518
1519 .. index::
1520 single: process; killing
1521 single: process; signalling
1522
1523 Send signal *sig* to the process *pid*. Constants for the specific signals
1524 available on the host platform are defined in the :mod:`signal` module.
Brian Curtineb24d742010-04-12 17:16:38 +00001525
1526 Windows: The :data:`signal.CTRL_C_EVENT` and
1527 :data:`signal.CTRL_BREAK_EVENT` signals are special signals which can
1528 only be sent to console processes which share a common console window,
1529 e.g., some subprocesses. Any other value for *sig* will cause the process
1530 to be unconditionally killed by the TerminateProcess API, and the exit code
1531 will be set to *sig*. The Windows version of :func:`kill` additionally takes
1532 process handles to be killed.
Georg Brandl116aa622007-08-15 14:28:22 +00001533
Brian Curtin904bd392010-04-20 15:28:06 +00001534 .. versionadded:: 3.2 Windows support
1535
Georg Brandl116aa622007-08-15 14:28:22 +00001536
1537.. function:: killpg(pgid, sig)
1538
1539 .. index::
1540 single: process; killing
1541 single: process; signalling
1542
Georg Brandlc575c902008-09-13 17:46:05 +00001543 Send the signal *sig* to the process group *pgid*. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001544
Georg Brandl116aa622007-08-15 14:28:22 +00001545
1546.. function:: nice(increment)
1547
1548 Add *increment* to the process's "niceness". Return the new niceness.
Georg Brandlc575c902008-09-13 17:46:05 +00001549 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001550
1551
1552.. function:: plock(op)
1553
1554 Lock program segments into memory. The value of *op* (defined in
Georg Brandlc575c902008-09-13 17:46:05 +00001555 ``<sys/lock.h>``) determines which segments are locked. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001556
1557
1558.. function:: popen(...)
1559 :noindex:
1560
1561 Run child processes, returning opened pipes for communications. These functions
1562 are described in section :ref:`os-newstreams`.
1563
1564
1565.. function:: spawnl(mode, path, ...)
1566 spawnle(mode, path, ..., env)
1567 spawnlp(mode, file, ...)
1568 spawnlpe(mode, file, ..., env)
1569 spawnv(mode, path, args)
1570 spawnve(mode, path, args, env)
1571 spawnvp(mode, file, args)
1572 spawnvpe(mode, file, args, env)
1573
1574 Execute the program *path* in a new process.
1575
1576 (Note that the :mod:`subprocess` module provides more powerful facilities for
1577 spawning new processes and retrieving their results; using that module is
Benjamin Peterson87c8d872009-06-11 22:54:11 +00001578 preferable to using these functions. Check especially the
1579 :ref:`subprocess-replacements` section.)
Georg Brandl116aa622007-08-15 14:28:22 +00001580
Christian Heimesfaf2f632008-01-06 16:59:19 +00001581 If *mode* is :const:`P_NOWAIT`, this function returns the process id of the new
Georg Brandl116aa622007-08-15 14:28:22 +00001582 process; if *mode* is :const:`P_WAIT`, returns the process's exit code if it
1583 exits normally, or ``-signal``, where *signal* is the signal that killed the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001584 process. On Windows, the process id will actually be the process handle, so can
Georg Brandl116aa622007-08-15 14:28:22 +00001585 be used with the :func:`waitpid` function.
1586
Christian Heimesfaf2f632008-01-06 16:59:19 +00001587 The "l" and "v" variants of the :func:`spawn\*` functions differ in how
1588 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001589 to work with if the number of parameters is fixed when the code is written; the
1590 individual parameters simply become additional parameters to the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001591 :func:`spawnl\*` functions. The "v" variants are good when the number of
Georg Brandl116aa622007-08-15 14:28:22 +00001592 parameters is variable, with the arguments being passed in a list or tuple as
1593 the *args* parameter. In either case, the arguments to the child process must
1594 start with the name of the command being run.
1595
Christian Heimesfaf2f632008-01-06 16:59:19 +00001596 The variants which include a second "p" near the end (:func:`spawnlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001597 :func:`spawnlpe`, :func:`spawnvp`, and :func:`spawnvpe`) will use the
1598 :envvar:`PATH` environment variable to locate the program *file*. When the
1599 environment is being replaced (using one of the :func:`spawn\*e` variants,
1600 discussed in the next paragraph), the new environment is used as the source of
1601 the :envvar:`PATH` variable. The other variants, :func:`spawnl`,
1602 :func:`spawnle`, :func:`spawnv`, and :func:`spawnve`, will not use the
1603 :envvar:`PATH` variable to locate the executable; *path* must contain an
1604 appropriate absolute or relative path.
1605
1606 For :func:`spawnle`, :func:`spawnlpe`, :func:`spawnve`, and :func:`spawnvpe`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001607 (note that these all end in "e"), the *env* parameter must be a mapping
Christian Heimesa342c012008-04-20 21:01:16 +00001608 which is used to define the environment variables for the new process (they are
1609 used instead of the current process' environment); the functions
Georg Brandl116aa622007-08-15 14:28:22 +00001610 :func:`spawnl`, :func:`spawnlp`, :func:`spawnv`, and :func:`spawnvp` all cause
Benjamin Petersond23f8222009-04-05 19:13:16 +00001611 the new process to inherit the environment of the current process. Note that
1612 keys and values in the *env* dictionary must be strings; invalid keys or
1613 values will cause the function to fail, with a return value of ``127``.
Georg Brandl116aa622007-08-15 14:28:22 +00001614
1615 As an example, the following calls to :func:`spawnlp` and :func:`spawnvpe` are
1616 equivalent::
1617
1618 import os
1619 os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1620
1621 L = ['cp', 'index.html', '/dev/null']
1622 os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1623
1624 Availability: Unix, Windows. :func:`spawnlp`, :func:`spawnlpe`, :func:`spawnvp`
1625 and :func:`spawnvpe` are not available on Windows.
1626
Georg Brandl116aa622007-08-15 14:28:22 +00001627
1628.. data:: P_NOWAIT
1629 P_NOWAITO
1630
1631 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1632 functions. If either of these values is given, the :func:`spawn\*` functions
Christian Heimesfaf2f632008-01-06 16:59:19 +00001633 will return as soon as the new process has been created, with the process id as
Georg Brandlc575c902008-09-13 17:46:05 +00001634 the return value. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001635
Georg Brandl116aa622007-08-15 14:28:22 +00001636
1637.. data:: P_WAIT
1638
1639 Possible value for the *mode* parameter to the :func:`spawn\*` family of
1640 functions. If this is given as *mode*, the :func:`spawn\*` functions will not
1641 return until the new process has run to completion and will return the exit code
1642 of the process the run is successful, or ``-signal`` if a signal kills the
Georg Brandlc575c902008-09-13 17:46:05 +00001643 process. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001644
Georg Brandl116aa622007-08-15 14:28:22 +00001645
1646.. data:: P_DETACH
1647 P_OVERLAY
1648
1649 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1650 functions. These are less portable than those listed above. :const:`P_DETACH`
1651 is similar to :const:`P_NOWAIT`, but the new process is detached from the
1652 console of the calling process. If :const:`P_OVERLAY` is used, the current
1653 process will be replaced; the :func:`spawn\*` function will not return.
1654 Availability: Windows.
1655
Georg Brandl116aa622007-08-15 14:28:22 +00001656
1657.. function:: startfile(path[, operation])
1658
1659 Start a file with its associated application.
1660
1661 When *operation* is not specified or ``'open'``, this acts like double-clicking
1662 the file in Windows Explorer, or giving the file name as an argument to the
1663 :program:`start` command from the interactive command shell: the file is opened
1664 with whatever application (if any) its extension is associated.
1665
1666 When another *operation* is given, it must be a "command verb" that specifies
1667 what should be done with the file. Common verbs documented by Microsoft are
1668 ``'print'`` and ``'edit'`` (to be used on files) as well as ``'explore'`` and
1669 ``'find'`` (to be used on directories).
1670
1671 :func:`startfile` returns as soon as the associated application is launched.
1672 There is no option to wait for the application to close, and no way to retrieve
1673 the application's exit status. The *path* parameter is relative to the current
1674 directory. If you want to use an absolute path, make sure the first character
1675 is not a slash (``'/'``); the underlying Win32 :cfunc:`ShellExecute` function
1676 doesn't work if it is. Use the :func:`os.path.normpath` function to ensure that
1677 the path is properly encoded for Win32. Availability: Windows.
1678
Georg Brandl116aa622007-08-15 14:28:22 +00001679
1680.. function:: system(command)
1681
1682 Execute the command (a string) in a subshell. This is implemented by calling
Georg Brandl495f7b52009-10-27 15:28:25 +00001683 the Standard C function :cfunc:`system`, and has the same limitations.
1684 Changes to :data:`sys.stdin`, etc. are not reflected in the environment of the
1685 executed command.
Georg Brandl116aa622007-08-15 14:28:22 +00001686
1687 On Unix, the return value is the exit status of the process encoded in the
1688 format specified for :func:`wait`. Note that POSIX does not specify the meaning
1689 of the return value of the C :cfunc:`system` function, so the return value of
1690 the Python function is system-dependent.
1691
1692 On Windows, the return value is that returned by the system shell after running
1693 *command*, given by the Windows environment variable :envvar:`COMSPEC`: on
1694 :program:`command.com` systems (Windows 95, 98 and ME) this is always ``0``; on
1695 :program:`cmd.exe` systems (Windows NT, 2000 and XP) this is the exit status of
1696 the command run; on systems using a non-native shell, consult your shell
1697 documentation.
1698
Georg Brandlc575c902008-09-13 17:46:05 +00001699 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001700
1701 The :mod:`subprocess` module provides more powerful facilities for spawning new
1702 processes and retrieving their results; using that module is preferable to using
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001703 this function. Use the :mod:`subprocess` module. Check especially the
1704 :ref:`subprocess-replacements` section.
Georg Brandl116aa622007-08-15 14:28:22 +00001705
1706
1707.. function:: times()
1708
1709 Return a 5-tuple of floating point numbers indicating accumulated (processor or
1710 other) times, in seconds. The items are: user time, system time, children's
1711 user time, children's system time, and elapsed real time since a fixed point in
1712 the past, in that order. See the Unix manual page :manpage:`times(2)` or the
Georg Brandlc575c902008-09-13 17:46:05 +00001713 corresponding Windows Platform API documentation. Availability: Unix,
Christian Heimes68f5fbe2008-02-14 08:27:37 +00001714 Windows. On Windows, only the first two items are filled, the others are zero.
Georg Brandl116aa622007-08-15 14:28:22 +00001715
1716
1717.. function:: wait()
1718
1719 Wait for completion of a child process, and return a tuple containing its pid
1720 and exit status indication: a 16-bit number, whose low byte is the signal number
1721 that killed the process, and whose high byte is the exit status (if the signal
1722 number is zero); the high bit of the low byte is set if a core file was
Georg Brandlc575c902008-09-13 17:46:05 +00001723 produced. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001724
1725
1726.. function:: waitpid(pid, options)
1727
1728 The details of this function differ on Unix and Windows.
1729
1730 On Unix: Wait for completion of a child process given by process id *pid*, and
1731 return a tuple containing its process id and exit status indication (encoded as
1732 for :func:`wait`). The semantics of the call are affected by the value of the
1733 integer *options*, which should be ``0`` for normal operation.
1734
1735 If *pid* is greater than ``0``, :func:`waitpid` requests status information for
1736 that specific process. If *pid* is ``0``, the request is for the status of any
1737 child in the process group of the current process. If *pid* is ``-1``, the
1738 request pertains to any child of the current process. If *pid* is less than
1739 ``-1``, status is requested for any process in the process group ``-pid`` (the
1740 absolute value of *pid*).
1741
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001742 An :exc:`OSError` is raised with the value of errno when the syscall
1743 returns -1.
1744
Georg Brandl116aa622007-08-15 14:28:22 +00001745 On Windows: Wait for completion of a process given by process handle *pid*, and
1746 return a tuple containing *pid*, and its exit status shifted left by 8 bits
1747 (shifting makes cross-platform use of the function easier). A *pid* less than or
1748 equal to ``0`` has no special meaning on Windows, and raises an exception. The
1749 value of integer *options* has no effect. *pid* can refer to any process whose
1750 id is known, not necessarily a child process. The :func:`spawn` functions called
1751 with :const:`P_NOWAIT` return suitable process handles.
1752
1753
1754.. function:: wait3([options])
1755
1756 Similar to :func:`waitpid`, except no process id argument is given and a
1757 3-element tuple containing the child's process id, exit status indication, and
1758 resource usage information is returned. Refer to :mod:`resource`.\
1759 :func:`getrusage` for details on resource usage information. The option
1760 argument is the same as that provided to :func:`waitpid` and :func:`wait4`.
1761 Availability: Unix.
1762
Georg Brandl116aa622007-08-15 14:28:22 +00001763
1764.. function:: wait4(pid, options)
1765
1766 Similar to :func:`waitpid`, except a 3-element tuple, containing the child's
1767 process id, exit status indication, and resource usage information is returned.
1768 Refer to :mod:`resource`.\ :func:`getrusage` for details on resource usage
1769 information. The arguments to :func:`wait4` are the same as those provided to
1770 :func:`waitpid`. Availability: Unix.
1771
Georg Brandl116aa622007-08-15 14:28:22 +00001772
1773.. data:: WNOHANG
1774
1775 The option for :func:`waitpid` to return immediately if no child process status
1776 is available immediately. The function returns ``(0, 0)`` in this case.
Georg Brandlc575c902008-09-13 17:46:05 +00001777 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001778
1779
1780.. data:: WCONTINUED
1781
1782 This option causes child processes to be reported if they have been continued
1783 from a job control stop since their status was last reported. Availability: Some
1784 Unix systems.
1785
Georg Brandl116aa622007-08-15 14:28:22 +00001786
1787.. data:: WUNTRACED
1788
1789 This option causes child processes to be reported if they have been stopped but
1790 their current state has not been reported since they were stopped. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001791 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001792
Georg Brandl116aa622007-08-15 14:28:22 +00001793
1794The following functions take a process status code as returned by
1795:func:`system`, :func:`wait`, or :func:`waitpid` as a parameter. They may be
1796used to determine the disposition of a process.
1797
Georg Brandl116aa622007-08-15 14:28:22 +00001798.. function:: WCOREDUMP(status)
1799
Christian Heimesfaf2f632008-01-06 16:59:19 +00001800 Return ``True`` if a core dump was generated for the process, otherwise
Georg Brandlc575c902008-09-13 17:46:05 +00001801 return ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001802
Georg Brandl116aa622007-08-15 14:28:22 +00001803
1804.. function:: WIFCONTINUED(status)
1805
Christian Heimesfaf2f632008-01-06 16:59:19 +00001806 Return ``True`` if the process has been continued from a job control stop,
1807 otherwise return ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001808
Georg Brandl116aa622007-08-15 14:28:22 +00001809
1810.. function:: WIFSTOPPED(status)
1811
Christian Heimesfaf2f632008-01-06 16:59:19 +00001812 Return ``True`` if the process has been stopped, otherwise return
Georg Brandl116aa622007-08-15 14:28:22 +00001813 ``False``. Availability: Unix.
1814
1815
1816.. function:: WIFSIGNALED(status)
1817
Christian Heimesfaf2f632008-01-06 16:59:19 +00001818 Return ``True`` if the process exited due to a signal, otherwise return
Georg Brandlc575c902008-09-13 17:46:05 +00001819 ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001820
1821
1822.. function:: WIFEXITED(status)
1823
Christian Heimesfaf2f632008-01-06 16:59:19 +00001824 Return ``True`` if the process exited using the :manpage:`exit(2)` system call,
Georg Brandlc575c902008-09-13 17:46:05 +00001825 otherwise return ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001826
1827
1828.. function:: WEXITSTATUS(status)
1829
1830 If ``WIFEXITED(status)`` is true, return the integer parameter to the
1831 :manpage:`exit(2)` system call. Otherwise, the return value is meaningless.
Georg Brandlc575c902008-09-13 17:46:05 +00001832 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001833
1834
1835.. function:: WSTOPSIG(status)
1836
Georg Brandlc575c902008-09-13 17:46:05 +00001837 Return the signal which caused the process to stop. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001838
1839
1840.. function:: WTERMSIG(status)
1841
Georg Brandlc575c902008-09-13 17:46:05 +00001842 Return the signal which caused the process to exit. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001843
1844
1845.. _os-path:
1846
1847Miscellaneous System Information
1848--------------------------------
1849
1850
1851.. function:: confstr(name)
1852
1853 Return string-valued system configuration values. *name* specifies the
1854 configuration value to retrieve; it may be a string which is the name of a
1855 defined system value; these names are specified in a number of standards (POSIX,
1856 Unix 95, Unix 98, and others). Some platforms define additional names as well.
1857 The names known to the host operating system are given as the keys of the
1858 ``confstr_names`` dictionary. For configuration variables not included in that
1859 mapping, passing an integer for *name* is also accepted. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001860 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001861
1862 If the configuration value specified by *name* isn't defined, ``None`` is
1863 returned.
1864
1865 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1866 specific value for *name* is not supported by the host system, even if it is
1867 included in ``confstr_names``, an :exc:`OSError` is raised with
1868 :const:`errno.EINVAL` for the error number.
1869
1870
1871.. data:: confstr_names
1872
1873 Dictionary mapping names accepted by :func:`confstr` to the integer values
1874 defined for those names by the host operating system. This can be used to
Georg Brandlc575c902008-09-13 17:46:05 +00001875 determine the set of names known to the system. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001876
1877
1878.. function:: getloadavg()
1879
Christian Heimesa62da1d2008-01-12 19:39:10 +00001880 Return the number of processes in the system run queue averaged over the last
1881 1, 5, and 15 minutes or raises :exc:`OSError` if the load average was
Georg Brandlf08a9dd2008-06-10 16:57:31 +00001882 unobtainable. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001883
Georg Brandl116aa622007-08-15 14:28:22 +00001884
1885.. function:: sysconf(name)
1886
1887 Return integer-valued system configuration values. If the configuration value
1888 specified by *name* isn't defined, ``-1`` is returned. The comments regarding
1889 the *name* parameter for :func:`confstr` apply here as well; the dictionary that
1890 provides information on the known names is given by ``sysconf_names``.
Georg Brandlc575c902008-09-13 17:46:05 +00001891 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001892
1893
1894.. data:: sysconf_names
1895
1896 Dictionary mapping names accepted by :func:`sysconf` to the integer values
1897 defined for those names by the host operating system. This can be used to
Georg Brandlc575c902008-09-13 17:46:05 +00001898 determine the set of names known to the system. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001899
Christian Heimesfaf2f632008-01-06 16:59:19 +00001900The following data values are used to support path manipulation operations. These
Georg Brandl116aa622007-08-15 14:28:22 +00001901are defined for all platforms.
1902
1903Higher-level operations on pathnames are defined in the :mod:`os.path` module.
1904
1905
1906.. data:: curdir
1907
1908 The constant string used by the operating system to refer to the current
Georg Brandlc575c902008-09-13 17:46:05 +00001909 directory. This is ``'.'`` for Windows and POSIX. Also available via
1910 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00001911
1912
1913.. data:: pardir
1914
1915 The constant string used by the operating system to refer to the parent
Georg Brandlc575c902008-09-13 17:46:05 +00001916 directory. This is ``'..'`` for Windows and POSIX. Also available via
1917 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00001918
1919
1920.. data:: sep
1921
Georg Brandlc575c902008-09-13 17:46:05 +00001922 The character used by the operating system to separate pathname components.
1923 This is ``'/'`` for POSIX and ``'\\'`` for Windows. Note that knowing this
1924 is not sufficient to be able to parse or concatenate pathnames --- use
Georg Brandl116aa622007-08-15 14:28:22 +00001925 :func:`os.path.split` and :func:`os.path.join` --- but it is occasionally
1926 useful. Also available via :mod:`os.path`.
1927
1928
1929.. data:: altsep
1930
1931 An alternative character used by the operating system to separate pathname
1932 components, or ``None`` if only one separator character exists. This is set to
1933 ``'/'`` on Windows systems where ``sep`` is a backslash. Also available via
1934 :mod:`os.path`.
1935
1936
1937.. data:: extsep
1938
1939 The character which separates the base filename from the extension; for example,
1940 the ``'.'`` in :file:`os.py`. Also available via :mod:`os.path`.
1941
Georg Brandl116aa622007-08-15 14:28:22 +00001942
1943.. data:: pathsep
1944
1945 The character conventionally used by the operating system to separate search
1946 path components (as in :envvar:`PATH`), such as ``':'`` for POSIX or ``';'`` for
1947 Windows. Also available via :mod:`os.path`.
1948
1949
1950.. data:: defpath
1951
1952 The default search path used by :func:`exec\*p\*` and :func:`spawn\*p\*` if the
1953 environment doesn't have a ``'PATH'`` key. Also available via :mod:`os.path`.
1954
1955
1956.. data:: linesep
1957
1958 The string used to separate (or, rather, terminate) lines on the current
Georg Brandlc575c902008-09-13 17:46:05 +00001959 platform. This may be a single character, such as ``'\n'`` for POSIX, or
1960 multiple characters, for example, ``'\r\n'`` for Windows. Do not use
1961 *os.linesep* as a line terminator when writing files opened in text mode (the
1962 default); use a single ``'\n'`` instead, on all platforms.
Georg Brandl116aa622007-08-15 14:28:22 +00001963
1964
1965.. data:: devnull
1966
Georg Brandlc575c902008-09-13 17:46:05 +00001967 The file path of the null device. For example: ``'/dev/null'`` for POSIX.
1968 Also available via :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00001969
Georg Brandl116aa622007-08-15 14:28:22 +00001970
1971.. _os-miscfunc:
1972
1973Miscellaneous Functions
1974-----------------------
1975
1976
1977.. function:: urandom(n)
1978
1979 Return a string of *n* random bytes suitable for cryptographic use.
1980
1981 This function returns random bytes from an OS-specific randomness source. The
1982 returned data should be unpredictable enough for cryptographic applications,
1983 though its exact quality depends on the OS implementation. On a UNIX-like
1984 system this will query /dev/urandom, and on Windows it will use CryptGenRandom.
1985 If a randomness source is not found, :exc:`NotImplementedError` will be raised.