blob: 05471549bb4c5f0ac97e544f9e0dc04e45a55111 [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
440
441.. function:: close(fd)
442
Georg Brandlc575c902008-09-13 17:46:05 +0000443 Close file descriptor *fd*. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000444
445 .. note::
446
447 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000448 descriptor as returned by :func:`os.open` or :func:`pipe`. To close a "file
Georg Brandl116aa622007-08-15 14:28:22 +0000449 object" returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000450 :func:`fdopen`, use its :meth:`~file.close` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000451
452
Christian Heimesfdab48e2008-01-20 09:06:41 +0000453.. function:: closerange(fd_low, fd_high)
454
455 Close all file descriptors from *fd_low* (inclusive) to *fd_high* (exclusive),
Georg Brandlc575c902008-09-13 17:46:05 +0000456 ignoring errors. Availability: Unix, Windows. Equivalent to::
Christian Heimesfdab48e2008-01-20 09:06:41 +0000457
Georg Brandlc9a5a0e2009-09-01 07:34:27 +0000458 for fd in range(fd_low, fd_high):
Christian Heimesfdab48e2008-01-20 09:06:41 +0000459 try:
460 os.close(fd)
461 except OSError:
462 pass
463
Christian Heimesfdab48e2008-01-20 09:06:41 +0000464
Georg Brandl81f11302007-12-21 08:45:42 +0000465.. function:: device_encoding(fd)
466
467 Return a string describing the encoding of the device associated with *fd*
468 if it is connected to a terminal; else return :const:`None`.
469
470
Georg Brandl116aa622007-08-15 14:28:22 +0000471.. function:: dup(fd)
472
Georg Brandlc575c902008-09-13 17:46:05 +0000473 Return a duplicate of file descriptor *fd*. Availability: Unix,
Georg Brandl116aa622007-08-15 14:28:22 +0000474 Windows.
475
476
477.. function:: dup2(fd, fd2)
478
479 Duplicate file descriptor *fd* to *fd2*, closing the latter first if necessary.
Georg Brandlc575c902008-09-13 17:46:05 +0000480 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000481
482
Christian Heimes4e30a842007-11-30 22:12:06 +0000483.. function:: fchmod(fd, mode)
484
485 Change the mode of the file given by *fd* to the numeric *mode*. See the docs
486 for :func:`chmod` for possible values of *mode*. Availability: Unix.
487
488
489.. function:: fchown(fd, uid, gid)
490
491 Change the owner and group id of the file given by *fd* to the numeric *uid*
492 and *gid*. To leave one of the ids unchanged, set it to -1.
493 Availability: Unix.
494
495
Georg Brandl116aa622007-08-15 14:28:22 +0000496.. function:: fdatasync(fd)
497
498 Force write of file with filedescriptor *fd* to disk. Does not force update of
499 metadata. Availability: Unix.
500
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000501 .. note::
502 This function is not available on MacOS.
503
Georg Brandl116aa622007-08-15 14:28:22 +0000504
505.. function:: fpathconf(fd, name)
506
507 Return system configuration information relevant to an open file. *name*
508 specifies the configuration value to retrieve; it may be a string which is the
509 name of a defined system value; these names are specified in a number of
510 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
511 additional names as well. The names known to the host operating system are
512 given in the ``pathconf_names`` dictionary. For configuration variables not
513 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandlc575c902008-09-13 17:46:05 +0000514 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000515
516 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
517 specific value for *name* is not supported by the host system, even if it is
518 included in ``pathconf_names``, an :exc:`OSError` is raised with
519 :const:`errno.EINVAL` for the error number.
520
521
522.. function:: fstat(fd)
523
524 Return status for file descriptor *fd*, like :func:`stat`. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +0000525 Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000526
527
528.. function:: fstatvfs(fd)
529
530 Return information about the filesystem containing the file associated with file
531 descriptor *fd*, like :func:`statvfs`. Availability: Unix.
532
533
534.. function:: fsync(fd)
535
536 Force write of file with filedescriptor *fd* to disk. On Unix, this calls the
537 native :cfunc:`fsync` function; on Windows, the MS :cfunc:`_commit` function.
538
539 If you're starting with a Python file object *f*, first do ``f.flush()``, and
540 then do ``os.fsync(f.fileno())``, to ensure that all internal buffers associated
Georg Brandlc575c902008-09-13 17:46:05 +0000541 with *f* are written to disk. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000542
543
544.. function:: ftruncate(fd, length)
545
546 Truncate the file corresponding to file descriptor *fd*, so that it is at most
Georg Brandlc575c902008-09-13 17:46:05 +0000547 *length* bytes in size. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000548
549
550.. function:: isatty(fd)
551
552 Return ``True`` if the file descriptor *fd* is open and connected to a
Georg Brandlc575c902008-09-13 17:46:05 +0000553 tty(-like) device, else ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000554
555
556.. function:: lseek(fd, pos, how)
557
Christian Heimesfaf2f632008-01-06 16:59:19 +0000558 Set the current position of file descriptor *fd* to position *pos*, modified
559 by *how*: :const:`SEEK_SET` or ``0`` to set the position relative to the
560 beginning of the file; :const:`SEEK_CUR` or ``1`` to set it relative to the
561 current position; :const:`os.SEEK_END` or ``2`` to set it relative to the end of
Georg Brandlc575c902008-09-13 17:46:05 +0000562 the file. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000563
564
565.. function:: open(file, flags[, mode])
566
Georg Brandlf4a41232008-05-26 17:55:52 +0000567 Open the file *file* and set various flags according to *flags* and possibly
568 its mode according to *mode*. The default *mode* is ``0o777`` (octal), and
569 the current umask value is first masked out. Return the file descriptor for
Georg Brandlc575c902008-09-13 17:46:05 +0000570 the newly opened file. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000571
572 For a description of the flag and mode values, see the C run-time documentation;
573 flag constants (like :const:`O_RDONLY` and :const:`O_WRONLY`) are defined in
574 this module too (see below).
575
576 .. note::
577
Georg Brandl502d9a52009-07-26 15:02:41 +0000578 This function is intended for low-level I/O. For normal usage, use the
579 built-in function :func:`open`, which returns a "file object" with
580 :meth:`~file.read` and :meth:`~file.write` methods (and many more). To
581 wrap a file descriptor in a "file object", use :func:`fdopen`.
Georg Brandl116aa622007-08-15 14:28:22 +0000582
583
584.. function:: openpty()
585
586 .. index:: module: pty
587
588 Open a new pseudo-terminal pair. Return a pair of file descriptors ``(master,
589 slave)`` for the pty and the tty, respectively. For a (slightly) more portable
Georg Brandlc575c902008-09-13 17:46:05 +0000590 approach, use the :mod:`pty` module. Availability: some flavors of
Georg Brandl116aa622007-08-15 14:28:22 +0000591 Unix.
592
593
594.. function:: pipe()
595
596 Create a pipe. Return a pair of file descriptors ``(r, w)`` usable for reading
Georg Brandlc575c902008-09-13 17:46:05 +0000597 and writing, respectively. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000598
599
600.. function:: read(fd, n)
601
Georg Brandlb90be692009-07-29 16:14:16 +0000602 Read at most *n* bytes from file descriptor *fd*. Return a bytestring containing the
Georg Brandl116aa622007-08-15 14:28:22 +0000603 bytes read. If the end of the file referred to by *fd* has been reached, an
Georg Brandlb90be692009-07-29 16:14:16 +0000604 empty bytes object is returned. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000605
606 .. note::
607
608 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000609 descriptor as returned by :func:`os.open` or :func:`pipe`. To read a "file object"
Georg Brandl116aa622007-08-15 14:28:22 +0000610 returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000611 :func:`fdopen`, or :data:`sys.stdin`, use its :meth:`~file.read` or
612 :meth:`~file.readline` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000613
614
615.. function:: tcgetpgrp(fd)
616
617 Return the process group associated with the terminal given by *fd* (an open
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000618 file descriptor as returned by :func:`os.open`). Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000619
620
621.. function:: tcsetpgrp(fd, pg)
622
623 Set the process group associated with the terminal given by *fd* (an open file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000624 descriptor as returned by :func:`os.open`) to *pg*. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000625
626
627.. function:: ttyname(fd)
628
629 Return a string which specifies the terminal device associated with
Georg Brandl9afde1c2007-11-01 20:32:30 +0000630 file descriptor *fd*. If *fd* is not associated with a terminal device, an
Georg Brandlc575c902008-09-13 17:46:05 +0000631 exception is raised. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000632
633
634.. function:: write(fd, str)
635
Georg Brandlb90be692009-07-29 16:14:16 +0000636 Write the bytestring in *str* to file descriptor *fd*. Return the number of
637 bytes actually written. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000638
639 .. note::
640
641 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000642 descriptor as returned by :func:`os.open` or :func:`pipe`. To write a "file
Georg Brandl116aa622007-08-15 14:28:22 +0000643 object" returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000644 :func:`fdopen`, or :data:`sys.stdout` or :data:`sys.stderr`, use its
645 :meth:`~file.write` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000646
Georg Brandlaf265f42008-12-07 15:06:20 +0000647The following constants are options for the *flags* parameter to the
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000648:func:`~os.open` function. They can be combined using the bitwise OR operator
Georg Brandlaf265f42008-12-07 15:06:20 +0000649``|``. Some of them are not available on all platforms. For descriptions of
650their availability and use, consult the :manpage:`open(2)` manual page on Unix
Doug Hellmanneb097fc2009-09-20 20:56:56 +0000651or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000652
653
654.. data:: O_RDONLY
655 O_WRONLY
656 O_RDWR
657 O_APPEND
658 O_CREAT
659 O_EXCL
660 O_TRUNC
661
Georg Brandlaf265f42008-12-07 15:06:20 +0000662 These constants are available on Unix and Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000663
664
665.. data:: O_DSYNC
666 O_RSYNC
667 O_SYNC
668 O_NDELAY
669 O_NONBLOCK
670 O_NOCTTY
671 O_SHLOCK
672 O_EXLOCK
673
Georg Brandlaf265f42008-12-07 15:06:20 +0000674 These constants are only available on Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000675
676
677.. data:: O_BINARY
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000678 O_NOINHERIT
Georg Brandl116aa622007-08-15 14:28:22 +0000679 O_SHORT_LIVED
680 O_TEMPORARY
681 O_RANDOM
682 O_SEQUENTIAL
683 O_TEXT
684
Georg Brandlaf265f42008-12-07 15:06:20 +0000685 These constants are only available on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000686
687
Alexandre Vassalottibee32532008-05-16 18:15:12 +0000688.. data:: O_ASYNC
689 O_DIRECT
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000690 O_DIRECTORY
691 O_NOFOLLOW
692 O_NOATIME
693
Georg Brandlaf265f42008-12-07 15:06:20 +0000694 These constants are GNU extensions and not present if they are not defined by
695 the C library.
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000696
697
Georg Brandl116aa622007-08-15 14:28:22 +0000698.. data:: SEEK_SET
699 SEEK_CUR
700 SEEK_END
701
702 Parameters to the :func:`lseek` function. Their values are 0, 1, and 2,
Georg Brandlc575c902008-09-13 17:46:05 +0000703 respectively. Availability: Windows, Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000704
Georg Brandl116aa622007-08-15 14:28:22 +0000705
706.. _os-file-dir:
707
708Files and Directories
709---------------------
710
Georg Brandl116aa622007-08-15 14:28:22 +0000711.. function:: access(path, mode)
712
713 Use the real uid/gid to test for access to *path*. Note that most operations
714 will use the effective uid/gid, therefore this routine can be used in a
715 suid/sgid environment to test if the invoking user has the specified access to
716 *path*. *mode* should be :const:`F_OK` to test the existence of *path*, or it
717 can be the inclusive OR of one or more of :const:`R_OK`, :const:`W_OK`, and
718 :const:`X_OK` to test permissions. Return :const:`True` if access is allowed,
719 :const:`False` if not. See the Unix man page :manpage:`access(2)` for more
Georg Brandlc575c902008-09-13 17:46:05 +0000720 information. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000721
722 .. note::
723
Georg Brandl502d9a52009-07-26 15:02:41 +0000724 Using :func:`access` to check if a user is authorized to e.g. open a file
725 before actually doing so using :func:`open` creates a security hole,
726 because the user might exploit the short time interval between checking
727 and opening the file to manipulate it.
Georg Brandl116aa622007-08-15 14:28:22 +0000728
729 .. note::
730
731 I/O operations may fail even when :func:`access` indicates that they would
732 succeed, particularly for operations on network filesystems which may have
733 permissions semantics beyond the usual POSIX permission-bit model.
734
735
736.. data:: F_OK
737
738 Value to pass as the *mode* parameter of :func:`access` to test the existence of
739 *path*.
740
741
742.. data:: R_OK
743
744 Value to include in the *mode* parameter of :func:`access` to test the
745 readability of *path*.
746
747
748.. data:: W_OK
749
750 Value to include in the *mode* parameter of :func:`access` to test the
751 writability of *path*.
752
753
754.. data:: X_OK
755
756 Value to include in the *mode* parameter of :func:`access` to determine if
757 *path* can be executed.
758
759
760.. function:: chdir(path)
761
762 .. index:: single: directory; changing
763
Georg Brandlc575c902008-09-13 17:46:05 +0000764 Change the current working directory to *path*. Availability: Unix,
Georg Brandl116aa622007-08-15 14:28:22 +0000765 Windows.
766
767
768.. function:: fchdir(fd)
769
770 Change the current working directory to the directory represented by the file
771 descriptor *fd*. The descriptor must refer to an opened directory, not an open
772 file. Availability: Unix.
773
Georg Brandl116aa622007-08-15 14:28:22 +0000774
775.. function:: getcwd()
776
Martin v. Löwis011e8422009-05-05 04:43:17 +0000777 Return a string representing the current working directory.
778 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000779
Martin v. Löwisa731b992008-10-07 06:36:31 +0000780.. function:: getcwdb()
Georg Brandl116aa622007-08-15 14:28:22 +0000781
Georg Brandl76e55382008-10-08 16:34:57 +0000782 Return a bytestring representing the current working directory.
Georg Brandlc575c902008-09-13 17:46:05 +0000783 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000784
Georg Brandl116aa622007-08-15 14:28:22 +0000785
786.. function:: chflags(path, flags)
787
788 Set the flags of *path* to the numeric *flags*. *flags* may take a combination
789 (bitwise OR) of the following values (as defined in the :mod:`stat` module):
790
791 * ``UF_NODUMP``
792 * ``UF_IMMUTABLE``
793 * ``UF_APPEND``
794 * ``UF_OPAQUE``
795 * ``UF_NOUNLINK``
796 * ``SF_ARCHIVED``
797 * ``SF_IMMUTABLE``
798 * ``SF_APPEND``
799 * ``SF_NOUNLINK``
800 * ``SF_SNAPSHOT``
801
Georg Brandlc575c902008-09-13 17:46:05 +0000802 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000803
Georg Brandl116aa622007-08-15 14:28:22 +0000804
805.. function:: chroot(path)
806
807 Change the root directory of the current process to *path*. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +0000808 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000809
Georg Brandl116aa622007-08-15 14:28:22 +0000810
811.. function:: chmod(path, mode)
812
813 Change the mode of *path* to the numeric *mode*. *mode* may take one of the
Christian Heimesfaf2f632008-01-06 16:59:19 +0000814 following values (as defined in the :mod:`stat` module) or bitwise ORed
Georg Brandl116aa622007-08-15 14:28:22 +0000815 combinations of them:
816
Alexandre Vassalottic22c6f22009-07-21 00:51:58 +0000817 * :data:`stat.S_ISUID`
818 * :data:`stat.S_ISGID`
819 * :data:`stat.S_ENFMT`
820 * :data:`stat.S_ISVTX`
821 * :data:`stat.S_IREAD`
822 * :data:`stat.S_IWRITE`
823 * :data:`stat.S_IEXEC`
824 * :data:`stat.S_IRWXU`
825 * :data:`stat.S_IRUSR`
826 * :data:`stat.S_IWUSR`
827 * :data:`stat.S_IXUSR`
828 * :data:`stat.S_IRWXG`
829 * :data:`stat.S_IRGRP`
830 * :data:`stat.S_IWGRP`
831 * :data:`stat.S_IXGRP`
832 * :data:`stat.S_IRWXO`
833 * :data:`stat.S_IROTH`
834 * :data:`stat.S_IWOTH`
835 * :data:`stat.S_IXOTH`
Georg Brandl116aa622007-08-15 14:28:22 +0000836
Georg Brandlc575c902008-09-13 17:46:05 +0000837 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000838
839 .. note::
840
841 Although Windows supports :func:`chmod`, you can only set the file's read-only
842 flag with it (via the ``stat.S_IWRITE`` and ``stat.S_IREAD``
843 constants or a corresponding integer value). All other bits are
844 ignored.
845
846
847.. function:: chown(path, uid, gid)
848
849 Change the owner and group id of *path* to the numeric *uid* and *gid*. To leave
Georg Brandlc575c902008-09-13 17:46:05 +0000850 one of the ids unchanged, set it to -1. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000851
852
853.. function:: lchflags(path, flags)
854
855 Set the flags of *path* to the numeric *flags*, like :func:`chflags`, but do not
856 follow symbolic links. Availability: Unix.
857
Georg Brandl116aa622007-08-15 14:28:22 +0000858
Christian Heimes93852662007-12-01 12:22:32 +0000859.. function:: lchmod(path, mode)
860
861 Change the mode of *path* to the numeric *mode*. If path is a symlink, this
862 affects the symlink rather than the target. See the docs for :func:`chmod`
863 for possible values of *mode*. Availability: Unix.
864
Christian Heimes93852662007-12-01 12:22:32 +0000865
Georg Brandl116aa622007-08-15 14:28:22 +0000866.. function:: lchown(path, uid, gid)
867
Christian Heimesfaf2f632008-01-06 16:59:19 +0000868 Change the owner and group id of *path* to the numeric *uid* and *gid*. This
Georg Brandlc575c902008-09-13 17:46:05 +0000869 function will not follow symbolic links. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000870
Georg Brandl116aa622007-08-15 14:28:22 +0000871
Benjamin Peterson5879d412009-03-30 14:51:56 +0000872.. function:: link(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +0000873
Benjamin Peterson5879d412009-03-30 14:51:56 +0000874 Create a hard link pointing to *source* named *link_name*. Availability:
875 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000876
877
878.. function:: listdir(path)
879
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000880 Return a list containing the names of the entries in the directory given by
881 *path*. The list is in arbitrary order. It does not include the special
882 entries ``'.'`` and ``'..'`` even if they are present in the directory.
883 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000884
Martin v. Löwis011e8422009-05-05 04:43:17 +0000885 This function can be called with a bytes or string argument, and returns
886 filenames of the same datatype.
Georg Brandl116aa622007-08-15 14:28:22 +0000887
888
889.. function:: lstat(path)
890
Guido van Rossum2cc30da2007-11-02 23:46:40 +0000891 Like :func:`stat`, but do not follow symbolic links. This is an alias for
892 :func:`stat` on platforms that do not support symbolic links, such as
893 Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000894
895
896.. function:: mkfifo(path[, mode])
897
Georg Brandlf4a41232008-05-26 17:55:52 +0000898 Create a FIFO (a named pipe) named *path* with numeric mode *mode*. The
899 default *mode* is ``0o666`` (octal). The current umask value is first masked
Georg Brandlc575c902008-09-13 17:46:05 +0000900 out from the mode. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000901
902 FIFOs are pipes that can be accessed like regular files. FIFOs exist until they
903 are deleted (for example with :func:`os.unlink`). Generally, FIFOs are used as
904 rendezvous between "client" and "server" type processes: the server opens the
905 FIFO for reading, and the client opens it for writing. Note that :func:`mkfifo`
906 doesn't open the FIFO --- it just creates the rendezvous point.
907
908
Georg Brandl18244152009-09-02 20:34:52 +0000909.. function:: mknod(filename[, mode=0o600[, device]])
Georg Brandl116aa622007-08-15 14:28:22 +0000910
911 Create a filesystem node (file, device special file or named pipe) named
Georg Brandl18244152009-09-02 20:34:52 +0000912 *filename*. *mode* specifies both the permissions to use and the type of node
913 to be created, being combined (bitwise OR) with one of ``stat.S_IFREG``,
914 ``stat.S_IFCHR``, ``stat.S_IFBLK``, and ``stat.S_IFIFO`` (those constants are
915 available in :mod:`stat`). For ``stat.S_IFCHR`` and ``stat.S_IFBLK``,
916 *device* defines the newly created device special file (probably using
Georg Brandl116aa622007-08-15 14:28:22 +0000917 :func:`os.makedev`), otherwise it is ignored.
918
Georg Brandl116aa622007-08-15 14:28:22 +0000919
920.. function:: major(device)
921
Christian Heimesfaf2f632008-01-06 16:59:19 +0000922 Extract the device major number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000923 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
924
Georg Brandl116aa622007-08-15 14:28:22 +0000925
926.. function:: minor(device)
927
Christian Heimesfaf2f632008-01-06 16:59:19 +0000928 Extract the device minor number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000929 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
930
Georg Brandl116aa622007-08-15 14:28:22 +0000931
932.. function:: makedev(major, minor)
933
Christian Heimesfaf2f632008-01-06 16:59:19 +0000934 Compose a raw device number from the major and minor device numbers.
Georg Brandl116aa622007-08-15 14:28:22 +0000935
Georg Brandl116aa622007-08-15 14:28:22 +0000936
937.. function:: mkdir(path[, mode])
938
Georg Brandlf4a41232008-05-26 17:55:52 +0000939 Create a directory named *path* with numeric mode *mode*. The default *mode*
940 is ``0o777`` (octal). On some systems, *mode* is ignored. Where it is used,
Georg Brandlc575c902008-09-13 17:46:05 +0000941 the current umask value is first masked out. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000942
Guido van Rossum2cc30da2007-11-02 23:46:40 +0000943 It is also possible to create temporary directories; see the
944 :mod:`tempfile` module's :func:`tempfile.mkdtemp` function.
945
Georg Brandl116aa622007-08-15 14:28:22 +0000946
947.. function:: makedirs(path[, mode])
948
949 .. index::
950 single: directory; creating
951 single: UNC paths; and os.makedirs()
952
953 Recursive directory creation function. Like :func:`mkdir`, but makes all
Georg Brandlf4a41232008-05-26 17:55:52 +0000954 intermediate-level directories needed to contain the leaf directory. Throws
955 an :exc:`error` exception if the leaf directory already exists or cannot be
956 created. The default *mode* is ``0o777`` (octal). On some systems, *mode*
957 is ignored. Where it is used, the current umask value is first masked out.
Georg Brandl116aa622007-08-15 14:28:22 +0000958
959 .. note::
960
961 :func:`makedirs` will become confused if the path elements to create include
Christian Heimesfaf2f632008-01-06 16:59:19 +0000962 :data:`os.pardir`.
Georg Brandl116aa622007-08-15 14:28:22 +0000963
Georg Brandl55ac8f02007-09-01 13:51:09 +0000964 This function handles UNC paths correctly.
Georg Brandl116aa622007-08-15 14:28:22 +0000965
966
967.. function:: pathconf(path, name)
968
969 Return system configuration information relevant to a named file. *name*
970 specifies the configuration value to retrieve; it may be a string which is the
971 name of a defined system value; these names are specified in a number of
972 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
973 additional names as well. The names known to the host operating system are
974 given in the ``pathconf_names`` dictionary. For configuration variables not
975 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandlc575c902008-09-13 17:46:05 +0000976 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000977
978 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
979 specific value for *name* is not supported by the host system, even if it is
980 included in ``pathconf_names``, an :exc:`OSError` is raised with
981 :const:`errno.EINVAL` for the error number.
982
983
984.. data:: pathconf_names
985
986 Dictionary mapping names accepted by :func:`pathconf` and :func:`fpathconf` to
987 the integer values defined for those names by the host operating system. This
988 can be used to determine the set of names known to the system. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +0000989 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000990
991
992.. function:: readlink(path)
993
994 Return a string representing the path to which the symbolic link points. The
995 result may be either an absolute or relative pathname; if it is relative, it may
996 be converted to an absolute pathname using ``os.path.join(os.path.dirname(path),
997 result)``.
998
Georg Brandl76e55382008-10-08 16:34:57 +0000999 If the *path* is a string object, the result will also be a string object,
1000 and the call may raise an UnicodeDecodeError. If the *path* is a bytes
1001 object, the result will be a bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +00001002
Georg Brandlc575c902008-09-13 17:46:05 +00001003 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001004
1005
1006.. function:: remove(path)
1007
Georg Brandla6053b42009-09-01 08:11:14 +00001008 Remove (delete) the file *path*. If *path* is a directory, :exc:`OSError` is
1009 raised; see :func:`rmdir` below to remove a directory. This is identical to
1010 the :func:`unlink` function documented below. On Windows, attempting to
1011 remove a file that is in use causes an exception to be raised; on Unix, the
1012 directory entry is removed but the storage allocated to the file is not made
1013 available until the original file is no longer in use. Availability: Unix,
Georg Brandl116aa622007-08-15 14:28:22 +00001014 Windows.
1015
1016
1017.. function:: removedirs(path)
1018
1019 .. index:: single: directory; deleting
1020
Christian Heimesfaf2f632008-01-06 16:59:19 +00001021 Remove directories recursively. Works like :func:`rmdir` except that, if the
Georg Brandl116aa622007-08-15 14:28:22 +00001022 leaf directory is successfully removed, :func:`removedirs` tries to
1023 successively remove every parent directory mentioned in *path* until an error
1024 is raised (which is ignored, because it generally means that a parent directory
1025 is not empty). For example, ``os.removedirs('foo/bar/baz')`` will first remove
1026 the directory ``'foo/bar/baz'``, and then remove ``'foo/bar'`` and ``'foo'`` if
1027 they are empty. Raises :exc:`OSError` if the leaf directory could not be
1028 successfully removed.
1029
Georg Brandl116aa622007-08-15 14:28:22 +00001030
1031.. function:: rename(src, dst)
1032
1033 Rename the file or directory *src* to *dst*. If *dst* is a directory,
1034 :exc:`OSError` will be raised. On Unix, if *dst* exists and is a file, it will
Christian Heimesfaf2f632008-01-06 16:59:19 +00001035 be replaced silently if the user has permission. The operation may fail on some
Georg Brandl116aa622007-08-15 14:28:22 +00001036 Unix flavors if *src* and *dst* are on different filesystems. If successful,
1037 the renaming will be an atomic operation (this is a POSIX requirement). On
1038 Windows, if *dst* already exists, :exc:`OSError` will be raised even if it is a
1039 file; there may be no way to implement an atomic rename when *dst* names an
Georg Brandlc575c902008-09-13 17:46:05 +00001040 existing file. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001041
1042
1043.. function:: renames(old, new)
1044
1045 Recursive directory or file renaming function. Works like :func:`rename`, except
1046 creation of any intermediate directories needed to make the new pathname good is
1047 attempted first. After the rename, directories corresponding to rightmost path
1048 segments of the old name will be pruned away using :func:`removedirs`.
1049
Georg Brandl116aa622007-08-15 14:28:22 +00001050 .. note::
1051
1052 This function can fail with the new directory structure made if you lack
1053 permissions needed to remove the leaf directory or file.
1054
1055
1056.. function:: rmdir(path)
1057
Georg Brandla6053b42009-09-01 08:11:14 +00001058 Remove (delete) the directory *path*. Only works when the directory is
1059 empty, otherwise, :exc:`OSError` is raised. In order to remove whole
1060 directory trees, :func:`shutil.rmtree` can be used. Availability: Unix,
1061 Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001062
1063
1064.. function:: stat(path)
1065
1066 Perform a :cfunc:`stat` system call on the given path. The return value is an
1067 object whose attributes correspond to the members of the :ctype:`stat`
1068 structure, namely: :attr:`st_mode` (protection bits), :attr:`st_ino` (inode
1069 number), :attr:`st_dev` (device), :attr:`st_nlink` (number of hard links),
Christian Heimesfaf2f632008-01-06 16:59:19 +00001070 :attr:`st_uid` (user id of owner), :attr:`st_gid` (group id of owner),
Georg Brandl116aa622007-08-15 14:28:22 +00001071 :attr:`st_size` (size of file, in bytes), :attr:`st_atime` (time of most recent
1072 access), :attr:`st_mtime` (time of most recent content modification),
1073 :attr:`st_ctime` (platform dependent; time of most recent metadata change on
1074 Unix, or the time of creation on Windows)::
1075
1076 >>> import os
1077 >>> statinfo = os.stat('somefile.txt')
1078 >>> statinfo
Georg Brandlf66df2b2010-01-16 14:41:21 +00001079 (33188, 422511, 769, 1, 1032, 100, 926, 1105022698,1105022732, 1105022732)
Georg Brandl116aa622007-08-15 14:28:22 +00001080 >>> statinfo.st_size
Georg Brandlf66df2b2010-01-16 14:41:21 +00001081 926
Georg Brandl116aa622007-08-15 14:28:22 +00001082 >>>
1083
Georg Brandl116aa622007-08-15 14:28:22 +00001084
1085 On some Unix systems (such as Linux), the following attributes may also be
1086 available: :attr:`st_blocks` (number of blocks allocated for file),
1087 :attr:`st_blksize` (filesystem blocksize), :attr:`st_rdev` (type of device if an
1088 inode device). :attr:`st_flags` (user defined flags for file).
1089
1090 On other Unix systems (such as FreeBSD), the following attributes may be
1091 available (but may be only filled out if root tries to use them): :attr:`st_gen`
1092 (file generation number), :attr:`st_birthtime` (time of file creation).
1093
1094 On Mac OS systems, the following attributes may also be available:
1095 :attr:`st_rsize`, :attr:`st_creator`, :attr:`st_type`.
1096
Georg Brandl116aa622007-08-15 14:28:22 +00001097 .. index:: module: stat
1098
1099 For backward compatibility, the return value of :func:`stat` is also accessible
1100 as a tuple of at least 10 integers giving the most important (and portable)
1101 members of the :ctype:`stat` structure, in the order :attr:`st_mode`,
1102 :attr:`st_ino`, :attr:`st_dev`, :attr:`st_nlink`, :attr:`st_uid`,
1103 :attr:`st_gid`, :attr:`st_size`, :attr:`st_atime`, :attr:`st_mtime`,
1104 :attr:`st_ctime`. More items may be added at the end by some implementations.
1105 The standard module :mod:`stat` defines functions and constants that are useful
1106 for extracting information from a :ctype:`stat` structure. (On Windows, some
1107 items are filled with dummy values.)
1108
1109 .. note::
1110
1111 The exact meaning and resolution of the :attr:`st_atime`, :attr:`st_mtime`, and
1112 :attr:`st_ctime` members depends on the operating system and the file system.
1113 For example, on Windows systems using the FAT or FAT32 file systems,
1114 :attr:`st_mtime` has 2-second resolution, and :attr:`st_atime` has only 1-day
1115 resolution. See your operating system documentation for details.
1116
Georg Brandlc575c902008-09-13 17:46:05 +00001117 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001118
Georg Brandl116aa622007-08-15 14:28:22 +00001119
1120.. function:: stat_float_times([newvalue])
1121
1122 Determine whether :class:`stat_result` represents time stamps as float objects.
1123 If *newvalue* is ``True``, future calls to :func:`stat` return floats, if it is
1124 ``False``, future calls return ints. If *newvalue* is omitted, return the
1125 current setting.
1126
1127 For compatibility with older Python versions, accessing :class:`stat_result` as
1128 a tuple always returns integers.
1129
Georg Brandl55ac8f02007-09-01 13:51:09 +00001130 Python now returns float values by default. Applications which do not work
1131 correctly with floating point time stamps can use this function to restore the
1132 old behaviour.
Georg Brandl116aa622007-08-15 14:28:22 +00001133
1134 The resolution of the timestamps (that is the smallest possible fraction)
1135 depends on the system. Some systems only support second resolution; on these
1136 systems, the fraction will always be zero.
1137
1138 It is recommended that this setting is only changed at program startup time in
1139 the *__main__* module; libraries should never change this setting. If an
1140 application uses a library that works incorrectly if floating point time stamps
1141 are processed, this application should turn the feature off until the library
1142 has been corrected.
1143
1144
1145.. function:: statvfs(path)
1146
1147 Perform a :cfunc:`statvfs` system call on the given path. The return value is
1148 an object whose attributes describe the filesystem on the given path, and
1149 correspond to the members of the :ctype:`statvfs` structure, namely:
1150 :attr:`f_bsize`, :attr:`f_frsize`, :attr:`f_blocks`, :attr:`f_bfree`,
1151 :attr:`f_bavail`, :attr:`f_files`, :attr:`f_ffree`, :attr:`f_favail`,
1152 :attr:`f_flag`, :attr:`f_namemax`. Availability: Unix.
1153
Georg Brandl116aa622007-08-15 14:28:22 +00001154
Benjamin Peterson5879d412009-03-30 14:51:56 +00001155.. function:: symlink(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +00001156
Benjamin Peterson5879d412009-03-30 14:51:56 +00001157 Create a symbolic link pointing to *source* named *link_name*. Availability:
1158 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001159
1160
Georg Brandl116aa622007-08-15 14:28:22 +00001161.. function:: unlink(path)
1162
Georg Brandla6053b42009-09-01 08:11:14 +00001163 Remove (delete) the file *path*. This is the same function as
1164 :func:`remove`; the :func:`unlink` name is its traditional Unix
1165 name. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001166
1167
1168.. function:: utime(path, times)
1169
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001170 Set the access and modified times of the file specified by *path*. If *times*
1171 is ``None``, then the file's access and modified times are set to the current
1172 time. (The effect is similar to running the Unix program :program:`touch` on
1173 the path.) Otherwise, *times* must be a 2-tuple of numbers, of the form
1174 ``(atime, mtime)`` which is used to set the access and modified times,
1175 respectively. Whether a directory can be given for *path* depends on whether
1176 the operating system implements directories as files (for example, Windows
1177 does not). Note that the exact times you set here may not be returned by a
1178 subsequent :func:`stat` call, depending on the resolution with which your
1179 operating system records access and modification times; see :func:`stat`.
Georg Brandl116aa622007-08-15 14:28:22 +00001180
Georg Brandlc575c902008-09-13 17:46:05 +00001181 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001182
1183
Georg Brandl18244152009-09-02 20:34:52 +00001184.. function:: walk(top, topdown=True, onerror=None, followlinks=False)
Georg Brandl116aa622007-08-15 14:28:22 +00001185
1186 .. index::
1187 single: directory; walking
1188 single: directory; traversal
1189
Christian Heimesfaf2f632008-01-06 16:59:19 +00001190 Generate the file names in a directory tree by walking the tree
1191 either top-down or bottom-up. For each directory in the tree rooted at directory
Georg Brandl116aa622007-08-15 14:28:22 +00001192 *top* (including *top* itself), it yields a 3-tuple ``(dirpath, dirnames,
1193 filenames)``.
1194
1195 *dirpath* is a string, the path to the directory. *dirnames* is a list of the
1196 names of the subdirectories in *dirpath* (excluding ``'.'`` and ``'..'``).
1197 *filenames* is a list of the names of the non-directory files in *dirpath*.
1198 Note that the names in the lists contain no path components. To get a full path
1199 (which begins with *top*) to a file or directory in *dirpath*, do
1200 ``os.path.join(dirpath, name)``.
1201
Christian Heimesfaf2f632008-01-06 16:59:19 +00001202 If optional argument *topdown* is ``True`` or not specified, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001203 directory is generated before the triples for any of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001204 (directories are generated top-down). If *topdown* is ``False``, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001205 directory is generated after the triples for all of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001206 (directories are generated bottom-up).
Georg Brandl116aa622007-08-15 14:28:22 +00001207
Christian Heimesfaf2f632008-01-06 16:59:19 +00001208 When *topdown* is ``True``, the caller can modify the *dirnames* list in-place
Georg Brandl116aa622007-08-15 14:28:22 +00001209 (perhaps using :keyword:`del` or slice assignment), and :func:`walk` will only
1210 recurse into the subdirectories whose names remain in *dirnames*; this can be
1211 used to prune the search, impose a specific order of visiting, or even to inform
1212 :func:`walk` about directories the caller creates or renames before it resumes
Christian Heimesfaf2f632008-01-06 16:59:19 +00001213 :func:`walk` again. Modifying *dirnames* when *topdown* is ``False`` is
Georg Brandl116aa622007-08-15 14:28:22 +00001214 ineffective, because in bottom-up mode the directories in *dirnames* are
1215 generated before *dirpath* itself is generated.
1216
Christian Heimesfaf2f632008-01-06 16:59:19 +00001217 By default errors from the :func:`listdir` call are ignored. If optional
Georg Brandl116aa622007-08-15 14:28:22 +00001218 argument *onerror* is specified, it should be a function; it will be called with
1219 one argument, an :exc:`OSError` instance. It can report the error to continue
1220 with the walk, or raise the exception to abort the walk. Note that the filename
1221 is available as the ``filename`` attribute of the exception object.
1222
1223 By default, :func:`walk` will not walk down into symbolic links that resolve to
Christian Heimesfaf2f632008-01-06 16:59:19 +00001224 directories. Set *followlinks* to ``True`` to visit directories pointed to by
Georg Brandl116aa622007-08-15 14:28:22 +00001225 symlinks, on systems that support them.
1226
Georg Brandl116aa622007-08-15 14:28:22 +00001227 .. note::
1228
Christian Heimesfaf2f632008-01-06 16:59:19 +00001229 Be aware that setting *followlinks* to ``True`` can lead to infinite recursion if a
Georg Brandl116aa622007-08-15 14:28:22 +00001230 link points to a parent directory of itself. :func:`walk` does not keep track of
1231 the directories it visited already.
1232
1233 .. note::
1234
1235 If you pass a relative pathname, don't change the current working directory
1236 between resumptions of :func:`walk`. :func:`walk` never changes the current
1237 directory, and assumes that its caller doesn't either.
1238
1239 This example displays the number of bytes taken by non-directory files in each
1240 directory under the starting directory, except that it doesn't look under any
1241 CVS subdirectory::
1242
1243 import os
1244 from os.path import join, getsize
1245 for root, dirs, files in os.walk('python/Lib/email'):
Georg Brandl6911e3c2007-09-04 07:15:32 +00001246 print(root, "consumes", end=" ")
1247 print(sum(getsize(join(root, name)) for name in files), end=" ")
1248 print("bytes in", len(files), "non-directory files")
Georg Brandl116aa622007-08-15 14:28:22 +00001249 if 'CVS' in dirs:
1250 dirs.remove('CVS') # don't visit CVS directories
1251
Christian Heimesfaf2f632008-01-06 16:59:19 +00001252 In the next example, walking the tree bottom-up is essential: :func:`rmdir`
Georg Brandl116aa622007-08-15 14:28:22 +00001253 doesn't allow deleting a directory before the directory is empty::
1254
Christian Heimesfaf2f632008-01-06 16:59:19 +00001255 # Delete everything reachable from the directory named in "top",
Georg Brandl116aa622007-08-15 14:28:22 +00001256 # assuming there are no symbolic links.
1257 # CAUTION: This is dangerous! For example, if top == '/', it
1258 # could delete all your disk files.
1259 import os
1260 for root, dirs, files in os.walk(top, topdown=False):
1261 for name in files:
1262 os.remove(os.path.join(root, name))
1263 for name in dirs:
1264 os.rmdir(os.path.join(root, name))
1265
Georg Brandl116aa622007-08-15 14:28:22 +00001266
1267.. _os-process:
1268
1269Process Management
1270------------------
1271
1272These functions may be used to create and manage processes.
1273
1274The various :func:`exec\*` functions take a list of arguments for the new
1275program loaded into the process. In each case, the first of these arguments is
1276passed to the new program as its own name rather than as an argument a user may
1277have typed on a command line. For the C programmer, this is the ``argv[0]``
1278passed to a program's :cfunc:`main`. For example, ``os.execv('/bin/echo',
1279['foo', 'bar'])`` will only print ``bar`` on standard output; ``foo`` will seem
1280to be ignored.
1281
1282
1283.. function:: abort()
1284
1285 Generate a :const:`SIGABRT` signal to the current process. On Unix, the default
1286 behavior is to produce a core dump; on Windows, the process immediately returns
1287 an exit code of ``3``. Be aware that programs which use :func:`signal.signal`
1288 to register a handler for :const:`SIGABRT` will behave differently.
Georg Brandlc575c902008-09-13 17:46:05 +00001289 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001290
1291
1292.. function:: execl(path, arg0, arg1, ...)
1293 execle(path, arg0, arg1, ..., env)
1294 execlp(file, arg0, arg1, ...)
1295 execlpe(file, arg0, arg1, ..., env)
1296 execv(path, args)
1297 execve(path, args, env)
1298 execvp(file, args)
1299 execvpe(file, args, env)
1300
1301 These functions all execute a new program, replacing the current process; they
1302 do not return. On Unix, the new executable is loaded into the current process,
Christian Heimesfaf2f632008-01-06 16:59:19 +00001303 and will have the same process id as the caller. Errors will be reported as
Georg Brandl48310cd2009-01-03 21:18:54 +00001304 :exc:`OSError` exceptions.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001305
1306 The current process is replaced immediately. Open file objects and
1307 descriptors are not flushed, so if there may be data buffered
1308 on these open files, you should flush them using
1309 :func:`sys.stdout.flush` or :func:`os.fsync` before calling an
1310 :func:`exec\*` function.
Georg Brandl116aa622007-08-15 14:28:22 +00001311
Christian Heimesfaf2f632008-01-06 16:59:19 +00001312 The "l" and "v" variants of the :func:`exec\*` functions differ in how
1313 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001314 to work with if the number of parameters is fixed when the code is written; the
1315 individual parameters simply become additional parameters to the :func:`execl\*`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001316 functions. The "v" variants are good when the number of parameters is
Georg Brandl116aa622007-08-15 14:28:22 +00001317 variable, with the arguments being passed in a list or tuple as the *args*
1318 parameter. In either case, the arguments to the child process should start with
1319 the name of the command being run, but this is not enforced.
1320
Christian Heimesfaf2f632008-01-06 16:59:19 +00001321 The variants which include a "p" near the end (:func:`execlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001322 :func:`execlpe`, :func:`execvp`, and :func:`execvpe`) will use the
1323 :envvar:`PATH` environment variable to locate the program *file*. When the
1324 environment is being replaced (using one of the :func:`exec\*e` variants,
1325 discussed in the next paragraph), the new environment is used as the source of
1326 the :envvar:`PATH` variable. The other variants, :func:`execl`, :func:`execle`,
1327 :func:`execv`, and :func:`execve`, will not use the :envvar:`PATH` variable to
1328 locate the executable; *path* must contain an appropriate absolute or relative
1329 path.
1330
1331 For :func:`execle`, :func:`execlpe`, :func:`execve`, and :func:`execvpe` (note
Christian Heimesfaf2f632008-01-06 16:59:19 +00001332 that these all end in "e"), the *env* parameter must be a mapping which is
Christian Heimesa342c012008-04-20 21:01:16 +00001333 used to define the environment variables for the new process (these are used
1334 instead of the current process' environment); the functions :func:`execl`,
Georg Brandl116aa622007-08-15 14:28:22 +00001335 :func:`execlp`, :func:`execv`, and :func:`execvp` all cause the new process to
Georg Brandl48310cd2009-01-03 21:18:54 +00001336 inherit the environment of the current process.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001337
1338 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001339
1340
1341.. function:: _exit(n)
1342
1343 Exit to the system with status *n*, without calling cleanup handlers, flushing
Georg Brandlc575c902008-09-13 17:46:05 +00001344 stdio buffers, etc. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001345
1346 .. note::
1347
1348 The standard way to exit is ``sys.exit(n)``. :func:`_exit` should normally only
1349 be used in the child process after a :func:`fork`.
1350
Christian Heimesfaf2f632008-01-06 16:59:19 +00001351The following exit codes are defined and can be used with :func:`_exit`,
Georg Brandl116aa622007-08-15 14:28:22 +00001352although they are not required. These are typically used for system programs
1353written in Python, such as a mail server's external command delivery program.
1354
1355.. note::
1356
1357 Some of these may not be available on all Unix platforms, since there is some
1358 variation. These constants are defined where they are defined by the underlying
1359 platform.
1360
1361
1362.. data:: EX_OK
1363
Georg Brandlc575c902008-09-13 17:46:05 +00001364 Exit code that means no error occurred. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001365
Georg Brandl116aa622007-08-15 14:28:22 +00001366
1367.. data:: EX_USAGE
1368
1369 Exit code that means the command was used incorrectly, such as when the wrong
Georg Brandlc575c902008-09-13 17:46:05 +00001370 number of arguments are given. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001371
Georg Brandl116aa622007-08-15 14:28:22 +00001372
1373.. data:: EX_DATAERR
1374
Georg Brandlc575c902008-09-13 17:46:05 +00001375 Exit code that means the input data was incorrect. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001376
Georg Brandl116aa622007-08-15 14:28:22 +00001377
1378.. data:: EX_NOINPUT
1379
1380 Exit code that means an input file did not exist or was not readable.
Georg Brandlc575c902008-09-13 17:46:05 +00001381 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001382
Georg Brandl116aa622007-08-15 14:28:22 +00001383
1384.. data:: EX_NOUSER
1385
Georg Brandlc575c902008-09-13 17:46:05 +00001386 Exit code that means a specified user did not exist. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001387
Georg Brandl116aa622007-08-15 14:28:22 +00001388
1389.. data:: EX_NOHOST
1390
Georg Brandlc575c902008-09-13 17:46:05 +00001391 Exit code that means a specified host did not exist. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001392
Georg Brandl116aa622007-08-15 14:28:22 +00001393
1394.. data:: EX_UNAVAILABLE
1395
1396 Exit code that means that a required service is unavailable. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001397 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001398
Georg Brandl116aa622007-08-15 14:28:22 +00001399
1400.. data:: EX_SOFTWARE
1401
1402 Exit code that means an internal software error was detected. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001403 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001404
Georg Brandl116aa622007-08-15 14:28:22 +00001405
1406.. data:: EX_OSERR
1407
1408 Exit code that means an operating system error was detected, such as the
Georg Brandlc575c902008-09-13 17:46:05 +00001409 inability to fork or create a pipe. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001410
Georg Brandl116aa622007-08-15 14:28:22 +00001411
1412.. data:: EX_OSFILE
1413
1414 Exit code that means some system file did not exist, could not be opened, or had
Georg Brandlc575c902008-09-13 17:46:05 +00001415 some other kind of error. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001416
Georg Brandl116aa622007-08-15 14:28:22 +00001417
1418.. data:: EX_CANTCREAT
1419
1420 Exit code that means a user specified output file could not be created.
Georg Brandlc575c902008-09-13 17:46:05 +00001421 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001422
Georg Brandl116aa622007-08-15 14:28:22 +00001423
1424.. data:: EX_IOERR
1425
1426 Exit code that means that an error occurred while doing I/O on some file.
Georg Brandlc575c902008-09-13 17:46:05 +00001427 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001428
Georg Brandl116aa622007-08-15 14:28:22 +00001429
1430.. data:: EX_TEMPFAIL
1431
1432 Exit code that means a temporary failure occurred. This indicates something
1433 that may not really be an error, such as a network connection that couldn't be
Georg Brandlc575c902008-09-13 17:46:05 +00001434 made during a retryable operation. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001435
Georg Brandl116aa622007-08-15 14:28:22 +00001436
1437.. data:: EX_PROTOCOL
1438
1439 Exit code that means that a protocol exchange was illegal, invalid, or not
Georg Brandlc575c902008-09-13 17:46:05 +00001440 understood. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001441
Georg Brandl116aa622007-08-15 14:28:22 +00001442
1443.. data:: EX_NOPERM
1444
1445 Exit code that means that there were insufficient permissions to perform the
Georg Brandlc575c902008-09-13 17:46:05 +00001446 operation (but not intended for file system problems). Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001447
Georg Brandl116aa622007-08-15 14:28:22 +00001448
1449.. data:: EX_CONFIG
1450
1451 Exit code that means that some kind of configuration error occurred.
Georg Brandlc575c902008-09-13 17:46:05 +00001452 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001453
Georg Brandl116aa622007-08-15 14:28:22 +00001454
1455.. data:: EX_NOTFOUND
1456
1457 Exit code that means something like "an entry was not found". Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001458 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001459
Georg Brandl116aa622007-08-15 14:28:22 +00001460
1461.. function:: fork()
1462
Christian Heimesfaf2f632008-01-06 16:59:19 +00001463 Fork a child process. Return ``0`` in the child and the child's process id in the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001464 parent. If an error occurs :exc:`OSError` is raised.
Benjamin Petersonbcd8ac32008-10-10 22:20:52 +00001465
1466 Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have
1467 known issues when using fork() from a thread.
1468
Georg Brandlc575c902008-09-13 17:46:05 +00001469 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001470
1471
1472.. function:: forkpty()
1473
1474 Fork a child process, using a new pseudo-terminal as the child's controlling
1475 terminal. Return a pair of ``(pid, fd)``, where *pid* is ``0`` in the child, the
1476 new child's process id in the parent, and *fd* is the file descriptor of the
1477 master end of the pseudo-terminal. For a more portable approach, use the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001478 :mod:`pty` module. If an error occurs :exc:`OSError` is raised.
Georg Brandlc575c902008-09-13 17:46:05 +00001479 Availability: some flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001480
1481
1482.. function:: kill(pid, sig)
1483
1484 .. index::
1485 single: process; killing
1486 single: process; signalling
1487
1488 Send signal *sig* to the process *pid*. Constants for the specific signals
1489 available on the host platform are defined in the :mod:`signal` module.
Georg Brandlc575c902008-09-13 17:46:05 +00001490 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001491
1492
1493.. function:: killpg(pgid, sig)
1494
1495 .. index::
1496 single: process; killing
1497 single: process; signalling
1498
Georg Brandlc575c902008-09-13 17:46:05 +00001499 Send the signal *sig* to the process group *pgid*. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001500
Georg Brandl116aa622007-08-15 14:28:22 +00001501
1502.. function:: nice(increment)
1503
1504 Add *increment* to the process's "niceness". Return the new niceness.
Georg Brandlc575c902008-09-13 17:46:05 +00001505 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001506
1507
1508.. function:: plock(op)
1509
1510 Lock program segments into memory. The value of *op* (defined in
Georg Brandlc575c902008-09-13 17:46:05 +00001511 ``<sys/lock.h>``) determines which segments are locked. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001512
1513
1514.. function:: popen(...)
1515 :noindex:
1516
1517 Run child processes, returning opened pipes for communications. These functions
1518 are described in section :ref:`os-newstreams`.
1519
1520
1521.. function:: spawnl(mode, path, ...)
1522 spawnle(mode, path, ..., env)
1523 spawnlp(mode, file, ...)
1524 spawnlpe(mode, file, ..., env)
1525 spawnv(mode, path, args)
1526 spawnve(mode, path, args, env)
1527 spawnvp(mode, file, args)
1528 spawnvpe(mode, file, args, env)
1529
1530 Execute the program *path* in a new process.
1531
1532 (Note that the :mod:`subprocess` module provides more powerful facilities for
1533 spawning new processes and retrieving their results; using that module is
Benjamin Peterson87c8d872009-06-11 22:54:11 +00001534 preferable to using these functions. Check especially the
1535 :ref:`subprocess-replacements` section.)
Georg Brandl116aa622007-08-15 14:28:22 +00001536
Christian Heimesfaf2f632008-01-06 16:59:19 +00001537 If *mode* is :const:`P_NOWAIT`, this function returns the process id of the new
Georg Brandl116aa622007-08-15 14:28:22 +00001538 process; if *mode* is :const:`P_WAIT`, returns the process's exit code if it
1539 exits normally, or ``-signal``, where *signal* is the signal that killed the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001540 process. On Windows, the process id will actually be the process handle, so can
Georg Brandl116aa622007-08-15 14:28:22 +00001541 be used with the :func:`waitpid` function.
1542
Christian Heimesfaf2f632008-01-06 16:59:19 +00001543 The "l" and "v" variants of the :func:`spawn\*` functions differ in how
1544 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001545 to work with if the number of parameters is fixed when the code is written; the
1546 individual parameters simply become additional parameters to the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001547 :func:`spawnl\*` functions. The "v" variants are good when the number of
Georg Brandl116aa622007-08-15 14:28:22 +00001548 parameters is variable, with the arguments being passed in a list or tuple as
1549 the *args* parameter. In either case, the arguments to the child process must
1550 start with the name of the command being run.
1551
Christian Heimesfaf2f632008-01-06 16:59:19 +00001552 The variants which include a second "p" near the end (:func:`spawnlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001553 :func:`spawnlpe`, :func:`spawnvp`, and :func:`spawnvpe`) will use the
1554 :envvar:`PATH` environment variable to locate the program *file*. When the
1555 environment is being replaced (using one of the :func:`spawn\*e` variants,
1556 discussed in the next paragraph), the new environment is used as the source of
1557 the :envvar:`PATH` variable. The other variants, :func:`spawnl`,
1558 :func:`spawnle`, :func:`spawnv`, and :func:`spawnve`, will not use the
1559 :envvar:`PATH` variable to locate the executable; *path* must contain an
1560 appropriate absolute or relative path.
1561
1562 For :func:`spawnle`, :func:`spawnlpe`, :func:`spawnve`, and :func:`spawnvpe`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001563 (note that these all end in "e"), the *env* parameter must be a mapping
Christian Heimesa342c012008-04-20 21:01:16 +00001564 which is used to define the environment variables for the new process (they are
1565 used instead of the current process' environment); the functions
Georg Brandl116aa622007-08-15 14:28:22 +00001566 :func:`spawnl`, :func:`spawnlp`, :func:`spawnv`, and :func:`spawnvp` all cause
Benjamin Petersond23f8222009-04-05 19:13:16 +00001567 the new process to inherit the environment of the current process. Note that
1568 keys and values in the *env* dictionary must be strings; invalid keys or
1569 values will cause the function to fail, with a return value of ``127``.
Georg Brandl116aa622007-08-15 14:28:22 +00001570
1571 As an example, the following calls to :func:`spawnlp` and :func:`spawnvpe` are
1572 equivalent::
1573
1574 import os
1575 os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1576
1577 L = ['cp', 'index.html', '/dev/null']
1578 os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1579
1580 Availability: Unix, Windows. :func:`spawnlp`, :func:`spawnlpe`, :func:`spawnvp`
1581 and :func:`spawnvpe` are not available on Windows.
1582
Georg Brandl116aa622007-08-15 14:28:22 +00001583
1584.. data:: P_NOWAIT
1585 P_NOWAITO
1586
1587 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1588 functions. If either of these values is given, the :func:`spawn\*` functions
Christian Heimesfaf2f632008-01-06 16:59:19 +00001589 will return as soon as the new process has been created, with the process id as
Georg Brandlc575c902008-09-13 17:46:05 +00001590 the return value. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001591
Georg Brandl116aa622007-08-15 14:28:22 +00001592
1593.. data:: P_WAIT
1594
1595 Possible value for the *mode* parameter to the :func:`spawn\*` family of
1596 functions. If this is given as *mode*, the :func:`spawn\*` functions will not
1597 return until the new process has run to completion and will return the exit code
1598 of the process the run is successful, or ``-signal`` if a signal kills the
Georg Brandlc575c902008-09-13 17:46:05 +00001599 process. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001600
Georg Brandl116aa622007-08-15 14:28:22 +00001601
1602.. data:: P_DETACH
1603 P_OVERLAY
1604
1605 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1606 functions. These are less portable than those listed above. :const:`P_DETACH`
1607 is similar to :const:`P_NOWAIT`, but the new process is detached from the
1608 console of the calling process. If :const:`P_OVERLAY` is used, the current
1609 process will be replaced; the :func:`spawn\*` function will not return.
1610 Availability: Windows.
1611
Georg Brandl116aa622007-08-15 14:28:22 +00001612
1613.. function:: startfile(path[, operation])
1614
1615 Start a file with its associated application.
1616
1617 When *operation* is not specified or ``'open'``, this acts like double-clicking
1618 the file in Windows Explorer, or giving the file name as an argument to the
1619 :program:`start` command from the interactive command shell: the file is opened
1620 with whatever application (if any) its extension is associated.
1621
1622 When another *operation* is given, it must be a "command verb" that specifies
1623 what should be done with the file. Common verbs documented by Microsoft are
1624 ``'print'`` and ``'edit'`` (to be used on files) as well as ``'explore'`` and
1625 ``'find'`` (to be used on directories).
1626
1627 :func:`startfile` returns as soon as the associated application is launched.
1628 There is no option to wait for the application to close, and no way to retrieve
1629 the application's exit status. The *path* parameter is relative to the current
1630 directory. If you want to use an absolute path, make sure the first character
1631 is not a slash (``'/'``); the underlying Win32 :cfunc:`ShellExecute` function
1632 doesn't work if it is. Use the :func:`os.path.normpath` function to ensure that
1633 the path is properly encoded for Win32. Availability: Windows.
1634
Georg Brandl116aa622007-08-15 14:28:22 +00001635
1636.. function:: system(command)
1637
1638 Execute the command (a string) in a subshell. This is implemented by calling
Georg Brandl495f7b52009-10-27 15:28:25 +00001639 the Standard C function :cfunc:`system`, and has the same limitations.
1640 Changes to :data:`sys.stdin`, etc. are not reflected in the environment of the
1641 executed command.
Georg Brandl116aa622007-08-15 14:28:22 +00001642
1643 On Unix, the return value is the exit status of the process encoded in the
1644 format specified for :func:`wait`. Note that POSIX does not specify the meaning
1645 of the return value of the C :cfunc:`system` function, so the return value of
1646 the Python function is system-dependent.
1647
1648 On Windows, the return value is that returned by the system shell after running
1649 *command*, given by the Windows environment variable :envvar:`COMSPEC`: on
1650 :program:`command.com` systems (Windows 95, 98 and ME) this is always ``0``; on
1651 :program:`cmd.exe` systems (Windows NT, 2000 and XP) this is the exit status of
1652 the command run; on systems using a non-native shell, consult your shell
1653 documentation.
1654
Georg Brandlc575c902008-09-13 17:46:05 +00001655 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001656
1657 The :mod:`subprocess` module provides more powerful facilities for spawning new
1658 processes and retrieving their results; using that module is preferable to using
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001659 this function. Use the :mod:`subprocess` module. Check especially the
1660 :ref:`subprocess-replacements` section.
Georg Brandl116aa622007-08-15 14:28:22 +00001661
1662
1663.. function:: times()
1664
1665 Return a 5-tuple of floating point numbers indicating accumulated (processor or
1666 other) times, in seconds. The items are: user time, system time, children's
1667 user time, children's system time, and elapsed real time since a fixed point in
1668 the past, in that order. See the Unix manual page :manpage:`times(2)` or the
Georg Brandlc575c902008-09-13 17:46:05 +00001669 corresponding Windows Platform API documentation. Availability: Unix,
Christian Heimes68f5fbe2008-02-14 08:27:37 +00001670 Windows. On Windows, only the first two items are filled, the others are zero.
Georg Brandl116aa622007-08-15 14:28:22 +00001671
1672
1673.. function:: wait()
1674
1675 Wait for completion of a child process, and return a tuple containing its pid
1676 and exit status indication: a 16-bit number, whose low byte is the signal number
1677 that killed the process, and whose high byte is the exit status (if the signal
1678 number is zero); the high bit of the low byte is set if a core file was
Georg Brandlc575c902008-09-13 17:46:05 +00001679 produced. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001680
1681
1682.. function:: waitpid(pid, options)
1683
1684 The details of this function differ on Unix and Windows.
1685
1686 On Unix: Wait for completion of a child process given by process id *pid*, and
1687 return a tuple containing its process id and exit status indication (encoded as
1688 for :func:`wait`). The semantics of the call are affected by the value of the
1689 integer *options*, which should be ``0`` for normal operation.
1690
1691 If *pid* is greater than ``0``, :func:`waitpid` requests status information for
1692 that specific process. If *pid* is ``0``, the request is for the status of any
1693 child in the process group of the current process. If *pid* is ``-1``, the
1694 request pertains to any child of the current process. If *pid* is less than
1695 ``-1``, status is requested for any process in the process group ``-pid`` (the
1696 absolute value of *pid*).
1697
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001698 An :exc:`OSError` is raised with the value of errno when the syscall
1699 returns -1.
1700
Georg Brandl116aa622007-08-15 14:28:22 +00001701 On Windows: Wait for completion of a process given by process handle *pid*, and
1702 return a tuple containing *pid*, and its exit status shifted left by 8 bits
1703 (shifting makes cross-platform use of the function easier). A *pid* less than or
1704 equal to ``0`` has no special meaning on Windows, and raises an exception. The
1705 value of integer *options* has no effect. *pid* can refer to any process whose
1706 id is known, not necessarily a child process. The :func:`spawn` functions called
1707 with :const:`P_NOWAIT` return suitable process handles.
1708
1709
1710.. function:: wait3([options])
1711
1712 Similar to :func:`waitpid`, except no process id argument is given and a
1713 3-element tuple containing the child's process id, exit status indication, and
1714 resource usage information is returned. Refer to :mod:`resource`.\
1715 :func:`getrusage` for details on resource usage information. The option
1716 argument is the same as that provided to :func:`waitpid` and :func:`wait4`.
1717 Availability: Unix.
1718
Georg Brandl116aa622007-08-15 14:28:22 +00001719
1720.. function:: wait4(pid, options)
1721
1722 Similar to :func:`waitpid`, except a 3-element tuple, containing the child's
1723 process id, exit status indication, and resource usage information is returned.
1724 Refer to :mod:`resource`.\ :func:`getrusage` for details on resource usage
1725 information. The arguments to :func:`wait4` are the same as those provided to
1726 :func:`waitpid`. Availability: Unix.
1727
Georg Brandl116aa622007-08-15 14:28:22 +00001728
1729.. data:: WNOHANG
1730
1731 The option for :func:`waitpid` to return immediately if no child process status
1732 is available immediately. The function returns ``(0, 0)`` in this case.
Georg Brandlc575c902008-09-13 17:46:05 +00001733 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001734
1735
1736.. data:: WCONTINUED
1737
1738 This option causes child processes to be reported if they have been continued
1739 from a job control stop since their status was last reported. Availability: Some
1740 Unix systems.
1741
Georg Brandl116aa622007-08-15 14:28:22 +00001742
1743.. data:: WUNTRACED
1744
1745 This option causes child processes to be reported if they have been stopped but
1746 their current state has not been reported since they were stopped. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001747 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001748
Georg Brandl116aa622007-08-15 14:28:22 +00001749
1750The following functions take a process status code as returned by
1751:func:`system`, :func:`wait`, or :func:`waitpid` as a parameter. They may be
1752used to determine the disposition of a process.
1753
Georg Brandl116aa622007-08-15 14:28:22 +00001754.. function:: WCOREDUMP(status)
1755
Christian Heimesfaf2f632008-01-06 16:59:19 +00001756 Return ``True`` if a core dump was generated for the process, otherwise
Georg Brandlc575c902008-09-13 17:46:05 +00001757 return ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001758
Georg Brandl116aa622007-08-15 14:28:22 +00001759
1760.. function:: WIFCONTINUED(status)
1761
Christian Heimesfaf2f632008-01-06 16:59:19 +00001762 Return ``True`` if the process has been continued from a job control stop,
1763 otherwise return ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001764
Georg Brandl116aa622007-08-15 14:28:22 +00001765
1766.. function:: WIFSTOPPED(status)
1767
Christian Heimesfaf2f632008-01-06 16:59:19 +00001768 Return ``True`` if the process has been stopped, otherwise return
Georg Brandl116aa622007-08-15 14:28:22 +00001769 ``False``. Availability: Unix.
1770
1771
1772.. function:: WIFSIGNALED(status)
1773
Christian Heimesfaf2f632008-01-06 16:59:19 +00001774 Return ``True`` if the process exited due to a signal, otherwise return
Georg Brandlc575c902008-09-13 17:46:05 +00001775 ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001776
1777
1778.. function:: WIFEXITED(status)
1779
Christian Heimesfaf2f632008-01-06 16:59:19 +00001780 Return ``True`` if the process exited using the :manpage:`exit(2)` system call,
Georg Brandlc575c902008-09-13 17:46:05 +00001781 otherwise return ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001782
1783
1784.. function:: WEXITSTATUS(status)
1785
1786 If ``WIFEXITED(status)`` is true, return the integer parameter to the
1787 :manpage:`exit(2)` system call. Otherwise, the return value is meaningless.
Georg Brandlc575c902008-09-13 17:46:05 +00001788 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001789
1790
1791.. function:: WSTOPSIG(status)
1792
Georg Brandlc575c902008-09-13 17:46:05 +00001793 Return the signal which caused the process to stop. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001794
1795
1796.. function:: WTERMSIG(status)
1797
Georg Brandlc575c902008-09-13 17:46:05 +00001798 Return the signal which caused the process to exit. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001799
1800
1801.. _os-path:
1802
1803Miscellaneous System Information
1804--------------------------------
1805
1806
1807.. function:: confstr(name)
1808
1809 Return string-valued system configuration values. *name* specifies the
1810 configuration value to retrieve; it may be a string which is the name of a
1811 defined system value; these names are specified in a number of standards (POSIX,
1812 Unix 95, Unix 98, and others). Some platforms define additional names as well.
1813 The names known to the host operating system are given as the keys of the
1814 ``confstr_names`` dictionary. For configuration variables not included in that
1815 mapping, passing an integer for *name* is also accepted. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001816 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001817
1818 If the configuration value specified by *name* isn't defined, ``None`` is
1819 returned.
1820
1821 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1822 specific value for *name* is not supported by the host system, even if it is
1823 included in ``confstr_names``, an :exc:`OSError` is raised with
1824 :const:`errno.EINVAL` for the error number.
1825
1826
1827.. data:: confstr_names
1828
1829 Dictionary mapping names accepted by :func:`confstr` to the integer values
1830 defined for those names by the host operating system. This can be used to
Georg Brandlc575c902008-09-13 17:46:05 +00001831 determine the set of names known to the system. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001832
1833
1834.. function:: getloadavg()
1835
Christian Heimesa62da1d2008-01-12 19:39:10 +00001836 Return the number of processes in the system run queue averaged over the last
1837 1, 5, and 15 minutes or raises :exc:`OSError` if the load average was
Georg Brandlf08a9dd2008-06-10 16:57:31 +00001838 unobtainable. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001839
Georg Brandl116aa622007-08-15 14:28:22 +00001840
1841.. function:: sysconf(name)
1842
1843 Return integer-valued system configuration values. If the configuration value
1844 specified by *name* isn't defined, ``-1`` is returned. The comments regarding
1845 the *name* parameter for :func:`confstr` apply here as well; the dictionary that
1846 provides information on the known names is given by ``sysconf_names``.
Georg Brandlc575c902008-09-13 17:46:05 +00001847 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001848
1849
1850.. data:: sysconf_names
1851
1852 Dictionary mapping names accepted by :func:`sysconf` to the integer values
1853 defined for those names by the host operating system. This can be used to
Georg Brandlc575c902008-09-13 17:46:05 +00001854 determine the set of names known to the system. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001855
Christian Heimesfaf2f632008-01-06 16:59:19 +00001856The following data values are used to support path manipulation operations. These
Georg Brandl116aa622007-08-15 14:28:22 +00001857are defined for all platforms.
1858
1859Higher-level operations on pathnames are defined in the :mod:`os.path` module.
1860
1861
1862.. data:: curdir
1863
1864 The constant string used by the operating system to refer to the current
Georg Brandlc575c902008-09-13 17:46:05 +00001865 directory. This is ``'.'`` for Windows and POSIX. Also available via
1866 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00001867
1868
1869.. data:: pardir
1870
1871 The constant string used by the operating system to refer to the parent
Georg Brandlc575c902008-09-13 17:46:05 +00001872 directory. This is ``'..'`` for Windows and POSIX. Also available via
1873 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00001874
1875
1876.. data:: sep
1877
Georg Brandlc575c902008-09-13 17:46:05 +00001878 The character used by the operating system to separate pathname components.
1879 This is ``'/'`` for POSIX and ``'\\'`` for Windows. Note that knowing this
1880 is not sufficient to be able to parse or concatenate pathnames --- use
Georg Brandl116aa622007-08-15 14:28:22 +00001881 :func:`os.path.split` and :func:`os.path.join` --- but it is occasionally
1882 useful. Also available via :mod:`os.path`.
1883
1884
1885.. data:: altsep
1886
1887 An alternative character used by the operating system to separate pathname
1888 components, or ``None`` if only one separator character exists. This is set to
1889 ``'/'`` on Windows systems where ``sep`` is a backslash. Also available via
1890 :mod:`os.path`.
1891
1892
1893.. data:: extsep
1894
1895 The character which separates the base filename from the extension; for example,
1896 the ``'.'`` in :file:`os.py`. Also available via :mod:`os.path`.
1897
Georg Brandl116aa622007-08-15 14:28:22 +00001898
1899.. data:: pathsep
1900
1901 The character conventionally used by the operating system to separate search
1902 path components (as in :envvar:`PATH`), such as ``':'`` for POSIX or ``';'`` for
1903 Windows. Also available via :mod:`os.path`.
1904
1905
1906.. data:: defpath
1907
1908 The default search path used by :func:`exec\*p\*` and :func:`spawn\*p\*` if the
1909 environment doesn't have a ``'PATH'`` key. Also available via :mod:`os.path`.
1910
1911
1912.. data:: linesep
1913
1914 The string used to separate (or, rather, terminate) lines on the current
Georg Brandlc575c902008-09-13 17:46:05 +00001915 platform. This may be a single character, such as ``'\n'`` for POSIX, or
1916 multiple characters, for example, ``'\r\n'`` for Windows. Do not use
1917 *os.linesep* as a line terminator when writing files opened in text mode (the
1918 default); use a single ``'\n'`` instead, on all platforms.
Georg Brandl116aa622007-08-15 14:28:22 +00001919
1920
1921.. data:: devnull
1922
Georg Brandlc575c902008-09-13 17:46:05 +00001923 The file path of the null device. For example: ``'/dev/null'`` for POSIX.
1924 Also available via :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00001925
Georg Brandl116aa622007-08-15 14:28:22 +00001926
1927.. _os-miscfunc:
1928
1929Miscellaneous Functions
1930-----------------------
1931
1932
1933.. function:: urandom(n)
1934
1935 Return a string of *n* random bytes suitable for cryptographic use.
1936
1937 This function returns random bytes from an OS-specific randomness source. The
1938 returned data should be unpredictable enough for cryptographic applications,
1939 though its exact quality depends on the OS implementation. On a UNIX-like
1940 system this will query /dev/urandom, and on Windows it will use CryptGenRandom.
1941 If a randomness source is not found, :exc:`NotImplementedError` will be raised.