Bump version to 0.9.1.
[python/dscho.git] / Doc / lib / libos.tex
blobd0571af3c817e47b67a8dc844a5fba98f01254c2
1 \section{\module{os} ---
2 Miscellaneous OS interfaces}
4 \declaremodule{standard}{os}
5 \modulesynopsis{Miscellaneous OS interfaces.}
8 This module provides a more portable way of using operating system
9 (OS) dependent functionality than importing an OS dependent built-in
10 module like \refmodule{posix} or \module{nt}.
12 This module searches for an OS 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 OS dependent
15 modules is such that as long as the same functionality is available,
16 it uses the same interface; e.g., 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 OS are also available through the
22 \module{os} module, but using them is of course a threat to
23 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 OS dependent built-in module, so there
28 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
46 system-related error (e.g., not for illegal argument types). This is
47 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 (e.g. \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.
63 When exceptions are strings, the string for the exception is
64 \code{'OSError'}.
65 \end{excdesc}
67 \begin{datadesc}{name}
68 The name of the OS dependent module imported. The following names
69 have currently been registered: \code{'posix'}, \code{'nt'},
70 \code{'dos'}, \code{'mac'}, \code{'os2'}, \code{'ce'}, \code{'java'}.
71 \end{datadesc}
73 \begin{datadesc}{path}
74 The corresponding OS dependent standard module for pathname
75 operations, e.g., \module{posixpath} or \module{macpath}. Thus, given
76 the proper imports, \code{os.path.split(\var{file})} is equivalent to but
77 more portable than \code{posixpath.split(\var{file})}. Note that this
78 is also a valid module: it may be imported directly as
79 \refmodule{os.path}.
80 \end{datadesc}
84 \subsection{Process Parameters \label{os-procinfo}}
86 These functions and data items provide information and operate on the
87 current process and user.
89 \begin{datadesc}{environ}
90 A mapping object representing the string environment. For example,
91 \code{environ['HOME']} is the pathname of your home directory (on some
92 platforms), and is equivalent to \code{getenv("HOME")} in C.
94 If the platform supports the \function{putenv()} function, this
95 mapping may be used to modify the environment as well as query the
96 environment. \function{putenv()} will be called automatically when
97 the mapping is modified.
99 If \function{putenv()} is not provided, this mapping may be passed to
100 the appropriate process-creation functions to cause child processes to
101 use a modified environment.
102 \end{datadesc}
104 \begin{funcdescni}{chdir}{path}
105 \funclineni{getcwd}{}
106 These functions are described in ``Files and Directories'' (section
107 \ref{os-file-dir}).
108 \end{funcdescni}
110 \begin{funcdesc}{ctermid}{}
111 Return the filename corresponding to the controlling terminal of the
112 process.
113 Availability: \UNIX{}.
114 \end{funcdesc}
116 \begin{funcdesc}{getegid}{}
117 Return the current process' effective group id.
118 Availability: \UNIX{}.
119 \end{funcdesc}
121 \begin{funcdesc}{geteuid}{}
122 \index{user!effective id}
123 Return the current process' effective user id.
124 Availability: \UNIX{}.
125 \end{funcdesc}
127 \begin{funcdesc}{getgid}{}
128 \index{process!group}
129 Return the current process' group id.
130 Availability: \UNIX{}.
131 \end{funcdesc}
133 \begin{funcdesc}{getgroups}{}
134 Return list of supplemental group ids associated with the current
135 process.
136 Availability: \UNIX{}.
137 \end{funcdesc}
139 \begin{funcdesc}{getlogin}{}
140 Return the actual login name for the current process, even if there
141 are multiple login names which map to the same user id.
142 Availability: \UNIX{}.
143 \end{funcdesc}
145 \begin{funcdesc}{getpgrp}{}
146 \index{process!group}
147 Return the current process group id.
148 Availability: \UNIX{}.
149 \end{funcdesc}
151 \begin{funcdesc}{getpid}{}
152 \index{process!id}
153 Return the current process id.
154 Availability: \UNIX{}, Windows.
155 \end{funcdesc}
157 \begin{funcdesc}{getppid}{}
158 \index{process!id of parent}
159 Return the parent's process id.
160 Availability: \UNIX{}.
161 \end{funcdesc}
163 \begin{funcdesc}{getuid}{}
164 \index{user!id}
165 Return the current process' user id.
166 Availability: \UNIX{}.
167 \end{funcdesc}
169 \begin{funcdesc}{putenv}{varname, value}
170 \index{environment variables!setting}
171 Set the environment variable named \var{varname} to the string
172 \var{value}. Such changes to the environment affect subprocesses
173 started with \function{os.system()}, \function{popen()} or
174 \function{fork()} and \function{execv()}.
175 Availability: most flavors of \UNIX{}, Windows.
177 When \function{putenv()} is
178 supported, assignments to items in \code{os.environ} are automatically
179 translated into corresponding calls to \function{putenv()}; however,
180 calls to \function{putenv()} don't update \code{os.environ}, so it is
181 actually preferable to assign to items of \code{os.environ}.
182 \end{funcdesc}
184 \begin{funcdesc}{setegid}{egid}
185 Set the current process's effective group id.
186 Availability: \UNIX{}.
187 \end{funcdesc}
189 \begin{funcdesc}{seteuid}{euid}
190 Set the current process's effective user id.
191 Availability: \UNIX{}.
192 \end{funcdesc}
194 \begin{funcdesc}{setgid}{gid}
195 Set the current process' group id.
196 Availability: \UNIX{}.
197 \end{funcdesc}
199 \begin{funcdesc}{setpgrp}{}
200 Calls the system call \cfunction{setpgrp()} or \cfunction{setpgrp(0,
201 0)} depending on which version is implemented (if any). See the
202 \UNIX{} manual for the semantics.
203 Availability: \UNIX{}.
204 \end{funcdesc}
206 \begin{funcdesc}{setpgid}{pid, pgrp}
207 Calls the system call \cfunction{setpgid()}. See the \UNIX{} manual
208 for the semantics.
209 Availability: \UNIX{}.
210 \end{funcdesc}
212 \begin{funcdesc}{setreuid}{ruid, euid}
213 Set the current process's real and effective user ids.
214 Availability: \UNIX{}.
215 \end{funcdesc}
217 \begin{funcdesc}{setregid}{rgid, egid}
218 Set the current process's real and effective group ids.
219 Availability: \UNIX{}.
220 \end{funcdesc}
222 \begin{funcdesc}{setsid}{}
223 Calls the system call \cfunction{setsid()}. See the \UNIX{} manual
224 for the semantics.
225 Availability: \UNIX{}.
226 \end{funcdesc}
228 \begin{funcdesc}{setuid}{uid}
229 \index{user!id, setting}
230 Set the current process' user id.
231 Availability: \UNIX{}.
232 \end{funcdesc}
234 % placed in this section since it relates to errno.... a little weak ;-(
235 \begin{funcdesc}{strerror}{code}
236 Return the error message corresponding to the error code in
237 \var{code}.
238 Availability: \UNIX{}, Windows.
239 \end{funcdesc}
241 \begin{funcdesc}{umask}{mask}
242 Set the current numeric umask and returns the previous umask.
243 Availability: \UNIX{}, Windows.
244 \end{funcdesc}
246 \begin{funcdesc}{uname}{}
247 Return a 5-tuple containing information identifying the current
248 operating system. The tuple contains 5 strings:
249 \code{(\var{sysname}, \var{nodename}, \var{release}, \var{version},
250 \var{machine})}. Some systems truncate the nodename to 8
251 characters or to the leading component; a better way to get the
252 hostname is \function{socket.gethostname()}
253 \withsubitem{(in module socket)}{\ttindex{gethostname()}}
254 or even
255 \withsubitem{(in module socket)}{\ttindex{gethostbyaddr()}}
256 \code{socket.gethostbyaddr(socket.gethostname())}.
257 Availability: recent flavors of \UNIX{}.
258 \end{funcdesc}
262 \subsection{File Object Creation \label{os-newstreams}}
264 These functions create new file objects.
267 \begin{funcdesc}{fdopen}{fd\optional{, mode\optional{, bufsize}}}
268 Return an open file object connected to the file descriptor \var{fd}.
269 \index{I/O control!buffering}
270 The \var{mode} and \var{bufsize} arguments have the same meaning as
271 the corresponding arguments to the built-in \function{open()}
272 function.
273 Availability: Macintosh, \UNIX{}, Windows.
274 \end{funcdesc}
276 \begin{funcdesc}{popen}{command\optional{, mode\optional{, bufsize}}}
277 Open a pipe to or from \var{command}. The return value is an open
278 file object connected to the pipe, which can be read or written
279 depending on whether \var{mode} is \code{'r'} (default) or \code{'w'}.
280 The \var{bufsize} argument has the same meaning as the corresponding
281 argument to the built-in \function{open()} function. The exit status of
282 the command (encoded in the format specified for \function{wait()}) is
283 available as the return value of the \method{close()} method of the file
284 object, except that when the exit status is zero (termination without
285 errors), \code{None} is returned. \strong{Note:} This function
286 behaves unreliably under Windows due to the native implementation of
287 \cfunction{popen()}.
288 Availability: \UNIX{}, Windows.
289 \end{funcdesc}
291 \begin{funcdesc}{tmpfile}{}
292 Return a new file object opened in update mode (\samp{w+}). The file
293 has no directory entries associated with it and will be automatically
294 deleted once there are no file descriptors for the file.
295 Availability: \UNIX{}.
296 \end{funcdesc}
299 \subsection{File Descriptor Operations \label{os-fd-ops}}
301 These functions operate on I/O streams referred to
302 using file descriptors.
305 \begin{funcdesc}{close}{fd}
306 Close file descriptor \var{fd}.
307 Availability: Macintosh, \UNIX{}, Windows.
309 Note: this function is intended for low-level I/O and must be applied
310 to a file descriptor as returned by \function{open()} or
311 \function{pipe()}. To close a ``file object'' returned by the
312 built-in function \function{open()} or by \function{popen()} or
313 \function{fdopen()}, use its \method{close()} method.
314 \end{funcdesc}
316 \begin{funcdesc}{dup}{fd}
317 Return a duplicate of file descriptor \var{fd}.
318 Availability: Macintosh, \UNIX{}, Windows.
319 \end{funcdesc}
321 \begin{funcdesc}{dup2}{fd, fd2}
322 Duplicate file descriptor \var{fd} to \var{fd2}, closing the latter
323 first if necessary.
324 Availability: \UNIX{}, Windows.
325 \end{funcdesc}
327 \begin{funcdesc}{fpathconf}{fd, name}
328 Return system configuration information relevant to an open file.
329 \var{name} specifies the configuration value to retrieve; it may be a
330 string which is the name of a defined system value; these names are
331 specified in a number of standards (\POSIX.1, Unix95, Unix98, and
332 others). Some platforms define additional names as well. The names
333 known to the host operating system are given in the
334 \code{pathconf_names} dictionary. For configuration variables not
335 included in that mapping, passing an integer for \var{name} is also
336 accepted.
337 Availability: \UNIX{}.
339 If \var{name} is a string and is not known, \exception{ValueError} is
340 raised. If a specific value for \var{name} is not supported by the
341 host system, even if it is included in \code{pathconf_names}, an
342 \exception{OSError} is raised with \constant{errno.EINVAL} for the
343 error number.
344 \end{funcdesc}
346 \begin{funcdesc}{fstat}{fd}
347 Return status for file descriptor \var{fd}, like \function{stat()}.
348 Availability: \UNIX{}, Windows.
349 \end{funcdesc}
351 \begin{funcdesc}{fstatvfs}{fd}
352 Return information about the filesystem containing the file associated
353 with file descriptor \var{fd}, like \function{statvfs()}.
354 Availability: \UNIX{}.
355 \end{funcdesc}
357 \begin{funcdesc}{ftruncate}{fd, length}
358 Truncate the file corresponding to file descriptor \var{fd},
359 so that it is at most \var{length} bytes in size.
360 Availability: \UNIX{}.
361 \end{funcdesc}
363 \begin{funcdesc}{isatty}{fd}
364 Return \code{1} if the file descriptor \var{fd} is open and connected to a
365 tty(-like) device, else \code{0}.
366 Availability: \UNIX{}
367 \end{funcdesc}
369 \begin{funcdesc}{lseek}{fd, pos, how}
370 Set the current position of file descriptor \var{fd} to position
371 \var{pos}, modified by \var{how}: \code{0} to set the position
372 relative to the beginning of the file; \code{1} to set it relative to
373 the current position; \code{2} to set it relative to the end of the
374 file.
375 Availability: Macintosh, \UNIX{}, Windows.
376 \end{funcdesc}
378 \begin{funcdesc}{open}{file, flags\optional{, mode}}
379 Open the file \var{file} and set various flags according to
380 \var{flags} and possibly its mode according to \var{mode}.
381 The default \var{mode} is \code{0777} (octal), and the current umask
382 value is first masked out. Return the file descriptor for the newly
383 opened file.
384 Availability: Macintosh, \UNIX{}, Windows.
386 For a description of the flag and mode values, see the C run-time
387 documentation; flag constants (like \constant{O_RDONLY} and
388 \constant{O_WRONLY}) are defined in this module too (see below).
390 Note: this function is intended for low-level I/O. For normal usage,
391 use the built-in function \function{open()}, which returns a ``file
392 object'' with \method{read()} and \method{write()} methods (and many
393 more).
394 \end{funcdesc}
396 \begin{funcdesc}{openpty}{}
397 Open a new pseudo-terminal pair. Return a pair of file descriptors
398 \code{(\var{master}, \var{slave})} for the pty and the tty,
399 respectively. For a (slightly) more portable approach, use the
400 \refmodule{pty}\refstmodindex{pty} module.
401 Availability: Some flavors of \UNIX{}
402 \end{funcdesc}
404 \begin{funcdesc}{pipe}{}
405 Create a pipe. Return a pair of file descriptors \code{(\var{r},
406 \var{w})} usable for reading and writing, respectively.
407 Availability: \UNIX{}, Windows.
408 \end{funcdesc}
410 \begin{funcdesc}{read}{fd, n}
411 Read at most \var{n} bytes from file descriptor \var{fd}.
412 Return a string containing the bytes read.
413 Availability: Macintosh, \UNIX{}, Windows.
415 Note: this function is intended for low-level I/O and must be applied
416 to a file descriptor as returned by \function{open()} or
417 \function{pipe()}. To read a ``file object'' returned by the
418 built-in function \function{open()} or by \function{popen()} or
419 \function{fdopen()}, or \code{sys.stdin}, use its
420 \method{read()} or \method{readline()} methods.
421 \end{funcdesc}
423 \begin{funcdesc}{tcgetpgrp}{fd}
424 Return the process group associated with the terminal given by
425 \var{fd} (an open file descriptor as returned by \function{open()}).
426 Availability: \UNIX{}.
427 \end{funcdesc}
429 \begin{funcdesc}{tcsetpgrp}{fd, pg}
430 Set the process group associated with the terminal given by
431 \var{fd} (an open file descriptor as returned by \function{open()})
432 to \var{pg}.
433 Availability: \UNIX{}.
434 \end{funcdesc}
436 \begin{funcdesc}{ttyname}{fd}
437 Return a string which specifies the terminal device associated with
438 file-descriptor \var{fd}. If \var{fd} is not associated with a terminal
439 device, an exception is raised.
440 Availability: \UNIX{}.
441 \end{funcdesc}
443 \begin{funcdesc}{write}{fd, str}
444 Write the string \var{str} to file descriptor \var{fd}.
445 Return the number of bytes actually written.
446 Availability: Macintosh, \UNIX{}, Windows.
448 Note: this function is intended for low-level I/O and must be applied
449 to a file descriptor as returned by \function{open()} or
450 \function{pipe()}. To write a ``file object'' returned by the
451 built-in function \function{open()} or by \function{popen()} or
452 \function{fdopen()}, or \code{sys.stdout} or \code{sys.stderr}, use
453 its \method{write()} method.
454 \end{funcdesc}
457 The following data items are available for use in constructing the
458 \var{flags} parameter to the \function{open()} function.
460 \begin{datadesc}{O_RDONLY}
461 \dataline{O_WRONLY}
462 \dataline{O_RDWR}
463 \dataline{O_NDELAY}
464 \dataline{O_NONBLOCK}
465 \dataline{O_APPEND}
466 \dataline{O_DSYNC}
467 \dataline{O_RSYNC}
468 \dataline{O_SYNC}
469 \dataline{O_NOCTTY}
470 \dataline{O_CREAT}
471 \dataline{O_EXCL}
472 \dataline{O_TRUNC}
473 Options for the \var{flag} argument to the \function{open()} function.
474 These can be bit-wise OR'd together.
475 Availability: Macintosh, \UNIX{}, Windows.
476 \end{datadesc}
478 \begin{datadesc}{O_BINARY}
479 Option for the \var{flag} argument to the \function{open()} function.
480 This can be bit-wise OR'd together with those listed above.
481 Availability: Macintosh, Windows.
482 % XXX need to check on the availability of this one.
483 \end{datadesc}
486 \subsection{Files and Directories \label{os-file-dir}}
488 \begin{funcdesc}{access}{path, mode}
489 Check read/write/execute permissions for this process or existence of
490 file \var{path}. \var{mode} should be \constant{F_OK} to test the
491 existence of \var{path}, or it can be the inclusive OR of one or more
492 of \constant{R_OK}, \constant{W_OK}, and \constant{X_OK} to test
493 permissions. Return \code{1} if access is allowed, \code{0} if not.
494 See the \UNIX{} man page \manpage{access}{2} for more information.
495 Availability: \UNIX{}, Windows.
496 \end{funcdesc}
498 \begin{datadesc}{F_OK}
499 Value to pass as the \var{mode} parameter of \function{access()} to
500 test the existence of \var{path}.
501 \end{datadesc}
503 \begin{datadesc}{R_OK}
504 Value to include in the \var{mode} parameter of \function{access()}
505 to test the readability of \var{path}.
506 \end{datadesc}
508 \begin{datadesc}{W_OK}
509 Value to include in the \var{mode} parameter of \function{access()}
510 to test the writability of \var{path}.
511 \end{datadesc}
513 \begin{datadesc}{X_OK}
514 Value to include in the \var{mode} parameter of \function{access()}
515 to determine if \var{path} can be executed.
516 \end{datadesc}
518 \begin{funcdesc}{chdir}{path}
519 \index{directory!changing}
520 Change the current working directory to \var{path}.
521 Availability: Macintosh, \UNIX{}, Windows.
522 \end{funcdesc}
524 \begin{funcdesc}{getcwd}{}
525 Return a string representing the current working directory.
526 Availability: Macintosh, \UNIX{}, Windows.
527 \end{funcdesc}
529 \begin{funcdesc}{chmod}{path, mode}
530 Change the mode of \var{path} to the numeric \var{mode}.
531 Availability: \UNIX{}, Windows.
532 \end{funcdesc}
534 \begin{funcdesc}{chown}{path, uid, gid}
535 Change the owner and group id of \var{path} to the numeric \var{uid}
536 and \var{gid}.
537 Availability: \UNIX{}.
538 \end{funcdesc}
540 \begin{funcdesc}{link}{src, dst}
541 Create a hard link pointing to \var{src} named \var{dst}.
542 Availability: \UNIX{}.
543 \end{funcdesc}
545 \begin{funcdesc}{listdir}{path}
546 Return a list containing the names of the entries in the directory.
547 The list is in arbitrary order. It does not include the special
548 entries \code{'.'} and \code{'..'} even if they are present in the
549 directory.
550 Availability: Macintosh, \UNIX{}, Windows.
551 \end{funcdesc}
553 \begin{funcdesc}{lstat}{path}
554 Like \function{stat()}, but do not follow symbolic links.
555 Availability: \UNIX{}.
556 \end{funcdesc}
558 \begin{funcdesc}{mkfifo}{path\optional{, mode}}
559 Create a FIFO (a named pipe) named \var{path} with numeric mode
560 \var{mode}. The default \var{mode} is \code{0666} (octal). The current
561 umask value is first masked out from the mode.
562 Availability: \UNIX{}.
564 FIFOs are pipes that can be accessed like regular files. FIFOs exist
565 until they are deleted (for example with \function{os.unlink()}).
566 Generally, FIFOs are used as rendezvous between ``client'' and
567 ``server'' type processes: the server opens the FIFO for reading, and
568 the client opens it for writing. Note that \function{mkfifo()}
569 doesn't open the FIFO --- it just creates the rendezvous point.
570 \end{funcdesc}
572 \begin{funcdesc}{mkdir}{path\optional{, mode}}
573 Create a directory named \var{path} with numeric mode \var{mode}.
574 The default \var{mode} is \code{0777} (octal). On some systems,
575 \var{mode} is ignored. Where it is used, the current umask value is
576 first masked out.
577 Availability: Macintosh, \UNIX{}, Windows.
578 \end{funcdesc}
580 \begin{funcdesc}{makedirs}{path\optional{, mode}}
581 \index{directory!creating}
582 Recursive directory creation function. Like \function{mkdir()},
583 but makes all intermediate-level directories needed to contain the
584 leaf directory. Throws an \exception{error} exception if the leaf
585 directory already exists or cannot be created. The default \var{mode}
586 is \code{0777} (octal).
587 \versionadded{1.5.2}
588 \end{funcdesc}
590 \begin{funcdesc}{pathconf}{path, name}
591 Return system configuration information relevant to a named file.
592 \var{name} specifies the configuration value to retrieve; it may be a
593 string which is the name of a defined system value; these names are
594 specified in a number of standards (\POSIX.1, Unix95, Unix98, and
595 others). Some platforms define additional names as well. The names
596 known to the host operating system are given in the
597 \code{pathconf_names} dictionary. For configuration variables not
598 included in that mapping, passing an integer for \var{name} is also
599 accepted.
600 Availability: \UNIX{}.
602 If \var{name} is a string and is not known, \exception{ValueError} is
603 raised. If a specific value for \var{name} is not supported by the
604 host system, even if it is included in \code{pathconf_names}, an
605 \exception{OSError} is raised with \constant{errno.EINVAL} for the
606 error number.
607 \end{funcdesc}
609 \begin{datadesc}{pathconf_names}
610 Dictionary mapping names accepted by \function{pathconf()} and
611 \function{fpathconf()} to the integer values defined for those names
612 by the host operating system. This can be used to determine the set
613 of names known to the system.
614 Availability: \UNIX.
615 \end{datadesc}
617 \begin{funcdesc}{readlink}{path}
618 Return a string representing the path to which the symbolic link
619 points.
620 Availability: \UNIX{}.
621 \end{funcdesc}
623 \begin{funcdesc}{remove}{path}
624 Remove the file \var{path}. See \function{rmdir()} below to remove a
625 directory. This is identical to the \function{unlink()} function
626 documented below.
627 Availability: Macintosh, \UNIX{}, Windows.
628 \end{funcdesc}
630 \begin{funcdesc}{removedirs}{path}
631 \index{directory!deleting}
632 Recursive directory removal function. Works like
633 \function{rmdir()} except that, if the leaf directory is
634 successfully removed, directories corresponding to rightmost path
635 segments will be pruned way until either the whole path is consumed or
636 an error is raised (which is ignored, because it generally means that
637 a parent directory is not empty). Throws an \exception{error}
638 exception if the leaf directory could not be successfully removed.
639 \versionadded{1.5.2}
640 \end{funcdesc}
642 \begin{funcdesc}{rename}{src, dst}
643 Rename the file or directory \var{src} to \var{dst}.
644 Availability: Macintosh, \UNIX{}, Windows.
645 \end{funcdesc}
647 \begin{funcdesc}{renames}{old, new}
648 Recursive directory or file renaming function.
649 Works like \function{rename()}, except creation of any intermediate
650 directories needed to make the new pathname good is attempted first.
651 After the rename, directories corresponding to rightmost path segments
652 of the old name will be pruned away using \function{removedirs()}.
654 Note: this function can fail with the new directory structure made if
655 you lack permissions needed to remove the leaf directory or file.
656 \versionadded{1.5.2}
657 \end{funcdesc}
659 \begin{funcdesc}{rmdir}{path}
660 Remove the directory \var{path}.
661 Availability: Macintosh, \UNIX{}, Windows.
662 \end{funcdesc}
664 \begin{funcdesc}{stat}{path}
665 Perform a \cfunction{stat()} system call on the given path. The
666 return value is a tuple of at least 10 integers giving the most
667 important (and portable) members of the \emph{stat} structure, in the
668 order
669 \code{st_mode},
670 \code{st_ino},
671 \code{st_dev},
672 \code{st_nlink},
673 \code{st_uid},
674 \code{st_gid},
675 \code{st_size},
676 \code{st_atime},
677 \code{st_mtime},
678 \code{st_ctime}.
679 More items may be added at the end by some implementations.
680 (On MS Windows, some items are filled with dummy values.)
681 Availability: Macintosh, \UNIX{}, Windows.
683 Note: The standard module \refmodule{stat}\refstmodindex{stat} defines
684 functions and constants that are useful for extracting information
685 from a \ctype{stat} structure.
686 \end{funcdesc}
688 \begin{funcdesc}{statvfs}{path}
689 Perform a \cfunction{statvfs()} system call on the given path. The
690 return value is a tuple of 10 integers giving the most common
691 members of the \ctype{statvfs} structure, in the order
692 \code{f_bsize},
693 \code{f_frsize},
694 \code{f_blocks},
695 \code{f_bfree},
696 \code{f_bavail},
697 \code{f_files},
698 \code{f_ffree},
699 \code{f_favail},
700 \code{f_flag},
701 \code{f_namemax}.
702 Availability: \UNIX{}.
704 Note: The standard module \module{statvfs}\refstmodindex{statvfs}
705 defines constants that are useful for extracting information
706 from a \ctype{statvfs} structure.
707 \end{funcdesc}
709 \begin{funcdesc}{symlink}{src, dst}
710 Create a symbolic link pointing to \var{src} named \var{dst}.
711 Availability: \UNIX{}.
712 \end{funcdesc}
714 \begin{funcdesc}{tempnam}{\optional{dir\optional{, prefix}}}
715 Return a unique path name that is reasonable for creating a temporary
716 file. This will be an absolute path that names a potential directory
717 entry in the directory \var{dir} or a common location for temporary
718 files if \var{dir} is omitted or \code{None}. If given and not
719 \code{None}, \var{prefix} is used to provide a short prefix to the
720 filename. Applications are responsible for properly creating and
721 managing files created using paths returned by \function{tempnam()};
722 no automatic cleanup is provided.
723 \end{funcdesc}
725 \begin{funcdesc}{tmpnam}{}
726 Return a unique path name that is reasonable for creating a temporary
727 file. This will be an absolute path that names a potential directory
728 entry in a common location for temporary files. Applications are
729 responsible for properly creating and managing files created using
730 paths returned by \function{tmpnam()}; no automatic cleanup is
731 provided.
732 \end{funcdesc}
734 \begin{datadesc}{TMP_MAX}
735 The maximum number of unique names that \function{tmpnam()} will
736 generate before reusing names.
737 \end{datadesc}
739 \begin{funcdesc}{unlink}{path}
740 Remove the file \var{path}. This is the same function as
741 \function{remove()}; the \function{unlink()} name is its traditional
742 \UNIX{} name.
743 Availability: Macintosh, \UNIX{}, Windows.
744 \end{funcdesc}
746 \begin{funcdesc}{utime}{path, times}
747 Set the access and modified times of the file specified by \var{path}.
748 If \var{times} is \code{None}, then the file's access and modified
749 times are set to the current time. Otherwise, \var{times} must be a
750 2-tuple of numbers, of the form \code{(\var{atime}, \var{mtime})}
751 which is used to set the access and modified times, respectively.
752 \versionchanged[added support for \code{None} for \var{times}]{2.0}
753 Availability: Macintosh, \UNIX{}, Windows.
754 \end{funcdesc}
757 \subsection{Process Management \label{os-process}}
759 These functions may be used to create and manage processes.
762 \begin{funcdesc}{abort}{}
763 Generate a \constant{SIGABRT} signal to the current process. On
764 \UNIX, the default behavior is to produce a core dump; on Windows, the
765 process immediately returns an exit code of \code{3}. Be aware that
766 programs which use \function{signal.signal()} to register a handler
767 for \constant{SIGABRT} will behave differently.
768 Availability: \UNIX, Windows.
769 \end{funcdesc}
771 \begin{funcdesc}{execl}{path, arg0, arg1, ...}
772 This is equivalent to
773 \samp{execv(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
774 Availability: \UNIX{}, Windows.
775 \end{funcdesc}
777 \begin{funcdesc}{execle}{path, arg0, arg1, ..., env}
778 This is equivalent to
779 \samp{execve(\var{path}, (\var{arg0}, \var{arg1}, ...), \var{env})}.
780 Availability: \UNIX{}, Windows.
781 \end{funcdesc}
783 \begin{funcdesc}{execlp}{path, arg0, arg1, ...}
784 This is equivalent to
785 \samp{execvp(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
786 Availability: \UNIX{}, Windows.
787 \end{funcdesc}
789 \begin{funcdesc}{execv}{path, args}
790 Execute the executable \var{path} with argument list \var{args},
791 replacing the current process (i.e., the Python interpreter).
792 The argument list may be a tuple or list of strings.
793 Availability: \UNIX{}, Windows.
794 \end{funcdesc}
796 \begin{funcdesc}{execve}{path, args, env}
797 Execute the executable \var{path} with argument list \var{args},
798 and environment \var{env},
799 replacing the current process (i.e., the Python interpreter).
800 The argument list may be a tuple or list of strings.
801 The environment must be a dictionary mapping strings to strings.
802 Availability: \UNIX{}, Windows.
803 \end{funcdesc}
805 \begin{funcdesc}{execvp}{path, args}
806 This is like \samp{execv(\var{path}, \var{args})} but duplicates
807 the shell's actions in searching for an executable file in a list of
808 directories. The directory list is obtained from
809 \code{environ['PATH']}.
810 Availability: \UNIX{}, Windows.
811 \end{funcdesc}
813 \begin{funcdesc}{execvpe}{path, args, env}
814 This is a cross between \function{execve()} and \function{execvp()}.
815 The directory list is obtained from \code{\var{env}['PATH']}.
816 Availability: \UNIX{}, Windows.
817 \end{funcdesc}
819 \begin{funcdesc}{_exit}{n}
820 Exit to the system with status \var{n}, without calling cleanup
821 handlers, flushing stdio buffers, etc.
822 Availability: \UNIX{}, Windows.
824 Note: the standard way to exit is \code{sys.exit(\var{n})}.
825 \function{_exit()} should normally only be used in the child process
826 after a \function{fork()}.
827 \end{funcdesc}
829 \begin{funcdesc}{fork}{}
830 Fork a child process. Return \code{0} in the child, the child's
831 process id in the parent.
832 Availability: \UNIX{}.
833 \end{funcdesc}
835 \begin{funcdesc}{forkpty}{}
836 Fork a child process, using a new pseudo-terminal as the child's
837 controlling terminal. Return a pair of \code{(\var{pid}, \var{fd})},
838 where \var{pid} is \code{0} in the child, the new child's process id
839 in the parent, and \code{fd} is the file descriptor of the master end
840 of the pseudo-terminal. For a more portable approach, use the
841 \refmodule{pty} module.
842 Availability: Some flavors of \UNIX{}
843 \end{funcdesc}
845 \begin{funcdesc}{kill}{pid, sig}
846 \index{process!killing}
847 \index{process!signalling}
848 Kill the process \var{pid} with signal \var{sig}.
849 Availability: \UNIX{}.
850 \end{funcdesc}
852 \begin{funcdesc}{nice}{increment}
853 Add \var{increment} to the process's ``niceness''. Return the new
854 niceness.
855 Availability: \UNIX{}.
856 \end{funcdesc}
858 \begin{funcdesc}{plock}{op}
859 Lock program segments into memory. The value of \var{op}
860 (defined in \code{<sys/lock.h>}) determines which segments are locked.
861 Availability: \UNIX{}.
862 \end{funcdesc}
864 \begin{funcdesc}{spawnv}{mode, path, args}
865 Execute the program \var{path} in a new process, passing the arguments
866 specified in \var{args} as command-line parameters. \var{args} may be
867 a list or a tuple. \var{mode} is a magic operational constant. See
868 the Visual \Cpp{} Runtime Library documentation for further
869 information; the constants are exposed to the Python programmer as
870 listed below.
871 Availability: \UNIX{}, Windows.
872 \versionadded{1.5.2}
873 \end{funcdesc}
875 \begin{funcdesc}{spawnve}{mode, path, args, env}
876 Execute the program \var{path} in a new process, passing the arguments
877 specified in \var{args} as command-line parameters and the contents of
878 the mapping \var{env} as the environment. \var{args} may be a list or
879 a tuple. \var{mode} is a magic operational constant. See the Visual
880 \Cpp{} Runtime Library documentation for further information; the
881 constants are exposed to the Python programmer as listed below.
882 Availability: \UNIX{}, Windows.
883 \versionadded{1.5.2}
884 \end{funcdesc}
886 \begin{datadesc}{P_WAIT}
887 \dataline{P_NOWAIT}
888 \dataline{P_NOWAITO}
889 Possible values for the \var{mode} parameter to \function{spawnv()}
890 and \function{spawnve()}.
891 Availability: \UNIX{}, Windows.
892 \versionadded{1.5.2}
893 \end{datadesc}
895 \begin{datadesc}{P_OVERLAY}
896 \dataline{P_DETACH}
897 Possible values for the \var{mode} parameter to \function{spawnv()}
898 and \function{spawnve()}. These are less portable than those listed
899 above.
900 Availability: Windows.
901 \versionadded{1.5.2}
902 \end{datadesc}
904 \begin{funcdesc}{system}{command}
905 Execute the command (a string) in a subshell. This is implemented by
906 calling the Standard C function \cfunction{system()}, and has the
907 same limitations. Changes to \code{posix.environ}, \code{sys.stdin},
908 etc.\ are not reflected in the environment of the executed command.
909 The return value is the exit status of the process encoded in the
910 format specified for \function{wait()}, except on Windows 95 and 98,
911 where it is always \code{0}. Note that \POSIX{} does not specify the
912 meaning of the return value of the C \cfunction{system()} function,
913 so the return value of the Python function is system-dependent.
914 Availability: \UNIX{}, Windows.
915 \end{funcdesc}
917 \begin{funcdesc}{times}{}
918 Return a 5-tuple of floating point numbers indicating accumulated (CPU
919 or other)
920 times, in seconds. The items are: user time, system time, children's
921 user time, children's system time, and elapsed real time since a fixed
922 point in the past, in that order. See the \UNIX{} manual page
923 \manpage{times}{2} or the corresponding Windows Platform API
924 documentation.
925 Availability: \UNIX{}, Windows.
926 \end{funcdesc}
928 \begin{funcdesc}{wait}{}
929 Wait for completion of a child process, and return a tuple containing
930 its pid and exit status indication: a 16-bit number, whose low byte is
931 the signal number that killed the process, and whose high byte is the
932 exit status (if the signal number is zero); the high bit of the low
933 byte is set if a core file was produced.
934 Availability: \UNIX{}.
935 \end{funcdesc}
937 \begin{funcdesc}{waitpid}{pid, options}
938 Wait for completion of a child process given by process id \var{pid},
939 and return a tuple containing its process id and exit status
940 indication (encoded as for \function{wait()}). The semantics of the
941 call are affected by the value of the integer \var{options}, which
942 should be \code{0} for normal operation.
943 Availability: \UNIX{}.
945 If \var{pid} is greater than \code{0}, \function{waitpid()} requests
946 status information for that specific process. If \var{pid} is
947 \code{0}, the request is for the status of any child in the process
948 group of the current process. If \var{pid} is \code{-1}, the request
949 pertains to any child of the current process. If \var{pid} is less
950 than \code{-1}, status is requested for any process in the process
951 group \code{-\var{pid}} (the absolute value of \var{pid}).
952 \end{funcdesc}
954 \begin{datadesc}{WNOHANG}
955 The option for \function{waitpid()} to avoid hanging if no child
956 process status is available immediately.
957 Availability: \UNIX{}.
958 \end{datadesc}
960 The following functions take a process status code as returned by
961 \function{system()}, \function{wait()}, or \function{waitpid()} as a
962 parameter. They may be used to determine the disposition of a
963 process.
965 \begin{funcdesc}{WIFSTOPPED}{status}
966 Return true if the process has been stopped.
967 Availability: \UNIX{}.
968 \end{funcdesc}
970 \begin{funcdesc}{WIFSIGNALED}{status}
971 Return true if the process exited due to a signal.
972 Availability: \UNIX{}.
973 \end{funcdesc}
975 \begin{funcdesc}{WIFEXITED}{status}
976 Return true if the process exited using the \manpage{exit}{2} system
977 call.
978 Availability: \UNIX{}.
979 \end{funcdesc}
981 \begin{funcdesc}{WEXITSTATUS}{status}
982 If \code{WIFEXITED(\var{status})} is true, return the integer
983 parameter to the \manpage{exit}{2} system call. Otherwise, the return
984 value is meaningless.
985 Availability: \UNIX{}.
986 \end{funcdesc}
988 \begin{funcdesc}{WSTOPSIG}{status}
989 Return the signal which caused the process to stop.
990 Availability: \UNIX{}.
991 \end{funcdesc}
993 \begin{funcdesc}{WTERMSIG}{status}
994 Return the signal which caused the process to exit.
995 Availability: \UNIX{}.
996 \end{funcdesc}
999 \subsection{Miscellaneous System Information \label{os-path}}
1002 \begin{funcdesc}{confstr}{name}
1003 Return string-valued system configuration values.
1004 \var{name} specifies the configuration value to retrieve; it may be a
1005 string which is the name of a defined system value; these names are
1006 specified in a number of standards (\POSIX, Unix95, Unix98, and
1007 others). Some platforms define additional names as well. The names
1008 known to the host operating system are given in the
1009 \code{confstr_names} dictionary. For configuration variables not
1010 included in that mapping, passing an integer for \var{name} is also
1011 accepted.
1012 Availability: \UNIX{}.
1014 If the configuration value specified by \var{name} isn't defined, the
1015 empty string is returned.
1017 If \var{name} is a string and is not known, \exception{ValueError} is
1018 raised. If a specific value for \var{name} is not supported by the
1019 host system, even if it is included in \code{confstr_names}, an
1020 \exception{OSError} is raised with \constant{errno.EINVAL} for the
1021 error number.
1022 \end{funcdesc}
1024 \begin{datadesc}{confstr_names}
1025 Dictionary mapping names accepted by \function{confstr()} to the
1026 integer values defined for those names by the host operating system.
1027 This can be used to determine the set of names known to the system.
1028 Availability: \UNIX.
1029 \end{datadesc}
1031 \begin{funcdesc}{sysconf}{name}
1032 Return integer-valued system configuration values.
1033 If the configuration value specified by \var{name} isn't defined,
1034 \code{-1} is returned. The comments regarding the \var{name}
1035 parameter for \function{confstr()} apply here as well; the dictionary
1036 that provides information on the known names is given by
1037 \code{sysconf_names}.
1038 Availability: \UNIX{}.
1039 \end{funcdesc}
1041 \begin{datadesc}{sysconf_names}
1042 Dictionary mapping names accepted by \function{sysconf()} to the
1043 integer values defined for those names by the host operating system.
1044 This can be used to determine the set of names known to the system.
1045 Availability: \UNIX.
1046 \end{datadesc}
1049 The follow data values are used to support path manipulation
1050 operations. These are defined for all platforms.
1052 Higher-level operations on pathnames are defined in the
1053 \refmodule{os.path} module.
1056 \begin{datadesc}{curdir}
1057 The constant string used by the OS to refer to the current directory,
1058 e.g.\ \code{'.'} for \POSIX{} or \code{':'} for the Macintosh.
1059 \end{datadesc}
1061 \begin{datadesc}{pardir}
1062 The constant string used by the OS to refer to the parent directory,
1063 e.g.\ \code{'..'} for \POSIX{} or \code{'::'} for the Macintosh.
1064 \end{datadesc}
1066 \begin{datadesc}{sep}
1067 The character used by the OS to separate pathname components,
1068 e.g.\ \character{/} for \POSIX{} or \character{:} for the Macintosh.
1069 Note that knowing this is not sufficient to be able to parse or
1070 concatenate pathnames --- use \function{os.path.split()} and
1071 \function{os.path.join()} --- but it is occasionally useful.
1072 \end{datadesc}
1074 \begin{datadesc}{altsep}
1075 An alternative character used by the OS to separate pathname components,
1076 or \code{None} if only one separator character exists. This is set to
1077 \character{/} on DOS and Windows systems where \code{sep} is a backslash.
1078 \end{datadesc}
1080 \begin{datadesc}{pathsep}
1081 The character conventionally used by the OS to separate search patch
1082 components (as in \envvar{PATH}), e.g.\ \character{:} for \POSIX{} or
1083 \character{;} for DOS and Windows.
1084 \end{datadesc}
1086 \begin{datadesc}{defpath}
1087 The default search path used by \function{exec*p*()} if the environment
1088 doesn't have a \code{'PATH'} key.
1089 \end{datadesc}
1091 \begin{datadesc}{linesep}
1092 The string used to separate (or, rather, terminate) lines on the
1093 current platform. This may be a single character,
1094 e.g.\ \code{'\e n'} for \POSIX{} or \code{'\e r'} for MacOS, or multiple
1095 characters, e.g.\ \code{'\e r\e n'} for MS-DOS and MS Windows.
1096 \end{datadesc}