blob: f8804e812a764521a094cfc4aea3b493c6133017 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{os} ---
Fred Drake8ee679f2001-07-14 02:50:55 +00002 Miscellaneous operating system interfaces}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakeec6baaf1999-04-21 18:13:31 +00004\declaremodule{standard}{os}
Fred Drake8ee679f2001-07-14 02:50:55 +00005\modulesynopsis{Miscellaneous operating system interfaces.}
Fred Drakeb91e9341998-07-23 17:59:49 +00006
Fred Drakec4f15af1998-03-10 03:17:26 +00007
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00008This module provides a more portable way of using operating system
Fred Drake8ee679f2001-07-14 02:50:55 +00009dependent functionality than importing a operating system dependent
10built-in module like \refmodule{posix} or \module{nt}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000011
Fred Drake8ee679f2001-07-14 02:50:55 +000012This module searches for an operating system dependent built-in module like
Fred Drake2f979011999-06-11 18:28:37 +000013\module{mac} or \refmodule{posix} and exports the same functions and data
Fred Drake8ee679f2001-07-14 02:50:55 +000014as found there. The design of all Python's built-in operating system dependent
Fred Drake215fe2f1999-02-02 19:02:35 +000015modules is such that as long as the same functionality is available,
Fred Drake907e76b2001-07-06 20:30:11 +000016it uses the same interface; for example, the function
Fred Drakeec6baaf1999-04-21 18:13:31 +000017\code{os.stat(\var{path})} returns stat information about \var{path} in
18the same format (which happens to have originated with the
19\POSIX{} interface).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000020
Fred Drake8ee679f2001-07-14 02:50:55 +000021Extensions peculiar to a particular operating system are also
22available through the \module{os} module, but using them is of course a
23threat to portability!
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000024
Fred Drakec4f15af1998-03-10 03:17:26 +000025Note that after the first time \module{os} is imported, there is
26\emph{no} performance penalty in using functions from \module{os}
Fred Drake8ee679f2001-07-14 02:50:55 +000027instead of directly from the operating system dependent built-in module,
28so there should be \emph{no} reason not to use \module{os}!
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000029
Fred Drake215fe2f1999-02-02 19:02:35 +000030
Fred Drake859dc531999-07-01 13:54:40 +000031% 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.
36%
37\ifhtml
38The \module{os} module contains many functions and data values.
39The items below and in the following sub-sections are all available
40directly from the \module{os} module.
41\fi
42
43
Fred Drake215fe2f1999-02-02 19:02:35 +000044\begin{excdesc}{error}
Fred Drake907e76b2001-07-06 20:30:11 +000045This exception is raised when a function returns a system-related
46error (not for illegal argument types or other incidental errors).
47This is also known as the built-in exception \exception{OSError}. The
Fred Drake215fe2f1999-02-02 19:02:35 +000048accompanying value is a pair containing the numeric error code from
49\cdata{errno} and the corresponding string, as would be printed by the
50C function \cfunction{perror()}. See the module
51\refmodule{errno}\refbimodindex{errno}, which contains names for the
52error codes defined by the underlying operating system.
53
54When exceptions are classes, this exception carries two attributes,
55\member{errno} and \member{strerror}. The first holds the value of
56the C \cdata{errno} variable, and the latter holds the corresponding
57error message from \cfunction{strerror()}. For exceptions that
Fred Drake907e76b2001-07-06 20:30:11 +000058involve a file system path (such as \function{chdir()} or
Fred Drake215fe2f1999-02-02 19:02:35 +000059\function{unlink()}), the exception instance will contain a third
60attribute, \member{filename}, which is the file name passed to the
61function.
62
63When exceptions are strings, the string for the exception is
64\code{'OSError'}.
65\end{excdesc}
Guido van Rossum470be141995-03-17 16:07:09 +000066
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000067\begin{datadesc}{name}
Fred Drake8ee679f2001-07-14 02:50:55 +000068The name of the operating system dependent module imported. The
69following names have currently been registered: \code{'posix'}, \code{'nt'},
Fred Drake933d5a71999-09-17 14:38:39 +000070\code{'dos'}, \code{'mac'}, \code{'os2'}, \code{'ce'}, \code{'java'}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000071\end{datadesc}
72
73\begin{datadesc}{path}
Fred Drake8ee679f2001-07-14 02:50:55 +000074The corresponding operating system dependent standard module for pathname
Fred Drake907e76b2001-07-06 20:30:11 +000075operations, such as \module{posixpath} or \module{macpath}. Thus,
76given the proper imports, \code{os.path.split(\var{file})} is
77equivalent to but more portable than
78\code{posixpath.split(\var{file})}. Note that this is also an
79importable module: it may be imported directly as
Fred Drake215fe2f1999-02-02 19:02:35 +000080\refmodule{os.path}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000081\end{datadesc}
82
Fred Drake215fe2f1999-02-02 19:02:35 +000083
84
85\subsection{Process Parameters \label{os-procinfo}}
86
87These functions and data items provide information and operate on the
88current process and user.
89
Fred Drake215fe2f1999-02-02 19:02:35 +000090\begin{datadesc}{environ}
Fred Drake0e1de8b1999-04-29 12:57:32 +000091A mapping object representing the string environment. For example,
92\code{environ['HOME']} is the pathname of your home directory (on some
93platforms), and is equivalent to \code{getenv("HOME")} in C.
Fred Drake215fe2f1999-02-02 19:02:35 +000094
95If the platform supports the \function{putenv()} function, this
96mapping may be used to modify the environment as well as query the
97environment. \function{putenv()} will be called automatically when
98the mapping is modified.
99
100If \function{putenv()} is not provided, this mapping may be passed to
101the appropriate process-creation functions to cause child processes to
102use a modified environment.
103\end{datadesc}
104
Fred Drake6db897c1999-07-12 16:49:30 +0000105\begin{funcdescni}{chdir}{path}
106\funclineni{getcwd}{}
107These functions are described in ``Files and Directories'' (section
108\ref{os-file-dir}).
109\end{funcdescni}
Fred Drake215fe2f1999-02-02 19:02:35 +0000110
Fred Drake18f7a451999-12-09 22:11:43 +0000111\begin{funcdesc}{ctermid}{}
112Return the filename corresponding to the controlling terminal of the
113process.
114Availability: \UNIX{}.
115\end{funcdesc}
116
Fred Drake215fe2f1999-02-02 19:02:35 +0000117\begin{funcdesc}{getegid}{}
118Return the current process' effective group id.
119Availability: \UNIX{}.
120\end{funcdesc}
121
122\begin{funcdesc}{geteuid}{}
Fred Drake6b330ba81999-05-25 13:42:26 +0000123\index{user!effective id}
Fred Drake215fe2f1999-02-02 19:02:35 +0000124Return the current process' effective user id.
125Availability: \UNIX{}.
126\end{funcdesc}
127
128\begin{funcdesc}{getgid}{}
Fred Drake6b330ba81999-05-25 13:42:26 +0000129\index{process!group}
Fred Drake215fe2f1999-02-02 19:02:35 +0000130Return the current process' group id.
131Availability: \UNIX{}.
132\end{funcdesc}
133
Fred Drake88f6ca21999-12-15 19:39:04 +0000134\begin{funcdesc}{getgroups}{}
135Return list of supplemental group ids associated with the current
136process.
137Availability: \UNIX{}.
138\end{funcdesc}
139
140\begin{funcdesc}{getlogin}{}
141Return the actual login name for the current process, even if there
142are multiple login names which map to the same user id.
143Availability: \UNIX{}.
144\end{funcdesc}
145
Fred Drake215fe2f1999-02-02 19:02:35 +0000146\begin{funcdesc}{getpgrp}{}
147\index{process!group}
148Return the current process group id.
149Availability: \UNIX{}.
150\end{funcdesc}
151
152\begin{funcdesc}{getpid}{}
153\index{process!id}
154Return the current process id.
155Availability: \UNIX{}, Windows.
156\end{funcdesc}
157
158\begin{funcdesc}{getppid}{}
159\index{process!id of parent}
160Return the parent's process id.
161Availability: \UNIX{}.
162\end{funcdesc}
163
164\begin{funcdesc}{getuid}{}
Fred Drake6b330ba81999-05-25 13:42:26 +0000165\index{user!id}
Fred Drake215fe2f1999-02-02 19:02:35 +0000166Return the current process' user id.
167Availability: \UNIX{}.
168\end{funcdesc}
169
Fred Drake81e142b2001-05-31 20:27:46 +0000170\begin{funcdesc}{getenv}{varname\optional{, value}}
171Return the value of the environment variable \var{varname} if it
172exists, or \var{value} if it doesn't. \var{value} defaults to
173\code{None}.
174Availability: most flavors of \UNIX{}, Windows.
175\end{funcdesc}
176
Fred Drake215fe2f1999-02-02 19:02:35 +0000177\begin{funcdesc}{putenv}{varname, value}
178\index{environment variables!setting}
179Set the environment variable named \var{varname} to the string
180\var{value}. Such changes to the environment affect subprocesses
181started with \function{os.system()}, \function{popen()} or
182\function{fork()} and \function{execv()}.
183Availability: most flavors of \UNIX{}, Windows.
184
185When \function{putenv()} is
186supported, assignments to items in \code{os.environ} are automatically
187translated into corresponding calls to \function{putenv()}; however,
188calls to \function{putenv()} don't update \code{os.environ}, so it is
189actually preferable to assign to items of \code{os.environ}.
190\end{funcdesc}
191
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +0000192\begin{funcdesc}{setegid}{egid}
193Set the current process's effective group id.
194Availability: \UNIX{}.
195\end{funcdesc}
196
197\begin{funcdesc}{seteuid}{euid}
198Set the current process's effective user id.
199Availability: \UNIX{}.
200\end{funcdesc}
201
Fred Drake215fe2f1999-02-02 19:02:35 +0000202\begin{funcdesc}{setgid}{gid}
203Set the current process' group id.
204Availability: \UNIX{}.
205\end{funcdesc}
206
207\begin{funcdesc}{setpgrp}{}
208Calls the system call \cfunction{setpgrp()} or \cfunction{setpgrp(0,
2090)} depending on which version is implemented (if any). See the
210\UNIX{} manual for the semantics.
211Availability: \UNIX{}.
212\end{funcdesc}
213
214\begin{funcdesc}{setpgid}{pid, pgrp}
215Calls the system call \cfunction{setpgid()}. See the \UNIX{} manual
216for the semantics.
217Availability: \UNIX{}.
218\end{funcdesc}
219
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +0000220\begin{funcdesc}{setreuid}{ruid, euid}
221Set the current process's real and effective user ids.
222Availability: \UNIX{}.
223\end{funcdesc}
224
225\begin{funcdesc}{setregid}{rgid, egid}
226Set the current process's real and effective group ids.
227Availability: \UNIX{}.
228\end{funcdesc}
229
Fred Drake215fe2f1999-02-02 19:02:35 +0000230\begin{funcdesc}{setsid}{}
231Calls the system call \cfunction{setsid()}. See the \UNIX{} manual
232for the semantics.
233Availability: \UNIX{}.
234\end{funcdesc}
235
236\begin{funcdesc}{setuid}{uid}
Fred Drake6b330ba81999-05-25 13:42:26 +0000237\index{user!id, setting}
Fred Drake215fe2f1999-02-02 19:02:35 +0000238Set the current process' user id.
239Availability: \UNIX{}.
240\end{funcdesc}
241
242% placed in this section since it relates to errno.... a little weak ;-(
243\begin{funcdesc}{strerror}{code}
244Return the error message corresponding to the error code in
245\var{code}.
246Availability: \UNIX{}, Windows.
247\end{funcdesc}
248
249\begin{funcdesc}{umask}{mask}
250Set the current numeric umask and returns the previous umask.
251Availability: \UNIX{}, Windows.
252\end{funcdesc}
253
254\begin{funcdesc}{uname}{}
255Return a 5-tuple containing information identifying the current
256operating system. The tuple contains 5 strings:
257\code{(\var{sysname}, \var{nodename}, \var{release}, \var{version},
258\var{machine})}. Some systems truncate the nodename to 8
259characters or to the leading component; a better way to get the
260hostname is \function{socket.gethostname()}
261\withsubitem{(in module socket)}{\ttindex{gethostname()}}
262or even
263\withsubitem{(in module socket)}{\ttindex{gethostbyaddr()}}
264\code{socket.gethostbyaddr(socket.gethostname())}.
265Availability: recent flavors of \UNIX{}.
266\end{funcdesc}
267
268
269
270\subsection{File Object Creation \label{os-newstreams}}
271
272These functions create new file objects.
273
274
275\begin{funcdesc}{fdopen}{fd\optional{, mode\optional{, bufsize}}}
276Return an open file object connected to the file descriptor \var{fd}.
Fred Drake8c9fc001999-08-05 13:41:31 +0000277\index{I/O control!buffering}
Fred Drake215fe2f1999-02-02 19:02:35 +0000278The \var{mode} and \var{bufsize} arguments have the same meaning as
279the corresponding arguments to the built-in \function{open()}
280function.
281Availability: Macintosh, \UNIX{}, Windows.
282\end{funcdesc}
283
284\begin{funcdesc}{popen}{command\optional{, mode\optional{, bufsize}}}
285Open a pipe to or from \var{command}. The return value is an open
286file object connected to the pipe, which can be read or written
287depending on whether \var{mode} is \code{'r'} (default) or \code{'w'}.
288The \var{bufsize} argument has the same meaning as the corresponding
289argument to the built-in \function{open()} function. The exit status of
290the command (encoded in the format specified for \function{wait()}) is
291available as the return value of the \method{close()} method of the file
292object, except that when the exit status is zero (termination without
Fred Drake1319e3e2000-10-03 17:14:27 +0000293errors), \code{None} is returned.
Fred Drake215fe2f1999-02-02 19:02:35 +0000294Availability: \UNIX{}, Windows.
Fred Drakec71c23e2000-10-04 13:57:27 +0000295
296\versionchanged[This function worked unreliably under Windows in
297 earlier versions of Python. This was due to the use of the
298 \cfunction{_popen()} function from the libraries provided with
299 Windows. Newer versions of Python do not use the broken
300 implementation from the Windows libraries]{2.0}
Fred Drake215fe2f1999-02-02 19:02:35 +0000301\end{funcdesc}
302
Fred Drake18f7a451999-12-09 22:11:43 +0000303\begin{funcdesc}{tmpfile}{}
304Return a new file object opened in update mode (\samp{w+}). The file
305has no directory entries associated with it and will be automatically
306deleted once there are no file descriptors for the file.
Fred Drakeefaef132001-07-17 20:39:18 +0000307Availability: \UNIX{}, Windows.
Fred Drake18f7a451999-12-09 22:11:43 +0000308\end{funcdesc}
Fred Drake215fe2f1999-02-02 19:02:35 +0000309
310
Fred Drake8a9db992000-09-28 20:27:51 +0000311For each of these \function{popen()} variants, if \var{bufsize} is
312specified, it specifies the buffer size for the I/O pipes.
313\var{mode}, if provided, should be the string \code{'b'} or
314\code{'t'}; on Windows this is needed to determine whether the file
315objects should be opened in binary or text mode. The default value
316for \var{mode} is \code{'t'}.
317
Fred Drake098d7fa2001-09-11 19:56:51 +0000318These methods do not make it possible to retrieve the return code from
319the child processes. The only way to control the input and output
320streams and also retrieve the return codes is to use the
321\class{Popen3} and \class{Popen4} classes from the \refmodule{popen2}
322module; these are only available on \UNIX.
323
Fred Drake046f4d82001-06-11 15:21:48 +0000324\begin{funcdesc}{popen2}{cmd\optional{, mode\optional{, bufsize}}}
Fred Drake8a9db992000-09-28 20:27:51 +0000325Executes \var{cmd} as a sub-process. Returns the file objects
326\code{(\var{child_stdin}, \var{child_stdout})}.
Fred Drake0b9bc202001-06-11 18:25:34 +0000327Availability: \UNIX{}, Windows.
Fred Drake8a9db992000-09-28 20:27:51 +0000328\versionadded{2.0}
329\end{funcdesc}
330
Fred Drake046f4d82001-06-11 15:21:48 +0000331\begin{funcdesc}{popen3}{cmd\optional{, mode\optional{, bufsize}}}
Fred Drake8a9db992000-09-28 20:27:51 +0000332Executes \var{cmd} as a sub-process. Returns the file objects
333\code{(\var{child_stdin}, \var{child_stdout}, \var{child_stderr})}.
Fred Drake0b9bc202001-06-11 18:25:34 +0000334Availability: \UNIX{}, Windows.
Fred Drake8a9db992000-09-28 20:27:51 +0000335\versionadded{2.0}
336\end{funcdesc}
337
Fred Drake046f4d82001-06-11 15:21:48 +0000338\begin{funcdesc}{popen4}{cmd\optional{, mode\optional{, bufsize}}}
Fred Drake8a9db992000-09-28 20:27:51 +0000339Executes \var{cmd} as a sub-process. Returns the file objects
340\code{(\var{child_stdin}, \var{child_stdout_and_stderr})}.
Fred Drake0b9bc202001-06-11 18:25:34 +0000341Availability: \UNIX{}, Windows.
Fred Drake8a9db992000-09-28 20:27:51 +0000342\versionadded{2.0}
343\end{funcdesc}
344
345This functionality is also available in the \refmodule{popen2} module
346using functions of the same names, but the return values of those
347functions have a different order.
348
349
Fred Drake215fe2f1999-02-02 19:02:35 +0000350\subsection{File Descriptor Operations \label{os-fd-ops}}
351
352These functions operate on I/O streams referred to
353using file descriptors.
354
355
356\begin{funcdesc}{close}{fd}
357Close file descriptor \var{fd}.
358Availability: Macintosh, \UNIX{}, Windows.
359
360Note: this function is intended for low-level I/O and must be applied
361to a file descriptor as returned by \function{open()} or
362\function{pipe()}. To close a ``file object'' returned by the
363built-in function \function{open()} or by \function{popen()} or
364\function{fdopen()}, use its \method{close()} method.
365\end{funcdesc}
366
367\begin{funcdesc}{dup}{fd}
368Return a duplicate of file descriptor \var{fd}.
369Availability: Macintosh, \UNIX{}, Windows.
370\end{funcdesc}
371
372\begin{funcdesc}{dup2}{fd, fd2}
373Duplicate file descriptor \var{fd} to \var{fd2}, closing the latter
374first if necessary.
375Availability: \UNIX{}, Windows.
376\end{funcdesc}
377
Fred Drake88f6ca21999-12-15 19:39:04 +0000378\begin{funcdesc}{fpathconf}{fd, name}
Thomas Woutersf8316632000-07-16 19:01:10 +0000379Return system configuration information relevant to an open file.
Fred Drake88f6ca21999-12-15 19:39:04 +0000380\var{name} specifies the configuration value to retrieve; it may be a
381string which is the name of a defined system value; these names are
382specified in a number of standards (\POSIX.1, Unix95, Unix98, and
383others). Some platforms define additional names as well. The names
384known to the host operating system are given in the
385\code{pathconf_names} dictionary. For configuration variables not
386included in that mapping, passing an integer for \var{name} is also
387accepted.
388Availability: \UNIX{}.
389
390If \var{name} is a string and is not known, \exception{ValueError} is
391raised. If a specific value for \var{name} is not supported by the
392host system, even if it is included in \code{pathconf_names}, an
393\exception{OSError} is raised with \constant{errno.EINVAL} for the
394error number.
395\end{funcdesc}
396
Fred Drake215fe2f1999-02-02 19:02:35 +0000397\begin{funcdesc}{fstat}{fd}
398Return status for file descriptor \var{fd}, like \function{stat()}.
399Availability: \UNIX{}, Windows.
400\end{funcdesc}
401
402\begin{funcdesc}{fstatvfs}{fd}
403Return information about the filesystem containing the file associated
404with file descriptor \var{fd}, like \function{statvfs()}.
405Availability: \UNIX{}.
406\end{funcdesc}
407
408\begin{funcdesc}{ftruncate}{fd, length}
409Truncate the file corresponding to file descriptor \var{fd},
410so that it is at most \var{length} bytes in size.
411Availability: \UNIX{}.
412\end{funcdesc}
413
Skip Montanarod3725212000-07-19 17:30:58 +0000414\begin{funcdesc}{isatty}{fd}
415Return \code{1} if the file descriptor \var{fd} is open and connected to a
416tty(-like) device, else \code{0}.
417Availability: \UNIX{}
418\end{funcdesc}
419
Fred Drake215fe2f1999-02-02 19:02:35 +0000420\begin{funcdesc}{lseek}{fd, pos, how}
421Set the current position of file descriptor \var{fd} to position
422\var{pos}, modified by \var{how}: \code{0} to set the position
423relative to the beginning of the file; \code{1} to set it relative to
424the current position; \code{2} to set it relative to the end of the
425file.
426Availability: Macintosh, \UNIX{}, Windows.
427\end{funcdesc}
428
429\begin{funcdesc}{open}{file, flags\optional{, mode}}
430Open the file \var{file} and set various flags according to
431\var{flags} and possibly its mode according to \var{mode}.
432The default \var{mode} is \code{0777} (octal), and the current umask
433value is first masked out. Return the file descriptor for the newly
434opened file.
435Availability: Macintosh, \UNIX{}, Windows.
436
437For a description of the flag and mode values, see the C run-time
438documentation; flag constants (like \constant{O_RDONLY} and
439\constant{O_WRONLY}) are defined in this module too (see below).
440
441Note: this function is intended for low-level I/O. For normal usage,
442use the built-in function \function{open()}, which returns a ``file
443object'' with \method{read()} and \method{write()} methods (and many
444more).
445\end{funcdesc}
446
Fred Drakec82634c2000-06-28 17:27:48 +0000447\begin{funcdesc}{openpty}{}
448Open a new pseudo-terminal pair. Return a pair of file descriptors
449\code{(\var{master}, \var{slave})} for the pty and the tty,
450respectively. For a (slightly) more portable approach, use the
451\refmodule{pty}\refstmodindex{pty} module.
452Availability: Some flavors of \UNIX{}
453\end{funcdesc}
454
Fred Drake215fe2f1999-02-02 19:02:35 +0000455\begin{funcdesc}{pipe}{}
456Create a pipe. Return a pair of file descriptors \code{(\var{r},
457\var{w})} usable for reading and writing, respectively.
458Availability: \UNIX{}, Windows.
459\end{funcdesc}
460
461\begin{funcdesc}{read}{fd, n}
462Read at most \var{n} bytes from file descriptor \var{fd}.
463Return a string containing the bytes read.
464Availability: Macintosh, \UNIX{}, Windows.
465
466Note: this function is intended for low-level I/O and must be applied
467to a file descriptor as returned by \function{open()} or
468\function{pipe()}. To read a ``file object'' returned by the
469built-in function \function{open()} or by \function{popen()} or
470\function{fdopen()}, or \code{sys.stdin}, use its
471\method{read()} or \method{readline()} methods.
472\end{funcdesc}
473
474\begin{funcdesc}{tcgetpgrp}{fd}
475Return the process group associated with the terminal given by
476\var{fd} (an open file descriptor as returned by \function{open()}).
477Availability: \UNIX{}.
478\end{funcdesc}
479
480\begin{funcdesc}{tcsetpgrp}{fd, pg}
481Set the process group associated with the terminal given by
482\var{fd} (an open file descriptor as returned by \function{open()})
483to \var{pg}.
484Availability: \UNIX{}.
485\end{funcdesc}
486
487\begin{funcdesc}{ttyname}{fd}
488Return a string which specifies the terminal device associated with
489file-descriptor \var{fd}. If \var{fd} is not associated with a terminal
490device, an exception is raised.
491Availability: \UNIX{}.
492\end{funcdesc}
493
494\begin{funcdesc}{write}{fd, str}
495Write the string \var{str} to file descriptor \var{fd}.
496Return the number of bytes actually written.
497Availability: Macintosh, \UNIX{}, Windows.
498
499Note: this function is intended for low-level I/O and must be applied
500to a file descriptor as returned by \function{open()} or
501\function{pipe()}. To write a ``file object'' returned by the
502built-in function \function{open()} or by \function{popen()} or
503\function{fdopen()}, or \code{sys.stdout} or \code{sys.stderr}, use
504its \method{write()} method.
505\end{funcdesc}
506
507
508The following data items are available for use in constructing the
509\var{flags} parameter to the \function{open()} function.
510
511\begin{datadesc}{O_RDONLY}
512\dataline{O_WRONLY}
513\dataline{O_RDWR}
514\dataline{O_NDELAY}
515\dataline{O_NONBLOCK}
516\dataline{O_APPEND}
517\dataline{O_DSYNC}
518\dataline{O_RSYNC}
519\dataline{O_SYNC}
520\dataline{O_NOCTTY}
521\dataline{O_CREAT}
522\dataline{O_EXCL}
523\dataline{O_TRUNC}
524Options for the \var{flag} argument to the \function{open()} function.
525These can be bit-wise OR'd together.
526Availability: Macintosh, \UNIX{}, Windows.
527\end{datadesc}
528
Fred Drake3ac977e2000-08-11 20:19:51 +0000529\begin{datadesc}{O_BINARY}
530Option for the \var{flag} argument to the \function{open()} function.
531This can be bit-wise OR'd together with those listed above.
532Availability: Macintosh, Windows.
533% XXX need to check on the availability of this one.
534\end{datadesc}
535
Fred Drake215fe2f1999-02-02 19:02:35 +0000536
537\subsection{Files and Directories \label{os-file-dir}}
538
539\begin{funcdesc}{access}{path, mode}
Fred Drake38e5d272000-04-03 20:13:55 +0000540Check read/write/execute permissions for this process or existence of
541file \var{path}. \var{mode} should be \constant{F_OK} to test the
542existence of \var{path}, or it can be the inclusive OR of one or more
543of \constant{R_OK}, \constant{W_OK}, and \constant{X_OK} to test
544permissions. Return \code{1} if access is allowed, \code{0} if not.
545See the \UNIX{} man page \manpage{access}{2} for more information.
Fred Drake3ac977e2000-08-11 20:19:51 +0000546Availability: \UNIX{}, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000547\end{funcdesc}
548
Fred Drake38e5d272000-04-03 20:13:55 +0000549\begin{datadesc}{F_OK}
550 Value to pass as the \var{mode} parameter of \function{access()} to
551 test the existence of \var{path}.
552\end{datadesc}
553
554\begin{datadesc}{R_OK}
555 Value to include in the \var{mode} parameter of \function{access()}
556 to test the readability of \var{path}.
557\end{datadesc}
558
559\begin{datadesc}{W_OK}
560 Value to include in the \var{mode} parameter of \function{access()}
561 to test the writability of \var{path}.
562\end{datadesc}
563
564\begin{datadesc}{X_OK}
565 Value to include in the \var{mode} parameter of \function{access()}
566 to determine if \var{path} can be executed.
567\end{datadesc}
568
Fred Drake6db897c1999-07-12 16:49:30 +0000569\begin{funcdesc}{chdir}{path}
570\index{directory!changing}
571Change the current working directory to \var{path}.
572Availability: Macintosh, \UNIX{}, Windows.
573\end{funcdesc}
574
575\begin{funcdesc}{getcwd}{}
576Return a string representing the current working directory.
577Availability: Macintosh, \UNIX{}, Windows.
578\end{funcdesc}
579
Martin v. Löwis244edc82001-10-04 22:44:26 +0000580\begin{funcdesc}{chroot}{path}
581Change the root directory of the current process to \var{path}.
582Availability: \UNIX{}.
583\versionadded{2.2}
584\end{funcdesc}
585
Fred Drake215fe2f1999-02-02 19:02:35 +0000586\begin{funcdesc}{chmod}{path, mode}
587Change the mode of \var{path} to the numeric \var{mode}.
588Availability: \UNIX{}, Windows.
589\end{funcdesc}
590
591\begin{funcdesc}{chown}{path, uid, gid}
592Change the owner and group id of \var{path} to the numeric \var{uid}
593and \var{gid}.
594Availability: \UNIX{}.
595\end{funcdesc}
596
597\begin{funcdesc}{link}{src, dst}
598Create a hard link pointing to \var{src} named \var{dst}.
599Availability: \UNIX{}.
600\end{funcdesc}
601
602\begin{funcdesc}{listdir}{path}
603Return a list containing the names of the entries in the directory.
604The list is in arbitrary order. It does not include the special
605entries \code{'.'} and \code{'..'} even if they are present in the
606directory.
607Availability: Macintosh, \UNIX{}, Windows.
608\end{funcdesc}
609
610\begin{funcdesc}{lstat}{path}
611Like \function{stat()}, but do not follow symbolic links.
612Availability: \UNIX{}.
613\end{funcdesc}
614
615\begin{funcdesc}{mkfifo}{path\optional{, mode}}
616Create a FIFO (a named pipe) named \var{path} with numeric mode
617\var{mode}. The default \var{mode} is \code{0666} (octal). The current
618umask value is first masked out from the mode.
619Availability: \UNIX{}.
620
621FIFOs are pipes that can be accessed like regular files. FIFOs exist
622until they are deleted (for example with \function{os.unlink()}).
623Generally, FIFOs are used as rendezvous between ``client'' and
624``server'' type processes: the server opens the FIFO for reading, and
625the client opens it for writing. Note that \function{mkfifo()}
626doesn't open the FIFO --- it just creates the rendezvous point.
627\end{funcdesc}
628
629\begin{funcdesc}{mkdir}{path\optional{, mode}}
630Create a directory named \var{path} with numeric mode \var{mode}.
631The default \var{mode} is \code{0777} (octal). On some systems,
632\var{mode} is ignored. Where it is used, the current umask value is
633first masked out.
634Availability: Macintosh, \UNIX{}, Windows.
635\end{funcdesc}
636
637\begin{funcdesc}{makedirs}{path\optional{, mode}}
638\index{directory!creating}
639Recursive directory creation function. Like \function{mkdir()},
640but makes all intermediate-level directories needed to contain the
641leaf directory. Throws an \exception{error} exception if the leaf
642directory already exists or cannot be created. The default \var{mode}
Fred Drakebbf7a402001-09-28 16:14:18 +0000643is \code{0777} (octal). This function does not properly handle UNC
644paths (only relevant on Windows systems).
Fred Drake215fe2f1999-02-02 19:02:35 +0000645\versionadded{1.5.2}
646\end{funcdesc}
647
Fred Drake88f6ca21999-12-15 19:39:04 +0000648\begin{funcdesc}{pathconf}{path, name}
Thomas Woutersf8316632000-07-16 19:01:10 +0000649Return system configuration information relevant to a named file.
Fred Drake88f6ca21999-12-15 19:39:04 +0000650\var{name} specifies the configuration value to retrieve; it may be a
651string which is the name of a defined system value; these names are
Fred Drake8ee679f2001-07-14 02:50:55 +0000652specified in a number of standards (\POSIX.1, \UNIX 95, \UNIX 98, and
Fred Drake88f6ca21999-12-15 19:39:04 +0000653others). Some platforms define additional names as well. The names
654known to the host operating system are given in the
655\code{pathconf_names} dictionary. For configuration variables not
656included in that mapping, passing an integer for \var{name} is also
657accepted.
658Availability: \UNIX{}.
659
660If \var{name} is a string and is not known, \exception{ValueError} is
661raised. If a specific value for \var{name} is not supported by the
662host system, even if it is included in \code{pathconf_names}, an
663\exception{OSError} is raised with \constant{errno.EINVAL} for the
664error number.
665\end{funcdesc}
666
667\begin{datadesc}{pathconf_names}
668Dictionary mapping names accepted by \function{pathconf()} and
669\function{fpathconf()} to the integer values defined for those names
670by the host operating system. This can be used to determine the set
671of names known to the system.
672Availability: \UNIX.
673\end{datadesc}
674
Fred Drake215fe2f1999-02-02 19:02:35 +0000675\begin{funcdesc}{readlink}{path}
676Return a string representing the path to which the symbolic link
Fred Drakedc9e7e42001-05-29 18:13:06 +0000677points. The result may be either an absolute or relative pathname; if
678it is relative, it may be converted to an absolute pathname using
679\code{os.path.join(os.path.dirname(\var{path}), \var{result})}.
Fred Drake215fe2f1999-02-02 19:02:35 +0000680Availability: \UNIX{}.
681\end{funcdesc}
682
683\begin{funcdesc}{remove}{path}
Fred Drakedc9e7e42001-05-29 18:13:06 +0000684Remove the file \var{path}. If \var{path} is a directory,
685\exception{OSError} is raised; see \function{rmdir()} below to remove
686a directory. This is identical to the \function{unlink()} function
687documented below. On Windows, attempting to remove a file that is in
688use causes an exception to be raised; on \UNIX, the directory entry is
689removed but the storage allocated to the file is not made available
690until the original file is no longer in use.
Fred Drake215fe2f1999-02-02 19:02:35 +0000691Availability: Macintosh, \UNIX{}, Windows.
692\end{funcdesc}
693
694\begin{funcdesc}{removedirs}{path}
695\index{directory!deleting}
696Recursive directory removal function. Works like
697\function{rmdir()} except that, if the leaf directory is
698successfully removed, directories corresponding to rightmost path
699segments will be pruned way until either the whole path is consumed or
700an error is raised (which is ignored, because it generally means that
701a parent directory is not empty). Throws an \exception{error}
702exception if the leaf directory could not be successfully removed.
703\versionadded{1.5.2}
704\end{funcdesc}
705
706\begin{funcdesc}{rename}{src, dst}
Fred Drakedc9e7e42001-05-29 18:13:06 +0000707Rename the file or directory \var{src} to \var{dst}. If \var{dst} is
708a directory, \exception{OSError} will be raised. On \UNIX, if
709\var{dst} exists and is a file, it will be removed silently if the
710user has permission. The operation may fail on some \UNIX{} flavors
Skip Montanarob9d973d2001-06-04 15:31:17 +0000711if \var{src} and \var{dst} are on different filesystems. If
Fred Drakedc9e7e42001-05-29 18:13:06 +0000712successful, the renaming will be an atomic operation (this is a
713\POSIX{} requirement). On Windows, if \var{dst} already exists,
714\exception{OSError} will be raised even if it is a file; there may be
715no way to implement an atomic rename when \var{dst} names an existing
716file.
Fred Drake215fe2f1999-02-02 19:02:35 +0000717Availability: Macintosh, \UNIX{}, Windows.
718\end{funcdesc}
719
720\begin{funcdesc}{renames}{old, new}
721Recursive directory or file renaming function.
722Works like \function{rename()}, except creation of any intermediate
723directories needed to make the new pathname good is attempted first.
724After the rename, directories corresponding to rightmost path segments
725of the old name will be pruned away using \function{removedirs()}.
726
727Note: this function can fail with the new directory structure made if
728you lack permissions needed to remove the leaf directory or file.
729\versionadded{1.5.2}
730\end{funcdesc}
731
732\begin{funcdesc}{rmdir}{path}
733Remove the directory \var{path}.
734Availability: Macintosh, \UNIX{}, Windows.
735\end{funcdesc}
736
737\begin{funcdesc}{stat}{path}
738Perform a \cfunction{stat()} system call on the given path. The
739return value is a tuple of at least 10 integers giving the most
740important (and portable) members of the \emph{stat} structure, in the
741order
742\code{st_mode},
743\code{st_ino},
744\code{st_dev},
745\code{st_nlink},
746\code{st_uid},
747\code{st_gid},
748\code{st_size},
749\code{st_atime},
750\code{st_mtime},
751\code{st_ctime}.
Fred Drake21c9df72000-10-14 05:46:11 +0000752More items may be added at the end by some implementations. Note that
Fred Drake8ee679f2001-07-14 02:50:55 +0000753on the Mac OS, the time values are floating point values, like all
754time values on the Mac OS.
755(On Windows, some items are filled with dummy values.)
Fred Drake215fe2f1999-02-02 19:02:35 +0000756Availability: Macintosh, \UNIX{}, Windows.
757
758Note: The standard module \refmodule{stat}\refstmodindex{stat} defines
759functions and constants that are useful for extracting information
760from a \ctype{stat} structure.
761\end{funcdesc}
762
763\begin{funcdesc}{statvfs}{path}
764Perform a \cfunction{statvfs()} system call on the given path. The
Guido van Rossum0c9608c1999-02-03 16:32:37 +0000765return value is a tuple of 10 integers giving the most common
Fred Drake215fe2f1999-02-02 19:02:35 +0000766members of the \ctype{statvfs} structure, in the order
767\code{f_bsize},
768\code{f_frsize},
769\code{f_blocks},
770\code{f_bfree},
771\code{f_bavail},
772\code{f_files},
773\code{f_ffree},
774\code{f_favail},
Fred Drake215fe2f1999-02-02 19:02:35 +0000775\code{f_flag},
776\code{f_namemax}.
777Availability: \UNIX{}.
778
779Note: The standard module \module{statvfs}\refstmodindex{statvfs}
780defines constants that are useful for extracting information
781from a \ctype{statvfs} structure.
782\end{funcdesc}
783
784\begin{funcdesc}{symlink}{src, dst}
785Create a symbolic link pointing to \var{src} named \var{dst}.
786Availability: \UNIX{}.
787\end{funcdesc}
788
Fred Drake18f7a451999-12-09 22:11:43 +0000789\begin{funcdesc}{tempnam}{\optional{dir\optional{, prefix}}}
790Return a unique path name that is reasonable for creating a temporary
791file. This will be an absolute path that names a potential directory
792entry in the directory \var{dir} or a common location for temporary
793files if \var{dir} is omitted or \code{None}. If given and not
794\code{None}, \var{prefix} is used to provide a short prefix to the
795filename. Applications are responsible for properly creating and
796managing files created using paths returned by \function{tempnam()};
797no automatic cleanup is provided.
Fred Drakeefaef132001-07-17 20:39:18 +0000798Availability: \UNIX, Windows.
Fred Drake18f7a451999-12-09 22:11:43 +0000799\end{funcdesc}
800
801\begin{funcdesc}{tmpnam}{}
802Return a unique path name that is reasonable for creating a temporary
803file. This will be an absolute path that names a potential directory
804entry in a common location for temporary files. Applications are
805responsible for properly creating and managing files created using
806paths returned by \function{tmpnam()}; no automatic cleanup is
807provided.
Fred Drakeefaef132001-07-17 20:39:18 +0000808Availability: \UNIX, Windows.
Fred Drake18f7a451999-12-09 22:11:43 +0000809\end{funcdesc}
810
811\begin{datadesc}{TMP_MAX}
812The maximum number of unique names that \function{tmpnam()} will
813generate before reusing names.
814\end{datadesc}
815
Fred Drake215fe2f1999-02-02 19:02:35 +0000816\begin{funcdesc}{unlink}{path}
817Remove the file \var{path}. This is the same function as
818\function{remove()}; the \function{unlink()} name is its traditional
819\UNIX{} name.
820Availability: Macintosh, \UNIX{}, Windows.
821\end{funcdesc}
822
Barry Warsaw93a8eac2000-05-01 16:18:22 +0000823\begin{funcdesc}{utime}{path, times}
824Set the access and modified times of the file specified by \var{path}.
825If \var{times} is \code{None}, then the file's access and modified
826times are set to the current time. Otherwise, \var{times} must be a
Fred Drakee06d0252000-05-02 17:29:35 +00008272-tuple of numbers, of the form \code{(\var{atime}, \var{mtime})}
828which is used to set the access and modified times, respectively.
Fred Drake4a152632000-10-19 05:33:46 +0000829\versionchanged[Added support for \code{None} for \var{times}]{2.0}
Fred Drake215fe2f1999-02-02 19:02:35 +0000830Availability: Macintosh, \UNIX{}, Windows.
831\end{funcdesc}
832
833
834\subsection{Process Management \label{os-process}}
835
Fred Drake18f7a451999-12-09 22:11:43 +0000836These functions may be used to create and manage processes.
Fred Drake215fe2f1999-02-02 19:02:35 +0000837
Fred Drake7be31152000-09-23 05:22:07 +0000838The various \function{exec*()} functions take a list of arguments for
839the new program loaded into the process. In each case, the first of
840these arguments is passed to the new program as its own name rather
841than as an argument a user may have typed on a command line. For the
842C programmer, this is the \code{argv[0]} passed to a program's
843\cfunction{main()}. For example, \samp{os.execv('/bin/echo', ['foo',
844'bar'])} will only print \samp{bar} on standard output; \samp{foo}
845will seem to be ignored.
846
Fred Drake215fe2f1999-02-02 19:02:35 +0000847
Fred Drake18f7a451999-12-09 22:11:43 +0000848\begin{funcdesc}{abort}{}
849Generate a \constant{SIGABRT} signal to the current process. On
850\UNIX, the default behavior is to produce a core dump; on Windows, the
851process immediately returns an exit code of \code{3}. Be aware that
852programs which use \function{signal.signal()} to register a handler
853for \constant{SIGABRT} will behave differently.
854Availability: \UNIX, Windows.
855\end{funcdesc}
856
Fred Drake215fe2f1999-02-02 19:02:35 +0000857\begin{funcdesc}{execl}{path, arg0, arg1, ...}
858This is equivalent to
859\samp{execv(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
860Availability: \UNIX{}, Windows.
861\end{funcdesc}
862
863\begin{funcdesc}{execle}{path, arg0, arg1, ..., env}
864This is equivalent to
865\samp{execve(\var{path}, (\var{arg0}, \var{arg1}, ...), \var{env})}.
866Availability: \UNIX{}, Windows.
867\end{funcdesc}
868
869\begin{funcdesc}{execlp}{path, arg0, arg1, ...}
870This is equivalent to
871\samp{execvp(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
872Availability: \UNIX{}, Windows.
873\end{funcdesc}
874
875\begin{funcdesc}{execv}{path, args}
876Execute the executable \var{path} with argument list \var{args},
Fred Drake907e76b2001-07-06 20:30:11 +0000877replacing the current process (the Python interpreter).
Fred Drake215fe2f1999-02-02 19:02:35 +0000878The argument list may be a tuple or list of strings.
879Availability: \UNIX{}, Windows.
880\end{funcdesc}
881
882\begin{funcdesc}{execve}{path, args, env}
883Execute the executable \var{path} with argument list \var{args},
Fred Drake907e76b2001-07-06 20:30:11 +0000884and environment \var{env}, replacing the current process (the Python
885interpreter).
Fred Drake215fe2f1999-02-02 19:02:35 +0000886The argument list may be a tuple or list of strings.
887The environment must be a dictionary mapping strings to strings.
888Availability: \UNIX{}, Windows.
889\end{funcdesc}
890
891\begin{funcdesc}{execvp}{path, args}
892This is like \samp{execv(\var{path}, \var{args})} but duplicates
893the shell's actions in searching for an executable file in a list of
894directories. The directory list is obtained from
895\code{environ['PATH']}.
896Availability: \UNIX{}, Windows.
897\end{funcdesc}
898
899\begin{funcdesc}{execvpe}{path, args, env}
900This is a cross between \function{execve()} and \function{execvp()}.
901The directory list is obtained from \code{\var{env}['PATH']}.
902Availability: \UNIX{}, Windows.
903\end{funcdesc}
904
905\begin{funcdesc}{_exit}{n}
906Exit to the system with status \var{n}, without calling cleanup
907handlers, flushing stdio buffers, etc.
908Availability: \UNIX{}, Windows.
909
910Note: the standard way to exit is \code{sys.exit(\var{n})}.
911\function{_exit()} should normally only be used in the child process
912after a \function{fork()}.
913\end{funcdesc}
914
915\begin{funcdesc}{fork}{}
916Fork a child process. Return \code{0} in the child, the child's
917process id in the parent.
918Availability: \UNIX{}.
919\end{funcdesc}
920
Fred Drakec82634c2000-06-28 17:27:48 +0000921\begin{funcdesc}{forkpty}{}
922Fork a child process, using a new pseudo-terminal as the child's
923controlling terminal. Return a pair of \code{(\var{pid}, \var{fd})},
924where \var{pid} is \code{0} in the child, the new child's process id
925in the parent, and \code{fd} is the file descriptor of the master end
926of the pseudo-terminal. For a more portable approach, use the
927\refmodule{pty} module.
928Availability: Some flavors of \UNIX{}
929\end{funcdesc}
930
Fred Drake215fe2f1999-02-02 19:02:35 +0000931\begin{funcdesc}{kill}{pid, sig}
932\index{process!killing}
933\index{process!signalling}
934Kill the process \var{pid} with signal \var{sig}.
935Availability: \UNIX{}.
936\end{funcdesc}
937
938\begin{funcdesc}{nice}{increment}
939Add \var{increment} to the process's ``niceness''. Return the new
940niceness.
941Availability: \UNIX{}.
942\end{funcdesc}
943
944\begin{funcdesc}{plock}{op}
945Lock program segments into memory. The value of \var{op}
946(defined in \code{<sys/lock.h>}) determines which segments are locked.
Fred Drake39063631999-02-26 14:05:02 +0000947Availability: \UNIX{}.
Fred Drake215fe2f1999-02-02 19:02:35 +0000948\end{funcdesc}
949
Fred Drake046f4d82001-06-11 15:21:48 +0000950\begin{funcdescni}{popen}{\unspecified}
951\funclineni{popen2}{\unspecified}
952\funclineni{popen3}{\unspecified}
953\funclineni{popen4}{\unspecified}
954Run child processes, returning opened pipes for communications. These
955functions are described in section \ref{os-newstreams}.
956\end{funcdescni}
957
Fred Drake739282d2001-08-16 21:21:28 +0000958\begin{funcdesc}{spawnl}{mode, path, \moreargs}
959\funcline{spawnle}{mode, path, \moreargs, env}
960\funcline{spawnlp}{mode, path, \moreargs}
961\funcline{spawnlpe}{mode, path, \moreargs, env}
962\funcline{spawnv}{mode, path, args}
963\funcline{spawnve}{mode, path, args, env}
964\funcline{spawnvp}{mode, path, args}
965\funcline{spawnvpe}{mode, path, args, env}
966Execute the program \var{path} in a new process. If \var{mode} is
967\constant{P_NOWAIT}, this function returns the process ID of the new
968process; it \var{mode} is \constant{P_WAIT}, returns the process's
969exit code if it exits normally, or \code{-\var{signal}}, where
970\var{signal} is the signal that killed the process.
Fred Drake215fe2f1999-02-02 19:02:35 +0000971
Fred Drake739282d2001-08-16 21:21:28 +0000972For \function{spawnle()}, \function{spawnlpe()}, \function{spawnve()},
973and \function{spawnvpe()} (note that these all end in \character{e}),
974the \var{env} parameter must be a mapping which is used to define the
975environment variables for the new process; the \function{spawnl()},
976\function{spawnlp()}, \function{spawnv()}, and \function{spawnvp()}
977all cause the new process to inherit the environment of the current
978process.
979
980The variants which include a second \character{p} near the end
981(\function{spawnlp()}, \function{spawnlpe()}, \function{spawnvp()},
982and \function{spawnvpe()}) will use the \envvar{PATH} environment
983variable to locate the program \var{path}. The other variants,
984\function{spawnl()}, \function{spawnle()}, \function{spawnv()}, and
985\function{spawnve()}, will not use the \envvar{PATH} variable to
986locate the executable.
987
988The \character{l} and \character{v} variants of the
989\function{spawn*()} functions differ in how command-line arguments are
990passed. The \character{l} variants are perhaps the easiest to work
991with if the number of parameters is fixed when the code is written;
992the individual parameters simply become additional parameters to the
993\function{spawnl*()} functions. The \character{v} variants are good
994when the number of parameters is variable, with the arguments being
995passed in a list or tuple as the \var{args} parameter. In either
996case, the arguments to the child process must start with the name of
997the command being run.
998
999As an example, the following calls to \function{spawnlp()} and
1000\function{spawnvpe()} are equivalent:
1001
1002\begin{verbatim}
1003import os
1004os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1005
1006L = ['cp', 'index.html', '/dev/null']
1007os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1008\end{verbatim}
1009
Fred Drake15861b22000-02-29 05:19:38 +00001010Availability: \UNIX{}, Windows.
Fred Drake0b9bc202001-06-11 18:25:34 +00001011\versionadded{1.6}
Fred Drake215fe2f1999-02-02 19:02:35 +00001012\end{funcdesc}
1013
Fred Drake9329e5e1999-02-16 19:40:19 +00001014\begin{datadesc}{P_WAIT}
1015\dataline{P_NOWAIT}
1016\dataline{P_NOWAITO}
Fred Drake215fe2f1999-02-02 19:02:35 +00001017Possible values for the \var{mode} parameter to \function{spawnv()}
1018and \function{spawnve()}.
Fred Drake15861b22000-02-29 05:19:38 +00001019Availability: \UNIX{}, Windows.
Fred Drake0b9bc202001-06-11 18:25:34 +00001020\versionadded{1.6}
Fred Drake15861b22000-02-29 05:19:38 +00001021\end{datadesc}
1022
1023\begin{datadesc}{P_OVERLAY}
1024\dataline{P_DETACH}
1025Possible values for the \var{mode} parameter to \function{spawnv()}
1026and \function{spawnve()}. These are less portable than those listed
1027above.
Fred Drake215fe2f1999-02-02 19:02:35 +00001028Availability: Windows.
Fred Drake0b9bc202001-06-11 18:25:34 +00001029\versionadded{1.6}
Fred Drake215fe2f1999-02-02 19:02:35 +00001030\end{datadesc}
1031
Fred Drake4ce4f2e2000-09-29 04:15:19 +00001032\begin{funcdesc}{startfile}{path}
1033Start a file with its associated application. This acts like
1034double-clicking the file in Windows Explorer, or giving the file name
Fred Drake8ee679f2001-07-14 02:50:55 +00001035as an argument to the \program{start} command from the interactive
1036command shell: the file is opened with whatever application (if any)
1037its extension is associated.
Fred Drake4ce4f2e2000-09-29 04:15:19 +00001038
1039\function{startfile()} returns as soon as the associated application
1040is launched. There is no option to wait for the application to close,
1041and no way to retrieve the application's exit status. The \var{path}
1042parameter is relative to the current directory. If you want to use an
1043absolute path, make sure the first character is not a slash
1044(\character{/}); the underlying Win32 \cfunction{ShellExecute()}
Fred Drake8a2adcf2001-07-23 19:20:56 +00001045function doesn't work if it is. Use the \function{os.path.normpath()}
Fred Drake4ce4f2e2000-09-29 04:15:19 +00001046function to ensure that the path is properly encoded for Win32.
1047Availability: Windows.
1048\versionadded{2.0}
1049\end{funcdesc}
1050
Fred Drake215fe2f1999-02-02 19:02:35 +00001051\begin{funcdesc}{system}{command}
1052Execute the command (a string) in a subshell. This is implemented by
1053calling the Standard C function \cfunction{system()}, and has the
Fred Drakeec6baaf1999-04-21 18:13:31 +00001054same limitations. Changes to \code{posix.environ}, \code{sys.stdin},
Fred Drake215fe2f1999-02-02 19:02:35 +00001055etc.\ are not reflected in the environment of the executed command.
1056The return value is the exit status of the process encoded in the
Fred Drake7a621281999-06-10 15:07:05 +00001057format specified for \function{wait()}, except on Windows 95 and 98,
Fred Drakea88ef001999-06-18 19:11:25 +00001058where it is always \code{0}. Note that \POSIX{} does not specify the
1059meaning of the return value of the C \cfunction{system()} function,
1060so the return value of the Python function is system-dependent.
Fred Drake215fe2f1999-02-02 19:02:35 +00001061Availability: \UNIX{}, Windows.
1062\end{funcdesc}
1063
1064\begin{funcdesc}{times}{}
Fred Drake8ee679f2001-07-14 02:50:55 +00001065Return a 5-tuple of floating point numbers indicating accumulated
1066(processor or other)
Fred Drake215fe2f1999-02-02 19:02:35 +00001067times, in seconds. The items are: user time, system time, children's
1068user time, children's system time, and elapsed real time since a fixed
Fred Drakeec6baaf1999-04-21 18:13:31 +00001069point in the past, in that order. See the \UNIX{} manual page
1070\manpage{times}{2} or the corresponding Windows Platform API
1071documentation.
Fred Drake215fe2f1999-02-02 19:02:35 +00001072Availability: \UNIX{}, Windows.
1073\end{funcdesc}
1074
1075\begin{funcdesc}{wait}{}
1076Wait for completion of a child process, and return a tuple containing
1077its pid and exit status indication: a 16-bit number, whose low byte is
1078the signal number that killed the process, and whose high byte is the
1079exit status (if the signal number is zero); the high bit of the low
1080byte is set if a core file was produced.
1081Availability: \UNIX{}.
1082\end{funcdesc}
1083
1084\begin{funcdesc}{waitpid}{pid, options}
Fred Drake31e5e371999-08-13 13:36:33 +00001085Wait for completion of a child process given by process id \var{pid},
1086and return a tuple containing its process id and exit status
1087indication (encoded as for \function{wait()}). The semantics of the
1088call are affected by the value of the integer \var{options}, which
1089should be \code{0} for normal operation.
Fred Drake215fe2f1999-02-02 19:02:35 +00001090Availability: \UNIX{}.
Fred Drake31e5e371999-08-13 13:36:33 +00001091
1092If \var{pid} is greater than \code{0}, \function{waitpid()} requests
1093status information for that specific process. If \var{pid} is
1094\code{0}, the request is for the status of any child in the process
1095group of the current process. If \var{pid} is \code{-1}, the request
1096pertains to any child of the current process. If \var{pid} is less
1097than \code{-1}, status is requested for any process in the process
1098group \code{-\var{pid}} (the absolute value of \var{pid}).
Fred Drake215fe2f1999-02-02 19:02:35 +00001099\end{funcdesc}
1100
1101\begin{datadesc}{WNOHANG}
1102The option for \function{waitpid()} to avoid hanging if no child
1103process status is available immediately.
1104Availability: \UNIX{}.
1105\end{datadesc}
1106
Fred Drake38e5d272000-04-03 20:13:55 +00001107The following functions take a process status code as returned by
1108\function{system()}, \function{wait()}, or \function{waitpid()} as a
1109parameter. They may be used to determine the disposition of a
1110process.
Fred Drake215fe2f1999-02-02 19:02:35 +00001111
1112\begin{funcdesc}{WIFSTOPPED}{status}
1113Return true if the process has been stopped.
1114Availability: \UNIX{}.
1115\end{funcdesc}
1116
1117\begin{funcdesc}{WIFSIGNALED}{status}
1118Return true if the process exited due to a signal.
1119Availability: \UNIX{}.
1120\end{funcdesc}
1121
1122\begin{funcdesc}{WIFEXITED}{status}
1123Return true if the process exited using the \manpage{exit}{2} system
1124call.
1125Availability: \UNIX{}.
1126\end{funcdesc}
1127
1128\begin{funcdesc}{WEXITSTATUS}{status}
1129If \code{WIFEXITED(\var{status})} is true, return the integer
1130parameter to the \manpage{exit}{2} system call. Otherwise, the return
1131value is meaningless.
1132Availability: \UNIX{}.
1133\end{funcdesc}
1134
1135\begin{funcdesc}{WSTOPSIG}{status}
Fred Drake35c3ffd1999-03-04 14:08:10 +00001136Return the signal which caused the process to stop.
1137Availability: \UNIX{}.
1138\end{funcdesc}
1139
1140\begin{funcdesc}{WTERMSIG}{status}
Fred Drake215fe2f1999-02-02 19:02:35 +00001141Return the signal which caused the process to exit.
1142Availability: \UNIX{}.
1143\end{funcdesc}
1144
1145
Thomas Woutersf8316632000-07-16 19:01:10 +00001146\subsection{Miscellaneous System Information \label{os-path}}
Fred Drake88f6ca21999-12-15 19:39:04 +00001147
1148
1149\begin{funcdesc}{confstr}{name}
1150Return string-valued system configuration values.
1151\var{name} specifies the configuration value to retrieve; it may be a
1152string which is the name of a defined system value; these names are
Fred Drake8ee679f2001-07-14 02:50:55 +00001153specified in a number of standards (\POSIX, \UNIX 95, \UNIX 98, and
Fred Drake88f6ca21999-12-15 19:39:04 +00001154others). Some platforms define additional names as well. The names
1155known to the host operating system are given in the
1156\code{confstr_names} dictionary. For configuration variables not
1157included in that mapping, passing an integer for \var{name} is also
1158accepted.
1159Availability: \UNIX{}.
1160
1161If the configuration value specified by \var{name} isn't defined, the
1162empty string is returned.
1163
1164If \var{name} is a string and is not known, \exception{ValueError} is
1165raised. If a specific value for \var{name} is not supported by the
1166host system, even if it is included in \code{confstr_names}, an
1167\exception{OSError} is raised with \constant{errno.EINVAL} for the
1168error number.
1169\end{funcdesc}
1170
1171\begin{datadesc}{confstr_names}
1172Dictionary mapping names accepted by \function{confstr()} to the
1173integer values defined for those names by the host operating system.
1174This can be used to determine the set of names known to the system.
1175Availability: \UNIX.
1176\end{datadesc}
1177
1178\begin{funcdesc}{sysconf}{name}
1179Return integer-valued system configuration values.
1180If the configuration value specified by \var{name} isn't defined,
1181\code{-1} is returned. The comments regarding the \var{name}
1182parameter for \function{confstr()} apply here as well; the dictionary
1183that provides information on the known names is given by
1184\code{sysconf_names}.
1185Availability: \UNIX{}.
1186\end{funcdesc}
1187
1188\begin{datadesc}{sysconf_names}
1189Dictionary mapping names accepted by \function{sysconf()} to the
1190integer values defined for those names by the host operating system.
1191This can be used to determine the set of names known to the system.
1192Availability: \UNIX.
1193\end{datadesc}
1194
Fred Drake215fe2f1999-02-02 19:02:35 +00001195
1196The follow data values are used to support path manipulation
1197operations. These are defined for all platforms.
1198
1199Higher-level operations on pathnames are defined in the
1200\refmodule{os.path} module.
1201
1202
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001203\begin{datadesc}{curdir}
Fred Drake8ee679f2001-07-14 02:50:55 +00001204The constant string used by the operating system to refer to the current
1205directory.
Fred Drake907e76b2001-07-06 20:30:11 +00001206For example: \code{'.'} for \POSIX{} or \code{':'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001207\end{datadesc}
1208
1209\begin{datadesc}{pardir}
Fred Drake8ee679f2001-07-14 02:50:55 +00001210The constant string used by the operating system to refer to the parent
1211directory.
Fred Drake907e76b2001-07-06 20:30:11 +00001212For example: \code{'..'} for \POSIX{} or \code{'::'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001213\end{datadesc}
1214
1215\begin{datadesc}{sep}
Fred Drake8ee679f2001-07-14 02:50:55 +00001216The character used by the operating system to separate pathname components,
Fred Drake907e76b2001-07-06 20:30:11 +00001217for example, \character{/} for \POSIX{} or \character{:} for the
1218Macintosh. Note that knowing this is not sufficient to be able to
1219parse or concatenate pathnames --- use \function{os.path.split()} and
Fred Drake1a3c2a01998-08-06 15:18:23 +00001220\function{os.path.join()} --- but it is occasionally useful.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001221\end{datadesc}
1222
Guido van Rossumb2afc811997-08-29 22:37:44 +00001223\begin{datadesc}{altsep}
Fred Drake8ee679f2001-07-14 02:50:55 +00001224An alternative character used by the operating system to separate pathname
1225components, or \code{None} if only one separator character exists. This is
1226set to \character{/} on DOS and Windows systems where \code{sep} is a
1227backslash.
Guido van Rossumb2afc811997-08-29 22:37:44 +00001228\end{datadesc}
1229
Guido van Rossum470be141995-03-17 16:07:09 +00001230\begin{datadesc}{pathsep}
Fred Drake8ee679f2001-07-14 02:50:55 +00001231The character conventionally used by the operating system to separate
1232search patch components (as in \envvar{PATH}), such as \character{:} for
1233\POSIX{} or \character{;} for DOS and Windows.
Guido van Rossum9c59ce91998-06-30 15:54:27 +00001234\end{datadesc}
1235
Guido van Rossum470be141995-03-17 16:07:09 +00001236\begin{datadesc}{defpath}
Fred Drake1a3c2a01998-08-06 15:18:23 +00001237The default search path used by \function{exec*p*()} if the environment
Guido van Rossum470be141995-03-17 16:07:09 +00001238doesn't have a \code{'PATH'} key.
1239\end{datadesc}
1240
Fred Drake215fe2f1999-02-02 19:02:35 +00001241\begin{datadesc}{linesep}
1242The string used to separate (or, rather, terminate) lines on the
Fred Drake907e76b2001-07-06 20:30:11 +00001243current platform. This may be a single character, such as \code{'\e
Fred Drake8ee679f2001-07-14 02:50:55 +00001244n'} for \POSIX{} or \code{'\e r'} for the Mac OS, or multiple characters,
1245for example, \code{'\e r\e n'} for DOS and Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +00001246\end{datadesc}