test_whitespace_eater_unicode(): Make this test Python 2.1 compatible.
[python/dscho.git] / Doc / lib / libos.tex
blob844704b059174048c1284929a22e1923d455f09f
1 \section{\module{os} ---
2 Miscellaneous operating system interfaces}
4 \declaremodule{standard}{os}
5 \modulesynopsis{Miscellaneous operating system interfaces.}
8 This module provides a more portable way of using operating system
9 dependent functionality than importing a operating system dependent
10 built-in module like \refmodule{posix} or \module{nt}.
12 This module searches for an operating system dependent built-in module like
13 \module{mac} or \refmodule{posix} and exports the same functions and data
14 as found there. The design of all Python's built-in operating system dependent
15 modules is such that as long as the same functionality is available,
16 it uses the same interface; for example, the function
17 \code{os.stat(\var{path})} returns stat information about \var{path} in
18 the same format (which happens to have originated with the
19 \POSIX{} interface).
21 Extensions peculiar to a particular operating system are also
22 available through the \module{os} module, but using them is of course a
23 threat to portability!
25 Note that after the first time \module{os} is imported, there is
26 \emph{no} performance penalty in using functions from \module{os}
27 instead of directly from the operating system dependent built-in module,
28 so there should be \emph{no} reason not to use \module{os}!
31 % Frank Stajano <fstajano@uk.research.att.com> complained that it
32 % wasn't clear that the entries described in the subsections were all
33 % available at the module level (most uses of subsections are
34 % different); I think this is only a problem for the HTML version,
35 % where the relationship may not be as clear.
37 \ifhtml
38 The \module{os} module contains many functions and data values.
39 The items below and in the following sub-sections are all available
40 directly from the \module{os} module.
41 \fi
44 \begin{excdesc}{error}
45 This exception is raised when a function returns a system-related
46 error (not for illegal argument types or other incidental errors).
47 This is also known as the built-in exception \exception{OSError}. The
48 accompanying value is a pair containing the numeric error code from
49 \cdata{errno} and the corresponding string, as would be printed by the
50 C function \cfunction{perror()}. See the module
51 \refmodule{errno}\refbimodindex{errno}, which contains names for the
52 error codes defined by the underlying operating system.
54 When exceptions are classes, this exception carries two attributes,
55 \member{errno} and \member{strerror}. The first holds the value of
56 the C \cdata{errno} variable, and the latter holds the corresponding
57 error message from \cfunction{strerror()}. For exceptions that
58 involve a file system path (such as \function{chdir()} or
59 \function{unlink()}), the exception instance will contain a third
60 attribute, \member{filename}, which is the file name passed to the
61 function.
62 \end{excdesc}
64 \begin{datadesc}{name}
65 The name of the operating system dependent module imported. The
66 following names have currently been registered: \code{'posix'},
67 \code{'nt'}, \code{'mac'}, \code{'os2'}, \code{'ce'},
68 \code{'java'}, \code{'riscos'}.
69 \end{datadesc}
71 \begin{datadesc}{path}
72 The corresponding operating system dependent standard module for pathname
73 operations, such as \module{posixpath} or \module{macpath}. Thus,
74 given the proper imports, \code{os.path.split(\var{file})} is
75 equivalent to but more portable than
76 \code{posixpath.split(\var{file})}. Note that this is also an
77 importable module: it may be imported directly as
78 \refmodule{os.path}.
79 \end{datadesc}
83 \subsection{Process Parameters \label{os-procinfo}}
85 These functions and data items provide information and operate on the
86 current process and user.
88 \begin{datadesc}{environ}
89 A mapping object representing the string environment. For example,
90 \code{environ['HOME']} is the pathname of your home directory (on some
91 platforms), and is equivalent to \code{getenv("HOME")} in C.
93 If the platform supports the \function{putenv()} function, this
94 mapping may be used to modify the environment as well as query the
95 environment. \function{putenv()} will be called automatically when
96 the mapping is modified. \note{On some platforms, including
97 FreeBSD and Mac OS X, setting \code{environ} may cause memory leaks.
98 Refer to the system documentation for putenv.}
100 If \function{putenv()} is not provided, this mapping may be passed to
101 the appropriate process-creation functions to cause child processes to
102 use a modified environment.
103 \end{datadesc}
105 \begin{funcdescni}{chdir}{path}
106 \funclineni{fchdir}{fd}
107 \funclineni{getcwd}{}
108 These functions are described in ``Files and Directories'' (section
109 \ref{os-file-dir}).
110 \end{funcdescni}
112 \begin{funcdesc}{ctermid}{}
113 Return the filename corresponding to the controlling terminal of the
114 process.
115 Availability: \UNIX.
116 \end{funcdesc}
118 \begin{funcdesc}{getegid}{}
119 Return the effective group id of the current process. This
120 corresponds to the `set id' bit on the file being executed in the
121 current process.
122 Availability: \UNIX.
123 \end{funcdesc}
125 \begin{funcdesc}{geteuid}{}
126 \index{user!effective id}
127 Return the current process' effective user id.
128 Availability: \UNIX.
129 \end{funcdesc}
131 \begin{funcdesc}{getgid}{}
132 \index{process!group}
133 Return the real group id of the current process.
134 Availability: \UNIX.
135 \end{funcdesc}
137 \begin{funcdesc}{getgroups}{}
138 Return list of supplemental group ids associated with the current
139 process.
140 Availability: \UNIX.
141 \end{funcdesc}
143 \begin{funcdesc}{getlogin}{}
144 Return the name of the user logged in on the controlling terminal of
145 the process. For most purposes, it is more useful to use the
146 environment variable \envvar{LOGNAME} to find out who the user is,
147 or \code{pwd.getpwuid(os.getuid())[0]} to get the login name
148 of the currently effective user ID.
149 Availability: \UNIX.
150 \end{funcdesc}
152 \begin{funcdesc}{getpgid}{pid}
153 Return the process group id of the process with process id \var{pid}.
154 If \var{pid} is 0, the process group id of the current process is
155 returned. Availability: \UNIX.
156 \versionadded{2.3}
157 \end{funcdesc}
159 \begin{funcdesc}{getpgrp}{}
160 \index{process!group}
161 Return the id of the current process group.
162 Availability: \UNIX.
163 \end{funcdesc}
165 \begin{funcdesc}{getpid}{}
166 \index{process!id}
167 Return the current process id.
168 Availability: \UNIX, Windows.
169 \end{funcdesc}
171 \begin{funcdesc}{getppid}{}
172 \index{process!id of parent}
173 Return the parent's process id.
174 Availability: \UNIX.
175 \end{funcdesc}
177 \begin{funcdesc}{getuid}{}
178 \index{user!id}
179 Return the current process' user id.
180 Availability: \UNIX.
181 \end{funcdesc}
183 \begin{funcdesc}{getenv}{varname\optional{, value}}
184 Return the value of the environment variable \var{varname} if it
185 exists, or \var{value} if it doesn't. \var{value} defaults to
186 \code{None}.
187 Availability: most flavors of \UNIX, Windows.
188 \end{funcdesc}
190 \begin{funcdesc}{putenv}{varname, value}
191 \index{environment variables!setting}
192 Set the environment variable named \var{varname} to the string
193 \var{value}. Such changes to the environment affect subprocesses
194 started with \function{os.system()}, \function{popen()} or
195 \function{fork()} and \function{execv()}.
196 Availability: most flavors of \UNIX, Windows.
198 \note{On some platforms, including FreeBSD and Mac OS X,
199 setting \code{environ} may cause memory leaks.
200 Refer to the system documentation for putenv.}
202 When \function{putenv()} is
203 supported, assignments to items in \code{os.environ} are automatically
204 translated into corresponding calls to \function{putenv()}; however,
205 calls to \function{putenv()} don't update \code{os.environ}, so it is
206 actually preferable to assign to items of \code{os.environ}.
207 \end{funcdesc}
209 \begin{funcdesc}{setegid}{egid}
210 Set the current process's effective group id.
211 Availability: \UNIX.
212 \end{funcdesc}
214 \begin{funcdesc}{seteuid}{euid}
215 Set the current process's effective user id.
216 Availability: \UNIX.
217 \end{funcdesc}
219 \begin{funcdesc}{setgid}{gid}
220 Set the current process' group id.
221 Availability: \UNIX.
222 \end{funcdesc}
224 \begin{funcdesc}{setgroups}{groups}
225 Set the list of supplemental group ids associated with the current
226 process to \var{groups}. \var{groups} must be a sequence, and each
227 element must be an integer identifying a group. This operation is
228 typical available only to the superuser.
229 Availability: \UNIX.
230 \versionadded{2.2}
231 \end{funcdesc}
233 \begin{funcdesc}{setpgrp}{}
234 Calls the system call \cfunction{setpgrp()} or \cfunction{setpgrp(0,
235 0)} depending on which version is implemented (if any). See the
236 \UNIX{} manual for the semantics.
237 Availability: \UNIX.
238 \end{funcdesc}
240 \begin{funcdesc}{setpgid}{pid, pgrp} Calls the system call
241 \cfunction{setpgid()} to set the process group id of the process with
242 id \var{pid} to the process group with id \var{pgrp}. See the \UNIX{}
243 manual for the semantics.
244 Availability: \UNIX.
245 \end{funcdesc}
247 \begin{funcdesc}{setreuid}{ruid, euid}
248 Set the current process's real and effective user ids.
249 Availability: \UNIX.
250 \end{funcdesc}
252 \begin{funcdesc}{setregid}{rgid, egid}
253 Set the current process's real and effective group ids.
254 Availability: \UNIX.
255 \end{funcdesc}
257 \begin{funcdesc}{setsid}{}
258 Calls the system call \cfunction{setsid()}. See the \UNIX{} manual
259 for the semantics.
260 Availability: \UNIX.
261 \end{funcdesc}
263 \begin{funcdesc}{setuid}{uid}
264 \index{user!id, setting}
265 Set the current process' user id.
266 Availability: \UNIX.
267 \end{funcdesc}
269 % placed in this section since it relates to errno.... a little weak ;-(
270 \begin{funcdesc}{strerror}{code}
271 Return the error message corresponding to the error code in
272 \var{code}.
273 Availability: \UNIX, Windows.
274 \end{funcdesc}
276 \begin{funcdesc}{umask}{mask}
277 Set the current numeric umask and returns the previous umask.
278 Availability: \UNIX, Windows.
279 \end{funcdesc}
281 \begin{funcdesc}{uname}{}
282 Return a 5-tuple containing information identifying the current
283 operating system. The tuple contains 5 strings:
284 \code{(\var{sysname}, \var{nodename}, \var{release}, \var{version},
285 \var{machine})}. Some systems truncate the nodename to 8
286 characters or to the leading component; a better way to get the
287 hostname is \function{socket.gethostname()}
288 \withsubitem{(in module socket)}{\ttindex{gethostname()}}
289 or even
290 \withsubitem{(in module socket)}{\ttindex{gethostbyaddr()}}
291 \code{socket.gethostbyaddr(socket.gethostname())}.
292 Availability: recent flavors of \UNIX.
293 \end{funcdesc}
297 \subsection{File Object Creation \label{os-newstreams}}
299 These functions create new file objects.
302 \begin{funcdesc}{fdopen}{fd\optional{, mode\optional{, bufsize}}}
303 Return an open file object connected to the file descriptor \var{fd}.
304 \index{I/O control!buffering}
305 The \var{mode} and \var{bufsize} arguments have the same meaning as
306 the corresponding arguments to the built-in \function{open()}
307 function.
308 Availability: Macintosh, \UNIX, Windows.
310 \versionchanged[When specified, the \var{mode} argument must now start
311 with one of the letters \character{r}, \character{w}, or \character{a},
312 otherwise a \exception{ValueError} is raised]{2.3}
313 \end{funcdesc}
315 \begin{funcdesc}{popen}{command\optional{, mode\optional{, bufsize}}}
316 Open a pipe to or from \var{command}. The return value is an open
317 file object connected to the pipe, which can be read or written
318 depending on whether \var{mode} is \code{'r'} (default) or \code{'w'}.
319 The \var{bufsize} argument has the same meaning as the corresponding
320 argument to the built-in \function{open()} function. The exit status of
321 the command (encoded in the format specified for \function{wait()}) is
322 available as the return value of the \method{close()} method of the file
323 object, except that when the exit status is zero (termination without
324 errors), \code{None} is returned.
325 Availability: \UNIX, Windows.
327 \versionchanged[This function worked unreliably under Windows in
328 earlier versions of Python. This was due to the use of the
329 \cfunction{_popen()} function from the libraries provided with
330 Windows. Newer versions of Python do not use the broken
331 implementation from the Windows libraries]{2.0}
332 \end{funcdesc}
334 \begin{funcdesc}{tmpfile}{}
335 Return a new file object opened in update mode (\samp{w+b}). The file
336 has no directory entries associated with it and will be automatically
337 deleted once there are no file descriptors for the file.
338 Availability: \UNIX, Windows.
339 \end{funcdesc}
342 For each of these \function{popen()} variants, if \var{bufsize} is
343 specified, it specifies the buffer size for the I/O pipes.
344 \var{mode}, if provided, should be the string \code{'b'} or
345 \code{'t'}; on Windows this is needed to determine whether the file
346 objects should be opened in binary or text mode. The default value
347 for \var{mode} is \code{'t'}.
349 These methods do not make it possible to retrieve the return code from
350 the child processes. The only way to control the input and output
351 streams and also retrieve the return codes is to use the
352 \class{Popen3} and \class{Popen4} classes from the \refmodule{popen2}
353 module; these are only available on \UNIX.
355 For a discussion of possible deadlock conditions related to the use
356 of these functions, see ``\ulink{Flow Control
357 Issues}{popen2-flow-control.html}''
358 (section~\ref{popen2-flow-control}).
360 \begin{funcdesc}{popen2}{cmd\optional{, mode\optional{, bufsize}}}
361 Executes \var{cmd} as a sub-process. Returns the file objects
362 \code{(\var{child_stdin}, \var{child_stdout})}.
363 Availability: \UNIX, Windows.
364 \versionadded{2.0}
365 \end{funcdesc}
367 \begin{funcdesc}{popen3}{cmd\optional{, mode\optional{, bufsize}}}
368 Executes \var{cmd} as a sub-process. Returns the file objects
369 \code{(\var{child_stdin}, \var{child_stdout}, \var{child_stderr})}.
370 Availability: \UNIX, Windows.
371 \versionadded{2.0}
372 \end{funcdesc}
374 \begin{funcdesc}{popen4}{cmd\optional{, mode\optional{, bufsize}}}
375 Executes \var{cmd} as a sub-process. Returns the file objects
376 \code{(\var{child_stdin}, \var{child_stdout_and_stderr})}.
377 Availability: \UNIX, Windows.
378 \versionadded{2.0}
379 \end{funcdesc}
381 This functionality is also available in the \refmodule{popen2} module
382 using functions of the same names, but the return values of those
383 functions have a different order.
386 \subsection{File Descriptor Operations \label{os-fd-ops}}
388 These functions operate on I/O streams referred to
389 using file descriptors.
392 \begin{funcdesc}{close}{fd}
393 Close file descriptor \var{fd}.
394 Availability: Macintosh, \UNIX, Windows.
396 Note: this function is intended for low-level I/O and must be applied
397 to a file descriptor as returned by \function{open()} or
398 \function{pipe()}. To close a ``file object'' returned by the
399 built-in function \function{open()} or by \function{popen()} or
400 \function{fdopen()}, use its \method{close()} method.
401 \end{funcdesc}
403 \begin{funcdesc}{dup}{fd}
404 Return a duplicate of file descriptor \var{fd}.
405 Availability: Macintosh, \UNIX, Windows.
406 \end{funcdesc}
408 \begin{funcdesc}{dup2}{fd, fd2}
409 Duplicate file descriptor \var{fd} to \var{fd2}, closing the latter
410 first if necessary.
411 Availability: \UNIX, Windows.
412 \end{funcdesc}
414 \begin{funcdesc}{fdatasync}{fd}
415 Force write of file with filedescriptor \var{fd} to disk.
416 Does not force update of metadata.
417 Availability: \UNIX.
418 \end{funcdesc}
420 \begin{funcdesc}{fpathconf}{fd, name}
421 Return system configuration information relevant to an open file.
422 \var{name} specifies the configuration value to retrieve; it may be a
423 string which is the name of a defined system value; these names are
424 specified in a number of standards (\POSIX.1, \UNIX 95, \UNIX 98, and
425 others). Some platforms define additional names as well. The names
426 known to the host operating system are given in the
427 \code{pathconf_names} dictionary. For configuration variables not
428 included in that mapping, passing an integer for \var{name} is also
429 accepted.
430 Availability: \UNIX.
432 If \var{name} is a string and is not known, \exception{ValueError} is
433 raised. If a specific value for \var{name} is not supported by the
434 host system, even if it is included in \code{pathconf_names}, an
435 \exception{OSError} is raised with \constant{errno.EINVAL} for the
436 error number.
437 \end{funcdesc}
439 \begin{funcdesc}{fstat}{fd}
440 Return status for file descriptor \var{fd}, like \function{stat()}.
441 Availability: \UNIX, Windows.
442 \end{funcdesc}
444 \begin{funcdesc}{fstatvfs}{fd}
445 Return information about the filesystem containing the file associated
446 with file descriptor \var{fd}, like \function{statvfs()}.
447 Availability: \UNIX.
448 \end{funcdesc}
450 \begin{funcdesc}{fsync}{fd}
451 Force write of file with filedescriptor \var{fd} to disk.
452 Availability: \UNIX.
453 \end{funcdesc}
455 \begin{funcdesc}{ftruncate}{fd, length}
456 Truncate the file corresponding to file descriptor \var{fd},
457 so that it is at most \var{length} bytes in size.
458 Availability: \UNIX.
459 \end{funcdesc}
461 \begin{funcdesc}{isatty}{fd}
462 Return \code{True} if the file descriptor \var{fd} is open and
463 connected to a tty(-like) device, else \code{False}.
464 Availability: \UNIX.
465 \end{funcdesc}
467 \begin{funcdesc}{lseek}{fd, pos, how}
468 Set the current position of file descriptor \var{fd} to position
469 \var{pos}, modified by \var{how}: \code{0} to set the position
470 relative to the beginning of the file; \code{1} to set it relative to
471 the current position; \code{2} to set it relative to the end of the
472 file.
473 Availability: Macintosh, \UNIX, Windows.
474 \end{funcdesc}
476 \begin{funcdesc}{open}{file, flags\optional{, mode}}
477 Open the file \var{file} and set various flags according to
478 \var{flags} and possibly its mode according to \var{mode}.
479 The default \var{mode} is \code{0777} (octal), and the current umask
480 value is first masked out. Return the file descriptor for the newly
481 opened file.
482 Availability: Macintosh, \UNIX, Windows.
484 For a description of the flag and mode values, see the C run-time
485 documentation; flag constants (like \constant{O_RDONLY} and
486 \constant{O_WRONLY}) are defined in this module too (see below).
488 Note: this function is intended for low-level I/O. For normal usage,
489 use the built-in function \function{open()}, which returns a ``file
490 object'' with \method{read()} and \method{write()} methods (and many
491 more).
492 \end{funcdesc}
494 \begin{funcdesc}{openpty}{}
495 Open a new pseudo-terminal pair. Return a pair of file descriptors
496 \code{(\var{master}, \var{slave})} for the pty and the tty,
497 respectively. For a (slightly) more portable approach, use the
498 \refmodule{pty}\refstmodindex{pty} module.
499 Availability: Some flavors of \UNIX.
500 \end{funcdesc}
502 \begin{funcdesc}{pipe}{}
503 Create a pipe. Return a pair of file descriptors \code{(\var{r},
504 \var{w})} usable for reading and writing, respectively.
505 Availability: \UNIX, Windows.
506 \end{funcdesc}
508 \begin{funcdesc}{read}{fd, n}
509 Read at most \var{n} bytes from file descriptor \var{fd}.
510 Return a string containing the bytes read. If the end of the file
511 referred to by \var{fd} has been reached, an empty string is
512 returned.
513 Availability: Macintosh, \UNIX, Windows.
515 Note: this function is intended for low-level I/O and must be applied
516 to a file descriptor as returned by \function{open()} or
517 \function{pipe()}. To read a ``file object'' returned by the
518 built-in function \function{open()} or by \function{popen()} or
519 \function{fdopen()}, or \code{sys.stdin}, use its
520 \method{read()} or \method{readline()} methods.
521 \end{funcdesc}
523 \begin{funcdesc}{tcgetpgrp}{fd}
524 Return the process group associated with the terminal given by
525 \var{fd} (an open file descriptor as returned by \function{open()}).
526 Availability: \UNIX.
527 \end{funcdesc}
529 \begin{funcdesc}{tcsetpgrp}{fd, pg}
530 Set the process group associated with the terminal given by
531 \var{fd} (an open file descriptor as returned by \function{open()})
532 to \var{pg}.
533 Availability: \UNIX.
534 \end{funcdesc}
536 \begin{funcdesc}{ttyname}{fd}
537 Return a string which specifies the terminal device associated with
538 file-descriptor \var{fd}. If \var{fd} is not associated with a terminal
539 device, an exception is raised.
540 Availability: \UNIX.
541 \end{funcdesc}
543 \begin{funcdesc}{write}{fd, str}
544 Write the string \var{str} to file descriptor \var{fd}.
545 Return the number of bytes actually written.
546 Availability: Macintosh, \UNIX, Windows.
548 Note: this function is intended for low-level I/O and must be applied
549 to a file descriptor as returned by \function{open()} or
550 \function{pipe()}. To write a ``file object'' returned by the
551 built-in function \function{open()} or by \function{popen()} or
552 \function{fdopen()}, or \code{sys.stdout} or \code{sys.stderr}, use
553 its \method{write()} method.
554 \end{funcdesc}
557 The following data items are available for use in constructing the
558 \var{flags} parameter to the \function{open()} function.
560 \begin{datadesc}{O_RDONLY}
561 \dataline{O_WRONLY}
562 \dataline{O_RDWR}
563 \dataline{O_NDELAY}
564 \dataline{O_NONBLOCK}
565 \dataline{O_APPEND}
566 \dataline{O_DSYNC}
567 \dataline{O_RSYNC}
568 \dataline{O_SYNC}
569 \dataline{O_NOCTTY}
570 \dataline{O_CREAT}
571 \dataline{O_EXCL}
572 \dataline{O_TRUNC}
573 Options for the \var{flag} argument to the \function{open()} function.
574 These can be bit-wise OR'd together.
575 Availability: Macintosh, \UNIX, Windows.
576 % XXX O_NDELAY, O_NONBLOCK, O_DSYNC, O_RSYNC, O_SYNC, O_NOCTTY are not on Windows.
577 \end{datadesc}
579 \begin{datadesc}{O_BINARY}
580 Option for the \var{flag} argument to the \function{open()} function.
581 This can be bit-wise OR'd together with those listed above.
582 Availability: Macintosh, Windows.
583 % XXX need to check on the availability of this one.
584 \end{datadesc}
586 \begin{datadesc}{O_NOINHERIT}
587 \dataline{O_SHORT_LIVED}
588 \dataline{O_TEMPORARY}
589 \dataline{O_RANDOM}
590 \dataline{O_SEQUENTIAL}
591 \dataline{O_TEXT}
592 Options for the \var{flag} argument to the \function{open()} function.
593 These can be bit-wise OR'd together.
594 Availability: Windows.
595 \end{datadesc}
597 \subsection{Files and Directories \label{os-file-dir}}
599 \begin{funcdesc}{access}{path, mode}
600 Use the real uid/gid to test for access to \var{path}. Note that most
601 operations will use the effective uid/gid, therefore this routine can
602 be used in a suid/sgid environment to test if the invoking user has the
603 specified access to \var{path}. \var{mode} should be \constant{F_OK}
604 to test the existence of \var{path}, or it can be the inclusive OR of
605 one or more of \constant{R_OK}, \constant{W_OK}, and \constant{X_OK} to
606 test permissions. Return \code{1} if access is allowed, \code{0} if not.
607 See the \UNIX{} man page \manpage{access}{2} for more information.
608 Availability: \UNIX, Windows.
609 \end{funcdesc}
611 \begin{datadesc}{F_OK}
612 Value to pass as the \var{mode} parameter of \function{access()} to
613 test the existence of \var{path}.
614 \end{datadesc}
616 \begin{datadesc}{R_OK}
617 Value to include in the \var{mode} parameter of \function{access()}
618 to test the readability of \var{path}.
619 \end{datadesc}
621 \begin{datadesc}{W_OK}
622 Value to include in the \var{mode} parameter of \function{access()}
623 to test the writability of \var{path}.
624 \end{datadesc}
626 \begin{datadesc}{X_OK}
627 Value to include in the \var{mode} parameter of \function{access()}
628 to determine if \var{path} can be executed.
629 \end{datadesc}
631 \begin{funcdesc}{chdir}{path}
632 \index{directory!changing}
633 Change the current working directory to \var{path}.
634 Availability: Macintosh, \UNIX, Windows.
635 \end{funcdesc}
637 \begin{funcdesc}{fchdir}{fd}
638 Change the current working directory to the directory represented by
639 the file descriptor \var{fd}. The descriptor must refer to an opened
640 directory, not an open file.
641 Availability: \UNIX.
642 \versionadded{2.3}
643 \end{funcdesc}
645 \begin{funcdesc}{getcwd}{}
646 Return a string representing the current working directory.
647 Availability: Macintosh, \UNIX, Windows.
648 \end{funcdesc}
650 \begin{funcdesc}{getcwdu}{}
651 Return a Unicode object representing the current working directory.
652 Availability: \UNIX, Windows.
653 \versionadded{2.3}
654 \end{funcdesc}
656 \begin{funcdesc}{chroot}{path}
657 Change the root directory of the current process to \var{path}.
658 Availability: \UNIX.
659 \versionadded{2.2}
660 \end{funcdesc}
662 \begin{funcdesc}{chmod}{path, mode}
663 Change the mode of \var{path} to the numeric \var{mode}.
664 \var{mode} may take one of the following values:
665 \begin{itemize}
666 \item \code{S_ISUID}
667 \item \code{S_ISGID}
668 \item \code{S_ENFMT}
669 \item \code{S_ISVTX}
670 \item \code{S_IREAD}
671 \item \code{S_IWRITE}
672 \item \code{S_IEXEC}
673 \item \code{S_IRWXU}
674 \item \code{S_IRUSR}
675 \item \code{S_IWUSR}
676 \item \code{S_IXUSR}
677 \item \code{S_IRWXG}
678 \item \code{S_IRGRP}
679 \item \code{S_IWGRP}
680 \item \code{S_IXGRP}
681 \item \code{S_IRWXO}
682 \item \code{S_IROTH}
683 \item \code{S_IWOTH}
684 \item \code{S_IXOTH}
685 \end{itemize}
686 Availability: \UNIX, Windows.
687 \end{funcdesc}
689 \begin{funcdesc}{chown}{path, uid, gid}
690 Change the owner and group id of \var{path} to the numeric \var{uid}
691 and \var{gid}.
692 Availability: \UNIX.
693 \end{funcdesc}
695 \begin{funcdesc}{lchown}{path, uid, gid}
696 Change the owner and group id of \var{path} to the numeric \var{uid}
697 and gid. This function will not follow symbolic links.
698 Availability: \UNIX.
699 \versionadded{2.3}
700 \end{funcdesc}
702 \begin{funcdesc}{link}{src, dst}
703 Create a hard link pointing to \var{src} named \var{dst}.
704 Availability: \UNIX.
705 \end{funcdesc}
707 \begin{funcdesc}{listdir}{path}
708 Return a list containing the names of the entries in the directory.
709 The list is in arbitrary order. It does not include the special
710 entries \code{'.'} and \code{'..'} even if they are present in the
711 directory.
712 Availability: Macintosh, \UNIX, Windows.
714 \versionadded[On Windows NT/2k/XP and Unix, if \var{path} is a Unicode
715 object, the result will be a list of Unicode objects.]{2.3}
716 \end{funcdesc}
718 \begin{funcdesc}{lstat}{path}
719 Like \function{stat()}, but do not follow symbolic links.
720 Availability: \UNIX.
721 \end{funcdesc}
723 \begin{funcdesc}{mkfifo}{path\optional{, mode}}
724 Create a FIFO (a named pipe) named \var{path} with numeric mode
725 \var{mode}. The default \var{mode} is \code{0666} (octal). The current
726 umask value is first masked out from the mode.
727 Availability: \UNIX.
729 FIFOs are pipes that can be accessed like regular files. FIFOs exist
730 until they are deleted (for example with \function{os.unlink()}).
731 Generally, FIFOs are used as rendezvous between ``client'' and
732 ``server'' type processes: the server opens the FIFO for reading, and
733 the client opens it for writing. Note that \function{mkfifo()}
734 doesn't open the FIFO --- it just creates the rendezvous point.
735 \end{funcdesc}
737 \begin{funcdesc}{mknod}{path\optional{, mode=0600, device}}
738 Create a filesystem node (file, device special file or named pipe)
739 named filename. \var{mode} specifies both the permissions to use and
740 the type of node to be created, being combined (bitwise OR) with one
741 of S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO (those constants are
742 available in \module{stat}). For S_IFCHR and S_IFBLK, \var{device}
743 defines the newly created device special file (probably using
744 \function{os.makedev()}), otherwise it is ignored.
746 \versionadded{2.3}
747 \end{funcdesc}
749 \begin{funcdesc}{major}{device}
750 Extracts a device major number from a raw device number.
752 \versionadded{2.3}
753 \end{funcdesc}
755 \begin{funcdesc}{minor}{device}
756 Extracts a device minor number from a raw device number.
758 \versionadded{2.3}
759 \end{funcdesc}
761 \begin{funcdesc}{makedev}{major, minor}
762 Composes a raw device number from the major and minor device numbers.
764 \versionadded{2.3}
765 \end{funcdesc}
767 \begin{funcdesc}{mkdir}{path\optional{, mode}}
768 Create a directory named \var{path} with numeric mode \var{mode}.
769 The default \var{mode} is \code{0777} (octal). On some systems,
770 \var{mode} is ignored. Where it is used, the current umask value is
771 first masked out.
772 Availability: Macintosh, \UNIX, Windows.
773 \end{funcdesc}
775 \begin{funcdesc}{makedirs}{path\optional{, mode}}
776 \index{directory!creating}
777 Recursive directory creation function. Like \function{mkdir()},
778 but makes all intermediate-level directories needed to contain the
779 leaf directory. Throws an \exception{error} exception if the leaf
780 directory already exists or cannot be created. The default \var{mode}
781 is \code{0777} (octal). This function does not properly handle UNC
782 paths (only relevant on Windows systems).
783 \versionadded{1.5.2}
784 \end{funcdesc}
786 \begin{funcdesc}{pathconf}{path, name}
787 Return system configuration information relevant to a named file.
788 \var{name} specifies the configuration value to retrieve; it may be a
789 string which is the name of a defined system value; these names are
790 specified in a number of standards (\POSIX.1, \UNIX 95, \UNIX 98, and
791 others). Some platforms define additional names as well. The names
792 known to the host operating system are given in the
793 \code{pathconf_names} dictionary. For configuration variables not
794 included in that mapping, passing an integer for \var{name} is also
795 accepted.
796 Availability: \UNIX.
798 If \var{name} is a string and is not known, \exception{ValueError} is
799 raised. If a specific value for \var{name} is not supported by the
800 host system, even if it is included in \code{pathconf_names}, an
801 \exception{OSError} is raised with \constant{errno.EINVAL} for the
802 error number.
803 \end{funcdesc}
805 \begin{datadesc}{pathconf_names}
806 Dictionary mapping names accepted by \function{pathconf()} and
807 \function{fpathconf()} to the integer values defined for those names
808 by the host operating system. This can be used to determine the set
809 of names known to the system.
810 Availability: \UNIX.
811 \end{datadesc}
813 \begin{funcdesc}{readlink}{path}
814 Return a string representing the path to which the symbolic link
815 points. The result may be either an absolute or relative pathname; if
816 it is relative, it may be converted to an absolute pathname using
817 \code{os.path.join(os.path.dirname(\var{path}), \var{result})}.
818 Availability: \UNIX.
819 \end{funcdesc}
821 \begin{funcdesc}{remove}{path}
822 Remove the file \var{path}. If \var{path} is a directory,
823 \exception{OSError} is raised; see \function{rmdir()} below to remove
824 a directory. This is identical to the \function{unlink()} function
825 documented below. On Windows, attempting to remove a file that is in
826 use causes an exception to be raised; on \UNIX, the directory entry is
827 removed but the storage allocated to the file is not made available
828 until the original file is no longer in use.
829 Availability: Macintosh, \UNIX, Windows.
830 \end{funcdesc}
832 \begin{funcdesc}{removedirs}{path}
833 \index{directory!deleting}
834 Removes directories recursively. Works like
835 \function{rmdir()} except that, if the leaf directory is
836 successfully removed, directories corresponding to rightmost path
837 segments will be pruned way until either the whole path is consumed or
838 an error is raised (which is ignored, because it generally means that
839 a parent directory is not empty). Throws an \exception{error}
840 exception if the leaf directory could not be successfully removed.
841 \versionadded{1.5.2}
842 \end{funcdesc}
844 \begin{funcdesc}{rename}{src, dst}
845 Rename the file or directory \var{src} to \var{dst}. If \var{dst} is
846 a directory, \exception{OSError} will be raised. On \UNIX, if
847 \var{dst} exists and is a file, it will be removed silently if the
848 user has permission. The operation may fail on some \UNIX{} flavors
849 if \var{src} and \var{dst} are on different filesystems. If
850 successful, the renaming will be an atomic operation (this is a
851 \POSIX{} requirement). On Windows, if \var{dst} already exists,
852 \exception{OSError} will be raised even if it is a file; there may be
853 no way to implement an atomic rename when \var{dst} names an existing
854 file.
855 Availability: Macintosh, \UNIX, Windows.
856 \end{funcdesc}
858 \begin{funcdesc}{renames}{old, new}
859 Recursive directory or file renaming function.
860 Works like \function{rename()}, except creation of any intermediate
861 directories needed to make the new pathname good is attempted first.
862 After the rename, directories corresponding to rightmost path segments
863 of the old name will be pruned away using \function{removedirs()}.
865 Note: this function can fail with the new directory structure made if
866 you lack permissions needed to remove the leaf directory or file.
867 \versionadded{1.5.2}
868 \end{funcdesc}
870 \begin{funcdesc}{rmdir}{path}
871 Remove the directory \var{path}.
872 Availability: Macintosh, \UNIX, Windows.
873 \end{funcdesc}
875 \begin{funcdesc}{stat}{path}
876 Perform a \cfunction{stat()} system call on the given path. The
877 return value is an object whose attributes correspond to the members of
878 the \ctype{stat} structure, namely:
879 \member{st_mode} (protection bits),
880 \member{st_ino} (inode number),
881 \member{st_dev} (device),
882 \member{st_nlink} (number of hard links,
883 \member{st_uid} (user ID of owner),
884 \member{st_gid} (group ID of owner),
885 \member{st_size} (size of file, in bytes),
886 \member{st_atime} (time of most recent access),
887 \member{st_mtime} (time of most recent content modification),
888 \member{st_ctime}
889 (time of most recent content modification or metadata change).
891 \versionchanged [If \function{stat_float_times} returns true, the time
892 values are floats, measuring seconds. Fractions of a second may be
893 reported if the system supports that. On Mac OS, the times are always
894 floats. See \function{stat_float_times} for further discussion. ]{2.3}
896 On some Unix systems (such as Linux), the following attributes may
897 also be available:
898 \member{st_blocks} (number of blocks allocated for file),
899 \member{st_blksize} (filesystem blocksize),
900 \member{st_rdev} (type of device if an inode device).
902 On Mac OS systems, the following attributes may also be available:
903 \member{st_rsize},
904 \member{st_creator},
905 \member{st_type}.
907 On RISCOS systems, the following attributes are also available:
908 \member{st_ftype} (file type),
909 \member{st_attrs} (attributes),
910 \member{st_obtype} (object type).
912 For backward compatibility, the return value of \function{stat()} is
913 also accessible as a tuple of at least 10 integers giving the most
914 important (and portable) members of the \ctype{stat} structure, in the
915 order
916 \member{st_mode},
917 \member{st_ino},
918 \member{st_dev},
919 \member{st_nlink},
920 \member{st_uid},
921 \member{st_gid},
922 \member{st_size},
923 \member{st_atime},
924 \member{st_mtime},
925 \member{st_ctime}.
926 More items may be added at the end by some implementations.
927 The standard module \refmodule{stat}\refstmodindex{stat} defines
928 functions and constants that are useful for extracting information
929 from a \ctype{stat} structure.
930 (On Windows, some items are filled with dummy values.)
931 Availability: Macintosh, \UNIX, Windows.
933 \versionchanged
934 [Added access to values as attributes of the returned object]{2.2}
935 \end{funcdesc}
937 \begin{funcdesc}{stat_float_times}{\optional{newvalue}}
938 Determine whether \class{stat_result} represents time stamps as float
939 objects. If newval is True, future calls to stat() return floats, if
940 it is False, future calls return ints. If newval is omitted, return
941 the current setting.
943 For compatibility with older Python versions, accessing
944 \class{stat_result} as a tuple always returns integers. For
945 compatibility with Python 2.2, accessing the time stamps by field name
946 also returns integers. Applications that want to determine the
947 fractions of a second in a time stamp can use this function to have
948 time stamps represented as floats. Whether they will actually observe
949 non-zero fractions depends on the system.
951 Future Python releases will change the default of this setting;
952 applications that cannot deal with floating point time stamps can then
953 use this function to turn the feature off.
955 It is recommended that this setting is only changed at program startup
956 time in the \var{__main__} module; libraries should never change this
957 setting. If an application uses a library that works incorrectly if
958 floating point time stamps are processed, this application should turn
959 the feature off until the library has been corrected.
961 \end{funcdesc}
963 \begin{funcdesc}{statvfs}{path}
964 Perform a \cfunction{statvfs()} system call on the given path. The
965 return value is an object whose attributes describe the filesystem on
966 the given path, and correspond to the members of the
967 \ctype{statvfs} structure, namely:
968 \member{f_frsize},
969 \member{f_blocks},
970 \member{f_bfree},
971 \member{f_bavail},
972 \member{f_files},
973 \member{f_ffree},
974 \member{f_favail},
975 \member{f_flag},
976 \member{f_namemax}.
977 Availability: \UNIX.
979 For backward compatibility, the return value is also accessible as a
980 tuple whose values correspond to the attributes, in the order given above.
981 The standard module \refmodule{statvfs}\refstmodindex{statvfs}
982 defines constants that are useful for extracting information
983 from a \ctype{statvfs} structure when accessing it as a sequence; this
984 remains useful when writing code that needs to work with versions of
985 Python that don't support accessing the fields as attributes.
987 \versionchanged
988 [Added access to values as attributes of the returned object]{2.2}
989 \end{funcdesc}
991 \begin{funcdesc}{symlink}{src, dst}
992 Create a symbolic link pointing to \var{src} named \var{dst}.
993 Availability: \UNIX.
994 \end{funcdesc}
996 \begin{funcdesc}{tempnam}{\optional{dir\optional{, prefix}}}
997 Return a unique path name that is reasonable for creating a temporary
998 file. This will be an absolute path that names a potential directory
999 entry in the directory \var{dir} or a common location for temporary
1000 files if \var{dir} is omitted or \code{None}. If given and not
1001 \code{None}, \var{prefix} is used to provide a short prefix to the
1002 filename. Applications are responsible for properly creating and
1003 managing files created using paths returned by \function{tempnam()};
1004 no automatic cleanup is provided.
1005 On \UNIX, the environment variable \envvar{TMPDIR} overrides
1006 \var{dir}, while on Windows the \envvar{TMP} is used. The specific
1007 behavior of this function depends on the C library implementation;
1008 some aspects are underspecified in system documentation.
1009 \warning{Use of \function{tempnam()} is vulnerable to symlink attacks;
1010 consider using \function{tmpfile()} instead.}
1011 Availability: \UNIX, Windows.
1012 \end{funcdesc}
1014 \begin{funcdesc}{tmpnam}{}
1015 Return a unique path name that is reasonable for creating a temporary
1016 file. This will be an absolute path that names a potential directory
1017 entry in a common location for temporary files. Applications are
1018 responsible for properly creating and managing files created using
1019 paths returned by \function{tmpnam()}; no automatic cleanup is
1020 provided.
1021 \warning{Use of \function{tmpnam()} is vulnerable to symlink attacks;
1022 consider using \function{tmpfile()} instead.}
1023 Availability: \UNIX, Windows.
1024 \end{funcdesc}
1026 \begin{datadesc}{TMP_MAX}
1027 The maximum number of unique names that \function{tmpnam()} will
1028 generate before reusing names.
1029 \end{datadesc}
1031 \begin{funcdesc}{unlink}{path}
1032 Remove the file \var{path}. This is the same function as
1033 \function{remove()}; the \function{unlink()} name is its traditional
1034 \UNIX{} name.
1035 Availability: Macintosh, \UNIX, Windows.
1036 \end{funcdesc}
1038 \begin{funcdesc}{utime}{path, times}
1039 Set the access and modified times of the file specified by \var{path}.
1040 If \var{times} is \code{None}, then the file's access and modified
1041 times are set to the current time. Otherwise, \var{times} must be a
1042 2-tuple of numbers, of the form \code{(\var{atime}, \var{mtime})}
1043 which is used to set the access and modified times, respectively.
1044 \versionchanged[Added support for \code{None} for \var{times}]{2.0}
1045 Availability: Macintosh, \UNIX, Windows.
1046 \end{funcdesc}
1049 \subsection{Process Management \label{os-process}}
1051 These functions may be used to create and manage processes.
1053 The various \function{exec*()} functions take a list of arguments for
1054 the new program loaded into the process. In each case, the first of
1055 these arguments is passed to the new program as its own name rather
1056 than as an argument a user may have typed on a command line. For the
1057 C programmer, this is the \code{argv[0]} passed to a program's
1058 \cfunction{main()}. For example, \samp{os.execv('/bin/echo', ['foo',
1059 'bar'])} will only print \samp{bar} on standard output; \samp{foo}
1060 will seem to be ignored.
1063 \begin{funcdesc}{abort}{}
1064 Generate a \constant{SIGABRT} signal to the current process. On
1065 \UNIX, the default behavior is to produce a core dump; on Windows, the
1066 process immediately returns an exit code of \code{3}. Be aware that
1067 programs which use \function{signal.signal()} to register a handler
1068 for \constant{SIGABRT} will behave differently.
1069 Availability: \UNIX, Windows.
1070 \end{funcdesc}
1072 \begin{funcdesc}{execl}{path, arg0, arg1, \moreargs}
1073 \funcline{execle}{path, arg0, arg1, \moreargs, env}
1074 \funcline{execlp}{file, arg0, arg1, \moreargs}
1075 \funcline{execlpe}{file, arg0, arg1, \moreargs, env}
1076 \funcline{execv}{path, args}
1077 \funcline{execve}{path, args, env}
1078 \funcline{execvp}{file, args}
1079 \funcline{execvpe}{file, args, env}
1080 These functions all execute a new program, replacing the current
1081 process; they do not return. On \UNIX, the new executable is loaded
1082 into the current process, and will have the same process ID as the
1083 caller. Errors will be reported as \exception{OSError} exceptions.
1085 The \character{l} and \character{v} variants of the
1086 \function{exec*()} functions differ in how command-line arguments are
1087 passed. The \character{l} variants are perhaps the easiest to work
1088 with if the number of parameters is fixed when the code is written;
1089 the individual parameters simply become additional parameters to the
1090 \function{execl*()} functions. The \character{v} variants are good
1091 when the number of parameters is variable, with the arguments being
1092 passed in a list or tuple as the \var{args} parameter. In either
1093 case, the arguments to the child process must start with the name of
1094 the command being run.
1096 The variants which include a \character{p} near the end
1097 (\function{execlp()}, \function{execlpe()}, \function{execvp()},
1098 and \function{execvpe()}) will use the \envvar{PATH} environment
1099 variable to locate the program \var{file}. When the environment is
1100 being replaced (using one of the \function{exec*e()} variants,
1101 discussed in the next paragraph), the
1102 new environment is used as the source of the \envvar{PATH} variable.
1103 The other variants, \function{execl()}, \function{execle()},
1104 \function{execv()}, and \function{execve()}, will not use the
1105 \envvar{PATH} variable to locate the executable; \var{path} must
1106 contain an appropriate absolute or relative path.
1108 For \function{execle()}, \function{execlpe()}, \function{execve()},
1109 and \function{execvpe()} (note that these all end in \character{e}),
1110 the \var{env} parameter must be a mapping which is used to define the
1111 environment variables for the new process; the \function{execl()},
1112 \function{execlp()}, \function{execv()}, and \function{execvp()}
1113 all cause the new process to inherit the environment of the current
1114 process.
1115 Availability: \UNIX, Windows.
1116 \end{funcdesc}
1118 \begin{funcdesc}{_exit}{n}
1119 Exit to the system with status \var{n}, without calling cleanup
1120 handlers, flushing stdio buffers, etc.
1121 Availability: \UNIX, Windows.
1123 Note: the standard way to exit is \code{sys.exit(\var{n})}.
1124 \function{_exit()} should normally only be used in the child process
1125 after a \function{fork()}.
1126 \end{funcdesc}
1128 The following exit codes are a defined, and can be used with
1129 \function{_exit()}, although they are not required. These are
1130 typically used for system programs written in Python, such as a
1131 mail server's external command delivery program.
1133 \begin{datadesc}{EX_OK}
1134 Exit code that means no error occurred.
1135 Availability: \UNIX.
1136 \versionadded{2.3}
1137 \end{datadesc}
1139 \begin{datadesc}{EX_USAGE}
1140 Exit code that means the command was used incorrectly, such as when
1141 the wrong number of arguments are given.
1142 Availability: \UNIX.
1143 \versionadded{2.3}
1144 \end{datadesc}
1146 \begin{datadesc}{EX_DATAERR}
1147 Exit code that means the input data was incorrect.
1148 Availability: \UNIX.
1149 \versionadded{2.3}
1150 \end{datadesc}
1152 \begin{datadesc}{EX_NOINPUT}
1153 Exit code that means an input file did not exist or was not readable.
1154 Availability: \UNIX.
1155 \versionadded{2.3}
1156 \end{datadesc}
1158 \begin{datadesc}{EX_NOUSER}
1159 Exit code that means a specified user did not exist.
1160 Availability: \UNIX.
1161 \versionadded{2.3}
1162 \end{datadesc}
1164 \begin{datadesc}{EX_NOHOST}
1165 Exit code that means a specified host did not exist.
1166 Availability: \UNIX.
1167 \versionadded{2.3}
1168 \end{datadesc}
1170 \begin{datadesc}{EX_UNAVAILABLE}
1171 Exit code that means that a required service is unavailable.
1172 Availability: \UNIX.
1173 \versionadded{2.3}
1174 \end{datadesc}
1176 \begin{datadesc}{EX_SOFTWARE}
1177 Exit code that means an internal software error was detected.
1178 Availability: \UNIX.
1179 \versionadded{2.3}
1180 \end{datadesc}
1182 \begin{datadesc}{EX_OSERR}
1183 Exit code that means an operating system error was detected, such as
1184 the inability to fork or create a pipe.
1185 Availability: \UNIX.
1186 \versionadded{2.3}
1187 \end{datadesc}
1189 \begin{datadesc}{EX_OSFILE}
1190 Exit code that means some system file did not exist, could not be
1191 opened, or had some other kind of error.
1192 Availability: \UNIX.
1193 \versionadded{2.3}
1194 \end{datadesc}
1196 \begin{datadesc}{EX_CANTCREAT}
1197 Exit code that means a user specified output file could not be created.
1198 Availability: \UNIX.
1199 \versionadded{2.3}
1200 \end{datadesc}
1202 \begin{datadesc}{EX_IOERR}
1203 Exit code that means that an error occurred while doing I/O on some file.
1204 Availability: \UNIX.
1205 \versionadded{2.3}
1206 \end{datadesc}
1208 \begin{datadesc}{EX_TEMPFAIL}
1209 Exit code that means a temporary failure occurred. This indicates
1210 something that may not really be an error, such as a network
1211 connection that couldn't be made during a retryable operation.
1212 Availability: \UNIX.
1213 \versionadded{2.3}
1214 \end{datadesc}
1216 \begin{datadesc}{EX_PROTOCOL}
1217 Exit code that means that a protocol exchange was illegal, invalid, or
1218 not understood.
1219 Availability: \UNIX.
1220 \versionadded{2.3}
1221 \end{datadesc}
1223 \begin{datadesc}{EX_NOPERM}
1224 Exit code that means that there were insufficient permissions to
1225 perform the operation (but not intended for file system problems).
1226 Availability: \UNIX.
1227 \versionadded{2.3}
1228 \end{datadesc}
1230 \begin{datadesc}{EX_CONFIG}
1231 Exit code that means that some kind of configuration error occurred.
1232 Availability: \UNIX.
1233 \versionadded{2.3}
1234 \end{datadesc}
1236 \begin{datadesc}{EX_NOTFOUND}
1237 Exit code that means something like ``an entry was not found''.
1238 Availability: \UNIX.
1239 \versionadded{2.3}
1240 \end{datadesc}
1242 \begin{funcdesc}{fork}{}
1243 Fork a child process. Return \code{0} in the child, the child's
1244 process id in the parent.
1245 Availability: \UNIX.
1246 \end{funcdesc}
1248 \begin{funcdesc}{forkpty}{}
1249 Fork a child process, using a new pseudo-terminal as the child's
1250 controlling terminal. Return a pair of \code{(\var{pid}, \var{fd})},
1251 where \var{pid} is \code{0} in the child, the new child's process id
1252 in the parent, and \var{fd} is the file descriptor of the master end
1253 of the pseudo-terminal. For a more portable approach, use the
1254 \refmodule{pty} module.
1255 Availability: Some flavors of \UNIX.
1256 \end{funcdesc}
1258 \begin{funcdesc}{kill}{pid, sig}
1259 \index{process!killing}
1260 \index{process!signalling}
1261 Kill the process \var{pid} with signal \var{sig}. Constants for the
1262 specific signals available on the host platform are defined in the
1263 \refmodule{signal} module.
1264 Availability: \UNIX.
1265 \end{funcdesc}
1267 \begin{funcdesc}{killpg}{pgid, sig}
1268 \index{process!killing}
1269 \index{process!signalling}
1270 Kill the process group \var{pgid} with the signal \var{sig}.
1271 Availability: \UNIX.
1272 \versionadded{2.3}
1273 \end{funcdesc}
1275 \begin{funcdesc}{nice}{increment}
1276 Add \var{increment} to the process's ``niceness''. Return the new
1277 niceness.
1278 Availability: \UNIX.
1279 \end{funcdesc}
1281 \begin{funcdesc}{plock}{op}
1282 Lock program segments into memory. The value of \var{op}
1283 (defined in \code{<sys/lock.h>}) determines which segments are locked.
1284 Availability: \UNIX.
1285 \end{funcdesc}
1287 \begin{funcdescni}{popen}{\unspecified}
1288 \funclineni{popen2}{\unspecified}
1289 \funclineni{popen3}{\unspecified}
1290 \funclineni{popen4}{\unspecified}
1291 Run child processes, returning opened pipes for communications. These
1292 functions are described in section \ref{os-newstreams}.
1293 \end{funcdescni}
1295 \begin{funcdesc}{spawnl}{mode, path, \moreargs}
1296 \funcline{spawnle}{mode, path, \moreargs, env}
1297 \funcline{spawnlp}{mode, file, \moreargs}
1298 \funcline{spawnlpe}{mode, file, \moreargs, env}
1299 \funcline{spawnv}{mode, path, args}
1300 \funcline{spawnve}{mode, path, args, env}
1301 \funcline{spawnvp}{mode, file, args}
1302 \funcline{spawnvpe}{mode, file, args, env}
1303 Execute the program \var{path} in a new process. If \var{mode} is
1304 \constant{P_NOWAIT}, this function returns the process ID of the new
1305 process; if \var{mode} is \constant{P_WAIT}, returns the process's
1306 exit code if it exits normally, or \code{-\var{signal}}, where
1307 \var{signal} is the signal that killed the process. On Windows, the
1308 process ID will actually be the process handle, so can be used with
1309 the \function{waitpid()} function.
1311 The \character{l} and \character{v} variants of the
1312 \function{spawn*()} functions differ in how command-line arguments are
1313 passed. The \character{l} variants are perhaps the easiest to work
1314 with if the number of parameters is fixed when the code is written;
1315 the individual parameters simply become additional parameters to the
1316 \function{spawnl*()} functions. The \character{v} variants are good
1317 when the number of parameters is variable, with the arguments being
1318 passed in a list or tuple as the \var{args} parameter. In either
1319 case, the arguments to the child process must start with the name of
1320 the command being run.
1322 The variants which include a second \character{p} near the end
1323 (\function{spawnlp()}, \function{spawnlpe()}, \function{spawnvp()},
1324 and \function{spawnvpe()}) will use the \envvar{PATH} environment
1325 variable to locate the program \var{file}. When the environment is
1326 being replaced (using one of the \function{spawn*e()} variants,
1327 discussed in the next paragraph), the new environment is used as the
1328 source of the \envvar{PATH} variable. The other variants,
1329 \function{spawnl()}, \function{spawnle()}, \function{spawnv()}, and
1330 \function{spawnve()}, will not use the \envvar{PATH} variable to
1331 locate the executable; \var{path} must contain an appropriate absolute
1332 or relative path.
1334 For \function{spawnle()}, \function{spawnlpe()}, \function{spawnve()},
1335 and \function{spawnvpe()} (note that these all end in \character{e}),
1336 the \var{env} parameter must be a mapping which is used to define the
1337 environment variables for the new process; the \function{spawnl()},
1338 \function{spawnlp()}, \function{spawnv()}, and \function{spawnvp()}
1339 all cause the new process to inherit the environment of the current
1340 process.
1342 As an example, the following calls to \function{spawnlp()} and
1343 \function{spawnvpe()} are equivalent:
1345 \begin{verbatim}
1346 import os
1347 os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1349 L = ['cp', 'index.html', '/dev/null']
1350 os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1351 \end{verbatim}
1353 Availability: \UNIX, Windows. \function{spawnlp()},
1354 \function{spawnlpe()}, \function{spawnvp()} and \function{spawnvpe()}
1355 are not available on Windows.
1356 \versionadded{1.6}
1357 \end{funcdesc}
1359 \begin{datadesc}{P_NOWAIT}
1360 \dataline{P_NOWAITO}
1361 Possible values for the \var{mode} parameter to the \function{spawn*()}
1362 family of functions. If either of these values is given, the
1363 \function{spawn*()} functions will return as soon as the new process
1364 has been created, with the process ID as the return value.
1365 Availability: \UNIX, Windows.
1366 \versionadded{1.6}
1367 \end{datadesc}
1369 \begin{datadesc}{P_WAIT}
1370 Possible value for the \var{mode} parameter to the \function{spawn*()}
1371 family of functions. If this is given as \var{mode}, the
1372 \function{spawn*()} functions will not return until the new process
1373 has run to completion and will return the exit code of the process the
1374 run is successful, or \code{-\var{signal}} if a signal kills the
1375 process.
1376 Availability: \UNIX, Windows.
1377 \versionadded{1.6}
1378 \end{datadesc}
1380 \begin{datadesc}{P_DETACH}
1381 \dataline{P_OVERLAY}
1382 Possible values for the \var{mode} parameter to the
1383 \function{spawn*()} family of functions. These are less portable than
1384 those listed above.
1385 \constant{P_DETACH} is similar to \constant{P_NOWAIT}, but the new
1386 process is detached from the console of the calling process.
1387 If \constant{P_OVERLAY} is used, the current process will be replaced;
1388 the \function{spawn*()} function will not return.
1389 Availability: Windows.
1390 \versionadded{1.6}
1391 \end{datadesc}
1393 \begin{funcdesc}{startfile}{path}
1394 Start a file with its associated application. This acts like
1395 double-clicking the file in Windows Explorer, or giving the file name
1396 as an argument to the \program{start} command from the interactive
1397 command shell: the file is opened with whatever application (if any)
1398 its extension is associated.
1400 \function{startfile()} returns as soon as the associated application
1401 is launched. There is no option to wait for the application to close,
1402 and no way to retrieve the application's exit status. The \var{path}
1403 parameter is relative to the current directory. If you want to use an
1404 absolute path, make sure the first character is not a slash
1405 (\character{/}); the underlying Win32 \cfunction{ShellExecute()}
1406 function doesn't work if it is. Use the \function{os.path.normpath()}
1407 function to ensure that the path is properly encoded for Win32.
1408 Availability: Windows.
1409 \versionadded{2.0}
1410 \end{funcdesc}
1412 \begin{funcdesc}{system}{command}
1413 Execute the command (a string) in a subshell. This is implemented by
1414 calling the Standard C function \cfunction{system()}, and has the
1415 same limitations. Changes to \code{posix.environ}, \code{sys.stdin},
1416 etc.\ are not reflected in the environment of the executed command.
1417 The return value is the exit status of the process encoded in the
1418 format specified for \function{wait()}, except on Windows 95 and 98,
1419 where it is always \code{0}. Note that \POSIX{} does not specify the
1420 meaning of the return value of the C \cfunction{system()} function,
1421 so the return value of the Python function is system-dependent.
1422 Availability: \UNIX, Windows.
1423 \end{funcdesc}
1425 \begin{funcdesc}{times}{}
1426 Return a 5-tuple of floating point numbers indicating accumulated
1427 (processor or other)
1428 times, in seconds. The items are: user time, system time, children's
1429 user time, children's system time, and elapsed real time since a fixed
1430 point in the past, in that order. See the \UNIX{} manual page
1431 \manpage{times}{2} or the corresponding Windows Platform API
1432 documentation.
1433 Availability: \UNIX, Windows.
1434 \end{funcdesc}
1436 \begin{funcdesc}{wait}{}
1437 Wait for completion of a child process, and return a tuple containing
1438 its pid and exit status indication: a 16-bit number, whose low byte is
1439 the signal number that killed the process, and whose high byte is the
1440 exit status (if the signal number is zero); the high bit of the low
1441 byte is set if a core file was produced.
1442 Availability: \UNIX.
1443 \end{funcdesc}
1445 \begin{funcdesc}{waitpid}{pid, options}
1446 The details of this function differ on \UNIX{} and Windows.
1448 On \UNIX:
1449 Wait for completion of a child process given by process id \var{pid},
1450 and return a tuple containing its process id and exit status
1451 indication (encoded as for \function{wait()}). The semantics of the
1452 call are affected by the value of the integer \var{options}, which
1453 should be \code{0} for normal operation.
1455 If \var{pid} is greater than \code{0}, \function{waitpid()} requests
1456 status information for that specific process. If \var{pid} is
1457 \code{0}, the request is for the status of any child in the process
1458 group of the current process. If \var{pid} is \code{-1}, the request
1459 pertains to any child of the current process. If \var{pid} is less
1460 than \code{-1}, status is requested for any process in the process
1461 group \code{-\var{pid}} (the absolute value of \var{pid}).
1463 On Windows:
1464 Wait for completion of a process given by process handle \var{pid},
1465 and return a tuple containing \var{pid},
1466 and its exit status shifted left by 8 bits (shifting makes cross-platform
1467 use of the function easier).
1468 A \var{pid} less than or equal to \code{0} has no special meaning on
1469 Windows, and raises an exception.
1470 The value of integer \var{options} has no effect.
1471 \var{pid} can refer to any process whose id is known, not necessarily a
1472 child process.
1473 The \function{spawn()} functions called with \constant{P_NOWAIT}
1474 return suitable process handles.
1475 \end{funcdesc}
1477 \begin{datadesc}{WNOHANG}
1478 The option for \function{waitpid()} to avoid hanging if no child
1479 process status is available immediately.
1480 Availability: \UNIX.
1481 \end{datadesc}
1483 \begin{datadesc}{WCONTINUED}
1484 This option causes child processes to be reported if they have been
1485 continued from a job control stop since their status was last
1486 reported.
1487 Availability: Some \UNIX{} systems.
1488 \versionadded{2.3}
1489 \end{datadesc}
1491 \begin{datadesc}{WUNTRACED}
1492 This option causes child processes to be reported if they have been
1493 stopped but their current state has not been reported since they were
1494 stopped.
1495 Availability: \UNIX.
1496 \versionadded{2.3}
1497 \end{datadesc}
1499 The following functions take a process status code as returned by
1500 \function{system()}, \function{wait()}, or \function{waitpid()} as a
1501 parameter. They may be used to determine the disposition of a
1502 process.
1504 \begin{funcdesc}{WCOREDUMP}{status}
1505 Returns \code{True} if a core dump was generated for the process,
1506 otherwise it returns \code{False}.
1507 Availability: \UNIX.
1508 \versionadded{2.3}
1509 \end{funcdesc}
1511 \begin{funcdesc}{WIFCONTINUED}{status}
1512 Returns \code{True} if the process has been continued from a job
1513 control stop, otherwise it returns \code{False}.
1514 Availability: \UNIX.
1515 \versionadded{2.3}
1516 \end{funcdesc}
1518 \begin{funcdesc}{WIFSTOPPED}{status}
1519 Returns \code{True} if the process has been stopped, otherwise it
1520 returns \code{False}.
1521 Availability: \UNIX.
1522 \end{funcdesc}
1524 \begin{funcdesc}{WIFSIGNALED}{status}
1525 Returns \code{True} if the process exited due to a signal, otherwise
1526 it returns \code{False}.
1527 Availability: \UNIX.
1528 \end{funcdesc}
1530 \begin{funcdesc}{WIFEXITED}{status}
1531 Returns \code{True} if the process exited using the \manpage{exit}{2}
1532 system call, otherwise it returns \code{False}.
1533 Availability: \UNIX.
1534 \end{funcdesc}
1536 \begin{funcdesc}{WEXITSTATUS}{status}
1537 If \code{WIFEXITED(\var{status})} is true, return the integer
1538 parameter to the \manpage{exit}{2} system call. Otherwise, the return
1539 value is meaningless.
1540 Availability: \UNIX.
1541 \end{funcdesc}
1543 \begin{funcdesc}{WSTOPSIG}{status}
1544 Return the signal which caused the process to stop.
1545 Availability: \UNIX.
1546 \end{funcdesc}
1548 \begin{funcdesc}{WTERMSIG}{status}
1549 Return the signal which caused the process to exit.
1550 Availability: \UNIX.
1551 \end{funcdesc}
1554 \subsection{Miscellaneous System Information \label{os-path}}
1557 \begin{funcdesc}{confstr}{name}
1558 Return string-valued system configuration values.
1559 \var{name} specifies the configuration value to retrieve; it may be a
1560 string which is the name of a defined system value; these names are
1561 specified in a number of standards (\POSIX, \UNIX 95, \UNIX 98, and
1562 others). Some platforms define additional names as well. The names
1563 known to the host operating system are given in the
1564 \code{confstr_names} dictionary. For configuration variables not
1565 included in that mapping, passing an integer for \var{name} is also
1566 accepted.
1567 Availability: \UNIX.
1569 If the configuration value specified by \var{name} isn't defined, the
1570 empty string is returned.
1572 If \var{name} is a string and is not known, \exception{ValueError} is
1573 raised. If a specific value for \var{name} is not supported by the
1574 host system, even if it is included in \code{confstr_names}, an
1575 \exception{OSError} is raised with \constant{errno.EINVAL} for the
1576 error number.
1577 \end{funcdesc}
1579 \begin{datadesc}{confstr_names}
1580 Dictionary mapping names accepted by \function{confstr()} to the
1581 integer values defined for those names by the host operating system.
1582 This can be used to determine the set of names known to the system.
1583 Availability: \UNIX.
1584 \end{datadesc}
1586 \begin{funcdesc}{getloadavg}{}
1587 Return the number of processes in the system run queue averaged over
1588 the last 1, 5, and 15 minutes or raises OSError if the load average
1589 was unobtainable.
1591 \versionadded{2.3}
1592 \end{funcdesc}
1594 \begin{funcdesc}{sysconf}{name}
1595 Return integer-valued system configuration values.
1596 If the configuration value specified by \var{name} isn't defined,
1597 \code{-1} is returned. The comments regarding the \var{name}
1598 parameter for \function{confstr()} apply here as well; the dictionary
1599 that provides information on the known names is given by
1600 \code{sysconf_names}.
1601 Availability: \UNIX.
1602 \end{funcdesc}
1604 \begin{datadesc}{sysconf_names}
1605 Dictionary mapping names accepted by \function{sysconf()} to the
1606 integer values defined for those names by the host operating system.
1607 This can be used to determine the set of names known to the system.
1608 Availability: \UNIX.
1609 \end{datadesc}
1612 The follow data values are used to support path manipulation
1613 operations. These are defined for all platforms.
1615 Higher-level operations on pathnames are defined in the
1616 \refmodule{os.path} module.
1619 \begin{datadesc}{curdir}
1620 The constant string used by the operating system to refer to the current
1621 directory.
1622 For example: \code{'.'} for \POSIX{} or \code{':'} for the Macintosh.
1623 Also available via \module{os.path}.
1624 \end{datadesc}
1626 \begin{datadesc}{pardir}
1627 The constant string used by the operating system to refer to the parent
1628 directory.
1629 For example: \code{'..'} for \POSIX{} or \code{'::'} for the Macintosh.
1630 Also available via \module{os.path}.
1631 \end{datadesc}
1633 \begin{datadesc}{sep}
1634 The character used by the operating system to separate pathname components,
1635 for example, \character{/} for \POSIX{} or \character{:} for the
1636 Macintosh. Note that knowing this is not sufficient to be able to
1637 parse or concatenate pathnames --- use \function{os.path.split()} and
1638 \function{os.path.join()} --- but it is occasionally useful.
1639 Also available via \module{os.path}.
1640 \end{datadesc}
1642 \begin{datadesc}{altsep}
1643 An alternative character used by the operating system to separate pathname
1644 components, or \code{None} if only one separator character exists. This is
1645 set to \character{/} on Windows systems where \code{sep} is a
1646 backslash.
1647 Also available via \module{os.path}.
1648 \end{datadesc}
1650 \begin{datadesc}{extsep}
1651 The character which separates the base filename from the extension;
1652 for example, the \character{.} in \file{os.py}.
1653 Also available via \module{os.path}.
1654 \versionadded{2.2}
1655 \end{datadesc}
1657 \begin{datadesc}{pathsep}
1658 The character conventionally used by the operating system to separate
1659 search patch components (as in \envvar{PATH}), such as \character{:} for
1660 \POSIX{} or \character{;} for Windows.
1661 Also available via \module{os.path}.
1662 \end{datadesc}
1664 \begin{datadesc}{defpath}
1665 The default search path used by \function{exec*p*()} and
1666 \function{spawn*p*()} if the environment doesn't have a \code{'PATH'}
1667 key.
1668 Also available via \module{os.path}.
1669 \end{datadesc}
1671 \begin{datadesc}{linesep}
1672 The string used to separate (or, rather, terminate) lines on the
1673 current platform. This may be a single character, such as \code{'\e
1674 n'} for \POSIX{} or \code{'\e r'} for Mac OS, or multiple characters,
1675 for example, \code{'\e r\e n'} for Windows.
1676 \end{datadesc}