blob: 42d28fa4cf997e2eeac2d937c1e89cb8fdded779 [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
Christian Heimesa62da1d2008-01-12 19:39:10 +000016The design of all built-in operating system dependent modules of Python is such
17that as long as the same functionality is available, it uses the same interface;
18for example, the function ``os.stat(path)`` returns stat information about
19*path* in the same format (which happens to have originated with the POSIX
Georg Brandl116aa622007-08-15 14:28:22 +000020interface).
21
22Extensions peculiar to a particular operating system are also available through
23the :mod:`os` module, but using them is of course a threat to portability!
24
Christian Heimesa62da1d2008-01-12 19:39:10 +000025.. note::
Georg Brandl116aa622007-08-15 14:28:22 +000026
Georg Brandl76e55382008-10-08 16:34:57 +000027 All functions accepting path or file names accept both bytes and string
28 objects, and result in an object of the same type, if a path or file name is
29 returned.
30
31.. note::
32
Georg Brandlc575c902008-09-13 17:46:05 +000033 If not separately noted, all functions that claim "Availability: Unix" are
34 supported on Mac OS X, which builds on a Unix core.
35
36.. note::
37
Christian Heimesa62da1d2008-01-12 19:39:10 +000038 All functions in this module raise :exc:`OSError` in the case of invalid or
39 inaccessible file names and paths, or other arguments that have the correct
40 type, but are not accepted by the operating system.
Georg Brandl116aa622007-08-15 14:28:22 +000041
Georg Brandl116aa622007-08-15 14:28:22 +000042.. exception:: error
43
Christian Heimesa62da1d2008-01-12 19:39:10 +000044 An alias for the built-in :exc:`OSError` exception.
Georg Brandl116aa622007-08-15 14:28:22 +000045
46
47.. data:: name
48
49 The name of the operating system dependent module imported. The following names
50 have currently been registered: ``'posix'``, ``'nt'``, ``'mac'``, ``'os2'``,
Skip Montanaro7a98be22007-08-16 14:35:24 +000051 ``'ce'``, ``'java'``.
Georg Brandl116aa622007-08-15 14:28:22 +000052
53
Martin v. Löwis011e8422009-05-05 04:43:17 +000054.. _os-filenames:
55
56File Names, Command Line Arguments, and Environment Variables
57-------------------------------------------------------------
58
59In Python, file names, command line arguments, and environment
60variables are represented using the string type. On some systems,
61decoding these strings to and from bytes is necessary before passing
62them to the operating system. Python uses the file system encoding to
63perform this conversion (see :func:`sys.getfilesystemencoding`).
64
65.. versionchanged:: 3.1
66 On some systems, conversion using the file system encoding may
Martin v. Löwis43c57782009-05-10 08:15:24 +000067 fail. In this case, Python uses the ``surrogateescape`` encoding
68 error handler, which means that undecodable bytes are replaced by a
Martin v. Löwis011e8422009-05-05 04:43:17 +000069 Unicode character U+DCxx on decoding, and these are again
70 translated to the original byte on encoding.
71
72
73The file system encoding must guarantee to successfully decode all
74bytes below 128. If the file system encoding fails to provide this
75guarantee, API functions may raise UnicodeErrors.
76
77
Georg Brandl116aa622007-08-15 14:28:22 +000078.. _os-procinfo:
79
80Process Parameters
81------------------
82
83These functions and data items provide information and operate on the current
84process and user.
85
86
87.. data:: environ
88
89 A mapping object representing the string environment. For example,
90 ``environ['HOME']`` is the pathname of your home directory (on some platforms),
91 and is equivalent to ``getenv("HOME")`` in C.
92
93 This mapping is captured the first time the :mod:`os` module is imported,
94 typically during Python startup as part of processing :file:`site.py`. Changes
95 to the environment made after this time are not reflected in ``os.environ``,
96 except for changes made by modifying ``os.environ`` directly.
97
98 If the platform supports the :func:`putenv` function, this mapping may be used
99 to modify the environment as well as query the environment. :func:`putenv` will
100 be called automatically when the mapping is modified.
101
102 .. note::
103
104 Calling :func:`putenv` directly does not change ``os.environ``, so it's better
105 to modify ``os.environ``.
106
107 .. note::
108
Georg Brandlc575c902008-09-13 17:46:05 +0000109 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
110 cause memory leaks. Refer to the system documentation for
111 :cfunc:`putenv`.
Georg Brandl116aa622007-08-15 14:28:22 +0000112
113 If :func:`putenv` is not provided, a modified copy of this mapping may be
114 passed to the appropriate process-creation functions to cause child processes
115 to use a modified environment.
116
Georg Brandl9afde1c2007-11-01 20:32:30 +0000117 If the platform supports the :func:`unsetenv` function, you can delete items in
Georg Brandl116aa622007-08-15 14:28:22 +0000118 this mapping to unset environment variables. :func:`unsetenv` will be called
Georg Brandl9afde1c2007-11-01 20:32:30 +0000119 automatically when an item is deleted from ``os.environ``, and when
120 one of the :meth:`pop` or :meth:`clear` methods is called.
121
Georg Brandl116aa622007-08-15 14:28:22 +0000122
123.. function:: chdir(path)
124 fchdir(fd)
125 getcwd()
126 :noindex:
127
128 These functions are described in :ref:`os-file-dir`.
129
130
131.. function:: ctermid()
132
133 Return the filename corresponding to the controlling terminal of the process.
134 Availability: Unix.
135
136
137.. function:: getegid()
138
139 Return the effective group id of the current process. This corresponds to the
Christian Heimesfaf2f632008-01-06 16:59:19 +0000140 "set id" bit on the file being executed in the current process. Availability:
Georg Brandl116aa622007-08-15 14:28:22 +0000141 Unix.
142
143
144.. function:: geteuid()
145
146 .. index:: single: user; effective id
147
Christian Heimesfaf2f632008-01-06 16:59:19 +0000148 Return the current process's effective user id. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000149
150
151.. function:: getgid()
152
153 .. index:: single: process; group
154
155 Return the real group id of the current process. Availability: Unix.
156
157
158.. function:: getgroups()
159
160 Return list of supplemental group ids associated with the current process.
161 Availability: Unix.
162
163
164.. function:: getlogin()
165
166 Return the name of the user logged in on the controlling terminal of the
167 process. For most purposes, it is more useful to use the environment variable
168 :envvar:`LOGNAME` to find out who the user is, or
169 ``pwd.getpwuid(os.getuid())[0]`` to get the login name of the currently
Christian Heimesfaf2f632008-01-06 16:59:19 +0000170 effective user id. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000171
172
173.. function:: getpgid(pid)
174
175 Return the process group id of the process with process id *pid*. If *pid* is 0,
176 the process group id of the current process is returned. Availability: Unix.
177
Georg Brandl116aa622007-08-15 14:28:22 +0000178
179.. function:: getpgrp()
180
181 .. index:: single: process; group
182
183 Return the id of the current process group. Availability: Unix.
184
185
186.. function:: getpid()
187
188 .. index:: single: process; id
189
190 Return the current process id. Availability: Unix, Windows.
191
192
193.. function:: getppid()
194
195 .. index:: single: process; id of parent
196
197 Return the parent's process id. Availability: Unix.
198
Gregory P. Smithcf02c6a2009-11-27 17:54:17 +0000199.. function:: getresuid()
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000200
201 Return a tuple (ruid, euid, suid) denoting the current process's
202 real, effective, and saved user ids. Availability: Unix.
203
204 .. versionadded:: 2.7/3.2
205
Gregory P. Smithcf02c6a2009-11-27 17:54:17 +0000206.. function:: getresgid()
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000207
208 Return a tuple (rgid, egid, sgid) denoting the current process's
209 real, effective, and saved user ids. Availability: Unix.
210
211 .. versionadded:: 2.7/3.2
Georg Brandl116aa622007-08-15 14:28:22 +0000212
213.. function:: getuid()
214
215 .. index:: single: user; id
216
Christian Heimesfaf2f632008-01-06 16:59:19 +0000217 Return the current process's user id. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000218
219
Georg Brandl18244152009-09-02 20:34:52 +0000220.. function:: getenv(key, default=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000221
Georg Brandl18244152009-09-02 20:34:52 +0000222 Return the value of the environment variable *key* if it exists, or
223 *default* if it doesn't. Availability: most flavors of Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000224
225
Georg Brandl18244152009-09-02 20:34:52 +0000226.. function:: putenv(key, value)
Georg Brandl116aa622007-08-15 14:28:22 +0000227
228 .. index:: single: environment variables; setting
229
Georg Brandl18244152009-09-02 20:34:52 +0000230 Set the environment variable named *key* to the string *value*. Such
Georg Brandl116aa622007-08-15 14:28:22 +0000231 changes to the environment affect subprocesses started with :func:`os.system`,
232 :func:`popen` or :func:`fork` and :func:`execv`. Availability: most flavors of
233 Unix, Windows.
234
235 .. note::
236
Georg Brandlc575c902008-09-13 17:46:05 +0000237 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
238 cause memory leaks. Refer to the system documentation for putenv.
Georg Brandl116aa622007-08-15 14:28:22 +0000239
240 When :func:`putenv` is supported, assignments to items in ``os.environ`` are
241 automatically translated into corresponding calls to :func:`putenv`; however,
242 calls to :func:`putenv` don't update ``os.environ``, so it is actually
243 preferable to assign to items of ``os.environ``.
244
245
246.. function:: setegid(egid)
247
248 Set the current process's effective group id. Availability: Unix.
249
250
251.. function:: seteuid(euid)
252
253 Set the current process's effective user id. Availability: Unix.
254
255
256.. function:: setgid(gid)
257
258 Set the current process' group id. Availability: Unix.
259
260
261.. function:: setgroups(groups)
262
263 Set the list of supplemental group ids associated with the current process to
264 *groups*. *groups* must be a sequence, and each element must be an integer
Christian Heimesfaf2f632008-01-06 16:59:19 +0000265 identifying a group. This operation is typically available only to the superuser.
Georg Brandl116aa622007-08-15 14:28:22 +0000266 Availability: Unix.
267
Georg Brandl116aa622007-08-15 14:28:22 +0000268
269.. function:: setpgrp()
270
Christian Heimesfaf2f632008-01-06 16:59:19 +0000271 Call the system call :cfunc:`setpgrp` or :cfunc:`setpgrp(0, 0)` depending on
Georg Brandl116aa622007-08-15 14:28:22 +0000272 which version is implemented (if any). See the Unix manual for the semantics.
273 Availability: Unix.
274
275
276.. function:: setpgid(pid, pgrp)
277
Christian Heimesfaf2f632008-01-06 16:59:19 +0000278 Call the system call :cfunc:`setpgid` to set the process group id of the
Georg Brandl116aa622007-08-15 14:28:22 +0000279 process with id *pid* to the process group with id *pgrp*. See the Unix manual
280 for the semantics. Availability: Unix.
281
282
Georg Brandl116aa622007-08-15 14:28:22 +0000283.. function:: setregid(rgid, egid)
284
285 Set the current process's real and effective group ids. Availability: Unix.
286
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000287.. function:: setresgid(rgid, egid, sgid)
288
289 Set the current process's real, effective, and saved group ids.
290 Availability: Unix.
291
292 .. versionadded:: 2.7/3.2
293
294.. function:: setresuid(ruid, euid, suid)
295
296 Set the current process's real, effective, and saved user ids.
297 Availibility: Unix.
298
299 .. versionadded:: 2.7/3.2
300
301.. function:: setreuid(ruid, euid)
302
303 Set the current process's real and effective user ids. Availability: Unix.
304
Georg Brandl116aa622007-08-15 14:28:22 +0000305
306.. function:: getsid(pid)
307
Christian Heimesfaf2f632008-01-06 16:59:19 +0000308 Call the system call :cfunc:`getsid`. See the Unix manual for the semantics.
Georg Brandl116aa622007-08-15 14:28:22 +0000309 Availability: Unix.
310
Georg Brandl116aa622007-08-15 14:28:22 +0000311
312.. function:: setsid()
313
Christian Heimesfaf2f632008-01-06 16:59:19 +0000314 Call the system call :cfunc:`setsid`. See the Unix manual for the semantics.
Georg Brandl116aa622007-08-15 14:28:22 +0000315 Availability: Unix.
316
317
318.. function:: setuid(uid)
319
320 .. index:: single: user; id, setting
321
Christian Heimesfaf2f632008-01-06 16:59:19 +0000322 Set the current process's user id. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000323
Georg Brandl116aa622007-08-15 14:28:22 +0000324
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000325.. placed in this section since it relates to errno.... a little weak
Georg Brandl116aa622007-08-15 14:28:22 +0000326.. function:: strerror(code)
327
328 Return the error message corresponding to the error code in *code*.
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000329 On platforms where :cfunc:`strerror` returns ``NULL`` when given an unknown
330 error number, :exc:`ValueError` is raised. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000331
332
333.. function:: umask(mask)
334
Christian Heimesfaf2f632008-01-06 16:59:19 +0000335 Set the current numeric umask and return the previous umask. Availability:
Georg Brandl116aa622007-08-15 14:28:22 +0000336 Unix, Windows.
337
338
339.. function:: uname()
340
341 .. index::
342 single: gethostname() (in module socket)
343 single: gethostbyaddr() (in module socket)
344
345 Return a 5-tuple containing information identifying the current operating
346 system. The tuple contains 5 strings: ``(sysname, nodename, release, version,
347 machine)``. Some systems truncate the nodename to 8 characters or to the
348 leading component; a better way to get the hostname is
349 :func:`socket.gethostname` or even
350 ``socket.gethostbyaddr(socket.gethostname())``. Availability: recent flavors of
351 Unix.
352
353
Georg Brandl18244152009-09-02 20:34:52 +0000354.. function:: unsetenv(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000355
356 .. index:: single: environment variables; deleting
357
Georg Brandl18244152009-09-02 20:34:52 +0000358 Unset (delete) the environment variable named *key*. Such changes to the
Georg Brandl116aa622007-08-15 14:28:22 +0000359 environment affect subprocesses started with :func:`os.system`, :func:`popen` or
360 :func:`fork` and :func:`execv`. Availability: most flavors of Unix, Windows.
361
362 When :func:`unsetenv` is supported, deletion of items in ``os.environ`` is
363 automatically translated into a corresponding call to :func:`unsetenv`; however,
364 calls to :func:`unsetenv` don't update ``os.environ``, so it is actually
365 preferable to delete items of ``os.environ``.
366
367
368.. _os-newstreams:
369
370File Object Creation
371--------------------
372
373These functions create new file objects. (See also :func:`open`.)
374
375
376.. function:: fdopen(fd[, mode[, bufsize]])
377
378 .. index:: single: I/O control; buffering
379
380 Return an open file object connected to the file descriptor *fd*. The *mode*
381 and *bufsize* arguments have the same meaning as the corresponding arguments to
Georg Brandlc575c902008-09-13 17:46:05 +0000382 the built-in :func:`open` function. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000383
Georg Brandl55ac8f02007-09-01 13:51:09 +0000384 When specified, the *mode* argument must start with one of the letters
385 ``'r'``, ``'w'``, or ``'a'``, otherwise a :exc:`ValueError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000386
Georg Brandl55ac8f02007-09-01 13:51:09 +0000387 On Unix, when the *mode* argument starts with ``'a'``, the *O_APPEND* flag is
388 set on the file descriptor (which the :cfunc:`fdopen` implementation already
389 does on most platforms).
Georg Brandl116aa622007-08-15 14:28:22 +0000390
391
Georg Brandl116aa622007-08-15 14:28:22 +0000392.. _os-fd-ops:
393
394File Descriptor Operations
395--------------------------
396
397These functions operate on I/O streams referenced using file descriptors.
398
399File descriptors are small integers corresponding to a file that has been opened
400by the current process. For example, standard input is usually file descriptor
4010, standard output is 1, and standard error is 2. Further files opened by a
402process will then be assigned 3, 4, 5, and so forth. The name "file descriptor"
403is slightly deceptive; on Unix platforms, sockets and pipes are also referenced
404by file descriptors.
405
406
407.. function:: close(fd)
408
Georg Brandlc575c902008-09-13 17:46:05 +0000409 Close file descriptor *fd*. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000410
411 .. note::
412
413 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000414 descriptor as returned by :func:`os.open` or :func:`pipe`. To close a "file
Georg Brandl116aa622007-08-15 14:28:22 +0000415 object" returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000416 :func:`fdopen`, use its :meth:`~file.close` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000417
418
Christian Heimesfdab48e2008-01-20 09:06:41 +0000419.. function:: closerange(fd_low, fd_high)
420
421 Close all file descriptors from *fd_low* (inclusive) to *fd_high* (exclusive),
Georg Brandlc575c902008-09-13 17:46:05 +0000422 ignoring errors. Availability: Unix, Windows. Equivalent to::
Christian Heimesfdab48e2008-01-20 09:06:41 +0000423
Georg Brandlc9a5a0e2009-09-01 07:34:27 +0000424 for fd in range(fd_low, fd_high):
Christian Heimesfdab48e2008-01-20 09:06:41 +0000425 try:
426 os.close(fd)
427 except OSError:
428 pass
429
Christian Heimesfdab48e2008-01-20 09:06:41 +0000430
Georg Brandl81f11302007-12-21 08:45:42 +0000431.. function:: device_encoding(fd)
432
433 Return a string describing the encoding of the device associated with *fd*
434 if it is connected to a terminal; else return :const:`None`.
435
436
Georg Brandl116aa622007-08-15 14:28:22 +0000437.. function:: dup(fd)
438
Georg Brandlc575c902008-09-13 17:46:05 +0000439 Return a duplicate of file descriptor *fd*. Availability: Unix,
Georg Brandl116aa622007-08-15 14:28:22 +0000440 Windows.
441
442
443.. function:: dup2(fd, fd2)
444
445 Duplicate file descriptor *fd* to *fd2*, closing the latter first if necessary.
Georg Brandlc575c902008-09-13 17:46:05 +0000446 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000447
448
Christian Heimes4e30a842007-11-30 22:12:06 +0000449.. function:: fchmod(fd, mode)
450
451 Change the mode of the file given by *fd* to the numeric *mode*. See the docs
452 for :func:`chmod` for possible values of *mode*. Availability: Unix.
453
454
455.. function:: fchown(fd, uid, gid)
456
457 Change the owner and group id of the file given by *fd* to the numeric *uid*
458 and *gid*. To leave one of the ids unchanged, set it to -1.
459 Availability: Unix.
460
461
Georg Brandl116aa622007-08-15 14:28:22 +0000462.. function:: fdatasync(fd)
463
464 Force write of file with filedescriptor *fd* to disk. Does not force update of
465 metadata. Availability: Unix.
466
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000467 .. note::
468 This function is not available on MacOS.
469
Georg Brandl116aa622007-08-15 14:28:22 +0000470
471.. function:: fpathconf(fd, name)
472
473 Return system configuration information relevant to an open file. *name*
474 specifies the configuration value to retrieve; it may be a string which is the
475 name of a defined system value; these names are specified in a number of
476 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
477 additional names as well. The names known to the host operating system are
478 given in the ``pathconf_names`` dictionary. For configuration variables not
479 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandlc575c902008-09-13 17:46:05 +0000480 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000481
482 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
483 specific value for *name* is not supported by the host system, even if it is
484 included in ``pathconf_names``, an :exc:`OSError` is raised with
485 :const:`errno.EINVAL` for the error number.
486
487
488.. function:: fstat(fd)
489
490 Return status for file descriptor *fd*, like :func:`stat`. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +0000491 Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000492
493
494.. function:: fstatvfs(fd)
495
496 Return information about the filesystem containing the file associated with file
497 descriptor *fd*, like :func:`statvfs`. Availability: Unix.
498
499
500.. function:: fsync(fd)
501
502 Force write of file with filedescriptor *fd* to disk. On Unix, this calls the
503 native :cfunc:`fsync` function; on Windows, the MS :cfunc:`_commit` function.
504
505 If you're starting with a Python file object *f*, first do ``f.flush()``, and
506 then do ``os.fsync(f.fileno())``, to ensure that all internal buffers associated
Georg Brandlc575c902008-09-13 17:46:05 +0000507 with *f* are written to disk. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000508
509
510.. function:: ftruncate(fd, length)
511
512 Truncate the file corresponding to file descriptor *fd*, so that it is at most
Georg Brandlc575c902008-09-13 17:46:05 +0000513 *length* bytes in size. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000514
515
516.. function:: isatty(fd)
517
518 Return ``True`` if the file descriptor *fd* is open and connected to a
Georg Brandlc575c902008-09-13 17:46:05 +0000519 tty(-like) device, else ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000520
521
522.. function:: lseek(fd, pos, how)
523
Christian Heimesfaf2f632008-01-06 16:59:19 +0000524 Set the current position of file descriptor *fd* to position *pos*, modified
525 by *how*: :const:`SEEK_SET` or ``0`` to set the position relative to the
526 beginning of the file; :const:`SEEK_CUR` or ``1`` to set it relative to the
527 current position; :const:`os.SEEK_END` or ``2`` to set it relative to the end of
Georg Brandlc575c902008-09-13 17:46:05 +0000528 the file. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000529
530
531.. function:: open(file, flags[, mode])
532
Georg Brandlf4a41232008-05-26 17:55:52 +0000533 Open the file *file* and set various flags according to *flags* and possibly
534 its mode according to *mode*. The default *mode* is ``0o777`` (octal), and
535 the current umask value is first masked out. Return the file descriptor for
Georg Brandlc575c902008-09-13 17:46:05 +0000536 the newly opened file. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000537
538 For a description of the flag and mode values, see the C run-time documentation;
539 flag constants (like :const:`O_RDONLY` and :const:`O_WRONLY`) are defined in
540 this module too (see below).
541
542 .. note::
543
Georg Brandl502d9a52009-07-26 15:02:41 +0000544 This function is intended for low-level I/O. For normal usage, use the
545 built-in function :func:`open`, which returns a "file object" with
546 :meth:`~file.read` and :meth:`~file.write` methods (and many more). To
547 wrap a file descriptor in a "file object", use :func:`fdopen`.
Georg Brandl116aa622007-08-15 14:28:22 +0000548
549
550.. function:: openpty()
551
552 .. index:: module: pty
553
554 Open a new pseudo-terminal pair. Return a pair of file descriptors ``(master,
555 slave)`` for the pty and the tty, respectively. For a (slightly) more portable
Georg Brandlc575c902008-09-13 17:46:05 +0000556 approach, use the :mod:`pty` module. Availability: some flavors of
Georg Brandl116aa622007-08-15 14:28:22 +0000557 Unix.
558
559
560.. function:: pipe()
561
562 Create a pipe. Return a pair of file descriptors ``(r, w)`` usable for reading
Georg Brandlc575c902008-09-13 17:46:05 +0000563 and writing, respectively. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000564
565
566.. function:: read(fd, n)
567
Georg Brandlb90be692009-07-29 16:14:16 +0000568 Read at most *n* bytes from file descriptor *fd*. Return a bytestring containing the
Georg Brandl116aa622007-08-15 14:28:22 +0000569 bytes read. If the end of the file referred to by *fd* has been reached, an
Georg Brandlb90be692009-07-29 16:14:16 +0000570 empty bytes object is returned. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000571
572 .. note::
573
574 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000575 descriptor as returned by :func:`os.open` or :func:`pipe`. To read a "file object"
Georg Brandl116aa622007-08-15 14:28:22 +0000576 returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000577 :func:`fdopen`, or :data:`sys.stdin`, use its :meth:`~file.read` or
578 :meth:`~file.readline` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000579
580
581.. function:: tcgetpgrp(fd)
582
583 Return the process group associated with the terminal given by *fd* (an open
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000584 file descriptor as returned by :func:`os.open`). Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000585
586
587.. function:: tcsetpgrp(fd, pg)
588
589 Set the process group associated with the terminal given by *fd* (an open file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000590 descriptor as returned by :func:`os.open`) to *pg*. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000591
592
593.. function:: ttyname(fd)
594
595 Return a string which specifies the terminal device associated with
Georg Brandl9afde1c2007-11-01 20:32:30 +0000596 file descriptor *fd*. If *fd* is not associated with a terminal device, an
Georg Brandlc575c902008-09-13 17:46:05 +0000597 exception is raised. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000598
599
600.. function:: write(fd, str)
601
Georg Brandlb90be692009-07-29 16:14:16 +0000602 Write the bytestring in *str* to file descriptor *fd*. Return the number of
603 bytes actually written. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000604
605 .. note::
606
607 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000608 descriptor as returned by :func:`os.open` or :func:`pipe`. To write a "file
Georg Brandl116aa622007-08-15 14:28:22 +0000609 object" returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000610 :func:`fdopen`, or :data:`sys.stdout` or :data:`sys.stderr`, use its
611 :meth:`~file.write` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000612
Georg Brandlaf265f42008-12-07 15:06:20 +0000613The following constants are options for the *flags* parameter to the
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000614:func:`~os.open` function. They can be combined using the bitwise OR operator
Georg Brandlaf265f42008-12-07 15:06:20 +0000615``|``. Some of them are not available on all platforms. For descriptions of
616their availability and use, consult the :manpage:`open(2)` manual page on Unix
Doug Hellmanneb097fc2009-09-20 20:56:56 +0000617or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000618
619
620.. data:: O_RDONLY
621 O_WRONLY
622 O_RDWR
623 O_APPEND
624 O_CREAT
625 O_EXCL
626 O_TRUNC
627
Georg Brandlaf265f42008-12-07 15:06:20 +0000628 These constants are available on Unix and Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000629
630
631.. data:: O_DSYNC
632 O_RSYNC
633 O_SYNC
634 O_NDELAY
635 O_NONBLOCK
636 O_NOCTTY
637 O_SHLOCK
638 O_EXLOCK
639
Georg Brandlaf265f42008-12-07 15:06:20 +0000640 These constants are only available on Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000641
642
643.. data:: O_BINARY
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000644 O_NOINHERIT
Georg Brandl116aa622007-08-15 14:28:22 +0000645 O_SHORT_LIVED
646 O_TEMPORARY
647 O_RANDOM
648 O_SEQUENTIAL
649 O_TEXT
650
Georg Brandlaf265f42008-12-07 15:06:20 +0000651 These constants are only available on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000652
653
Alexandre Vassalottibee32532008-05-16 18:15:12 +0000654.. data:: O_ASYNC
655 O_DIRECT
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000656 O_DIRECTORY
657 O_NOFOLLOW
658 O_NOATIME
659
Georg Brandlaf265f42008-12-07 15:06:20 +0000660 These constants are GNU extensions and not present if they are not defined by
661 the C library.
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000662
663
Georg Brandl116aa622007-08-15 14:28:22 +0000664.. data:: SEEK_SET
665 SEEK_CUR
666 SEEK_END
667
668 Parameters to the :func:`lseek` function. Their values are 0, 1, and 2,
Georg Brandlc575c902008-09-13 17:46:05 +0000669 respectively. Availability: Windows, Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000670
Georg Brandl116aa622007-08-15 14:28:22 +0000671
672.. _os-file-dir:
673
674Files and Directories
675---------------------
676
Georg Brandl116aa622007-08-15 14:28:22 +0000677.. function:: access(path, mode)
678
679 Use the real uid/gid to test for access to *path*. Note that most operations
680 will use the effective uid/gid, therefore this routine can be used in a
681 suid/sgid environment to test if the invoking user has the specified access to
682 *path*. *mode* should be :const:`F_OK` to test the existence of *path*, or it
683 can be the inclusive OR of one or more of :const:`R_OK`, :const:`W_OK`, and
684 :const:`X_OK` to test permissions. Return :const:`True` if access is allowed,
685 :const:`False` if not. See the Unix man page :manpage:`access(2)` for more
Georg Brandlc575c902008-09-13 17:46:05 +0000686 information. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000687
688 .. note::
689
Georg Brandl502d9a52009-07-26 15:02:41 +0000690 Using :func:`access` to check if a user is authorized to e.g. open a file
691 before actually doing so using :func:`open` creates a security hole,
692 because the user might exploit the short time interval between checking
693 and opening the file to manipulate it.
Georg Brandl116aa622007-08-15 14:28:22 +0000694
695 .. note::
696
697 I/O operations may fail even when :func:`access` indicates that they would
698 succeed, particularly for operations on network filesystems which may have
699 permissions semantics beyond the usual POSIX permission-bit model.
700
701
702.. data:: F_OK
703
704 Value to pass as the *mode* parameter of :func:`access` to test the existence of
705 *path*.
706
707
708.. data:: R_OK
709
710 Value to include in the *mode* parameter of :func:`access` to test the
711 readability of *path*.
712
713
714.. data:: W_OK
715
716 Value to include in the *mode* parameter of :func:`access` to test the
717 writability of *path*.
718
719
720.. data:: X_OK
721
722 Value to include in the *mode* parameter of :func:`access` to determine if
723 *path* can be executed.
724
725
726.. function:: chdir(path)
727
728 .. index:: single: directory; changing
729
Georg Brandlc575c902008-09-13 17:46:05 +0000730 Change the current working directory to *path*. Availability: Unix,
Georg Brandl116aa622007-08-15 14:28:22 +0000731 Windows.
732
733
734.. function:: fchdir(fd)
735
736 Change the current working directory to the directory represented by the file
737 descriptor *fd*. The descriptor must refer to an opened directory, not an open
738 file. Availability: Unix.
739
Georg Brandl116aa622007-08-15 14:28:22 +0000740
741.. function:: getcwd()
742
Martin v. Löwis011e8422009-05-05 04:43:17 +0000743 Return a string representing the current working directory.
744 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000745
Martin v. Löwisa731b992008-10-07 06:36:31 +0000746.. function:: getcwdb()
Georg Brandl116aa622007-08-15 14:28:22 +0000747
Georg Brandl76e55382008-10-08 16:34:57 +0000748 Return a bytestring representing the current working directory.
Georg Brandlc575c902008-09-13 17:46:05 +0000749 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000750
Georg Brandl116aa622007-08-15 14:28:22 +0000751
752.. function:: chflags(path, flags)
753
754 Set the flags of *path* to the numeric *flags*. *flags* may take a combination
755 (bitwise OR) of the following values (as defined in the :mod:`stat` module):
756
757 * ``UF_NODUMP``
758 * ``UF_IMMUTABLE``
759 * ``UF_APPEND``
760 * ``UF_OPAQUE``
761 * ``UF_NOUNLINK``
762 * ``SF_ARCHIVED``
763 * ``SF_IMMUTABLE``
764 * ``SF_APPEND``
765 * ``SF_NOUNLINK``
766 * ``SF_SNAPSHOT``
767
Georg Brandlc575c902008-09-13 17:46:05 +0000768 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000769
Georg Brandl116aa622007-08-15 14:28:22 +0000770
771.. function:: chroot(path)
772
773 Change the root directory of the current process to *path*. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +0000774 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000775
Georg Brandl116aa622007-08-15 14:28:22 +0000776
777.. function:: chmod(path, mode)
778
779 Change the mode of *path* to the numeric *mode*. *mode* may take one of the
Christian Heimesfaf2f632008-01-06 16:59:19 +0000780 following values (as defined in the :mod:`stat` module) or bitwise ORed
Georg Brandl116aa622007-08-15 14:28:22 +0000781 combinations of them:
782
Alexandre Vassalottic22c6f22009-07-21 00:51:58 +0000783 * :data:`stat.S_ISUID`
784 * :data:`stat.S_ISGID`
785 * :data:`stat.S_ENFMT`
786 * :data:`stat.S_ISVTX`
787 * :data:`stat.S_IREAD`
788 * :data:`stat.S_IWRITE`
789 * :data:`stat.S_IEXEC`
790 * :data:`stat.S_IRWXU`
791 * :data:`stat.S_IRUSR`
792 * :data:`stat.S_IWUSR`
793 * :data:`stat.S_IXUSR`
794 * :data:`stat.S_IRWXG`
795 * :data:`stat.S_IRGRP`
796 * :data:`stat.S_IWGRP`
797 * :data:`stat.S_IXGRP`
798 * :data:`stat.S_IRWXO`
799 * :data:`stat.S_IROTH`
800 * :data:`stat.S_IWOTH`
801 * :data:`stat.S_IXOTH`
Georg Brandl116aa622007-08-15 14:28:22 +0000802
Georg Brandlc575c902008-09-13 17:46:05 +0000803 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000804
805 .. note::
806
807 Although Windows supports :func:`chmod`, you can only set the file's read-only
808 flag with it (via the ``stat.S_IWRITE`` and ``stat.S_IREAD``
809 constants or a corresponding integer value). All other bits are
810 ignored.
811
812
813.. function:: chown(path, uid, gid)
814
815 Change the owner and group id of *path* to the numeric *uid* and *gid*. To leave
Georg Brandlc575c902008-09-13 17:46:05 +0000816 one of the ids unchanged, set it to -1. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000817
818
819.. function:: lchflags(path, flags)
820
821 Set the flags of *path* to the numeric *flags*, like :func:`chflags`, but do not
822 follow symbolic links. Availability: Unix.
823
Georg Brandl116aa622007-08-15 14:28:22 +0000824
Christian Heimes93852662007-12-01 12:22:32 +0000825.. function:: lchmod(path, mode)
826
827 Change the mode of *path* to the numeric *mode*. If path is a symlink, this
828 affects the symlink rather than the target. See the docs for :func:`chmod`
829 for possible values of *mode*. Availability: Unix.
830
Christian Heimes93852662007-12-01 12:22:32 +0000831
Georg Brandl116aa622007-08-15 14:28:22 +0000832.. function:: lchown(path, uid, gid)
833
Christian Heimesfaf2f632008-01-06 16:59:19 +0000834 Change the owner and group id of *path* to the numeric *uid* and *gid*. This
Georg Brandlc575c902008-09-13 17:46:05 +0000835 function will not follow symbolic links. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000836
Georg Brandl116aa622007-08-15 14:28:22 +0000837
Benjamin Peterson5879d412009-03-30 14:51:56 +0000838.. function:: link(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +0000839
Benjamin Peterson5879d412009-03-30 14:51:56 +0000840 Create a hard link pointing to *source* named *link_name*. Availability:
841 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000842
843
844.. function:: listdir(path)
845
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000846 Return a list containing the names of the entries in the directory given by
847 *path*. The list is in arbitrary order. It does not include the special
848 entries ``'.'`` and ``'..'`` even if they are present in the directory.
849 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000850
Martin v. Löwis011e8422009-05-05 04:43:17 +0000851 This function can be called with a bytes or string argument, and returns
852 filenames of the same datatype.
Georg Brandl116aa622007-08-15 14:28:22 +0000853
854
855.. function:: lstat(path)
856
Guido van Rossum2cc30da2007-11-02 23:46:40 +0000857 Like :func:`stat`, but do not follow symbolic links. This is an alias for
858 :func:`stat` on platforms that do not support symbolic links, such as
859 Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000860
861
862.. function:: mkfifo(path[, mode])
863
Georg Brandlf4a41232008-05-26 17:55:52 +0000864 Create a FIFO (a named pipe) named *path* with numeric mode *mode*. The
865 default *mode* is ``0o666`` (octal). The current umask value is first masked
Georg Brandlc575c902008-09-13 17:46:05 +0000866 out from the mode. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000867
868 FIFOs are pipes that can be accessed like regular files. FIFOs exist until they
869 are deleted (for example with :func:`os.unlink`). Generally, FIFOs are used as
870 rendezvous between "client" and "server" type processes: the server opens the
871 FIFO for reading, and the client opens it for writing. Note that :func:`mkfifo`
872 doesn't open the FIFO --- it just creates the rendezvous point.
873
874
Georg Brandl18244152009-09-02 20:34:52 +0000875.. function:: mknod(filename[, mode=0o600[, device]])
Georg Brandl116aa622007-08-15 14:28:22 +0000876
877 Create a filesystem node (file, device special file or named pipe) named
Georg Brandl18244152009-09-02 20:34:52 +0000878 *filename*. *mode* specifies both the permissions to use and the type of node
879 to be created, being combined (bitwise OR) with one of ``stat.S_IFREG``,
880 ``stat.S_IFCHR``, ``stat.S_IFBLK``, and ``stat.S_IFIFO`` (those constants are
881 available in :mod:`stat`). For ``stat.S_IFCHR`` and ``stat.S_IFBLK``,
882 *device* defines the newly created device special file (probably using
Georg Brandl116aa622007-08-15 14:28:22 +0000883 :func:`os.makedev`), otherwise it is ignored.
884
Georg Brandl116aa622007-08-15 14:28:22 +0000885
886.. function:: major(device)
887
Christian Heimesfaf2f632008-01-06 16:59:19 +0000888 Extract the device major number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000889 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
890
Georg Brandl116aa622007-08-15 14:28:22 +0000891
892.. function:: minor(device)
893
Christian Heimesfaf2f632008-01-06 16:59:19 +0000894 Extract the device minor number from a raw device number (usually the
Georg Brandl116aa622007-08-15 14:28:22 +0000895 :attr:`st_dev` or :attr:`st_rdev` field from :ctype:`stat`).
896
Georg Brandl116aa622007-08-15 14:28:22 +0000897
898.. function:: makedev(major, minor)
899
Christian Heimesfaf2f632008-01-06 16:59:19 +0000900 Compose a raw device number from the major and minor device numbers.
Georg Brandl116aa622007-08-15 14:28:22 +0000901
Georg Brandl116aa622007-08-15 14:28:22 +0000902
903.. function:: mkdir(path[, mode])
904
Georg Brandlf4a41232008-05-26 17:55:52 +0000905 Create a directory named *path* with numeric mode *mode*. The default *mode*
906 is ``0o777`` (octal). On some systems, *mode* is ignored. Where it is used,
Georg Brandlc575c902008-09-13 17:46:05 +0000907 the current umask value is first masked out. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000908
Guido van Rossum2cc30da2007-11-02 23:46:40 +0000909 It is also possible to create temporary directories; see the
910 :mod:`tempfile` module's :func:`tempfile.mkdtemp` function.
911
Georg Brandl116aa622007-08-15 14:28:22 +0000912
913.. function:: makedirs(path[, mode])
914
915 .. index::
916 single: directory; creating
917 single: UNC paths; and os.makedirs()
918
919 Recursive directory creation function. Like :func:`mkdir`, but makes all
Georg Brandlf4a41232008-05-26 17:55:52 +0000920 intermediate-level directories needed to contain the leaf directory. Throws
921 an :exc:`error` exception if the leaf directory already exists or cannot be
922 created. The default *mode* is ``0o777`` (octal). On some systems, *mode*
923 is ignored. Where it is used, the current umask value is first masked out.
Georg Brandl116aa622007-08-15 14:28:22 +0000924
925 .. note::
926
927 :func:`makedirs` will become confused if the path elements to create include
Christian Heimesfaf2f632008-01-06 16:59:19 +0000928 :data:`os.pardir`.
Georg Brandl116aa622007-08-15 14:28:22 +0000929
Georg Brandl55ac8f02007-09-01 13:51:09 +0000930 This function handles UNC paths correctly.
Georg Brandl116aa622007-08-15 14:28:22 +0000931
932
933.. function:: pathconf(path, name)
934
935 Return system configuration information relevant to a named file. *name*
936 specifies the configuration value to retrieve; it may be a string which is the
937 name of a defined system value; these names are specified in a number of
938 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
939 additional names as well. The names known to the host operating system are
940 given in the ``pathconf_names`` dictionary. For configuration variables not
941 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandlc575c902008-09-13 17:46:05 +0000942 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000943
944 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
945 specific value for *name* is not supported by the host system, even if it is
946 included in ``pathconf_names``, an :exc:`OSError` is raised with
947 :const:`errno.EINVAL` for the error number.
948
949
950.. data:: pathconf_names
951
952 Dictionary mapping names accepted by :func:`pathconf` and :func:`fpathconf` to
953 the integer values defined for those names by the host operating system. This
954 can be used to determine the set of names known to the system. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +0000955 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000956
957
958.. function:: readlink(path)
959
960 Return a string representing the path to which the symbolic link points. The
961 result may be either an absolute or relative pathname; if it is relative, it may
962 be converted to an absolute pathname using ``os.path.join(os.path.dirname(path),
963 result)``.
964
Georg Brandl76e55382008-10-08 16:34:57 +0000965 If the *path* is a string object, the result will also be a string object,
966 and the call may raise an UnicodeDecodeError. If the *path* is a bytes
967 object, the result will be a bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +0000968
Georg Brandlc575c902008-09-13 17:46:05 +0000969 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000970
971
972.. function:: remove(path)
973
Georg Brandla6053b42009-09-01 08:11:14 +0000974 Remove (delete) the file *path*. If *path* is a directory, :exc:`OSError` is
975 raised; see :func:`rmdir` below to remove a directory. This is identical to
976 the :func:`unlink` function documented below. On Windows, attempting to
977 remove a file that is in use causes an exception to be raised; on Unix, the
978 directory entry is removed but the storage allocated to the file is not made
979 available until the original file is no longer in use. Availability: Unix,
Georg Brandl116aa622007-08-15 14:28:22 +0000980 Windows.
981
982
983.. function:: removedirs(path)
984
985 .. index:: single: directory; deleting
986
Christian Heimesfaf2f632008-01-06 16:59:19 +0000987 Remove directories recursively. Works like :func:`rmdir` except that, if the
Georg Brandl116aa622007-08-15 14:28:22 +0000988 leaf directory is successfully removed, :func:`removedirs` tries to
989 successively remove every parent directory mentioned in *path* until an error
990 is raised (which is ignored, because it generally means that a parent directory
991 is not empty). For example, ``os.removedirs('foo/bar/baz')`` will first remove
992 the directory ``'foo/bar/baz'``, and then remove ``'foo/bar'`` and ``'foo'`` if
993 they are empty. Raises :exc:`OSError` if the leaf directory could not be
994 successfully removed.
995
Georg Brandl116aa622007-08-15 14:28:22 +0000996
997.. function:: rename(src, dst)
998
999 Rename the file or directory *src* to *dst*. If *dst* is a directory,
1000 :exc:`OSError` will be raised. On Unix, if *dst* exists and is a file, it will
Christian Heimesfaf2f632008-01-06 16:59:19 +00001001 be replaced silently if the user has permission. The operation may fail on some
Georg Brandl116aa622007-08-15 14:28:22 +00001002 Unix flavors if *src* and *dst* are on different filesystems. If successful,
1003 the renaming will be an atomic operation (this is a POSIX requirement). On
1004 Windows, if *dst* already exists, :exc:`OSError` will be raised even if it is a
1005 file; there may be no way to implement an atomic rename when *dst* names an
Georg Brandlc575c902008-09-13 17:46:05 +00001006 existing file. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001007
1008
1009.. function:: renames(old, new)
1010
1011 Recursive directory or file renaming function. Works like :func:`rename`, except
1012 creation of any intermediate directories needed to make the new pathname good is
1013 attempted first. After the rename, directories corresponding to rightmost path
1014 segments of the old name will be pruned away using :func:`removedirs`.
1015
Georg Brandl116aa622007-08-15 14:28:22 +00001016 .. note::
1017
1018 This function can fail with the new directory structure made if you lack
1019 permissions needed to remove the leaf directory or file.
1020
1021
1022.. function:: rmdir(path)
1023
Georg Brandla6053b42009-09-01 08:11:14 +00001024 Remove (delete) the directory *path*. Only works when the directory is
1025 empty, otherwise, :exc:`OSError` is raised. In order to remove whole
1026 directory trees, :func:`shutil.rmtree` can be used. Availability: Unix,
1027 Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001028
1029
1030.. function:: stat(path)
1031
1032 Perform a :cfunc:`stat` system call on the given path. The return value is an
1033 object whose attributes correspond to the members of the :ctype:`stat`
1034 structure, namely: :attr:`st_mode` (protection bits), :attr:`st_ino` (inode
1035 number), :attr:`st_dev` (device), :attr:`st_nlink` (number of hard links),
Christian Heimesfaf2f632008-01-06 16:59:19 +00001036 :attr:`st_uid` (user id of owner), :attr:`st_gid` (group id of owner),
Georg Brandl116aa622007-08-15 14:28:22 +00001037 :attr:`st_size` (size of file, in bytes), :attr:`st_atime` (time of most recent
1038 access), :attr:`st_mtime` (time of most recent content modification),
1039 :attr:`st_ctime` (platform dependent; time of most recent metadata change on
1040 Unix, or the time of creation on Windows)::
1041
1042 >>> import os
1043 >>> statinfo = os.stat('somefile.txt')
1044 >>> statinfo
1045 (33188, 422511L, 769L, 1, 1032, 100, 926L, 1105022698,1105022732, 1105022732)
1046 >>> statinfo.st_size
1047 926L
1048 >>>
1049
Georg Brandl116aa622007-08-15 14:28:22 +00001050
1051 On some Unix systems (such as Linux), the following attributes may also be
1052 available: :attr:`st_blocks` (number of blocks allocated for file),
1053 :attr:`st_blksize` (filesystem blocksize), :attr:`st_rdev` (type of device if an
1054 inode device). :attr:`st_flags` (user defined flags for file).
1055
1056 On other Unix systems (such as FreeBSD), the following attributes may be
1057 available (but may be only filled out if root tries to use them): :attr:`st_gen`
1058 (file generation number), :attr:`st_birthtime` (time of file creation).
1059
1060 On Mac OS systems, the following attributes may also be available:
1061 :attr:`st_rsize`, :attr:`st_creator`, :attr:`st_type`.
1062
Georg Brandl116aa622007-08-15 14:28:22 +00001063 .. index:: module: stat
1064
1065 For backward compatibility, the return value of :func:`stat` is also accessible
1066 as a tuple of at least 10 integers giving the most important (and portable)
1067 members of the :ctype:`stat` structure, in the order :attr:`st_mode`,
1068 :attr:`st_ino`, :attr:`st_dev`, :attr:`st_nlink`, :attr:`st_uid`,
1069 :attr:`st_gid`, :attr:`st_size`, :attr:`st_atime`, :attr:`st_mtime`,
1070 :attr:`st_ctime`. More items may be added at the end by some implementations.
1071 The standard module :mod:`stat` defines functions and constants that are useful
1072 for extracting information from a :ctype:`stat` structure. (On Windows, some
1073 items are filled with dummy values.)
1074
1075 .. note::
1076
1077 The exact meaning and resolution of the :attr:`st_atime`, :attr:`st_mtime`, and
1078 :attr:`st_ctime` members depends on the operating system and the file system.
1079 For example, on Windows systems using the FAT or FAT32 file systems,
1080 :attr:`st_mtime` has 2-second resolution, and :attr:`st_atime` has only 1-day
1081 resolution. See your operating system documentation for details.
1082
Georg Brandlc575c902008-09-13 17:46:05 +00001083 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001084
Georg Brandl116aa622007-08-15 14:28:22 +00001085
1086.. function:: stat_float_times([newvalue])
1087
1088 Determine whether :class:`stat_result` represents time stamps as float objects.
1089 If *newvalue* is ``True``, future calls to :func:`stat` return floats, if it is
1090 ``False``, future calls return ints. If *newvalue* is omitted, return the
1091 current setting.
1092
1093 For compatibility with older Python versions, accessing :class:`stat_result` as
1094 a tuple always returns integers.
1095
Georg Brandl55ac8f02007-09-01 13:51:09 +00001096 Python now returns float values by default. Applications which do not work
1097 correctly with floating point time stamps can use this function to restore the
1098 old behaviour.
Georg Brandl116aa622007-08-15 14:28:22 +00001099
1100 The resolution of the timestamps (that is the smallest possible fraction)
1101 depends on the system. Some systems only support second resolution; on these
1102 systems, the fraction will always be zero.
1103
1104 It is recommended that this setting is only changed at program startup time in
1105 the *__main__* module; libraries should never change this setting. If an
1106 application uses a library that works incorrectly if floating point time stamps
1107 are processed, this application should turn the feature off until the library
1108 has been corrected.
1109
1110
1111.. function:: statvfs(path)
1112
1113 Perform a :cfunc:`statvfs` system call on the given path. The return value is
1114 an object whose attributes describe the filesystem on the given path, and
1115 correspond to the members of the :ctype:`statvfs` structure, namely:
1116 :attr:`f_bsize`, :attr:`f_frsize`, :attr:`f_blocks`, :attr:`f_bfree`,
1117 :attr:`f_bavail`, :attr:`f_files`, :attr:`f_ffree`, :attr:`f_favail`,
1118 :attr:`f_flag`, :attr:`f_namemax`. Availability: Unix.
1119
Georg Brandl116aa622007-08-15 14:28:22 +00001120
Benjamin Peterson5879d412009-03-30 14:51:56 +00001121.. function:: symlink(source, link_name)
Georg Brandl116aa622007-08-15 14:28:22 +00001122
Benjamin Peterson5879d412009-03-30 14:51:56 +00001123 Create a symbolic link pointing to *source* named *link_name*. Availability:
1124 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001125
1126
Georg Brandl116aa622007-08-15 14:28:22 +00001127.. function:: unlink(path)
1128
Georg Brandla6053b42009-09-01 08:11:14 +00001129 Remove (delete) the file *path*. This is the same function as
1130 :func:`remove`; the :func:`unlink` name is its traditional Unix
1131 name. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001132
1133
1134.. function:: utime(path, times)
1135
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001136 Set the access and modified times of the file specified by *path*. If *times*
1137 is ``None``, then the file's access and modified times are set to the current
1138 time. (The effect is similar to running the Unix program :program:`touch` on
1139 the path.) Otherwise, *times* must be a 2-tuple of numbers, of the form
1140 ``(atime, mtime)`` which is used to set the access and modified times,
1141 respectively. Whether a directory can be given for *path* depends on whether
1142 the operating system implements directories as files (for example, Windows
1143 does not). Note that the exact times you set here may not be returned by a
1144 subsequent :func:`stat` call, depending on the resolution with which your
1145 operating system records access and modification times; see :func:`stat`.
Georg Brandl116aa622007-08-15 14:28:22 +00001146
Georg Brandlc575c902008-09-13 17:46:05 +00001147 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001148
1149
Georg Brandl18244152009-09-02 20:34:52 +00001150.. function:: walk(top, topdown=True, onerror=None, followlinks=False)
Georg Brandl116aa622007-08-15 14:28:22 +00001151
1152 .. index::
1153 single: directory; walking
1154 single: directory; traversal
1155
Christian Heimesfaf2f632008-01-06 16:59:19 +00001156 Generate the file names in a directory tree by walking the tree
1157 either top-down or bottom-up. For each directory in the tree rooted at directory
Georg Brandl116aa622007-08-15 14:28:22 +00001158 *top* (including *top* itself), it yields a 3-tuple ``(dirpath, dirnames,
1159 filenames)``.
1160
1161 *dirpath* is a string, the path to the directory. *dirnames* is a list of the
1162 names of the subdirectories in *dirpath* (excluding ``'.'`` and ``'..'``).
1163 *filenames* is a list of the names of the non-directory files in *dirpath*.
1164 Note that the names in the lists contain no path components. To get a full path
1165 (which begins with *top*) to a file or directory in *dirpath*, do
1166 ``os.path.join(dirpath, name)``.
1167
Christian Heimesfaf2f632008-01-06 16:59:19 +00001168 If optional argument *topdown* is ``True`` or not specified, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001169 directory is generated before the triples for any of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001170 (directories are generated top-down). If *topdown* is ``False``, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00001171 directory is generated after the triples for all of its subdirectories
Christian Heimesfaf2f632008-01-06 16:59:19 +00001172 (directories are generated bottom-up).
Georg Brandl116aa622007-08-15 14:28:22 +00001173
Christian Heimesfaf2f632008-01-06 16:59:19 +00001174 When *topdown* is ``True``, the caller can modify the *dirnames* list in-place
Georg Brandl116aa622007-08-15 14:28:22 +00001175 (perhaps using :keyword:`del` or slice assignment), and :func:`walk` will only
1176 recurse into the subdirectories whose names remain in *dirnames*; this can be
1177 used to prune the search, impose a specific order of visiting, or even to inform
1178 :func:`walk` about directories the caller creates or renames before it resumes
Christian Heimesfaf2f632008-01-06 16:59:19 +00001179 :func:`walk` again. Modifying *dirnames* when *topdown* is ``False`` is
Georg Brandl116aa622007-08-15 14:28:22 +00001180 ineffective, because in bottom-up mode the directories in *dirnames* are
1181 generated before *dirpath* itself is generated.
1182
Christian Heimesfaf2f632008-01-06 16:59:19 +00001183 By default errors from the :func:`listdir` call are ignored. If optional
Georg Brandl116aa622007-08-15 14:28:22 +00001184 argument *onerror* is specified, it should be a function; it will be called with
1185 one argument, an :exc:`OSError` instance. It can report the error to continue
1186 with the walk, or raise the exception to abort the walk. Note that the filename
1187 is available as the ``filename`` attribute of the exception object.
1188
1189 By default, :func:`walk` will not walk down into symbolic links that resolve to
Christian Heimesfaf2f632008-01-06 16:59:19 +00001190 directories. Set *followlinks* to ``True`` to visit directories pointed to by
Georg Brandl116aa622007-08-15 14:28:22 +00001191 symlinks, on systems that support them.
1192
Georg Brandl116aa622007-08-15 14:28:22 +00001193 .. note::
1194
Christian Heimesfaf2f632008-01-06 16:59:19 +00001195 Be aware that setting *followlinks* to ``True`` can lead to infinite recursion if a
Georg Brandl116aa622007-08-15 14:28:22 +00001196 link points to a parent directory of itself. :func:`walk` does not keep track of
1197 the directories it visited already.
1198
1199 .. note::
1200
1201 If you pass a relative pathname, don't change the current working directory
1202 between resumptions of :func:`walk`. :func:`walk` never changes the current
1203 directory, and assumes that its caller doesn't either.
1204
1205 This example displays the number of bytes taken by non-directory files in each
1206 directory under the starting directory, except that it doesn't look under any
1207 CVS subdirectory::
1208
1209 import os
1210 from os.path import join, getsize
1211 for root, dirs, files in os.walk('python/Lib/email'):
Georg Brandl6911e3c2007-09-04 07:15:32 +00001212 print(root, "consumes", end=" ")
1213 print(sum(getsize(join(root, name)) for name in files), end=" ")
1214 print("bytes in", len(files), "non-directory files")
Georg Brandl116aa622007-08-15 14:28:22 +00001215 if 'CVS' in dirs:
1216 dirs.remove('CVS') # don't visit CVS directories
1217
Christian Heimesfaf2f632008-01-06 16:59:19 +00001218 In the next example, walking the tree bottom-up is essential: :func:`rmdir`
Georg Brandl116aa622007-08-15 14:28:22 +00001219 doesn't allow deleting a directory before the directory is empty::
1220
Christian Heimesfaf2f632008-01-06 16:59:19 +00001221 # Delete everything reachable from the directory named in "top",
Georg Brandl116aa622007-08-15 14:28:22 +00001222 # assuming there are no symbolic links.
1223 # CAUTION: This is dangerous! For example, if top == '/', it
1224 # could delete all your disk files.
1225 import os
1226 for root, dirs, files in os.walk(top, topdown=False):
1227 for name in files:
1228 os.remove(os.path.join(root, name))
1229 for name in dirs:
1230 os.rmdir(os.path.join(root, name))
1231
Georg Brandl116aa622007-08-15 14:28:22 +00001232
1233.. _os-process:
1234
1235Process Management
1236------------------
1237
1238These functions may be used to create and manage processes.
1239
1240The various :func:`exec\*` functions take a list of arguments for the new
1241program loaded into the process. In each case, the first of these arguments is
1242passed to the new program as its own name rather than as an argument a user may
1243have typed on a command line. For the C programmer, this is the ``argv[0]``
1244passed to a program's :cfunc:`main`. For example, ``os.execv('/bin/echo',
1245['foo', 'bar'])`` will only print ``bar`` on standard output; ``foo`` will seem
1246to be ignored.
1247
1248
1249.. function:: abort()
1250
1251 Generate a :const:`SIGABRT` signal to the current process. On Unix, the default
1252 behavior is to produce a core dump; on Windows, the process immediately returns
1253 an exit code of ``3``. Be aware that programs which use :func:`signal.signal`
1254 to register a handler for :const:`SIGABRT` will behave differently.
Georg Brandlc575c902008-09-13 17:46:05 +00001255 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001256
1257
1258.. function:: execl(path, arg0, arg1, ...)
1259 execle(path, arg0, arg1, ..., env)
1260 execlp(file, arg0, arg1, ...)
1261 execlpe(file, arg0, arg1, ..., env)
1262 execv(path, args)
1263 execve(path, args, env)
1264 execvp(file, args)
1265 execvpe(file, args, env)
1266
1267 These functions all execute a new program, replacing the current process; they
1268 do not return. On Unix, the new executable is loaded into the current process,
Christian Heimesfaf2f632008-01-06 16:59:19 +00001269 and will have the same process id as the caller. Errors will be reported as
Georg Brandl48310cd2009-01-03 21:18:54 +00001270 :exc:`OSError` exceptions.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001271
1272 The current process is replaced immediately. Open file objects and
1273 descriptors are not flushed, so if there may be data buffered
1274 on these open files, you should flush them using
1275 :func:`sys.stdout.flush` or :func:`os.fsync` before calling an
1276 :func:`exec\*` function.
Georg Brandl116aa622007-08-15 14:28:22 +00001277
Christian Heimesfaf2f632008-01-06 16:59:19 +00001278 The "l" and "v" variants of the :func:`exec\*` functions differ in how
1279 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001280 to work with if the number of parameters is fixed when the code is written; the
1281 individual parameters simply become additional parameters to the :func:`execl\*`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001282 functions. The "v" variants are good when the number of parameters is
Georg Brandl116aa622007-08-15 14:28:22 +00001283 variable, with the arguments being passed in a list or tuple as the *args*
1284 parameter. In either case, the arguments to the child process should start with
1285 the name of the command being run, but this is not enforced.
1286
Christian Heimesfaf2f632008-01-06 16:59:19 +00001287 The variants which include a "p" near the end (:func:`execlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001288 :func:`execlpe`, :func:`execvp`, and :func:`execvpe`) will use the
1289 :envvar:`PATH` environment variable to locate the program *file*. When the
1290 environment is being replaced (using one of the :func:`exec\*e` variants,
1291 discussed in the next paragraph), the new environment is used as the source of
1292 the :envvar:`PATH` variable. The other variants, :func:`execl`, :func:`execle`,
1293 :func:`execv`, and :func:`execve`, will not use the :envvar:`PATH` variable to
1294 locate the executable; *path* must contain an appropriate absolute or relative
1295 path.
1296
1297 For :func:`execle`, :func:`execlpe`, :func:`execve`, and :func:`execvpe` (note
Christian Heimesfaf2f632008-01-06 16:59:19 +00001298 that these all end in "e"), the *env* parameter must be a mapping which is
Christian Heimesa342c012008-04-20 21:01:16 +00001299 used to define the environment variables for the new process (these are used
1300 instead of the current process' environment); the functions :func:`execl`,
Georg Brandl116aa622007-08-15 14:28:22 +00001301 :func:`execlp`, :func:`execv`, and :func:`execvp` all cause the new process to
Georg Brandl48310cd2009-01-03 21:18:54 +00001302 inherit the environment of the current process.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001303
1304 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001305
1306
1307.. function:: _exit(n)
1308
1309 Exit to the system with status *n*, without calling cleanup handlers, flushing
Georg Brandlc575c902008-09-13 17:46:05 +00001310 stdio buffers, etc. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001311
1312 .. note::
1313
1314 The standard way to exit is ``sys.exit(n)``. :func:`_exit` should normally only
1315 be used in the child process after a :func:`fork`.
1316
Christian Heimesfaf2f632008-01-06 16:59:19 +00001317The following exit codes are defined and can be used with :func:`_exit`,
Georg Brandl116aa622007-08-15 14:28:22 +00001318although they are not required. These are typically used for system programs
1319written in Python, such as a mail server's external command delivery program.
1320
1321.. note::
1322
1323 Some of these may not be available on all Unix platforms, since there is some
1324 variation. These constants are defined where they are defined by the underlying
1325 platform.
1326
1327
1328.. data:: EX_OK
1329
Georg Brandlc575c902008-09-13 17:46:05 +00001330 Exit code that means no error occurred. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001331
Georg Brandl116aa622007-08-15 14:28:22 +00001332
1333.. data:: EX_USAGE
1334
1335 Exit code that means the command was used incorrectly, such as when the wrong
Georg Brandlc575c902008-09-13 17:46:05 +00001336 number of arguments are given. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001337
Georg Brandl116aa622007-08-15 14:28:22 +00001338
1339.. data:: EX_DATAERR
1340
Georg Brandlc575c902008-09-13 17:46:05 +00001341 Exit code that means the input data was incorrect. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001342
Georg Brandl116aa622007-08-15 14:28:22 +00001343
1344.. data:: EX_NOINPUT
1345
1346 Exit code that means an input file did not exist or was not readable.
Georg Brandlc575c902008-09-13 17:46:05 +00001347 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001348
Georg Brandl116aa622007-08-15 14:28:22 +00001349
1350.. data:: EX_NOUSER
1351
Georg Brandlc575c902008-09-13 17:46:05 +00001352 Exit code that means a specified user did not exist. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001353
Georg Brandl116aa622007-08-15 14:28:22 +00001354
1355.. data:: EX_NOHOST
1356
Georg Brandlc575c902008-09-13 17:46:05 +00001357 Exit code that means a specified host did not exist. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001358
Georg Brandl116aa622007-08-15 14:28:22 +00001359
1360.. data:: EX_UNAVAILABLE
1361
1362 Exit code that means that a required service is unavailable. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001363 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001364
Georg Brandl116aa622007-08-15 14:28:22 +00001365
1366.. data:: EX_SOFTWARE
1367
1368 Exit code that means an internal software error was detected. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001369 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001370
Georg Brandl116aa622007-08-15 14:28:22 +00001371
1372.. data:: EX_OSERR
1373
1374 Exit code that means an operating system error was detected, such as the
Georg Brandlc575c902008-09-13 17:46:05 +00001375 inability to fork or create a pipe. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001376
Georg Brandl116aa622007-08-15 14:28:22 +00001377
1378.. data:: EX_OSFILE
1379
1380 Exit code that means some system file did not exist, could not be opened, or had
Georg Brandlc575c902008-09-13 17:46:05 +00001381 some other kind of error. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001382
Georg Brandl116aa622007-08-15 14:28:22 +00001383
1384.. data:: EX_CANTCREAT
1385
1386 Exit code that means a user specified output file could not be created.
Georg Brandlc575c902008-09-13 17:46:05 +00001387 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001388
Georg Brandl116aa622007-08-15 14:28:22 +00001389
1390.. data:: EX_IOERR
1391
1392 Exit code that means that an error occurred while doing I/O on some file.
Georg Brandlc575c902008-09-13 17:46:05 +00001393 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001394
Georg Brandl116aa622007-08-15 14:28:22 +00001395
1396.. data:: EX_TEMPFAIL
1397
1398 Exit code that means a temporary failure occurred. This indicates something
1399 that may not really be an error, such as a network connection that couldn't be
Georg Brandlc575c902008-09-13 17:46:05 +00001400 made during a retryable operation. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001401
Georg Brandl116aa622007-08-15 14:28:22 +00001402
1403.. data:: EX_PROTOCOL
1404
1405 Exit code that means that a protocol exchange was illegal, invalid, or not
Georg Brandlc575c902008-09-13 17:46:05 +00001406 understood. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001407
Georg Brandl116aa622007-08-15 14:28:22 +00001408
1409.. data:: EX_NOPERM
1410
1411 Exit code that means that there were insufficient permissions to perform the
Georg Brandlc575c902008-09-13 17:46:05 +00001412 operation (but not intended for file system problems). Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001413
Georg Brandl116aa622007-08-15 14:28:22 +00001414
1415.. data:: EX_CONFIG
1416
1417 Exit code that means that some kind of configuration error occurred.
Georg Brandlc575c902008-09-13 17:46:05 +00001418 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001419
Georg Brandl116aa622007-08-15 14:28:22 +00001420
1421.. data:: EX_NOTFOUND
1422
1423 Exit code that means something like "an entry was not found". Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001424 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001425
Georg Brandl116aa622007-08-15 14:28:22 +00001426
1427.. function:: fork()
1428
Christian Heimesfaf2f632008-01-06 16:59:19 +00001429 Fork a child process. Return ``0`` in the child and the child's process id in the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001430 parent. If an error occurs :exc:`OSError` is raised.
Benjamin Petersonbcd8ac32008-10-10 22:20:52 +00001431
1432 Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have
1433 known issues when using fork() from a thread.
1434
Georg Brandlc575c902008-09-13 17:46:05 +00001435 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001436
1437
1438.. function:: forkpty()
1439
1440 Fork a child process, using a new pseudo-terminal as the child's controlling
1441 terminal. Return a pair of ``(pid, fd)``, where *pid* is ``0`` in the child, the
1442 new child's process id in the parent, and *fd* is the file descriptor of the
1443 master end of the pseudo-terminal. For a more portable approach, use the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001444 :mod:`pty` module. If an error occurs :exc:`OSError` is raised.
Georg Brandlc575c902008-09-13 17:46:05 +00001445 Availability: some flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001446
1447
1448.. function:: kill(pid, sig)
1449
1450 .. index::
1451 single: process; killing
1452 single: process; signalling
1453
1454 Send signal *sig* to the process *pid*. Constants for the specific signals
1455 available on the host platform are defined in the :mod:`signal` module.
Georg Brandlc575c902008-09-13 17:46:05 +00001456 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001457
1458
1459.. function:: killpg(pgid, sig)
1460
1461 .. index::
1462 single: process; killing
1463 single: process; signalling
1464
Georg Brandlc575c902008-09-13 17:46:05 +00001465 Send the signal *sig* to the process group *pgid*. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001466
Georg Brandl116aa622007-08-15 14:28:22 +00001467
1468.. function:: nice(increment)
1469
1470 Add *increment* to the process's "niceness". Return the new niceness.
Georg Brandlc575c902008-09-13 17:46:05 +00001471 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001472
1473
1474.. function:: plock(op)
1475
1476 Lock program segments into memory. The value of *op* (defined in
Georg Brandlc575c902008-09-13 17:46:05 +00001477 ``<sys/lock.h>``) determines which segments are locked. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001478
1479
1480.. function:: popen(...)
1481 :noindex:
1482
1483 Run child processes, returning opened pipes for communications. These functions
1484 are described in section :ref:`os-newstreams`.
1485
1486
1487.. function:: spawnl(mode, path, ...)
1488 spawnle(mode, path, ..., env)
1489 spawnlp(mode, file, ...)
1490 spawnlpe(mode, file, ..., env)
1491 spawnv(mode, path, args)
1492 spawnve(mode, path, args, env)
1493 spawnvp(mode, file, args)
1494 spawnvpe(mode, file, args, env)
1495
1496 Execute the program *path* in a new process.
1497
1498 (Note that the :mod:`subprocess` module provides more powerful facilities for
1499 spawning new processes and retrieving their results; using that module is
Benjamin Peterson87c8d872009-06-11 22:54:11 +00001500 preferable to using these functions. Check especially the
1501 :ref:`subprocess-replacements` section.)
Georg Brandl116aa622007-08-15 14:28:22 +00001502
Christian Heimesfaf2f632008-01-06 16:59:19 +00001503 If *mode* is :const:`P_NOWAIT`, this function returns the process id of the new
Georg Brandl116aa622007-08-15 14:28:22 +00001504 process; if *mode* is :const:`P_WAIT`, returns the process's exit code if it
1505 exits normally, or ``-signal``, where *signal* is the signal that killed the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001506 process. On Windows, the process id will actually be the process handle, so can
Georg Brandl116aa622007-08-15 14:28:22 +00001507 be used with the :func:`waitpid` function.
1508
Christian Heimesfaf2f632008-01-06 16:59:19 +00001509 The "l" and "v" variants of the :func:`spawn\*` functions differ in how
1510 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00001511 to work with if the number of parameters is fixed when the code is written; the
1512 individual parameters simply become additional parameters to the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001513 :func:`spawnl\*` functions. The "v" variants are good when the number of
Georg Brandl116aa622007-08-15 14:28:22 +00001514 parameters is variable, with the arguments being passed in a list or tuple as
1515 the *args* parameter. In either case, the arguments to the child process must
1516 start with the name of the command being run.
1517
Christian Heimesfaf2f632008-01-06 16:59:19 +00001518 The variants which include a second "p" near the end (:func:`spawnlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00001519 :func:`spawnlpe`, :func:`spawnvp`, and :func:`spawnvpe`) will use the
1520 :envvar:`PATH` environment variable to locate the program *file*. When the
1521 environment is being replaced (using one of the :func:`spawn\*e` variants,
1522 discussed in the next paragraph), the new environment is used as the source of
1523 the :envvar:`PATH` variable. The other variants, :func:`spawnl`,
1524 :func:`spawnle`, :func:`spawnv`, and :func:`spawnve`, will not use the
1525 :envvar:`PATH` variable to locate the executable; *path* must contain an
1526 appropriate absolute or relative path.
1527
1528 For :func:`spawnle`, :func:`spawnlpe`, :func:`spawnve`, and :func:`spawnvpe`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001529 (note that these all end in "e"), the *env* parameter must be a mapping
Christian Heimesa342c012008-04-20 21:01:16 +00001530 which is used to define the environment variables for the new process (they are
1531 used instead of the current process' environment); the functions
Georg Brandl116aa622007-08-15 14:28:22 +00001532 :func:`spawnl`, :func:`spawnlp`, :func:`spawnv`, and :func:`spawnvp` all cause
Benjamin Petersond23f8222009-04-05 19:13:16 +00001533 the new process to inherit the environment of the current process. Note that
1534 keys and values in the *env* dictionary must be strings; invalid keys or
1535 values will cause the function to fail, with a return value of ``127``.
Georg Brandl116aa622007-08-15 14:28:22 +00001536
1537 As an example, the following calls to :func:`spawnlp` and :func:`spawnvpe` are
1538 equivalent::
1539
1540 import os
1541 os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1542
1543 L = ['cp', 'index.html', '/dev/null']
1544 os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1545
1546 Availability: Unix, Windows. :func:`spawnlp`, :func:`spawnlpe`, :func:`spawnvp`
1547 and :func:`spawnvpe` are not available on Windows.
1548
Georg Brandl116aa622007-08-15 14:28:22 +00001549
1550.. data:: P_NOWAIT
1551 P_NOWAITO
1552
1553 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1554 functions. If either of these values is given, the :func:`spawn\*` functions
Christian Heimesfaf2f632008-01-06 16:59:19 +00001555 will return as soon as the new process has been created, with the process id as
Georg Brandlc575c902008-09-13 17:46:05 +00001556 the return value. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001557
Georg Brandl116aa622007-08-15 14:28:22 +00001558
1559.. data:: P_WAIT
1560
1561 Possible value for the *mode* parameter to the :func:`spawn\*` family of
1562 functions. If this is given as *mode*, the :func:`spawn\*` functions will not
1563 return until the new process has run to completion and will return the exit code
1564 of the process the run is successful, or ``-signal`` if a signal kills the
Georg Brandlc575c902008-09-13 17:46:05 +00001565 process. Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001566
Georg Brandl116aa622007-08-15 14:28:22 +00001567
1568.. data:: P_DETACH
1569 P_OVERLAY
1570
1571 Possible values for the *mode* parameter to the :func:`spawn\*` family of
1572 functions. These are less portable than those listed above. :const:`P_DETACH`
1573 is similar to :const:`P_NOWAIT`, but the new process is detached from the
1574 console of the calling process. If :const:`P_OVERLAY` is used, the current
1575 process will be replaced; the :func:`spawn\*` function will not return.
1576 Availability: Windows.
1577
Georg Brandl116aa622007-08-15 14:28:22 +00001578
1579.. function:: startfile(path[, operation])
1580
1581 Start a file with its associated application.
1582
1583 When *operation* is not specified or ``'open'``, this acts like double-clicking
1584 the file in Windows Explorer, or giving the file name as an argument to the
1585 :program:`start` command from the interactive command shell: the file is opened
1586 with whatever application (if any) its extension is associated.
1587
1588 When another *operation* is given, it must be a "command verb" that specifies
1589 what should be done with the file. Common verbs documented by Microsoft are
1590 ``'print'`` and ``'edit'`` (to be used on files) as well as ``'explore'`` and
1591 ``'find'`` (to be used on directories).
1592
1593 :func:`startfile` returns as soon as the associated application is launched.
1594 There is no option to wait for the application to close, and no way to retrieve
1595 the application's exit status. The *path* parameter is relative to the current
1596 directory. If you want to use an absolute path, make sure the first character
1597 is not a slash (``'/'``); the underlying Win32 :cfunc:`ShellExecute` function
1598 doesn't work if it is. Use the :func:`os.path.normpath` function to ensure that
1599 the path is properly encoded for Win32. Availability: Windows.
1600
Georg Brandl116aa622007-08-15 14:28:22 +00001601
1602.. function:: system(command)
1603
1604 Execute the command (a string) in a subshell. This is implemented by calling
Georg Brandl495f7b52009-10-27 15:28:25 +00001605 the Standard C function :cfunc:`system`, and has the same limitations.
1606 Changes to :data:`sys.stdin`, etc. are not reflected in the environment of the
1607 executed command.
Georg Brandl116aa622007-08-15 14:28:22 +00001608
1609 On Unix, the return value is the exit status of the process encoded in the
1610 format specified for :func:`wait`. Note that POSIX does not specify the meaning
1611 of the return value of the C :cfunc:`system` function, so the return value of
1612 the Python function is system-dependent.
1613
1614 On Windows, the return value is that returned by the system shell after running
1615 *command*, given by the Windows environment variable :envvar:`COMSPEC`: on
1616 :program:`command.com` systems (Windows 95, 98 and ME) this is always ``0``; on
1617 :program:`cmd.exe` systems (Windows NT, 2000 and XP) this is the exit status of
1618 the command run; on systems using a non-native shell, consult your shell
1619 documentation.
1620
Georg Brandlc575c902008-09-13 17:46:05 +00001621 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001622
1623 The :mod:`subprocess` module provides more powerful facilities for spawning new
1624 processes and retrieving their results; using that module is preferable to using
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001625 this function. Use the :mod:`subprocess` module. Check especially the
1626 :ref:`subprocess-replacements` section.
Georg Brandl116aa622007-08-15 14:28:22 +00001627
1628
1629.. function:: times()
1630
1631 Return a 5-tuple of floating point numbers indicating accumulated (processor or
1632 other) times, in seconds. The items are: user time, system time, children's
1633 user time, children's system time, and elapsed real time since a fixed point in
1634 the past, in that order. See the Unix manual page :manpage:`times(2)` or the
Georg Brandlc575c902008-09-13 17:46:05 +00001635 corresponding Windows Platform API documentation. Availability: Unix,
Christian Heimes68f5fbe2008-02-14 08:27:37 +00001636 Windows. On Windows, only the first two items are filled, the others are zero.
Georg Brandl116aa622007-08-15 14:28:22 +00001637
1638
1639.. function:: wait()
1640
1641 Wait for completion of a child process, and return a tuple containing its pid
1642 and exit status indication: a 16-bit number, whose low byte is the signal number
1643 that killed the process, and whose high byte is the exit status (if the signal
1644 number is zero); the high bit of the low byte is set if a core file was
Georg Brandlc575c902008-09-13 17:46:05 +00001645 produced. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001646
1647
1648.. function:: waitpid(pid, options)
1649
1650 The details of this function differ on Unix and Windows.
1651
1652 On Unix: Wait for completion of a child process given by process id *pid*, and
1653 return a tuple containing its process id and exit status indication (encoded as
1654 for :func:`wait`). The semantics of the call are affected by the value of the
1655 integer *options*, which should be ``0`` for normal operation.
1656
1657 If *pid* is greater than ``0``, :func:`waitpid` requests status information for
1658 that specific process. If *pid* is ``0``, the request is for the status of any
1659 child in the process group of the current process. If *pid* is ``-1``, the
1660 request pertains to any child of the current process. If *pid* is less than
1661 ``-1``, status is requested for any process in the process group ``-pid`` (the
1662 absolute value of *pid*).
1663
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001664 An :exc:`OSError` is raised with the value of errno when the syscall
1665 returns -1.
1666
Georg Brandl116aa622007-08-15 14:28:22 +00001667 On Windows: Wait for completion of a process given by process handle *pid*, and
1668 return a tuple containing *pid*, and its exit status shifted left by 8 bits
1669 (shifting makes cross-platform use of the function easier). A *pid* less than or
1670 equal to ``0`` has no special meaning on Windows, and raises an exception. The
1671 value of integer *options* has no effect. *pid* can refer to any process whose
1672 id is known, not necessarily a child process. The :func:`spawn` functions called
1673 with :const:`P_NOWAIT` return suitable process handles.
1674
1675
1676.. function:: wait3([options])
1677
1678 Similar to :func:`waitpid`, except no process id argument is given and a
1679 3-element tuple containing the child's process id, exit status indication, and
1680 resource usage information is returned. Refer to :mod:`resource`.\
1681 :func:`getrusage` for details on resource usage information. The option
1682 argument is the same as that provided to :func:`waitpid` and :func:`wait4`.
1683 Availability: Unix.
1684
Georg Brandl116aa622007-08-15 14:28:22 +00001685
1686.. function:: wait4(pid, options)
1687
1688 Similar to :func:`waitpid`, except a 3-element tuple, containing the child's
1689 process id, exit status indication, and resource usage information is returned.
1690 Refer to :mod:`resource`.\ :func:`getrusage` for details on resource usage
1691 information. The arguments to :func:`wait4` are the same as those provided to
1692 :func:`waitpid`. Availability: Unix.
1693
Georg Brandl116aa622007-08-15 14:28:22 +00001694
1695.. data:: WNOHANG
1696
1697 The option for :func:`waitpid` to return immediately if no child process status
1698 is available immediately. The function returns ``(0, 0)`` in this case.
Georg Brandlc575c902008-09-13 17:46:05 +00001699 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001700
1701
1702.. data:: WCONTINUED
1703
1704 This option causes child processes to be reported if they have been continued
1705 from a job control stop since their status was last reported. Availability: Some
1706 Unix systems.
1707
Georg Brandl116aa622007-08-15 14:28:22 +00001708
1709.. data:: WUNTRACED
1710
1711 This option causes child processes to be reported if they have been stopped but
1712 their current state has not been reported since they were stopped. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001713 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001714
Georg Brandl116aa622007-08-15 14:28:22 +00001715
1716The following functions take a process status code as returned by
1717:func:`system`, :func:`wait`, or :func:`waitpid` as a parameter. They may be
1718used to determine the disposition of a process.
1719
Georg Brandl116aa622007-08-15 14:28:22 +00001720.. function:: WCOREDUMP(status)
1721
Christian Heimesfaf2f632008-01-06 16:59:19 +00001722 Return ``True`` if a core dump was generated for the process, otherwise
Georg Brandlc575c902008-09-13 17:46:05 +00001723 return ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001724
Georg Brandl116aa622007-08-15 14:28:22 +00001725
1726.. function:: WIFCONTINUED(status)
1727
Christian Heimesfaf2f632008-01-06 16:59:19 +00001728 Return ``True`` if the process has been continued from a job control stop,
1729 otherwise return ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001730
Georg Brandl116aa622007-08-15 14:28:22 +00001731
1732.. function:: WIFSTOPPED(status)
1733
Christian Heimesfaf2f632008-01-06 16:59:19 +00001734 Return ``True`` if the process has been stopped, otherwise return
Georg Brandl116aa622007-08-15 14:28:22 +00001735 ``False``. Availability: Unix.
1736
1737
1738.. function:: WIFSIGNALED(status)
1739
Christian Heimesfaf2f632008-01-06 16:59:19 +00001740 Return ``True`` if the process exited due to a signal, otherwise return
Georg Brandlc575c902008-09-13 17:46:05 +00001741 ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001742
1743
1744.. function:: WIFEXITED(status)
1745
Christian Heimesfaf2f632008-01-06 16:59:19 +00001746 Return ``True`` if the process exited using the :manpage:`exit(2)` system call,
Georg Brandlc575c902008-09-13 17:46:05 +00001747 otherwise return ``False``. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001748
1749
1750.. function:: WEXITSTATUS(status)
1751
1752 If ``WIFEXITED(status)`` is true, return the integer parameter to the
1753 :manpage:`exit(2)` system call. Otherwise, the return value is meaningless.
Georg Brandlc575c902008-09-13 17:46:05 +00001754 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001755
1756
1757.. function:: WSTOPSIG(status)
1758
Georg Brandlc575c902008-09-13 17:46:05 +00001759 Return the signal which caused the process to stop. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001760
1761
1762.. function:: WTERMSIG(status)
1763
Georg Brandlc575c902008-09-13 17:46:05 +00001764 Return the signal which caused the process to exit. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001765
1766
1767.. _os-path:
1768
1769Miscellaneous System Information
1770--------------------------------
1771
1772
1773.. function:: confstr(name)
1774
1775 Return string-valued system configuration values. *name* specifies the
1776 configuration value to retrieve; it may be a string which is the name of a
1777 defined system value; these names are specified in a number of standards (POSIX,
1778 Unix 95, Unix 98, and others). Some platforms define additional names as well.
1779 The names known to the host operating system are given as the keys of the
1780 ``confstr_names`` dictionary. For configuration variables not included in that
1781 mapping, passing an integer for *name* is also accepted. Availability:
Georg Brandlc575c902008-09-13 17:46:05 +00001782 Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001783
1784 If the configuration value specified by *name* isn't defined, ``None`` is
1785 returned.
1786
1787 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1788 specific value for *name* is not supported by the host system, even if it is
1789 included in ``confstr_names``, an :exc:`OSError` is raised with
1790 :const:`errno.EINVAL` for the error number.
1791
1792
1793.. data:: confstr_names
1794
1795 Dictionary mapping names accepted by :func:`confstr` to the integer values
1796 defined for those names by the host operating system. This can be used to
Georg Brandlc575c902008-09-13 17:46:05 +00001797 determine the set of names known to the system. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001798
1799
1800.. function:: getloadavg()
1801
Christian Heimesa62da1d2008-01-12 19:39:10 +00001802 Return the number of processes in the system run queue averaged over the last
1803 1, 5, and 15 minutes or raises :exc:`OSError` if the load average was
Georg Brandlf08a9dd2008-06-10 16:57:31 +00001804 unobtainable. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001805
Georg Brandl116aa622007-08-15 14:28:22 +00001806
1807.. function:: sysconf(name)
1808
1809 Return integer-valued system configuration values. If the configuration value
1810 specified by *name* isn't defined, ``-1`` is returned. The comments regarding
1811 the *name* parameter for :func:`confstr` apply here as well; the dictionary that
1812 provides information on the known names is given by ``sysconf_names``.
Georg Brandlc575c902008-09-13 17:46:05 +00001813 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001814
1815
1816.. data:: sysconf_names
1817
1818 Dictionary mapping names accepted by :func:`sysconf` to the integer values
1819 defined for those names by the host operating system. This can be used to
Georg Brandlc575c902008-09-13 17:46:05 +00001820 determine the set of names known to the system. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001821
Christian Heimesfaf2f632008-01-06 16:59:19 +00001822The following data values are used to support path manipulation operations. These
Georg Brandl116aa622007-08-15 14:28:22 +00001823are defined for all platforms.
1824
1825Higher-level operations on pathnames are defined in the :mod:`os.path` module.
1826
1827
1828.. data:: curdir
1829
1830 The constant string used by the operating system to refer to the current
Georg Brandlc575c902008-09-13 17:46:05 +00001831 directory. This is ``'.'`` for Windows and POSIX. Also available via
1832 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00001833
1834
1835.. data:: pardir
1836
1837 The constant string used by the operating system to refer to the parent
Georg Brandlc575c902008-09-13 17:46:05 +00001838 directory. This is ``'..'`` for Windows and POSIX. Also available via
1839 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00001840
1841
1842.. data:: sep
1843
Georg Brandlc575c902008-09-13 17:46:05 +00001844 The character used by the operating system to separate pathname components.
1845 This is ``'/'`` for POSIX and ``'\\'`` for Windows. Note that knowing this
1846 is not sufficient to be able to parse or concatenate pathnames --- use
Georg Brandl116aa622007-08-15 14:28:22 +00001847 :func:`os.path.split` and :func:`os.path.join` --- but it is occasionally
1848 useful. Also available via :mod:`os.path`.
1849
1850
1851.. data:: altsep
1852
1853 An alternative character used by the operating system to separate pathname
1854 components, or ``None`` if only one separator character exists. This is set to
1855 ``'/'`` on Windows systems where ``sep`` is a backslash. Also available via
1856 :mod:`os.path`.
1857
1858
1859.. data:: extsep
1860
1861 The character which separates the base filename from the extension; for example,
1862 the ``'.'`` in :file:`os.py`. Also available via :mod:`os.path`.
1863
Georg Brandl116aa622007-08-15 14:28:22 +00001864
1865.. data:: pathsep
1866
1867 The character conventionally used by the operating system to separate search
1868 path components (as in :envvar:`PATH`), such as ``':'`` for POSIX or ``';'`` for
1869 Windows. Also available via :mod:`os.path`.
1870
1871
1872.. data:: defpath
1873
1874 The default search path used by :func:`exec\*p\*` and :func:`spawn\*p\*` if the
1875 environment doesn't have a ``'PATH'`` key. Also available via :mod:`os.path`.
1876
1877
1878.. data:: linesep
1879
1880 The string used to separate (or, rather, terminate) lines on the current
Georg Brandlc575c902008-09-13 17:46:05 +00001881 platform. This may be a single character, such as ``'\n'`` for POSIX, or
1882 multiple characters, for example, ``'\r\n'`` for Windows. Do not use
1883 *os.linesep* as a line terminator when writing files opened in text mode (the
1884 default); use a single ``'\n'`` instead, on all platforms.
Georg Brandl116aa622007-08-15 14:28:22 +00001885
1886
1887.. data:: devnull
1888
Georg Brandlc575c902008-09-13 17:46:05 +00001889 The file path of the null device. For example: ``'/dev/null'`` for POSIX.
1890 Also available via :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00001891
Georg Brandl116aa622007-08-15 14:28:22 +00001892
1893.. _os-miscfunc:
1894
1895Miscellaneous Functions
1896-----------------------
1897
1898
1899.. function:: urandom(n)
1900
1901 Return a string of *n* random bytes suitable for cryptographic use.
1902
1903 This function returns random bytes from an OS-specific randomness source. The
1904 returned data should be unpredictable enough for cryptographic applications,
1905 though its exact quality depends on the OS implementation. On a UNIX-like
1906 system this will query /dev/urandom, and on Windows it will use CryptGenRandom.
1907 If a randomness source is not found, :exc:`NotImplementedError` will be raised.