blob: c41ee1b45256367d233ec5f9d717082bba4f6a34 [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
110 .. note::
111
112 Calling :func:`putenv` directly does not change ``os.environ``, so it's better
113 to modify ``os.environ``.
114
115 .. note::
116
Georg Brandlc575c902008-09-13 17:46:05 +0000117 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
118 cause memory leaks. Refer to the system documentation for
119 :cfunc:`putenv`.
Georg Brandl116aa622007-08-15 14:28:22 +0000120
121 If :func:`putenv` is not provided, a modified copy of this mapping may be
122 passed to the appropriate process-creation functions to cause child processes
123 to use a modified environment.
124
Georg Brandl9afde1c2007-11-01 20:32:30 +0000125 If the platform supports the :func:`unsetenv` function, you can delete items in
Georg Brandl116aa622007-08-15 14:28:22 +0000126 this mapping to unset environment variables. :func:`unsetenv` will be called
Georg Brandl9afde1c2007-11-01 20:32:30 +0000127 automatically when an item is deleted from ``os.environ``, and when
128 one of the :meth:`pop` or :meth:`clear` methods is called.
129
Georg Brandl116aa622007-08-15 14:28:22 +0000130
131.. function:: chdir(path)
132 fchdir(fd)
133 getcwd()
134 :noindex:
135
136 These functions are described in :ref:`os-file-dir`.
137
138
Gregory P. Smithb6e8c7e2010-02-27 07:22:22 +0000139.. function:: get_exec_path(env=None)
140
141 Returns the list of directories that will be searched for a named
142 executable, similar to a shell, when launching a process.
143 *env*, when specified, should be an environment variable dictionary
144 to lookup the PATH in.
145 By default, when *env* is None, :data:`environ` is used.
146
147 .. versionadded:: 3.2
148
149
Georg Brandl116aa622007-08-15 14:28:22 +0000150.. function:: ctermid()
151
152 Return the filename corresponding to the controlling terminal of the process.
153 Availability: Unix.
154
155
156.. function:: getegid()
157
158 Return the effective group id of the current process. This corresponds to the
Christian Heimesfaf2f632008-01-06 16:59:19 +0000159 "set id" bit on the file being executed in the current process. Availability:
Georg Brandl116aa622007-08-15 14:28:22 +0000160 Unix.
161
162
163.. function:: geteuid()
164
165 .. index:: single: user; effective id
166
Christian Heimesfaf2f632008-01-06 16:59:19 +0000167 Return the current process's effective user id. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000168
169
170.. function:: getgid()
171
172 .. index:: single: process; group
173
174 Return the real group id of the current process. Availability: Unix.
175
176
177.. function:: getgroups()
178
179 Return list of supplemental group ids associated with the current process.
180 Availability: Unix.
181
182
Antoine Pitroub7572f02009-12-02 20:46:48 +0000183.. function:: initgroups(username, gid)
184
185 Call the system initgroups() to initialize the group access list with all of
186 the groups of which the specified username is a member, plus the specified
187 group id. Availability: Unix.
188
189 .. versionadded:: 3.2
190
191
Georg Brandl116aa622007-08-15 14:28:22 +0000192.. function:: getlogin()
193
194 Return the name of the user logged in on the controlling terminal of the
195 process. For most purposes, it is more useful to use the environment variable
196 :envvar:`LOGNAME` to find out who the user is, or
197 ``pwd.getpwuid(os.getuid())[0]`` to get the login name of the currently
Christian Heimesfaf2f632008-01-06 16:59:19 +0000198 effective user id. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000199
200
201.. function:: getpgid(pid)
202
203 Return the process group id of the process with process id *pid*. If *pid* is 0,
204 the process group id of the current process is returned. Availability: Unix.
205
Georg Brandl116aa622007-08-15 14:28:22 +0000206
207.. function:: getpgrp()
208
209 .. index:: single: process; group
210
211 Return the id of the current process group. Availability: Unix.
212
213
214.. function:: getpid()
215
216 .. index:: single: process; id
217
218 Return the current process id. Availability: Unix, Windows.
219
220
221.. function:: getppid()
222
223 .. index:: single: process; id of parent
224
225 Return the parent's process id. Availability: Unix.
226
Georg Brandl1b83a452009-11-28 11:12:26 +0000227
Gregory P. Smithcf02c6a2009-11-27 17:54:17 +0000228.. function:: getresuid()
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000229
230 Return a tuple (ruid, euid, suid) denoting the current process's
231 real, effective, and saved user ids. Availability: Unix.
232
Georg Brandl1b83a452009-11-28 11:12:26 +0000233 .. versionadded:: 3.2
234
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000235
Gregory P. Smithcf02c6a2009-11-27 17:54:17 +0000236.. function:: getresgid()
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000237
238 Return a tuple (rgid, egid, sgid) denoting the current process's
239 real, effective, and saved user ids. Availability: Unix.
240
Georg Brandl1b83a452009-11-28 11:12:26 +0000241 .. versionadded:: 3.2
242
Georg Brandl116aa622007-08-15 14:28:22 +0000243
244.. function:: getuid()
245
246 .. index:: single: user; id
247
Christian Heimesfaf2f632008-01-06 16:59:19 +0000248 Return the current process's user id. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000249
250
Georg Brandl18244152009-09-02 20:34:52 +0000251.. function:: getenv(key, default=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000252
Georg Brandl18244152009-09-02 20:34:52 +0000253 Return the value of the environment variable *key* if it exists, or
254 *default* if it doesn't. Availability: most flavors of Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000255
256
Georg Brandl18244152009-09-02 20:34:52 +0000257.. function:: putenv(key, value)
Georg Brandl116aa622007-08-15 14:28:22 +0000258
259 .. index:: single: environment variables; setting
260
Georg Brandl18244152009-09-02 20:34:52 +0000261 Set the environment variable named *key* to the string *value*. Such
Georg Brandl116aa622007-08-15 14:28:22 +0000262 changes to the environment affect subprocesses started with :func:`os.system`,
263 :func:`popen` or :func:`fork` and :func:`execv`. Availability: most flavors of
264 Unix, Windows.
265
266 .. note::
267
Georg Brandlc575c902008-09-13 17:46:05 +0000268 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
269 cause memory leaks. Refer to the system documentation for putenv.
Georg Brandl116aa622007-08-15 14:28:22 +0000270
271 When :func:`putenv` is supported, assignments to items in ``os.environ`` are
272 automatically translated into corresponding calls to :func:`putenv`; however,
273 calls to :func:`putenv` don't update ``os.environ``, so it is actually
274 preferable to assign to items of ``os.environ``.
275
276
277.. function:: setegid(egid)
278
279 Set the current process's effective group id. Availability: Unix.
280
281
282.. function:: seteuid(euid)
283
284 Set the current process's effective user id. Availability: Unix.
285
286
287.. function:: setgid(gid)
288
289 Set the current process' group id. Availability: Unix.
290
291
292.. function:: setgroups(groups)
293
294 Set the list of supplemental group ids associated with the current process to
295 *groups*. *groups* must be a sequence, and each element must be an integer
Christian Heimesfaf2f632008-01-06 16:59:19 +0000296 identifying a group. This operation is typically available only to the superuser.
Georg Brandl116aa622007-08-15 14:28:22 +0000297 Availability: Unix.
298
Georg Brandl116aa622007-08-15 14:28:22 +0000299
300.. function:: setpgrp()
301
Christian Heimesfaf2f632008-01-06 16:59:19 +0000302 Call the system call :cfunc:`setpgrp` or :cfunc:`setpgrp(0, 0)` depending on
Georg Brandl116aa622007-08-15 14:28:22 +0000303 which version is implemented (if any). See the Unix manual for the semantics.
304 Availability: Unix.
305
306
307.. function:: setpgid(pid, pgrp)
308
Christian Heimesfaf2f632008-01-06 16:59:19 +0000309 Call the system call :cfunc:`setpgid` to set the process group id of the
Georg Brandl116aa622007-08-15 14:28:22 +0000310 process with id *pid* to the process group with id *pgrp*. See the Unix manual
311 for the semantics. Availability: Unix.
312
313
Georg Brandl116aa622007-08-15 14:28:22 +0000314.. function:: setregid(rgid, egid)
315
316 Set the current process's real and effective group ids. Availability: Unix.
317
Georg Brandl1b83a452009-11-28 11:12:26 +0000318
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000319.. function:: setresgid(rgid, egid, sgid)
320
321 Set the current process's real, effective, and saved group ids.
322 Availability: Unix.
323
Georg Brandl1b83a452009-11-28 11:12:26 +0000324 .. versionadded:: 3.2
325
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000326
327.. function:: setresuid(ruid, euid, suid)
328
329 Set the current process's real, effective, and saved user ids.
330 Availibility: Unix.
331
Georg Brandl1b83a452009-11-28 11:12:26 +0000332 .. versionadded:: 3.2
333
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000334
335.. function:: setreuid(ruid, euid)
336
337 Set the current process's real and effective user ids. Availability: Unix.
338
Georg Brandl116aa622007-08-15 14:28:22 +0000339
340.. function:: getsid(pid)
341
Christian Heimesfaf2f632008-01-06 16:59:19 +0000342 Call the system call :cfunc:`getsid`. See the Unix manual for the semantics.
Georg Brandl116aa622007-08-15 14:28:22 +0000343 Availability: Unix.
344
Georg Brandl116aa622007-08-15 14:28:22 +0000345
346.. function:: setsid()
347
Christian Heimesfaf2f632008-01-06 16:59:19 +0000348 Call the system call :cfunc:`setsid`. See the Unix manual for the semantics.
Georg Brandl116aa622007-08-15 14:28:22 +0000349 Availability: Unix.
350
351
352.. function:: setuid(uid)
353
354 .. index:: single: user; id, setting
355
Christian Heimesfaf2f632008-01-06 16:59:19 +0000356 Set the current process's user id. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000357
Georg Brandl116aa622007-08-15 14:28:22 +0000358
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000359.. placed in this section since it relates to errno.... a little weak
Georg Brandl116aa622007-08-15 14:28:22 +0000360.. function:: strerror(code)
361
362 Return the error message corresponding to the error code in *code*.
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000363 On platforms where :cfunc:`strerror` returns ``NULL`` when given an unknown
364 error number, :exc:`ValueError` is raised. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000365
366
367.. function:: umask(mask)
368
Christian Heimesfaf2f632008-01-06 16:59:19 +0000369 Set the current numeric umask and return the previous umask. Availability:
Georg Brandl116aa622007-08-15 14:28:22 +0000370 Unix, Windows.
371
372
373.. function:: uname()
374
375 .. index::
376 single: gethostname() (in module socket)
377 single: gethostbyaddr() (in module socket)
378
379 Return a 5-tuple containing information identifying the current operating
380 system. The tuple contains 5 strings: ``(sysname, nodename, release, version,
381 machine)``. Some systems truncate the nodename to 8 characters or to the
382 leading component; a better way to get the hostname is
383 :func:`socket.gethostname` or even
384 ``socket.gethostbyaddr(socket.gethostname())``. Availability: recent flavors of
385 Unix.
386
387
Georg Brandl18244152009-09-02 20:34:52 +0000388.. function:: unsetenv(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000389
390 .. index:: single: environment variables; deleting
391
Georg Brandl18244152009-09-02 20:34:52 +0000392 Unset (delete) the environment variable named *key*. Such changes to the
Georg Brandl116aa622007-08-15 14:28:22 +0000393 environment affect subprocesses started with :func:`os.system`, :func:`popen` or
394 :func:`fork` and :func:`execv`. Availability: most flavors of Unix, Windows.
395
396 When :func:`unsetenv` is supported, deletion of items in ``os.environ`` is
397 automatically translated into a corresponding call to :func:`unsetenv`; however,
398 calls to :func:`unsetenv` don't update ``os.environ``, so it is actually
399 preferable to delete items of ``os.environ``.
400
401
402.. _os-newstreams:
403
404File Object Creation
405--------------------
406
407These functions create new file objects. (See also :func:`open`.)
408
409
410.. function:: fdopen(fd[, mode[, bufsize]])
411
412 .. index:: single: I/O control; buffering
413
414 Return an open file object connected to the file descriptor *fd*. The *mode*
415 and *bufsize* arguments have the same meaning as the corresponding arguments to
Georg Brandlc575c902008-09-13 17:46:05 +0000416 the built-in :func:`open` function. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000417
Georg Brandl55ac8f02007-09-01 13:51:09 +0000418 When specified, the *mode* argument must start with one of the letters
419 ``'r'``, ``'w'``, or ``'a'``, otherwise a :exc:`ValueError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000420
Georg Brandl55ac8f02007-09-01 13:51:09 +0000421 On Unix, when the *mode* argument starts with ``'a'``, the *O_APPEND* flag is
422 set on the file descriptor (which the :cfunc:`fdopen` implementation already
423 does on most platforms).
Georg Brandl116aa622007-08-15 14:28:22 +0000424
425
Georg Brandl116aa622007-08-15 14:28:22 +0000426.. _os-fd-ops:
427
428File Descriptor Operations
429--------------------------
430
431These functions operate on I/O streams referenced using file descriptors.
432
433File descriptors are small integers corresponding to a file that has been opened
434by the current process. For example, standard input is usually file descriptor
4350, standard output is 1, and standard error is 2. Further files opened by a
436process will then be assigned 3, 4, 5, and so forth. The name "file descriptor"
437is slightly deceptive; on Unix platforms, sockets and pipes are also referenced
438by file descriptors.
439
Benjamin Peterson08bf91c2010-04-11 16:12:57 +0000440The :meth:`~file.fileno` method can be used to obtain the file descriptor
441associated with a file object when required. Note that using the file
442descriptor directly will bypass the file object methods, ignoring aspects such
443as internal buffering of data.
Georg Brandl116aa622007-08-15 14:28:22 +0000444
445.. function:: close(fd)
446
Georg Brandlc575c902008-09-13 17:46:05 +0000447 Close file descriptor *fd*. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000448
449 .. note::
450
451 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000452 descriptor as returned by :func:`os.open` or :func:`pipe`. To close a "file
Georg Brandl116aa622007-08-15 14:28:22 +0000453 object" returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000454 :func:`fdopen`, use its :meth:`~file.close` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000455
456
Christian Heimesfdab48e2008-01-20 09:06:41 +0000457.. function:: closerange(fd_low, fd_high)
458
459 Close all file descriptors from *fd_low* (inclusive) to *fd_high* (exclusive),
Georg Brandlc575c902008-09-13 17:46:05 +0000460 ignoring errors. Availability: Unix, Windows. Equivalent to::
Christian Heimesfdab48e2008-01-20 09:06:41 +0000461
Georg Brandlc9a5a0e2009-09-01 07:34:27 +0000462 for fd in range(fd_low, fd_high):
Christian Heimesfdab48e2008-01-20 09:06:41 +0000463 try:
464 os.close(fd)
465 except OSError:
466 pass
467
Christian Heimesfdab48e2008-01-20 09:06:41 +0000468
Georg Brandl81f11302007-12-21 08:45:42 +0000469.. function:: device_encoding(fd)
470
471 Return a string describing the encoding of the device associated with *fd*
472 if it is connected to a terminal; else return :const:`None`.
473
474
Georg Brandl116aa622007-08-15 14:28:22 +0000475.. function:: dup(fd)
476
Georg Brandlc575c902008-09-13 17:46:05 +0000477 Return a duplicate of file descriptor *fd*. Availability: Unix,
Georg Brandl116aa622007-08-15 14:28:22 +0000478 Windows.
479
480
481.. function:: dup2(fd, fd2)
482
483 Duplicate file descriptor *fd* to *fd2*, closing the latter first if necessary.
Georg Brandlc575c902008-09-13 17:46:05 +0000484 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000485
486
Christian Heimes4e30a842007-11-30 22:12:06 +0000487.. function:: fchmod(fd, mode)
488
489 Change the mode of the file given by *fd* to the numeric *mode*. See the docs
490 for :func:`chmod` for possible values of *mode*. Availability: Unix.
491
492
493.. function:: fchown(fd, uid, gid)
494
495 Change the owner and group id of the file given by *fd* to the numeric *uid*
496 and *gid*. To leave one of the ids unchanged, set it to -1.
497 Availability: Unix.
498
499
Georg Brandl116aa622007-08-15 14:28:22 +0000500.. function:: fdatasync(fd)
501
502 Force write of file with filedescriptor *fd* to disk. Does not force update of
503 metadata. Availability: Unix.
504
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000505 .. note::
506 This function is not available on MacOS.
507
Georg Brandl116aa622007-08-15 14:28:22 +0000508
509.. function:: fpathconf(fd, name)
510
511 Return system configuration information relevant to an open file. *name*
512 specifies the configuration value to retrieve; it may be a string which is the
513 name of a defined system value; these names are specified in a number of
514 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
515 additional names as well. The names known to the host operating system are
516 given in the ``pathconf_names`` dictionary. For configuration variables not
517 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandlc575c902008-09-13 17:46:05 +0000518 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000519
520 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
521 specific value for *name* is not supported by the host system, even if it is
522 included in ``pathconf_names``, an :exc:`OSError` is raised with
523 :const:`errno.EINVAL` for the error number.
524
525
526.. function:: fstat(fd)
527
528 Return status for file descriptor *fd*, like :func:`stat`. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +0000529 Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000530
531
532.. function:: fstatvfs(fd)
533
534 Return information about the filesystem containing the file associated with file
535 descriptor *fd*, like :func:`statvfs`. Availability: Unix.
536
537
538.. function:: fsync(fd)
539
540 Force write of file with filedescriptor *fd* to disk. On Unix, this calls the
541 native :cfunc:`fsync` function; on Windows, the MS :cfunc:`_commit` function.
542
543 If you're starting with a Python file object *f*, first do ``f.flush()``, and
544 then do ``os.fsync(f.fileno())``, to ensure that all internal buffers associated
Georg Brandlc575c902008-09-13 17:46:05 +0000545 with *f* are written to disk. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000546
547
548.. function:: ftruncate(fd, length)
549
550 Truncate the file corresponding to file descriptor *fd*, so that it is at most
Georg Brandlc575c902008-09-13 17:46:05 +0000551 *length* bytes in size. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000552
553
554.. function:: isatty(fd)
555
556 Return ``True`` if the file descriptor *fd* is open and connected to a
Georg Brandlc575c902008-09-13 17:46:05 +0000557 tty(-like) device, else ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000558
559
560.. function:: lseek(fd, pos, how)
561
Christian Heimesfaf2f632008-01-06 16:59:19 +0000562 Set the current position of file descriptor *fd* to position *pos*, modified
563 by *how*: :const:`SEEK_SET` or ``0`` to set the position relative to the
564 beginning of the file; :const:`SEEK_CUR` or ``1`` to set it relative to the
565 current position; :const:`os.SEEK_END` or ``2`` to set it relative to the end of
Georg Brandlc575c902008-09-13 17:46:05 +0000566 the file. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000567
568
569.. function:: open(file, flags[, mode])
570
Georg Brandlf4a41232008-05-26 17:55:52 +0000571 Open the file *file* and set various flags according to *flags* and possibly
572 its mode according to *mode*. The default *mode* is ``0o777`` (octal), and
573 the current umask value is first masked out. Return the file descriptor for
Georg Brandlc575c902008-09-13 17:46:05 +0000574 the newly opened file. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000575
576 For a description of the flag and mode values, see the C run-time documentation;
577 flag constants (like :const:`O_RDONLY` and :const:`O_WRONLY`) are defined in
578 this module too (see below).
579
580 .. note::
581
Georg Brandl502d9a52009-07-26 15:02:41 +0000582 This function is intended for low-level I/O. For normal usage, use the
583 built-in function :func:`open`, which returns a "file object" with
584 :meth:`~file.read` and :meth:`~file.write` methods (and many more). To
585 wrap a file descriptor in a "file object", use :func:`fdopen`.
Georg Brandl116aa622007-08-15 14:28:22 +0000586
587
588.. function:: openpty()
589
590 .. index:: module: pty
591
592 Open a new pseudo-terminal pair. Return a pair of file descriptors ``(master,
593 slave)`` for the pty and the tty, respectively. For a (slightly) more portable
Georg Brandlc575c902008-09-13 17:46:05 +0000594 approach, use the :mod:`pty` module. Availability: some flavors of
Georg Brandl116aa622007-08-15 14:28:22 +0000595 Unix.
596
597
598.. function:: pipe()
599
600 Create a pipe. Return a pair of file descriptors ``(r, w)`` usable for reading
Georg Brandlc575c902008-09-13 17:46:05 +0000601 and writing, respectively. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000602
603
604.. function:: read(fd, n)
605
Georg Brandlb90be692009-07-29 16:14:16 +0000606 Read at most *n* bytes from file descriptor *fd*. Return a bytestring containing the
Georg Brandl116aa622007-08-15 14:28:22 +0000607 bytes read. If the end of the file referred to by *fd* has been reached, an
Georg Brandlb90be692009-07-29 16:14:16 +0000608 empty bytes object is returned. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000609
610 .. note::
611
612 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000613 descriptor as returned by :func:`os.open` or :func:`pipe`. To read a "file object"
Georg Brandl116aa622007-08-15 14:28:22 +0000614 returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000615 :func:`fdopen`, or :data:`sys.stdin`, use its :meth:`~file.read` or
616 :meth:`~file.readline` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000617
618
619.. function:: tcgetpgrp(fd)
620
621 Return the process group associated with the terminal given by *fd* (an open
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000622 file descriptor as returned by :func:`os.open`). Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000623
624
625.. function:: tcsetpgrp(fd, pg)
626
627 Set the process group associated with the terminal given by *fd* (an open file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000628 descriptor as returned by :func:`os.open`) to *pg*. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000629
630
631.. function:: ttyname(fd)
632
633 Return a string which specifies the terminal device associated with
Georg Brandl9afde1c2007-11-01 20:32:30 +0000634 file descriptor *fd*. If *fd* is not associated with a terminal device, an
Georg Brandlc575c902008-09-13 17:46:05 +0000635 exception is raised. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000636
637
638.. function:: write(fd, str)
639
Georg Brandlb90be692009-07-29 16:14:16 +0000640 Write the bytestring in *str* to file descriptor *fd*. Return the number of
641 bytes actually written. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000642
643 .. note::
644
645 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000646 descriptor as returned by :func:`os.open` or :func:`pipe`. To write a "file
Georg Brandl116aa622007-08-15 14:28:22 +0000647 object" returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000648 :func:`fdopen`, or :data:`sys.stdout` or :data:`sys.stderr`, use its
649 :meth:`~file.write` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000650
Georg Brandlaf265f42008-12-07 15:06:20 +0000651The following constants are options for the *flags* parameter to the
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000652:func:`~os.open` function. They can be combined using the bitwise OR operator
Georg Brandlaf265f42008-12-07 15:06:20 +0000653``|``. Some of them are not available on all platforms. For descriptions of
654their availability and use, consult the :manpage:`open(2)` manual page on Unix
Doug Hellmanneb097fc2009-09-20 20:56:56 +0000655or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000656
657
658.. data:: O_RDONLY
659 O_WRONLY
660 O_RDWR
661 O_APPEND
662 O_CREAT
663 O_EXCL
664 O_TRUNC
665
Georg Brandlaf265f42008-12-07 15:06:20 +0000666 These constants are available on Unix and Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000667
668
669.. data:: O_DSYNC
670 O_RSYNC
671 O_SYNC
672 O_NDELAY
673 O_NONBLOCK
674 O_NOCTTY
675 O_SHLOCK
676 O_EXLOCK
677
Georg Brandlaf265f42008-12-07 15:06:20 +0000678 These constants are only available on Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000679
680
681.. data:: O_BINARY
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000682 O_NOINHERIT
Georg Brandl116aa622007-08-15 14:28:22 +0000683 O_SHORT_LIVED
684 O_TEMPORARY
685 O_RANDOM
686 O_SEQUENTIAL
687 O_TEXT
688
Georg Brandlaf265f42008-12-07 15:06:20 +0000689 These constants are only available on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000690
691
Alexandre Vassalottibee32532008-05-16 18:15:12 +0000692.. data:: O_ASYNC
693 O_DIRECT
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000694 O_DIRECTORY
695 O_NOFOLLOW
696 O_NOATIME
697
Georg Brandlaf265f42008-12-07 15:06:20 +0000698 These constants are GNU extensions and not present if they are not defined by
699 the C library.
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000700
701
Georg Brandl116aa622007-08-15 14:28:22 +0000702.. data:: SEEK_SET
703 SEEK_CUR
704 SEEK_END
705
706 Parameters to the :func:`lseek` function. Their values are 0, 1, and 2,
Georg Brandlc575c902008-09-13 17:46:05 +0000707 respectively. Availability: Windows, Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000708
Georg Brandl116aa622007-08-15 14:28:22 +0000709
710.. _os-file-dir:
711
712Files and Directories
713---------------------
714
Georg Brandl116aa622007-08-15 14:28:22 +0000715.. function:: access(path, mode)
716
717 Use the real uid/gid to test for access to *path*. Note that most operations
718 will use the effective uid/gid, therefore this routine can be used in a
719 suid/sgid environment to test if the invoking user has the specified access to
720 *path*. *mode* should be :const:`F_OK` to test the existence of *path*, or it
721 can be the inclusive OR of one or more of :const:`R_OK`, :const:`W_OK`, and
722 :const:`X_OK` to test permissions. Return :const:`True` if access is allowed,
723 :const:`False` if not. See the Unix man page :manpage:`access(2)` for more
Georg Brandlc575c902008-09-13 17:46:05 +0000724 information. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000725
726 .. note::
727
Georg Brandl502d9a52009-07-26 15:02:41 +0000728 Using :func:`access` to check if a user is authorized to e.g. open a file
729 before actually doing so using :func:`open` creates a security hole,
730 because the user might exploit the short time interval between checking
731 and opening the file to manipulate it.
Georg Brandl116aa622007-08-15 14:28:22 +0000732
733 .. note::
734
735 I/O operations may fail even when :func:`access` indicates that they would
736 succeed, particularly for operations on network filesystems which may have
737 permissions semantics beyond the usual POSIX permission-bit model.
738
739
740.. data:: F_OK
741
742 Value to pass as the *mode* parameter of :func:`access` to test the existence of
743 *path*.
744
745
746.. data:: R_OK
747
748 Value to include in the *mode* parameter of :func:`access` to test the
749 readability of *path*.
750
751
752.. data:: W_OK
753
754 Value to include in the *mode* parameter of :func:`access` to test the
755 writability of *path*.
756
757
758.. data:: X_OK
759
760 Value to include in the *mode* parameter of :func:`access` to determine if
761 *path* can be executed.
762
763
764.. function:: chdir(path)
765
766 .. index:: single: directory; changing
767
Georg Brandlc575c902008-09-13 17:46:05 +0000768 Change the current working directory to *path*. Availability: Unix,
Georg Brandl116aa622007-08-15 14:28:22 +0000769 Windows.
770
771
772.. function:: fchdir(fd)
773
774 Change the current working directory to the directory represented by the file
775 descriptor *fd*. The descriptor must refer to an opened directory, not an open
776 file. Availability: Unix.
777
Georg Brandl116aa622007-08-15 14:28:22 +0000778
779.. function:: getcwd()
780
Martin v. Löwis011e8422009-05-05 04:43:17 +0000781 Return a string representing the current working directory.
782 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000783
Martin v. Löwisa731b992008-10-07 06:36:31 +0000784.. function:: getcwdb()
Georg Brandl116aa622007-08-15 14:28:22 +0000785
Georg Brandl76e55382008-10-08 16:34:57 +0000786 Return a bytestring representing the current working directory.
Georg Brandlc575c902008-09-13 17:46:05 +0000787 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000788
Georg Brandl116aa622007-08-15 14:28:22 +0000789
790.. function:: chflags(path, flags)
791
792 Set the flags of *path* to the numeric *flags*. *flags* may take a combination
793 (bitwise OR) of the following values (as defined in the :mod:`stat` module):
794
795 * ``UF_NODUMP``
796 * ``UF_IMMUTABLE``
797 * ``UF_APPEND``
798 * ``UF_OPAQUE``
799 * ``UF_NOUNLINK``
800 * ``SF_ARCHIVED``
801 * ``SF_IMMUTABLE``
802 * ``SF_APPEND``
803 * ``SF_NOUNLINK``
804 * ``SF_SNAPSHOT``
805
Georg Brandlc575c902008-09-13 17:46:05 +0000806 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000807
Georg Brandl116aa622007-08-15 14:28:22 +0000808
809.. function:: chroot(path)
810
811 Change the root directory of the current process to *path*. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +0000812 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000813
Georg Brandl116aa622007-08-15 14:28:22 +0000814
815.. function:: chmod(path, mode)
816
817 Change the mode of *path* to the numeric *mode*. *mode* may take one of the
Christian Heimesfaf2f632008-01-06 16:59:19 +0000818 following values (as defined in the :mod:`stat` module) or bitwise ORed
Georg Brandl116aa622007-08-15 14:28:22 +0000819 combinations of them:
820
Alexandre Vassalottic22c6f22009-07-21 00:51:58 +0000821 * :data:`stat.S_ISUID`
822 * :data:`stat.S_ISGID`
823 * :data:`stat.S_ENFMT`
824 * :data:`stat.S_ISVTX`
825 * :data:`stat.S_IREAD`
826 * :data:`stat.S_IWRITE`
827 * :data:`stat.S_IEXEC`
828 * :data:`stat.S_IRWXU`
829 * :data:`stat.S_IRUSR`
830 * :data:`stat.S_IWUSR`
831 * :data:`stat.S_IXUSR`
832 * :data:`stat.S_IRWXG`
833 * :data:`stat.S_IRGRP`
834 * :data:`stat.S_IWGRP`
835 * :data:`stat.S_IXGRP`
836 * :data:`stat.S_IRWXO`
837 * :data:`stat.S_IROTH`
838 * :data:`stat.S_IWOTH`
839 * :data:`stat.S_IXOTH`
Georg Brandl116aa622007-08-15 14:28:22 +0000840
Georg Brandlc575c902008-09-13 17:46:05 +0000841 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000842
843 .. note::
844
845 Although Windows supports :func:`chmod`, you can only set the file's read-only
846 flag with it (via the ``stat.S_IWRITE`` and ``stat.S_IREAD``
847 constants or a corresponding integer value). All other bits are
848 ignored.
849
850
851.. function:: chown(path, uid, gid)
852
853 Change the owner and group id of *path* to the numeric *uid* and *gid*. To leave
Georg Brandlc575c902008-09-13 17:46:05 +0000854 one of the ids unchanged, set it to -1. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000855
856
857.. function:: lchflags(path, flags)
858
859 Set the flags of *path* to the numeric *flags*, like :func:`chflags`, but do not
860 follow symbolic links. Availability: Unix.
861
Georg Brandl116aa622007-08-15 14:28:22 +0000862
Christian Heimes93852662007-12-01 12:22:32 +0000863.. function:: lchmod(path, mode)
864
865 Change the mode of *path* to the numeric *mode*. If path is a symlink, this
866 affects the symlink rather than the target. See the docs for :func:`chmod`
867 for possible values of *mode*. Availability: Unix.
868
Christian Heimes93852662007-12-01 12:22:32 +0000869
Georg Brandl116aa622007-08-15 14:28:22 +0000870.. function:: lchown(path, uid, gid)
871
Christian Heimesfaf2f632008-01-06 16:59:19 +0000872 Change the owner and group id of *path* to the numeric *uid* and *gid*. This
Georg Brandlc575c902008-09-13 17:46:05 +0000873 function will not follow symbolic links. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000874
Georg Brandl116aa622007-08-15 14:28:22 +0000875
Benjamin Peterson5879d412009-03-30 14:51:56 +0000876.. function:: link(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +0000877
Benjamin Peterson5879d412009-03-30 14:51:56 +0000878 Create a hard link pointing to *source* named *link_name*. Availability:
879 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000880
881
882.. function:: listdir(path)
883
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000884 Return a list containing the names of the entries in the directory given by
885 *path*. The list is in arbitrary order. It does not include the special
886 entries ``'.'`` and ``'..'`` even if they are present in the directory.
887 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000888
Martin v. Löwis011e8422009-05-05 04:43:17 +0000889 This function can be called with a bytes or string argument, and returns
890 filenames of the same datatype.
Georg Brandl116aa622007-08-15 14:28:22 +0000891
892
893.. function:: lstat(path)
894
Guido van Rossum2cc30da2007-11-02 23:46:40 +0000895 Like :func:`stat`, but do not follow symbolic links. This is an alias for
896 :func:`stat` on platforms that do not support symbolic links, such as
897 Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000898
899
900.. function:: mkfifo(path[, mode])
901
Georg Brandlf4a41232008-05-26 17:55:52 +0000902 Create a FIFO (a named pipe) named *path* with numeric mode *mode*. The
903 default *mode* is ``0o666`` (octal). The current umask value is first masked
Georg Brandlc575c902008-09-13 17:46:05 +0000904 out from the mode. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000905
906 FIFOs are pipes that can be accessed like regular files. FIFOs exist until they
907 are deleted (for example with :func:`os.unlink`). Generally, FIFOs are used as
908 rendezvous between "client" and "server" type processes: the server opens the
909 FIFO for reading, and the client opens it for writing. Note that :func:`mkfifo`
910 doesn't open the FIFO --- it just creates the rendezvous point.
911
912
Georg Brandl18244152009-09-02 20:34:52 +0000913.. function:: mknod(filename[, mode=0o600[, device]])
Georg Brandl116aa622007-08-15 14:28:22 +0000914
915 Create a filesystem node (file, device special file or named pipe) named
Georg Brandl18244152009-09-02 20:34:52 +0000916 *filename*. *mode* specifies both the permissions to use and the type of node
917 to be created, being combined (bitwise OR) with one of ``stat.S_IFREG``,
918 ``stat.S_IFCHR``, ``stat.S_IFBLK``, and ``stat.S_IFIFO`` (those constants are
919 available in :mod:`stat`). For ``stat.S_IFCHR`` and ``stat.S_IFBLK``,
920 *device* defines the newly created device special file (probably using
Georg Brandl116aa622007-08-15 14:28:22 +0000921 :func:`os.makedev`), otherwise it is ignored.
922
Georg Brandl116aa622007-08-15 14:28:22 +0000923
924.. function:: major(device)
925
Christian Heimesfaf2f632008-01-06 16:59:19 +0000926 Extract the device major number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000927 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
928
Georg Brandl116aa622007-08-15 14:28:22 +0000929
930.. function:: minor(device)
931
Christian Heimesfaf2f632008-01-06 16:59:19 +0000932 Extract the device minor number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000933 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
934
Georg Brandl116aa622007-08-15 14:28:22 +0000935
936.. function:: makedev(major, minor)
937
Christian Heimesfaf2f632008-01-06 16:59:19 +0000938 Compose a raw device number from the major and minor device numbers.
Georg Brandl116aa622007-08-15 14:28:22 +0000939
Georg Brandl116aa622007-08-15 14:28:22 +0000940
941.. function:: mkdir(path[, mode])
942
Georg Brandlf4a41232008-05-26 17:55:52 +0000943 Create a directory named *path* with numeric mode *mode*. The default *mode*
944 is ``0o777`` (octal). On some systems, *mode* is ignored. Where it is used,
Georg Brandlc575c902008-09-13 17:46:05 +0000945 the current umask value is first masked out. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000946
Guido van Rossum2cc30da2007-11-02 23:46:40 +0000947 It is also possible to create temporary directories; see the
948 :mod:`tempfile` module's :func:`tempfile.mkdtemp` function.
949
Georg Brandl116aa622007-08-15 14:28:22 +0000950
951.. function:: makedirs(path[, mode])
952
953 .. index::
954 single: directory; creating
955 single: UNC paths; and os.makedirs()
956
957 Recursive directory creation function. Like :func:`mkdir`, but makes all
Georg Brandlf4a41232008-05-26 17:55:52 +0000958 intermediate-level directories needed to contain the leaf directory. Throws
959 an :exc:`error` exception if the leaf directory already exists or cannot be
960 created. The default *mode* is ``0o777`` (octal). On some systems, *mode*
961 is ignored. Where it is used, the current umask value is first masked out.
Georg Brandl116aa622007-08-15 14:28:22 +0000962
963 .. note::
964
965 :func:`makedirs` will become confused if the path elements to create include
Christian Heimesfaf2f632008-01-06 16:59:19 +0000966 :data:`os.pardir`.
Georg Brandl116aa622007-08-15 14:28:22 +0000967
Georg Brandl55ac8f02007-09-01 13:51:09 +0000968 This function handles UNC paths correctly.
Georg Brandl116aa622007-08-15 14:28:22 +0000969
970
971.. function:: pathconf(path, name)
972
973 Return system configuration information relevant to a named file. *name*
974 specifies the configuration value to retrieve; it may be a string which is the
975 name of a defined system value; these names are specified in a number of
976 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
977 additional names as well. The names known to the host operating system are
978 given in the ``pathconf_names`` dictionary. For configuration variables not
979 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandlc575c902008-09-13 17:46:05 +0000980 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000981
982 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
983 specific value for *name* is not supported by the host system, even if it is
984 included in ``pathconf_names``, an :exc:`OSError` is raised with
985 :const:`errno.EINVAL` for the error number.
986
987
988.. data:: pathconf_names
989
990 Dictionary mapping names accepted by :func:`pathconf` and :func:`fpathconf` to
991 the integer values defined for those names by the host operating system. This
992 can be used to determine the set of names known to the system. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +0000993 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000994
995
996.. function:: readlink(path)
997
998 Return a string representing the path to which the symbolic link points. The
999 result may be either an absolute or relative pathname; if it is relative, it may
1000 be converted to an absolute pathname using ``os.path.join(os.path.dirname(path),
1001 result)``.
1002
Georg Brandl76e55382008-10-08 16:34:57 +00001003 If the *path* is a string object, the result will also be a string object,
1004 and the call may raise an UnicodeDecodeError. If the *path* is a bytes
1005 object, the result will be a bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +00001006
Georg Brandlc575c902008-09-13 17:46:05 +00001007 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001008
1009
1010.. function:: remove(path)
1011
Georg Brandla6053b42009-09-01 08:11:14 +00001012 Remove (delete) the file *path*. If *path* is a directory, :exc:`OSError` is
1013 raised; see :func:`rmdir` below to remove a directory. This is identical to
1014 the :func:`unlink` function documented below. On Windows, attempting to
1015 remove a file that is in use causes an exception to be raised; on Unix, the
1016 directory entry is removed but the storage allocated to the file is not made
1017 available until the original file is no longer in use. Availability: Unix,
Georg Brandl116aa622007-08-15 14:28:22 +00001018 Windows.
1019
1020
1021.. function:: removedirs(path)
1022
1023 .. index:: single: directory; deleting
1024
Christian Heimesfaf2f632008-01-06 16:59:19 +00001025 Remove directories recursively. Works like :func:`rmdir` except that, if the
Georg Brandl116aa622007-08-15 14:28:22 +00001026 leaf directory is successfully removed, :func:`removedirs` tries to
1027 successively remove every parent directory mentioned in *path* until an error
1028 is raised (which is ignored, because it generally means that a parent directory
1029 is not empty). For example, ``os.removedirs('foo/bar/baz')`` will first remove
1030 the directory ``'foo/bar/baz'``, and then remove ``'foo/bar'`` and ``'foo'`` if
1031 they are empty. Raises :exc:`OSError` if the leaf directory could not be
1032 successfully removed.
1033
Georg Brandl116aa622007-08-15 14:28:22 +00001034
1035.. function:: rename(src, dst)
1036
1037 Rename the file or directory *src* to *dst*. If *dst* is a directory,
1038 :exc:`OSError` will be raised. On Unix, if *dst* exists and is a file, it will
Christian Heimesfaf2f632008-01-06 16:59:19 +00001039 be replaced silently if the user has permission. The operation may fail on some
Georg Brandl116aa622007-08-15 14:28:22 +00001040 Unix flavors if *src* and *dst* are on different filesystems. If successful,
1041 the renaming will be an atomic operation (this is a POSIX requirement). On
1042 Windows, if *dst* already exists, :exc:`OSError` will be raised even if it is a
1043 file; there may be no way to implement an atomic rename when *dst* names an
Georg Brandlc575c902008-09-13 17:46:05 +00001044 existing file. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001045
1046
1047.. function:: renames(old, new)
1048
1049 Recursive directory or file renaming function. Works like :func:`rename`, except
1050 creation of any intermediate directories needed to make the new pathname good is
1051 attempted first. After the rename, directories corresponding to rightmost path
1052 segments of the old name will be pruned away using :func:`removedirs`.
1053
Georg Brandl116aa622007-08-15 14:28:22 +00001054 .. note::
1055
1056 This function can fail with the new directory structure made if you lack
1057 permissions needed to remove the leaf directory or file.
1058
1059
1060.. function:: rmdir(path)
1061
Georg Brandla6053b42009-09-01 08:11:14 +00001062 Remove (delete) the directory *path*. Only works when the directory is
1063 empty, otherwise, :exc:`OSError` is raised. In order to remove whole
1064 directory trees, :func:`shutil.rmtree` can be used. Availability: Unix,
1065 Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001066
1067
1068.. function:: stat(path)
1069
1070 Perform a :cfunc:`stat` system call on the given path. The return value is an
1071 object whose attributes correspond to the members of the :ctype:`stat`
1072 structure, namely: :attr:`st_mode` (protection bits), :attr:`st_ino` (inode
1073 number), :attr:`st_dev` (device), :attr:`st_nlink` (number of hard links),
Christian Heimesfaf2f632008-01-06 16:59:19 +00001074 :attr:`st_uid` (user id of owner), :attr:`st_gid` (group id of owner),
Georg Brandl116aa622007-08-15 14:28:22 +00001075 :attr:`st_size` (size of file, in bytes), :attr:`st_atime` (time of most recent
1076 access), :attr:`st_mtime` (time of most recent content modification),
1077 :attr:`st_ctime` (platform dependent; time of most recent metadata change on
1078 Unix, or the time of creation on Windows)::
1079
1080 >>> import os
1081 >>> statinfo = os.stat('somefile.txt')
1082 >>> statinfo
Georg Brandlf66df2b2010-01-16 14:41:21 +00001083 (33188, 422511, 769, 1, 1032, 100, 926, 1105022698,1105022732, 1105022732)
Georg Brandl116aa622007-08-15 14:28:22 +00001084 >>> statinfo.st_size
Georg Brandlf66df2b2010-01-16 14:41:21 +00001085 926
Georg Brandl116aa622007-08-15 14:28:22 +00001086 >>>
1087
Georg Brandl116aa622007-08-15 14:28:22 +00001088
1089 On some Unix systems (such as Linux), the following attributes may also be
1090 available: :attr:`st_blocks` (number of blocks allocated for file),
1091 :attr:`st_blksize` (filesystem blocksize), :attr:`st_rdev` (type of device if an
1092 inode device). :attr:`st_flags` (user defined flags for file).
1093
1094 On other Unix systems (such as FreeBSD), the following attributes may be
1095 available (but may be only filled out if root tries to use them): :attr:`st_gen`
1096 (file generation number), :attr:`st_birthtime` (time of file creation).
1097
1098 On Mac OS systems, the following attributes may also be available:
1099 :attr:`st_rsize`, :attr:`st_creator`, :attr:`st_type`.
1100
Georg Brandl116aa622007-08-15 14:28:22 +00001101 .. index:: module: stat
1102
1103 For backward compatibility, the return value of :func:`stat` is also accessible
1104 as a tuple of at least 10 integers giving the most important (and portable)
1105 members of the :ctype:`stat` structure, in the order :attr:`st_mode`,
1106 :attr:`st_ino`, :attr:`st_dev`, :attr:`st_nlink`, :attr:`st_uid`,
1107 :attr:`st_gid`, :attr:`st_size`, :attr:`st_atime`, :attr:`st_mtime`,
1108 :attr:`st_ctime`. More items may be added at the end by some implementations.
1109 The standard module :mod:`stat` defines functions and constants that are useful
1110 for extracting information from a :ctype:`stat` structure. (On Windows, some
1111 items are filled with dummy values.)
1112
1113 .. note::
1114
1115 The exact meaning and resolution of the :attr:`st_atime`, :attr:`st_mtime`, and
1116 :attr:`st_ctime` members depends on the operating system and the file system.
1117 For example, on Windows systems using the FAT or FAT32 file systems,
1118 :attr:`st_mtime` has 2-second resolution, and :attr:`st_atime` has only 1-day
1119 resolution. See your operating system documentation for details.
1120
Georg Brandlc575c902008-09-13 17:46:05 +00001121 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001122
Georg Brandl116aa622007-08-15 14:28:22 +00001123
1124.. function:: stat_float_times([newvalue])
1125
1126 Determine whether :class:`stat_result` represents time stamps as float objects.
1127 If *newvalue* is ``True``, future calls to :func:`stat` return floats, if it is
1128 ``False``, future calls return ints. If *newvalue* is omitted, return the
1129 current setting.
1130
1131 For compatibility with older Python versions, accessing :class:`stat_result` as
1132 a tuple always returns integers.
1133
Georg Brandl55ac8f02007-09-01 13:51:09 +00001134 Python now returns float values by default. Applications which do not work
1135 correctly with floating point time stamps can use this function to restore the
1136 old behaviour.
Georg Brandl116aa622007-08-15 14:28:22 +00001137
1138 The resolution of the timestamps (that is the smallest possible fraction)
1139 depends on the system. Some systems only support second resolution; on these
1140 systems, the fraction will always be zero.
1141
1142 It is recommended that this setting is only changed at program startup time in
1143 the *__main__* module; libraries should never change this setting. If an
1144 application uses a library that works incorrectly if floating point time stamps
1145 are processed, this application should turn the feature off until the library
1146 has been corrected.
1147
1148
1149.. function:: statvfs(path)
1150
1151 Perform a :cfunc:`statvfs` system call on the given path. The return value is
1152 an object whose attributes describe the filesystem on the given path, and
1153 correspond to the members of the :ctype:`statvfs` structure, namely:
1154 :attr:`f_bsize`, :attr:`f_frsize`, :attr:`f_blocks`, :attr:`f_bfree`,
1155 :attr:`f_bavail`, :attr:`f_files`, :attr:`f_ffree`, :attr:`f_favail`,
1156 :attr:`f_flag`, :attr:`f_namemax`. Availability: Unix.
1157
Georg Brandl116aa622007-08-15 14:28:22 +00001158
Benjamin Peterson5879d412009-03-30 14:51:56 +00001159.. function:: symlink(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +00001160
Benjamin Peterson5879d412009-03-30 14:51:56 +00001161 Create a symbolic link pointing to *source* named *link_name*. Availability:
1162 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001163
1164
Georg Brandl116aa622007-08-15 14:28:22 +00001165.. function:: unlink(path)
1166
Georg Brandla6053b42009-09-01 08:11:14 +00001167 Remove (delete) the file *path*. This is the same function as
1168 :func:`remove`; the :func:`unlink` name is its traditional Unix
1169 name. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001170
1171
1172.. function:: utime(path, times)
1173
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001174 Set the access and modified times of the file specified by *path*. If *times*
1175 is ``None``, then the file's access and modified times are set to the current
1176 time. (The effect is similar to running the Unix program :program:`touch` on
1177 the path.) Otherwise, *times* must be a 2-tuple of numbers, of the form
1178 ``(atime, mtime)`` which is used to set the access and modified times,
1179 respectively. Whether a directory can be given for *path* depends on whether
1180 the operating system implements directories as files (for example, Windows
1181 does not). Note that the exact times you set here may not be returned by a
1182 subsequent :func:`stat` call, depending on the resolution with which your
1183 operating system records access and modification times; see :func:`stat`.
Georg Brandl116aa622007-08-15 14:28:22 +00001184
Georg Brandlc575c902008-09-13 17:46:05 +00001185 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001186
1187
Georg Brandl18244152009-09-02 20:34:52 +00001188.. function:: walk(top, topdown=True, onerror=None, followlinks=False)
Georg Brandl116aa622007-08-15 14:28:22 +00001189
1190 .. index::
1191 single: directory; walking
1192 single: directory; traversal
1193
Christian Heimesfaf2f632008-01-06 16:59:19 +00001194 Generate the file names in a directory tree by walking the tree
1195 either top-down or bottom-up. For each directory in the tree rooted at directory
Georg Brandl116aa622007-08-15 14:28:22 +00001196 *top* (including *top* itself), it yields a 3-tuple ``(dirpath, dirnames,
1197 filenames)``.
1198
1199 *dirpath* is a string, the path to the directory. *dirnames* is a list of the
1200 names of the subdirectories in *dirpath* (excluding ``'.'`` and ``'..'``).
1201 *filenames* is a list of the names of the non-directory files in *dirpath*.
1202 Note that the names in the lists contain no path components. To get a full path
1203 (which begins with *top*) to a file or directory in *dirpath*, do
1204 ``os.path.join(dirpath, name)``.
1205
Christian Heimesfaf2f632008-01-06 16:59:19 +00001206 If optional argument *topdown* is ``True`` or not specified, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001207 directory is generated before the triples for any of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001208 (directories are generated top-down). If *topdown* is ``False``, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001209 directory is generated after the triples for all of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001210 (directories are generated bottom-up).
Georg Brandl116aa622007-08-15 14:28:22 +00001211
Christian Heimesfaf2f632008-01-06 16:59:19 +00001212 When *topdown* is ``True``, the caller can modify the *dirnames* list in-place
Georg Brandl116aa622007-08-15 14:28:22 +00001213 (perhaps using :keyword:`del` or slice assignment), and :func:`walk` will only
1214 recurse into the subdirectories whose names remain in *dirnames*; this can be
1215 used to prune the search, impose a specific order of visiting, or even to inform
1216 :func:`walk` about directories the caller creates or renames before it resumes
Christian Heimesfaf2f632008-01-06 16:59:19 +00001217 :func:`walk` again. Modifying *dirnames* when *topdown* is ``False`` is
Georg Brandl116aa622007-08-15 14:28:22 +00001218 ineffective, because in bottom-up mode the directories in *dirnames* are
1219 generated before *dirpath* itself is generated.
1220
Christian Heimesfaf2f632008-01-06 16:59:19 +00001221 By default errors from the :func:`listdir` call are ignored. If optional
Georg Brandl116aa622007-08-15 14:28:22 +00001222 argument *onerror* is specified, it should be a function; it will be called with
1223 one argument, an :exc:`OSError` instance. It can report the error to continue
1224 with the walk, or raise the exception to abort the walk. Note that the filename
1225 is available as the ``filename`` attribute of the exception object.
1226
1227 By default, :func:`walk` will not walk down into symbolic links that resolve to
Christian Heimesfaf2f632008-01-06 16:59:19 +00001228 directories. Set *followlinks* to ``True`` to visit directories pointed to by
Georg Brandl116aa622007-08-15 14:28:22 +00001229 symlinks, on systems that support them.
1230
Georg Brandl116aa622007-08-15 14:28:22 +00001231 .. note::
1232
Christian Heimesfaf2f632008-01-06 16:59:19 +00001233 Be aware that setting *followlinks* to ``True`` can lead to infinite recursion if a
Georg Brandl116aa622007-08-15 14:28:22 +00001234 link points to a parent directory of itself. :func:`walk` does not keep track of
1235 the directories it visited already.
1236
1237 .. note::
1238
1239 If you pass a relative pathname, don't change the current working directory
1240 between resumptions of :func:`walk`. :func:`walk` never changes the current
1241 directory, and assumes that its caller doesn't either.
1242
1243 This example displays the number of bytes taken by non-directory files in each
1244 directory under the starting directory, except that it doesn't look under any
1245 CVS subdirectory::
1246
1247 import os
1248 from os.path import join, getsize
1249 for root, dirs, files in os.walk('python/Lib/email'):
Georg Brandl6911e3c2007-09-04 07:15:32 +00001250 print(root, "consumes", end=" ")
1251 print(sum(getsize(join(root, name)) for name in files), end=" ")
1252 print("bytes in", len(files), "non-directory files")
Georg Brandl116aa622007-08-15 14:28:22 +00001253 if 'CVS' in dirs:
1254 dirs.remove('CVS') # don't visit CVS directories
1255
Christian Heimesfaf2f632008-01-06 16:59:19 +00001256 In the next example, walking the tree bottom-up is essential: :func:`rmdir`
Georg Brandl116aa622007-08-15 14:28:22 +00001257 doesn't allow deleting a directory before the directory is empty::
1258
Christian Heimesfaf2f632008-01-06 16:59:19 +00001259 # Delete everything reachable from the directory named in "top",
Georg Brandl116aa622007-08-15 14:28:22 +00001260 # assuming there are no symbolic links.
1261 # CAUTION: This is dangerous! For example, if top == '/', it
1262 # could delete all your disk files.
1263 import os
1264 for root, dirs, files in os.walk(top, topdown=False):
1265 for name in files:
1266 os.remove(os.path.join(root, name))
1267 for name in dirs:
1268 os.rmdir(os.path.join(root, name))
1269
Georg Brandl116aa622007-08-15 14:28:22 +00001270
1271.. _os-process:
1272
1273Process Management
1274------------------
1275
1276These functions may be used to create and manage processes.
1277
1278The various :func:`exec\*` functions take a list of arguments for the new
1279program loaded into the process. In each case, the first of these arguments is
1280passed to the new program as its own name rather than as an argument a user may
1281have typed on a command line. For the C programmer, this is the ``argv[0]``
1282passed to a program's :cfunc:`main`. For example, ``os.execv('/bin/echo',
1283['foo', 'bar'])`` will only print ``bar`` on standard output; ``foo`` will seem
1284to be ignored.
1285
1286
1287.. function:: abort()
1288
1289 Generate a :const:`SIGABRT` signal to the current process. On Unix, the default
1290 behavior is to produce a core dump; on Windows, the process immediately returns
1291 an exit code of ``3``. Be aware that programs which use :func:`signal.signal`
1292 to register a handler for :const:`SIGABRT` will behave differently.
Georg Brandlc575c902008-09-13 17:46:05 +00001293 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001294
1295
1296.. function:: execl(path, arg0, arg1, ...)
1297 execle(path, arg0, arg1, ..., env)
1298 execlp(file, arg0, arg1, ...)
1299 execlpe(file, arg0, arg1, ..., env)
1300 execv(path, args)
1301 execve(path, args, env)
1302 execvp(file, args)
1303 execvpe(file, args, env)
1304
1305 These functions all execute a new program, replacing the current process; they
1306 do not return. On Unix, the new executable is loaded into the current process,
Christian Heimesfaf2f632008-01-06 16:59:19 +00001307 and will have the same process id as the caller. Errors will be reported as
Georg Brandl48310cd2009-01-03 21:18:54 +00001308 :exc:`OSError` exceptions.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001309
1310 The current process is replaced immediately. Open file objects and
1311 descriptors are not flushed, so if there may be data buffered
1312 on these open files, you should flush them using
1313 :func:`sys.stdout.flush` or :func:`os.fsync` before calling an
1314 :func:`exec\*` function.
Georg Brandl116aa622007-08-15 14:28:22 +00001315
Christian Heimesfaf2f632008-01-06 16:59:19 +00001316 The "l" and "v" variants of the :func:`exec\*` functions differ in how
1317 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001318 to work with if the number of parameters is fixed when the code is written; the
1319 individual parameters simply become additional parameters to the :func:`execl\*`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001320 functions. The "v" variants are good when the number of parameters is
Georg Brandl116aa622007-08-15 14:28:22 +00001321 variable, with the arguments being passed in a list or tuple as the *args*
1322 parameter. In either case, the arguments to the child process should start with
1323 the name of the command being run, but this is not enforced.
1324
Christian Heimesfaf2f632008-01-06 16:59:19 +00001325 The variants which include a "p" near the end (:func:`execlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001326 :func:`execlpe`, :func:`execvp`, and :func:`execvpe`) will use the
1327 :envvar:`PATH` environment variable to locate the program *file*. When the
1328 environment is being replaced (using one of the :func:`exec\*e` variants,
1329 discussed in the next paragraph), the new environment is used as the source of
1330 the :envvar:`PATH` variable. The other variants, :func:`execl`, :func:`execle`,
1331 :func:`execv`, and :func:`execve`, will not use the :envvar:`PATH` variable to
1332 locate the executable; *path* must contain an appropriate absolute or relative
1333 path.
1334
1335 For :func:`execle`, :func:`execlpe`, :func:`execve`, and :func:`execvpe` (note
Christian Heimesfaf2f632008-01-06 16:59:19 +00001336 that these all end in "e"), the *env* parameter must be a mapping which is
Christian Heimesa342c012008-04-20 21:01:16 +00001337 used to define the environment variables for the new process (these are used
1338 instead of the current process' environment); the functions :func:`execl`,
Georg Brandl116aa622007-08-15 14:28:22 +00001339 :func:`execlp`, :func:`execv`, and :func:`execvp` all cause the new process to
Georg Brandl48310cd2009-01-03 21:18:54 +00001340 inherit the environment of the current process.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001341
1342 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001343
1344
1345.. function:: _exit(n)
1346
1347 Exit to the system with status *n*, without calling cleanup handlers, flushing
Georg Brandlc575c902008-09-13 17:46:05 +00001348 stdio buffers, etc. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001349
1350 .. note::
1351
1352 The standard way to exit is ``sys.exit(n)``. :func:`_exit` should normally only
1353 be used in the child process after a :func:`fork`.
1354
Christian Heimesfaf2f632008-01-06 16:59:19 +00001355The following exit codes are defined and can be used with :func:`_exit`,
Georg Brandl116aa622007-08-15 14:28:22 +00001356although they are not required. These are typically used for system programs
1357written in Python, such as a mail server's external command delivery program.
1358
1359.. note::
1360
1361 Some of these may not be available on all Unix platforms, since there is some
1362 variation. These constants are defined where they are defined by the underlying
1363 platform.
1364
1365
1366.. data:: EX_OK
1367
Georg Brandlc575c902008-09-13 17:46:05 +00001368 Exit code that means no error occurred. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001369
Georg Brandl116aa622007-08-15 14:28:22 +00001370
1371.. data:: EX_USAGE
1372
1373 Exit code that means the command was used incorrectly, such as when the wrong
Georg Brandlc575c902008-09-13 17:46:05 +00001374 number of arguments are given. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001375
Georg Brandl116aa622007-08-15 14:28:22 +00001376
1377.. data:: EX_DATAERR
1378
Georg Brandlc575c902008-09-13 17:46:05 +00001379 Exit code that means the input data was incorrect. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001380
Georg Brandl116aa622007-08-15 14:28:22 +00001381
1382.. data:: EX_NOINPUT
1383
1384 Exit code that means an input file did not exist or was not readable.
Georg Brandlc575c902008-09-13 17:46:05 +00001385 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001386
Georg Brandl116aa622007-08-15 14:28:22 +00001387
1388.. data:: EX_NOUSER
1389
Georg Brandlc575c902008-09-13 17:46:05 +00001390 Exit code that means a specified user did not exist. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001391
Georg Brandl116aa622007-08-15 14:28:22 +00001392
1393.. data:: EX_NOHOST
1394
Georg Brandlc575c902008-09-13 17:46:05 +00001395 Exit code that means a specified host did not exist. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001396
Georg Brandl116aa622007-08-15 14:28:22 +00001397
1398.. data:: EX_UNAVAILABLE
1399
1400 Exit code that means that a required service is unavailable. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001401 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001402
Georg Brandl116aa622007-08-15 14:28:22 +00001403
1404.. data:: EX_SOFTWARE
1405
1406 Exit code that means an internal software error was detected. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001407 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001408
Georg Brandl116aa622007-08-15 14:28:22 +00001409
1410.. data:: EX_OSERR
1411
1412 Exit code that means an operating system error was detected, such as the
Georg Brandlc575c902008-09-13 17:46:05 +00001413 inability to fork or create a pipe. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001414
Georg Brandl116aa622007-08-15 14:28:22 +00001415
1416.. data:: EX_OSFILE
1417
1418 Exit code that means some system file did not exist, could not be opened, or had
Georg Brandlc575c902008-09-13 17:46:05 +00001419 some other kind of error. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001420
Georg Brandl116aa622007-08-15 14:28:22 +00001421
1422.. data:: EX_CANTCREAT
1423
1424 Exit code that means a user specified output file could not be created.
Georg Brandlc575c902008-09-13 17:46:05 +00001425 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001426
Georg Brandl116aa622007-08-15 14:28:22 +00001427
1428.. data:: EX_IOERR
1429
1430 Exit code that means that an error occurred while doing I/O on some file.
Georg Brandlc575c902008-09-13 17:46:05 +00001431 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001432
Georg Brandl116aa622007-08-15 14:28:22 +00001433
1434.. data:: EX_TEMPFAIL
1435
1436 Exit code that means a temporary failure occurred. This indicates something
1437 that may not really be an error, such as a network connection that couldn't be
Georg Brandlc575c902008-09-13 17:46:05 +00001438 made during a retryable operation. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001439
Georg Brandl116aa622007-08-15 14:28:22 +00001440
1441.. data:: EX_PROTOCOL
1442
1443 Exit code that means that a protocol exchange was illegal, invalid, or not
Georg Brandlc575c902008-09-13 17:46:05 +00001444 understood. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001445
Georg Brandl116aa622007-08-15 14:28:22 +00001446
1447.. data:: EX_NOPERM
1448
1449 Exit code that means that there were insufficient permissions to perform the
Georg Brandlc575c902008-09-13 17:46:05 +00001450 operation (but not intended for file system problems). Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001451
Georg Brandl116aa622007-08-15 14:28:22 +00001452
1453.. data:: EX_CONFIG
1454
1455 Exit code that means that some kind of configuration error occurred.
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_NOTFOUND
1460
1461 Exit code that means something like "an entry was not found". Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001462 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001463
Georg Brandl116aa622007-08-15 14:28:22 +00001464
1465.. function:: fork()
1466
Christian Heimesfaf2f632008-01-06 16:59:19 +00001467 Fork a child process. Return ``0`` in the child and the child's process id in the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001468 parent. If an error occurs :exc:`OSError` is raised.
Benjamin Petersonbcd8ac32008-10-10 22:20:52 +00001469
1470 Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have
1471 known issues when using fork() from a thread.
1472
Georg Brandlc575c902008-09-13 17:46:05 +00001473 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001474
1475
1476.. function:: forkpty()
1477
1478 Fork a child process, using a new pseudo-terminal as the child's controlling
1479 terminal. Return a pair of ``(pid, fd)``, where *pid* is ``0`` in the child, the
1480 new child's process id in the parent, and *fd* is the file descriptor of the
1481 master end of the pseudo-terminal. For a more portable approach, use the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001482 :mod:`pty` module. If an error occurs :exc:`OSError` is raised.
Georg Brandlc575c902008-09-13 17:46:05 +00001483 Availability: some flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001484
1485
1486.. function:: kill(pid, sig)
1487
1488 .. index::
1489 single: process; killing
1490 single: process; signalling
1491
1492 Send signal *sig* to the process *pid*. Constants for the specific signals
1493 available on the host platform are defined in the :mod:`signal` module.
Brian Curtineb24d742010-04-12 17:16:38 +00001494
1495 Windows: The :data:`signal.CTRL_C_EVENT` and
1496 :data:`signal.CTRL_BREAK_EVENT` signals are special signals which can
1497 only be sent to console processes which share a common console window,
1498 e.g., some subprocesses. Any other value for *sig* will cause the process
1499 to be unconditionally killed by the TerminateProcess API, and the exit code
1500 will be set to *sig*. The Windows version of :func:`kill` additionally takes
1501 process handles to be killed.
Georg Brandl116aa622007-08-15 14:28:22 +00001502
Brian Curtin904bd392010-04-20 15:28:06 +00001503 .. versionadded:: 3.2 Windows support
1504
Georg Brandl116aa622007-08-15 14:28:22 +00001505
1506.. function:: killpg(pgid, sig)
1507
1508 .. index::
1509 single: process; killing
1510 single: process; signalling
1511
Georg Brandlc575c902008-09-13 17:46:05 +00001512 Send the signal *sig* to the process group *pgid*. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001513
Georg Brandl116aa622007-08-15 14:28:22 +00001514
1515.. function:: nice(increment)
1516
1517 Add *increment* to the process's "niceness". Return the new niceness.
Georg Brandlc575c902008-09-13 17:46:05 +00001518 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001519
1520
1521.. function:: plock(op)
1522
1523 Lock program segments into memory. The value of *op* (defined in
Georg Brandlc575c902008-09-13 17:46:05 +00001524 ``<sys/lock.h>``) determines which segments are locked. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001525
1526
1527.. function:: popen(...)
1528 :noindex:
1529
1530 Run child processes, returning opened pipes for communications. These functions
1531 are described in section :ref:`os-newstreams`.
1532
1533
1534.. function:: spawnl(mode, path, ...)
1535 spawnle(mode, path, ..., env)
1536 spawnlp(mode, file, ...)
1537 spawnlpe(mode, file, ..., env)
1538 spawnv(mode, path, args)
1539 spawnve(mode, path, args, env)
1540 spawnvp(mode, file, args)
1541 spawnvpe(mode, file, args, env)
1542
1543 Execute the program *path* in a new process.
1544
1545 (Note that the :mod:`subprocess` module provides more powerful facilities for
1546 spawning new processes and retrieving their results; using that module is
Benjamin Peterson87c8d872009-06-11 22:54:11 +00001547 preferable to using these functions. Check especially the
1548 :ref:`subprocess-replacements` section.)
Georg Brandl116aa622007-08-15 14:28:22 +00001549
Christian Heimesfaf2f632008-01-06 16:59:19 +00001550 If *mode* is :const:`P_NOWAIT`, this function returns the process id of the new
Georg Brandl116aa622007-08-15 14:28:22 +00001551 process; if *mode* is :const:`P_WAIT`, returns the process's exit code if it
1552 exits normally, or ``-signal``, where *signal* is the signal that killed the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001553 process. On Windows, the process id will actually be the process handle, so can
Georg Brandl116aa622007-08-15 14:28:22 +00001554 be used with the :func:`waitpid` function.
1555
Christian Heimesfaf2f632008-01-06 16:59:19 +00001556 The "l" and "v" variants of the :func:`spawn\*` functions differ in how
1557 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001558 to work with if the number of parameters is fixed when the code is written; the
1559 individual parameters simply become additional parameters to the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001560 :func:`spawnl\*` functions. The "v" variants are good when the number of
Georg Brandl116aa622007-08-15 14:28:22 +00001561 parameters is variable, with the arguments being passed in a list or tuple as
1562 the *args* parameter. In either case, the arguments to the child process must
1563 start with the name of the command being run.
1564
Christian Heimesfaf2f632008-01-06 16:59:19 +00001565 The variants which include a second "p" near the end (:func:`spawnlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001566 :func:`spawnlpe`, :func:`spawnvp`, and :func:`spawnvpe`) will use the
1567 :envvar:`PATH` environment variable to locate the program *file*. When the
1568 environment is being replaced (using one of the :func:`spawn\*e` variants,
1569 discussed in the next paragraph), the new environment is used as the source of
1570 the :envvar:`PATH` variable. The other variants, :func:`spawnl`,
1571 :func:`spawnle`, :func:`spawnv`, and :func:`spawnve`, will not use the
1572 :envvar:`PATH` variable to locate the executable; *path* must contain an
1573 appropriate absolute or relative path.
1574
1575 For :func:`spawnle`, :func:`spawnlpe`, :func:`spawnve`, and :func:`spawnvpe`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001576 (note that these all end in "e"), the *env* parameter must be a mapping
Christian Heimesa342c012008-04-20 21:01:16 +00001577 which is used to define the environment variables for the new process (they are
1578 used instead of the current process' environment); the functions
Georg Brandl116aa622007-08-15 14:28:22 +00001579 :func:`spawnl`, :func:`spawnlp`, :func:`spawnv`, and :func:`spawnvp` all cause
Benjamin Petersond23f8222009-04-05 19:13:16 +00001580 the new process to inherit the environment of the current process. Note that
1581 keys and values in the *env* dictionary must be strings; invalid keys or
1582 values will cause the function to fail, with a return value of ``127``.
Georg Brandl116aa622007-08-15 14:28:22 +00001583
1584 As an example, the following calls to :func:`spawnlp` and :func:`spawnvpe` are
1585 equivalent::
1586
1587 import os
1588 os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1589
1590 L = ['cp', 'index.html', '/dev/null']
1591 os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1592
1593 Availability: Unix, Windows. :func:`spawnlp`, :func:`spawnlpe`, :func:`spawnvp`
1594 and :func:`spawnvpe` are not available on Windows.
1595
Georg Brandl116aa622007-08-15 14:28:22 +00001596
1597.. data:: P_NOWAIT
1598 P_NOWAITO
1599
1600 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1601 functions. If either of these values is given, the :func:`spawn\*` functions
Christian Heimesfaf2f632008-01-06 16:59:19 +00001602 will return as soon as the new process has been created, with the process id as
Georg Brandlc575c902008-09-13 17:46:05 +00001603 the return value. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001604
Georg Brandl116aa622007-08-15 14:28:22 +00001605
1606.. data:: P_WAIT
1607
1608 Possible value for the *mode* parameter to the :func:`spawn\*` family of
1609 functions. If this is given as *mode*, the :func:`spawn\*` functions will not
1610 return until the new process has run to completion and will return the exit code
1611 of the process the run is successful, or ``-signal`` if a signal kills the
Georg Brandlc575c902008-09-13 17:46:05 +00001612 process. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001613
Georg Brandl116aa622007-08-15 14:28:22 +00001614
1615.. data:: P_DETACH
1616 P_OVERLAY
1617
1618 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1619 functions. These are less portable than those listed above. :const:`P_DETACH`
1620 is similar to :const:`P_NOWAIT`, but the new process is detached from the
1621 console of the calling process. If :const:`P_OVERLAY` is used, the current
1622 process will be replaced; the :func:`spawn\*` function will not return.
1623 Availability: Windows.
1624
Georg Brandl116aa622007-08-15 14:28:22 +00001625
1626.. function:: startfile(path[, operation])
1627
1628 Start a file with its associated application.
1629
1630 When *operation* is not specified or ``'open'``, this acts like double-clicking
1631 the file in Windows Explorer, or giving the file name as an argument to the
1632 :program:`start` command from the interactive command shell: the file is opened
1633 with whatever application (if any) its extension is associated.
1634
1635 When another *operation* is given, it must be a "command verb" that specifies
1636 what should be done with the file. Common verbs documented by Microsoft are
1637 ``'print'`` and ``'edit'`` (to be used on files) as well as ``'explore'`` and
1638 ``'find'`` (to be used on directories).
1639
1640 :func:`startfile` returns as soon as the associated application is launched.
1641 There is no option to wait for the application to close, and no way to retrieve
1642 the application's exit status. The *path* parameter is relative to the current
1643 directory. If you want to use an absolute path, make sure the first character
1644 is not a slash (``'/'``); the underlying Win32 :cfunc:`ShellExecute` function
1645 doesn't work if it is. Use the :func:`os.path.normpath` function to ensure that
1646 the path is properly encoded for Win32. Availability: Windows.
1647
Georg Brandl116aa622007-08-15 14:28:22 +00001648
1649.. function:: system(command)
1650
1651 Execute the command (a string) in a subshell. This is implemented by calling
Georg Brandl495f7b52009-10-27 15:28:25 +00001652 the Standard C function :cfunc:`system`, and has the same limitations.
1653 Changes to :data:`sys.stdin`, etc. are not reflected in the environment of the
1654 executed command.
Georg Brandl116aa622007-08-15 14:28:22 +00001655
1656 On Unix, the return value is the exit status of the process encoded in the
1657 format specified for :func:`wait`. Note that POSIX does not specify the meaning
1658 of the return value of the C :cfunc:`system` function, so the return value of
1659 the Python function is system-dependent.
1660
1661 On Windows, the return value is that returned by the system shell after running
1662 *command*, given by the Windows environment variable :envvar:`COMSPEC`: on
1663 :program:`command.com` systems (Windows 95, 98 and ME) this is always ``0``; on
1664 :program:`cmd.exe` systems (Windows NT, 2000 and XP) this is the exit status of
1665 the command run; on systems using a non-native shell, consult your shell
1666 documentation.
1667
Georg Brandlc575c902008-09-13 17:46:05 +00001668 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001669
1670 The :mod:`subprocess` module provides more powerful facilities for spawning new
1671 processes and retrieving their results; using that module is preferable to using
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001672 this function. Use the :mod:`subprocess` module. Check especially the
1673 :ref:`subprocess-replacements` section.
Georg Brandl116aa622007-08-15 14:28:22 +00001674
1675
1676.. function:: times()
1677
1678 Return a 5-tuple of floating point numbers indicating accumulated (processor or
1679 other) times, in seconds. The items are: user time, system time, children's
1680 user time, children's system time, and elapsed real time since a fixed point in
1681 the past, in that order. See the Unix manual page :manpage:`times(2)` or the
Georg Brandlc575c902008-09-13 17:46:05 +00001682 corresponding Windows Platform API documentation. Availability: Unix,
Christian Heimes68f5fbe2008-02-14 08:27:37 +00001683 Windows. On Windows, only the first two items are filled, the others are zero.
Georg Brandl116aa622007-08-15 14:28:22 +00001684
1685
1686.. function:: wait()
1687
1688 Wait for completion of a child process, and return a tuple containing its pid
1689 and exit status indication: a 16-bit number, whose low byte is the signal number
1690 that killed the process, and whose high byte is the exit status (if the signal
1691 number is zero); the high bit of the low byte is set if a core file was
Georg Brandlc575c902008-09-13 17:46:05 +00001692 produced. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001693
1694
1695.. function:: waitpid(pid, options)
1696
1697 The details of this function differ on Unix and Windows.
1698
1699 On Unix: Wait for completion of a child process given by process id *pid*, and
1700 return a tuple containing its process id and exit status indication (encoded as
1701 for :func:`wait`). The semantics of the call are affected by the value of the
1702 integer *options*, which should be ``0`` for normal operation.
1703
1704 If *pid* is greater than ``0``, :func:`waitpid` requests status information for
1705 that specific process. If *pid* is ``0``, the request is for the status of any
1706 child in the process group of the current process. If *pid* is ``-1``, the
1707 request pertains to any child of the current process. If *pid* is less than
1708 ``-1``, status is requested for any process in the process group ``-pid`` (the
1709 absolute value of *pid*).
1710
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001711 An :exc:`OSError` is raised with the value of errno when the syscall
1712 returns -1.
1713
Georg Brandl116aa622007-08-15 14:28:22 +00001714 On Windows: Wait for completion of a process given by process handle *pid*, and
1715 return a tuple containing *pid*, and its exit status shifted left by 8 bits
1716 (shifting makes cross-platform use of the function easier). A *pid* less than or
1717 equal to ``0`` has no special meaning on Windows, and raises an exception. The
1718 value of integer *options* has no effect. *pid* can refer to any process whose
1719 id is known, not necessarily a child process. The :func:`spawn` functions called
1720 with :const:`P_NOWAIT` return suitable process handles.
1721
1722
1723.. function:: wait3([options])
1724
1725 Similar to :func:`waitpid`, except no process id argument is given and a
1726 3-element tuple containing the child's process id, exit status indication, and
1727 resource usage information is returned. Refer to :mod:`resource`.\
1728 :func:`getrusage` for details on resource usage information. The option
1729 argument is the same as that provided to :func:`waitpid` and :func:`wait4`.
1730 Availability: Unix.
1731
Georg Brandl116aa622007-08-15 14:28:22 +00001732
1733.. function:: wait4(pid, options)
1734
1735 Similar to :func:`waitpid`, except a 3-element tuple, containing the child's
1736 process id, exit status indication, and resource usage information is returned.
1737 Refer to :mod:`resource`.\ :func:`getrusage` for details on resource usage
1738 information. The arguments to :func:`wait4` are the same as those provided to
1739 :func:`waitpid`. Availability: Unix.
1740
Georg Brandl116aa622007-08-15 14:28:22 +00001741
1742.. data:: WNOHANG
1743
1744 The option for :func:`waitpid` to return immediately if no child process status
1745 is available immediately. The function returns ``(0, 0)`` in this case.
Georg Brandlc575c902008-09-13 17:46:05 +00001746 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001747
1748
1749.. data:: WCONTINUED
1750
1751 This option causes child processes to be reported if they have been continued
1752 from a job control stop since their status was last reported. Availability: Some
1753 Unix systems.
1754
Georg Brandl116aa622007-08-15 14:28:22 +00001755
1756.. data:: WUNTRACED
1757
1758 This option causes child processes to be reported if they have been stopped but
1759 their current state has not been reported since they were stopped. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001760 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001761
Georg Brandl116aa622007-08-15 14:28:22 +00001762
1763The following functions take a process status code as returned by
1764:func:`system`, :func:`wait`, or :func:`waitpid` as a parameter. They may be
1765used to determine the disposition of a process.
1766
Georg Brandl116aa622007-08-15 14:28:22 +00001767.. function:: WCOREDUMP(status)
1768
Christian Heimesfaf2f632008-01-06 16:59:19 +00001769 Return ``True`` if a core dump was generated for the process, otherwise
Georg Brandlc575c902008-09-13 17:46:05 +00001770 return ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001771
Georg Brandl116aa622007-08-15 14:28:22 +00001772
1773.. function:: WIFCONTINUED(status)
1774
Christian Heimesfaf2f632008-01-06 16:59:19 +00001775 Return ``True`` if the process has been continued from a job control stop,
1776 otherwise return ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001777
Georg Brandl116aa622007-08-15 14:28:22 +00001778
1779.. function:: WIFSTOPPED(status)
1780
Christian Heimesfaf2f632008-01-06 16:59:19 +00001781 Return ``True`` if the process has been stopped, otherwise return
Georg Brandl116aa622007-08-15 14:28:22 +00001782 ``False``. Availability: Unix.
1783
1784
1785.. function:: WIFSIGNALED(status)
1786
Christian Heimesfaf2f632008-01-06 16:59:19 +00001787 Return ``True`` if the process exited due to a signal, otherwise return
Georg Brandlc575c902008-09-13 17:46:05 +00001788 ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001789
1790
1791.. function:: WIFEXITED(status)
1792
Christian Heimesfaf2f632008-01-06 16:59:19 +00001793 Return ``True`` if the process exited using the :manpage:`exit(2)` system call,
Georg Brandlc575c902008-09-13 17:46:05 +00001794 otherwise return ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001795
1796
1797.. function:: WEXITSTATUS(status)
1798
1799 If ``WIFEXITED(status)`` is true, return the integer parameter to the
1800 :manpage:`exit(2)` system call. Otherwise, the return value is meaningless.
Georg Brandlc575c902008-09-13 17:46:05 +00001801 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001802
1803
1804.. function:: WSTOPSIG(status)
1805
Georg Brandlc575c902008-09-13 17:46:05 +00001806 Return the signal which caused the process to stop. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001807
1808
1809.. function:: WTERMSIG(status)
1810
Georg Brandlc575c902008-09-13 17:46:05 +00001811 Return the signal which caused the process to exit. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001812
1813
1814.. _os-path:
1815
1816Miscellaneous System Information
1817--------------------------------
1818
1819
1820.. function:: confstr(name)
1821
1822 Return string-valued system configuration values. *name* specifies the
1823 configuration value to retrieve; it may be a string which is the name of a
1824 defined system value; these names are specified in a number of standards (POSIX,
1825 Unix 95, Unix 98, and others). Some platforms define additional names as well.
1826 The names known to the host operating system are given as the keys of the
1827 ``confstr_names`` dictionary. For configuration variables not included in that
1828 mapping, passing an integer for *name* is also accepted. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001829 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001830
1831 If the configuration value specified by *name* isn't defined, ``None`` is
1832 returned.
1833
1834 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1835 specific value for *name* is not supported by the host system, even if it is
1836 included in ``confstr_names``, an :exc:`OSError` is raised with
1837 :const:`errno.EINVAL` for the error number.
1838
1839
1840.. data:: confstr_names
1841
1842 Dictionary mapping names accepted by :func:`confstr` to the integer values
1843 defined for those names by the host operating system. This can be used to
Georg Brandlc575c902008-09-13 17:46:05 +00001844 determine the set of names known to the system. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001845
1846
1847.. function:: getloadavg()
1848
Christian Heimesa62da1d2008-01-12 19:39:10 +00001849 Return the number of processes in the system run queue averaged over the last
1850 1, 5, and 15 minutes or raises :exc:`OSError` if the load average was
Georg Brandlf08a9dd2008-06-10 16:57:31 +00001851 unobtainable. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001852
Georg Brandl116aa622007-08-15 14:28:22 +00001853
1854.. function:: sysconf(name)
1855
1856 Return integer-valued system configuration values. If the configuration value
1857 specified by *name* isn't defined, ``-1`` is returned. The comments regarding
1858 the *name* parameter for :func:`confstr` apply here as well; the dictionary that
1859 provides information on the known names is given by ``sysconf_names``.
Georg Brandlc575c902008-09-13 17:46:05 +00001860 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001861
1862
1863.. data:: sysconf_names
1864
1865 Dictionary mapping names accepted by :func:`sysconf` to the integer values
1866 defined for those names by the host operating system. This can be used to
Georg Brandlc575c902008-09-13 17:46:05 +00001867 determine the set of names known to the system. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001868
Christian Heimesfaf2f632008-01-06 16:59:19 +00001869The following data values are used to support path manipulation operations. These
Georg Brandl116aa622007-08-15 14:28:22 +00001870are defined for all platforms.
1871
1872Higher-level operations on pathnames are defined in the :mod:`os.path` module.
1873
1874
1875.. data:: curdir
1876
1877 The constant string used by the operating system to refer to the current
Georg Brandlc575c902008-09-13 17:46:05 +00001878 directory. This is ``'.'`` for Windows and POSIX. Also available via
1879 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00001880
1881
1882.. data:: pardir
1883
1884 The constant string used by the operating system to refer to the parent
Georg Brandlc575c902008-09-13 17:46:05 +00001885 directory. This is ``'..'`` for Windows and POSIX. Also available via
1886 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00001887
1888
1889.. data:: sep
1890
Georg Brandlc575c902008-09-13 17:46:05 +00001891 The character used by the operating system to separate pathname components.
1892 This is ``'/'`` for POSIX and ``'\\'`` for Windows. Note that knowing this
1893 is not sufficient to be able to parse or concatenate pathnames --- use
Georg Brandl116aa622007-08-15 14:28:22 +00001894 :func:`os.path.split` and :func:`os.path.join` --- but it is occasionally
1895 useful. Also available via :mod:`os.path`.
1896
1897
1898.. data:: altsep
1899
1900 An alternative character used by the operating system to separate pathname
1901 components, or ``None`` if only one separator character exists. This is set to
1902 ``'/'`` on Windows systems where ``sep`` is a backslash. Also available via
1903 :mod:`os.path`.
1904
1905
1906.. data:: extsep
1907
1908 The character which separates the base filename from the extension; for example,
1909 the ``'.'`` in :file:`os.py`. Also available via :mod:`os.path`.
1910
Georg Brandl116aa622007-08-15 14:28:22 +00001911
1912.. data:: pathsep
1913
1914 The character conventionally used by the operating system to separate search
1915 path components (as in :envvar:`PATH`), such as ``':'`` for POSIX or ``';'`` for
1916 Windows. Also available via :mod:`os.path`.
1917
1918
1919.. data:: defpath
1920
1921 The default search path used by :func:`exec\*p\*` and :func:`spawn\*p\*` if the
1922 environment doesn't have a ``'PATH'`` key. Also available via :mod:`os.path`.
1923
1924
1925.. data:: linesep
1926
1927 The string used to separate (or, rather, terminate) lines on the current
Georg Brandlc575c902008-09-13 17:46:05 +00001928 platform. This may be a single character, such as ``'\n'`` for POSIX, or
1929 multiple characters, for example, ``'\r\n'`` for Windows. Do not use
1930 *os.linesep* as a line terminator when writing files opened in text mode (the
1931 default); use a single ``'\n'`` instead, on all platforms.
Georg Brandl116aa622007-08-15 14:28:22 +00001932
1933
1934.. data:: devnull
1935
Georg Brandlc575c902008-09-13 17:46:05 +00001936 The file path of the null device. For example: ``'/dev/null'`` for POSIX.
1937 Also available via :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00001938
Georg Brandl116aa622007-08-15 14:28:22 +00001939
1940.. _os-miscfunc:
1941
1942Miscellaneous Functions
1943-----------------------
1944
1945
1946.. function:: urandom(n)
1947
1948 Return a string of *n* random bytes suitable for cryptographic use.
1949
1950 This function returns random bytes from an OS-specific randomness source. The
1951 returned data should be unpredictable enough for cryptographic applications,
1952 though its exact quality depends on the OS implementation. On a UNIX-like
1953 system this will query /dev/urandom, and on Windows it will use CryptGenRandom.
1954 If a randomness source is not found, :exc:`NotImplementedError` will be raised.