blob: 85355a5ee9c6e1a31b676d547300922dc0381533 [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
Fred Drake215fe2f1999-02-02 19:02:35 +0000580\begin{funcdesc}{chmod}{path, mode}
581Change the mode of \var{path} to the numeric \var{mode}.
582Availability: \UNIX{}, Windows.
583\end{funcdesc}
584
585\begin{funcdesc}{chown}{path, uid, gid}
586Change the owner and group id of \var{path} to the numeric \var{uid}
587and \var{gid}.
588Availability: \UNIX{}.
589\end{funcdesc}
590
591\begin{funcdesc}{link}{src, dst}
592Create a hard link pointing to \var{src} named \var{dst}.
593Availability: \UNIX{}.
594\end{funcdesc}
595
596\begin{funcdesc}{listdir}{path}
597Return a list containing the names of the entries in the directory.
598The list is in arbitrary order. It does not include the special
599entries \code{'.'} and \code{'..'} even if they are present in the
600directory.
601Availability: Macintosh, \UNIX{}, Windows.
602\end{funcdesc}
603
604\begin{funcdesc}{lstat}{path}
605Like \function{stat()}, but do not follow symbolic links.
606Availability: \UNIX{}.
607\end{funcdesc}
608
609\begin{funcdesc}{mkfifo}{path\optional{, mode}}
610Create a FIFO (a named pipe) named \var{path} with numeric mode
611\var{mode}. The default \var{mode} is \code{0666} (octal). The current
612umask value is first masked out from the mode.
613Availability: \UNIX{}.
614
615FIFOs are pipes that can be accessed like regular files. FIFOs exist
616until they are deleted (for example with \function{os.unlink()}).
617Generally, FIFOs are used as rendezvous between ``client'' and
618``server'' type processes: the server opens the FIFO for reading, and
619the client opens it for writing. Note that \function{mkfifo()}
620doesn't open the FIFO --- it just creates the rendezvous point.
621\end{funcdesc}
622
623\begin{funcdesc}{mkdir}{path\optional{, mode}}
624Create a directory named \var{path} with numeric mode \var{mode}.
625The default \var{mode} is \code{0777} (octal). On some systems,
626\var{mode} is ignored. Where it is used, the current umask value is
627first masked out.
628Availability: Macintosh, \UNIX{}, Windows.
629\end{funcdesc}
630
631\begin{funcdesc}{makedirs}{path\optional{, mode}}
632\index{directory!creating}
633Recursive directory creation function. Like \function{mkdir()},
634but makes all intermediate-level directories needed to contain the
635leaf directory. Throws an \exception{error} exception if the leaf
636directory already exists or cannot be created. The default \var{mode}
Fred Drakebbf7a402001-09-28 16:14:18 +0000637is \code{0777} (octal). This function does not properly handle UNC
638paths (only relevant on Windows systems).
Fred Drake215fe2f1999-02-02 19:02:35 +0000639\versionadded{1.5.2}
640\end{funcdesc}
641
Fred Drake88f6ca21999-12-15 19:39:04 +0000642\begin{funcdesc}{pathconf}{path, name}
Thomas Woutersf8316632000-07-16 19:01:10 +0000643Return system configuration information relevant to a named file.
Fred Drake88f6ca21999-12-15 19:39:04 +0000644\var{name} specifies the configuration value to retrieve; it may be a
645string which is the name of a defined system value; these names are
Fred Drake8ee679f2001-07-14 02:50:55 +0000646specified in a number of standards (\POSIX.1, \UNIX 95, \UNIX 98, and
Fred Drake88f6ca21999-12-15 19:39:04 +0000647others). Some platforms define additional names as well. The names
648known to the host operating system are given in the
649\code{pathconf_names} dictionary. For configuration variables not
650included in that mapping, passing an integer for \var{name} is also
651accepted.
652Availability: \UNIX{}.
653
654If \var{name} is a string and is not known, \exception{ValueError} is
655raised. If a specific value for \var{name} is not supported by the
656host system, even if it is included in \code{pathconf_names}, an
657\exception{OSError} is raised with \constant{errno.EINVAL} for the
658error number.
659\end{funcdesc}
660
661\begin{datadesc}{pathconf_names}
662Dictionary mapping names accepted by \function{pathconf()} and
663\function{fpathconf()} to the integer values defined for those names
664by the host operating system. This can be used to determine the set
665of names known to the system.
666Availability: \UNIX.
667\end{datadesc}
668
Fred Drake215fe2f1999-02-02 19:02:35 +0000669\begin{funcdesc}{readlink}{path}
670Return a string representing the path to which the symbolic link
Fred Drakedc9e7e42001-05-29 18:13:06 +0000671points. The result may be either an absolute or relative pathname; if
672it is relative, it may be converted to an absolute pathname using
673\code{os.path.join(os.path.dirname(\var{path}), \var{result})}.
Fred Drake215fe2f1999-02-02 19:02:35 +0000674Availability: \UNIX{}.
675\end{funcdesc}
676
677\begin{funcdesc}{remove}{path}
Fred Drakedc9e7e42001-05-29 18:13:06 +0000678Remove the file \var{path}. If \var{path} is a directory,
679\exception{OSError} is raised; see \function{rmdir()} below to remove
680a directory. This is identical to the \function{unlink()} function
681documented below. On Windows, attempting to remove a file that is in
682use causes an exception to be raised; on \UNIX, the directory entry is
683removed but the storage allocated to the file is not made available
684until the original file is no longer in use.
Fred Drake215fe2f1999-02-02 19:02:35 +0000685Availability: Macintosh, \UNIX{}, Windows.
686\end{funcdesc}
687
688\begin{funcdesc}{removedirs}{path}
689\index{directory!deleting}
690Recursive directory removal function. Works like
691\function{rmdir()} except that, if the leaf directory is
692successfully removed, directories corresponding to rightmost path
693segments will be pruned way until either the whole path is consumed or
694an error is raised (which is ignored, because it generally means that
695a parent directory is not empty). Throws an \exception{error}
696exception if the leaf directory could not be successfully removed.
697\versionadded{1.5.2}
698\end{funcdesc}
699
700\begin{funcdesc}{rename}{src, dst}
Fred Drakedc9e7e42001-05-29 18:13:06 +0000701Rename the file or directory \var{src} to \var{dst}. If \var{dst} is
702a directory, \exception{OSError} will be raised. On \UNIX, if
703\var{dst} exists and is a file, it will be removed silently if the
704user has permission. The operation may fail on some \UNIX{} flavors
Skip Montanarob9d973d2001-06-04 15:31:17 +0000705if \var{src} and \var{dst} are on different filesystems. If
Fred Drakedc9e7e42001-05-29 18:13:06 +0000706successful, the renaming will be an atomic operation (this is a
707\POSIX{} requirement). On Windows, if \var{dst} already exists,
708\exception{OSError} will be raised even if it is a file; there may be
709no way to implement an atomic rename when \var{dst} names an existing
710file.
Fred Drake215fe2f1999-02-02 19:02:35 +0000711Availability: Macintosh, \UNIX{}, Windows.
712\end{funcdesc}
713
714\begin{funcdesc}{renames}{old, new}
715Recursive directory or file renaming function.
716Works like \function{rename()}, except creation of any intermediate
717directories needed to make the new pathname good is attempted first.
718After the rename, directories corresponding to rightmost path segments
719of the old name will be pruned away using \function{removedirs()}.
720
721Note: this function can fail with the new directory structure made if
722you lack permissions needed to remove the leaf directory or file.
723\versionadded{1.5.2}
724\end{funcdesc}
725
726\begin{funcdesc}{rmdir}{path}
727Remove the directory \var{path}.
728Availability: Macintosh, \UNIX{}, Windows.
729\end{funcdesc}
730
731\begin{funcdesc}{stat}{path}
732Perform a \cfunction{stat()} system call on the given path. The
733return value is a tuple of at least 10 integers giving the most
734important (and portable) members of the \emph{stat} structure, in the
735order
736\code{st_mode},
737\code{st_ino},
738\code{st_dev},
739\code{st_nlink},
740\code{st_uid},
741\code{st_gid},
742\code{st_size},
743\code{st_atime},
744\code{st_mtime},
745\code{st_ctime}.
Fred Drake21c9df72000-10-14 05:46:11 +0000746More items may be added at the end by some implementations. Note that
Fred Drake8ee679f2001-07-14 02:50:55 +0000747on the Mac OS, the time values are floating point values, like all
748time values on the Mac OS.
749(On Windows, some items are filled with dummy values.)
Fred Drake215fe2f1999-02-02 19:02:35 +0000750Availability: Macintosh, \UNIX{}, Windows.
751
752Note: The standard module \refmodule{stat}\refstmodindex{stat} defines
753functions and constants that are useful for extracting information
754from a \ctype{stat} structure.
755\end{funcdesc}
756
757\begin{funcdesc}{statvfs}{path}
758Perform a \cfunction{statvfs()} system call on the given path. The
Guido van Rossum0c9608c1999-02-03 16:32:37 +0000759return value is a tuple of 10 integers giving the most common
Fred Drake215fe2f1999-02-02 19:02:35 +0000760members of the \ctype{statvfs} structure, in the order
761\code{f_bsize},
762\code{f_frsize},
763\code{f_blocks},
764\code{f_bfree},
765\code{f_bavail},
766\code{f_files},
767\code{f_ffree},
768\code{f_favail},
Fred Drake215fe2f1999-02-02 19:02:35 +0000769\code{f_flag},
770\code{f_namemax}.
771Availability: \UNIX{}.
772
773Note: The standard module \module{statvfs}\refstmodindex{statvfs}
774defines constants that are useful for extracting information
775from a \ctype{statvfs} structure.
776\end{funcdesc}
777
778\begin{funcdesc}{symlink}{src, dst}
779Create a symbolic link pointing to \var{src} named \var{dst}.
780Availability: \UNIX{}.
781\end{funcdesc}
782
Fred Drake18f7a451999-12-09 22:11:43 +0000783\begin{funcdesc}{tempnam}{\optional{dir\optional{, prefix}}}
784Return a unique path name that is reasonable for creating a temporary
785file. This will be an absolute path that names a potential directory
786entry in the directory \var{dir} or a common location for temporary
787files if \var{dir} is omitted or \code{None}. If given and not
788\code{None}, \var{prefix} is used to provide a short prefix to the
789filename. Applications are responsible for properly creating and
790managing files created using paths returned by \function{tempnam()};
791no automatic cleanup is provided.
Fred Drakeefaef132001-07-17 20:39:18 +0000792Availability: \UNIX, Windows.
Fred Drake18f7a451999-12-09 22:11:43 +0000793\end{funcdesc}
794
795\begin{funcdesc}{tmpnam}{}
796Return a unique path name that is reasonable for creating a temporary
797file. This will be an absolute path that names a potential directory
798entry in a common location for temporary files. Applications are
799responsible for properly creating and managing files created using
800paths returned by \function{tmpnam()}; no automatic cleanup is
801provided.
Fred Drakeefaef132001-07-17 20:39:18 +0000802Availability: \UNIX, Windows.
Fred Drake18f7a451999-12-09 22:11:43 +0000803\end{funcdesc}
804
805\begin{datadesc}{TMP_MAX}
806The maximum number of unique names that \function{tmpnam()} will
807generate before reusing names.
808\end{datadesc}
809
Fred Drake215fe2f1999-02-02 19:02:35 +0000810\begin{funcdesc}{unlink}{path}
811Remove the file \var{path}. This is the same function as
812\function{remove()}; the \function{unlink()} name is its traditional
813\UNIX{} name.
814Availability: Macintosh, \UNIX{}, Windows.
815\end{funcdesc}
816
Barry Warsaw93a8eac2000-05-01 16:18:22 +0000817\begin{funcdesc}{utime}{path, times}
818Set the access and modified times of the file specified by \var{path}.
819If \var{times} is \code{None}, then the file's access and modified
820times are set to the current time. Otherwise, \var{times} must be a
Fred Drakee06d0252000-05-02 17:29:35 +00008212-tuple of numbers, of the form \code{(\var{atime}, \var{mtime})}
822which is used to set the access and modified times, respectively.
Fred Drake4a152632000-10-19 05:33:46 +0000823\versionchanged[Added support for \code{None} for \var{times}]{2.0}
Fred Drake215fe2f1999-02-02 19:02:35 +0000824Availability: Macintosh, \UNIX{}, Windows.
825\end{funcdesc}
826
827
828\subsection{Process Management \label{os-process}}
829
Fred Drake18f7a451999-12-09 22:11:43 +0000830These functions may be used to create and manage processes.
Fred Drake215fe2f1999-02-02 19:02:35 +0000831
Fred Drake7be31152000-09-23 05:22:07 +0000832The various \function{exec*()} functions take a list of arguments for
833the new program loaded into the process. In each case, the first of
834these arguments is passed to the new program as its own name rather
835than as an argument a user may have typed on a command line. For the
836C programmer, this is the \code{argv[0]} passed to a program's
837\cfunction{main()}. For example, \samp{os.execv('/bin/echo', ['foo',
838'bar'])} will only print \samp{bar} on standard output; \samp{foo}
839will seem to be ignored.
840
Fred Drake215fe2f1999-02-02 19:02:35 +0000841
Fred Drake18f7a451999-12-09 22:11:43 +0000842\begin{funcdesc}{abort}{}
843Generate a \constant{SIGABRT} signal to the current process. On
844\UNIX, the default behavior is to produce a core dump; on Windows, the
845process immediately returns an exit code of \code{3}. Be aware that
846programs which use \function{signal.signal()} to register a handler
847for \constant{SIGABRT} will behave differently.
848Availability: \UNIX, Windows.
849\end{funcdesc}
850
Fred Drake215fe2f1999-02-02 19:02:35 +0000851\begin{funcdesc}{execl}{path, arg0, arg1, ...}
852This is equivalent to
853\samp{execv(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
854Availability: \UNIX{}, Windows.
855\end{funcdesc}
856
857\begin{funcdesc}{execle}{path, arg0, arg1, ..., env}
858This is equivalent to
859\samp{execve(\var{path}, (\var{arg0}, \var{arg1}, ...), \var{env})}.
860Availability: \UNIX{}, Windows.
861\end{funcdesc}
862
863\begin{funcdesc}{execlp}{path, arg0, arg1, ...}
864This is equivalent to
865\samp{execvp(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
866Availability: \UNIX{}, Windows.
867\end{funcdesc}
868
869\begin{funcdesc}{execv}{path, args}
870Execute the executable \var{path} with argument list \var{args},
Fred Drake907e76b2001-07-06 20:30:11 +0000871replacing the current process (the Python interpreter).
Fred Drake215fe2f1999-02-02 19:02:35 +0000872The argument list may be a tuple or list of strings.
873Availability: \UNIX{}, Windows.
874\end{funcdesc}
875
876\begin{funcdesc}{execve}{path, args, env}
877Execute the executable \var{path} with argument list \var{args},
Fred Drake907e76b2001-07-06 20:30:11 +0000878and environment \var{env}, replacing the current process (the Python
879interpreter).
Fred Drake215fe2f1999-02-02 19:02:35 +0000880The argument list may be a tuple or list of strings.
881The environment must be a dictionary mapping strings to strings.
882Availability: \UNIX{}, Windows.
883\end{funcdesc}
884
885\begin{funcdesc}{execvp}{path, args}
886This is like \samp{execv(\var{path}, \var{args})} but duplicates
887the shell's actions in searching for an executable file in a list of
888directories. The directory list is obtained from
889\code{environ['PATH']}.
890Availability: \UNIX{}, Windows.
891\end{funcdesc}
892
893\begin{funcdesc}{execvpe}{path, args, env}
894This is a cross between \function{execve()} and \function{execvp()}.
895The directory list is obtained from \code{\var{env}['PATH']}.
896Availability: \UNIX{}, Windows.
897\end{funcdesc}
898
899\begin{funcdesc}{_exit}{n}
900Exit to the system with status \var{n}, without calling cleanup
901handlers, flushing stdio buffers, etc.
902Availability: \UNIX{}, Windows.
903
904Note: the standard way to exit is \code{sys.exit(\var{n})}.
905\function{_exit()} should normally only be used in the child process
906after a \function{fork()}.
907\end{funcdesc}
908
909\begin{funcdesc}{fork}{}
910Fork a child process. Return \code{0} in the child, the child's
911process id in the parent.
912Availability: \UNIX{}.
913\end{funcdesc}
914
Fred Drakec82634c2000-06-28 17:27:48 +0000915\begin{funcdesc}{forkpty}{}
916Fork a child process, using a new pseudo-terminal as the child's
917controlling terminal. Return a pair of \code{(\var{pid}, \var{fd})},
918where \var{pid} is \code{0} in the child, the new child's process id
919in the parent, and \code{fd} is the file descriptor of the master end
920of the pseudo-terminal. For a more portable approach, use the
921\refmodule{pty} module.
922Availability: Some flavors of \UNIX{}
923\end{funcdesc}
924
Fred Drake215fe2f1999-02-02 19:02:35 +0000925\begin{funcdesc}{kill}{pid, sig}
926\index{process!killing}
927\index{process!signalling}
928Kill the process \var{pid} with signal \var{sig}.
929Availability: \UNIX{}.
930\end{funcdesc}
931
932\begin{funcdesc}{nice}{increment}
933Add \var{increment} to the process's ``niceness''. Return the new
934niceness.
935Availability: \UNIX{}.
936\end{funcdesc}
937
938\begin{funcdesc}{plock}{op}
939Lock program segments into memory. The value of \var{op}
940(defined in \code{<sys/lock.h>}) determines which segments are locked.
Fred Drake39063631999-02-26 14:05:02 +0000941Availability: \UNIX{}.
Fred Drake215fe2f1999-02-02 19:02:35 +0000942\end{funcdesc}
943
Fred Drake046f4d82001-06-11 15:21:48 +0000944\begin{funcdescni}{popen}{\unspecified}
945\funclineni{popen2}{\unspecified}
946\funclineni{popen3}{\unspecified}
947\funclineni{popen4}{\unspecified}
948Run child processes, returning opened pipes for communications. These
949functions are described in section \ref{os-newstreams}.
950\end{funcdescni}
951
Fred Drake739282d2001-08-16 21:21:28 +0000952\begin{funcdesc}{spawnl}{mode, path, \moreargs}
953\funcline{spawnle}{mode, path, \moreargs, env}
954\funcline{spawnlp}{mode, path, \moreargs}
955\funcline{spawnlpe}{mode, path, \moreargs, env}
956\funcline{spawnv}{mode, path, args}
957\funcline{spawnve}{mode, path, args, env}
958\funcline{spawnvp}{mode, path, args}
959\funcline{spawnvpe}{mode, path, args, env}
960Execute the program \var{path} in a new process. If \var{mode} is
961\constant{P_NOWAIT}, this function returns the process ID of the new
962process; it \var{mode} is \constant{P_WAIT}, returns the process's
963exit code if it exits normally, or \code{-\var{signal}}, where
964\var{signal} is the signal that killed the process.
Fred Drake215fe2f1999-02-02 19:02:35 +0000965
Fred Drake739282d2001-08-16 21:21:28 +0000966For \function{spawnle()}, \function{spawnlpe()}, \function{spawnve()},
967and \function{spawnvpe()} (note that these all end in \character{e}),
968the \var{env} parameter must be a mapping which is used to define the
969environment variables for the new process; the \function{spawnl()},
970\function{spawnlp()}, \function{spawnv()}, and \function{spawnvp()}
971all cause the new process to inherit the environment of the current
972process.
973
974The variants which include a second \character{p} near the end
975(\function{spawnlp()}, \function{spawnlpe()}, \function{spawnvp()},
976and \function{spawnvpe()}) will use the \envvar{PATH} environment
977variable to locate the program \var{path}. The other variants,
978\function{spawnl()}, \function{spawnle()}, \function{spawnv()}, and
979\function{spawnve()}, will not use the \envvar{PATH} variable to
980locate the executable.
981
982The \character{l} and \character{v} variants of the
983\function{spawn*()} functions differ in how command-line arguments are
984passed. The \character{l} variants are perhaps the easiest to work
985with if the number of parameters is fixed when the code is written;
986the individual parameters simply become additional parameters to the
987\function{spawnl*()} functions. The \character{v} variants are good
988when the number of parameters is variable, with the arguments being
989passed in a list or tuple as the \var{args} parameter. In either
990case, the arguments to the child process must start with the name of
991the command being run.
992
993As an example, the following calls to \function{spawnlp()} and
994\function{spawnvpe()} are equivalent:
995
996\begin{verbatim}
997import os
998os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
999
1000L = ['cp', 'index.html', '/dev/null']
1001os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1002\end{verbatim}
1003
Fred Drake15861b22000-02-29 05:19:38 +00001004Availability: \UNIX{}, Windows.
Fred Drake0b9bc202001-06-11 18:25:34 +00001005\versionadded{1.6}
Fred Drake215fe2f1999-02-02 19:02:35 +00001006\end{funcdesc}
1007
Fred Drake9329e5e1999-02-16 19:40:19 +00001008\begin{datadesc}{P_WAIT}
1009\dataline{P_NOWAIT}
1010\dataline{P_NOWAITO}
Fred Drake215fe2f1999-02-02 19:02:35 +00001011Possible values for the \var{mode} parameter to \function{spawnv()}
1012and \function{spawnve()}.
Fred Drake15861b22000-02-29 05:19:38 +00001013Availability: \UNIX{}, Windows.
Fred Drake0b9bc202001-06-11 18:25:34 +00001014\versionadded{1.6}
Fred Drake15861b22000-02-29 05:19:38 +00001015\end{datadesc}
1016
1017\begin{datadesc}{P_OVERLAY}
1018\dataline{P_DETACH}
1019Possible values for the \var{mode} parameter to \function{spawnv()}
1020and \function{spawnve()}. These are less portable than those listed
1021above.
Fred Drake215fe2f1999-02-02 19:02:35 +00001022Availability: Windows.
Fred Drake0b9bc202001-06-11 18:25:34 +00001023\versionadded{1.6}
Fred Drake215fe2f1999-02-02 19:02:35 +00001024\end{datadesc}
1025
Fred Drake4ce4f2e2000-09-29 04:15:19 +00001026\begin{funcdesc}{startfile}{path}
1027Start a file with its associated application. This acts like
1028double-clicking the file in Windows Explorer, or giving the file name
Fred Drake8ee679f2001-07-14 02:50:55 +00001029as an argument to the \program{start} command from the interactive
1030command shell: the file is opened with whatever application (if any)
1031its extension is associated.
Fred Drake4ce4f2e2000-09-29 04:15:19 +00001032
1033\function{startfile()} returns as soon as the associated application
1034is launched. There is no option to wait for the application to close,
1035and no way to retrieve the application's exit status. The \var{path}
1036parameter is relative to the current directory. If you want to use an
1037absolute path, make sure the first character is not a slash
1038(\character{/}); the underlying Win32 \cfunction{ShellExecute()}
Fred Drake8a2adcf2001-07-23 19:20:56 +00001039function doesn't work if it is. Use the \function{os.path.normpath()}
Fred Drake4ce4f2e2000-09-29 04:15:19 +00001040function to ensure that the path is properly encoded for Win32.
1041Availability: Windows.
1042\versionadded{2.0}
1043\end{funcdesc}
1044
Fred Drake215fe2f1999-02-02 19:02:35 +00001045\begin{funcdesc}{system}{command}
1046Execute the command (a string) in a subshell. This is implemented by
1047calling the Standard C function \cfunction{system()}, and has the
Fred Drakeec6baaf1999-04-21 18:13:31 +00001048same limitations. Changes to \code{posix.environ}, \code{sys.stdin},
Fred Drake215fe2f1999-02-02 19:02:35 +00001049etc.\ are not reflected in the environment of the executed command.
1050The return value is the exit status of the process encoded in the
Fred Drake7a621281999-06-10 15:07:05 +00001051format specified for \function{wait()}, except on Windows 95 and 98,
Fred Drakea88ef001999-06-18 19:11:25 +00001052where it is always \code{0}. Note that \POSIX{} does not specify the
1053meaning of the return value of the C \cfunction{system()} function,
1054so the return value of the Python function is system-dependent.
Fred Drake215fe2f1999-02-02 19:02:35 +00001055Availability: \UNIX{}, Windows.
1056\end{funcdesc}
1057
1058\begin{funcdesc}{times}{}
Fred Drake8ee679f2001-07-14 02:50:55 +00001059Return a 5-tuple of floating point numbers indicating accumulated
1060(processor or other)
Fred Drake215fe2f1999-02-02 19:02:35 +00001061times, in seconds. The items are: user time, system time, children's
1062user time, children's system time, and elapsed real time since a fixed
Fred Drakeec6baaf1999-04-21 18:13:31 +00001063point in the past, in that order. See the \UNIX{} manual page
1064\manpage{times}{2} or the corresponding Windows Platform API
1065documentation.
Fred Drake215fe2f1999-02-02 19:02:35 +00001066Availability: \UNIX{}, Windows.
1067\end{funcdesc}
1068
1069\begin{funcdesc}{wait}{}
1070Wait for completion of a child process, and return a tuple containing
1071its pid and exit status indication: a 16-bit number, whose low byte is
1072the signal number that killed the process, and whose high byte is the
1073exit status (if the signal number is zero); the high bit of the low
1074byte is set if a core file was produced.
1075Availability: \UNIX{}.
1076\end{funcdesc}
1077
1078\begin{funcdesc}{waitpid}{pid, options}
Fred Drake31e5e371999-08-13 13:36:33 +00001079Wait for completion of a child process given by process id \var{pid},
1080and return a tuple containing its process id and exit status
1081indication (encoded as for \function{wait()}). The semantics of the
1082call are affected by the value of the integer \var{options}, which
1083should be \code{0} for normal operation.
Fred Drake215fe2f1999-02-02 19:02:35 +00001084Availability: \UNIX{}.
Fred Drake31e5e371999-08-13 13:36:33 +00001085
1086If \var{pid} is greater than \code{0}, \function{waitpid()} requests
1087status information for that specific process. If \var{pid} is
1088\code{0}, the request is for the status of any child in the process
1089group of the current process. If \var{pid} is \code{-1}, the request
1090pertains to any child of the current process. If \var{pid} is less
1091than \code{-1}, status is requested for any process in the process
1092group \code{-\var{pid}} (the absolute value of \var{pid}).
Fred Drake215fe2f1999-02-02 19:02:35 +00001093\end{funcdesc}
1094
1095\begin{datadesc}{WNOHANG}
1096The option for \function{waitpid()} to avoid hanging if no child
1097process status is available immediately.
1098Availability: \UNIX{}.
1099\end{datadesc}
1100
Fred Drake38e5d272000-04-03 20:13:55 +00001101The following functions take a process status code as returned by
1102\function{system()}, \function{wait()}, or \function{waitpid()} as a
1103parameter. They may be used to determine the disposition of a
1104process.
Fred Drake215fe2f1999-02-02 19:02:35 +00001105
1106\begin{funcdesc}{WIFSTOPPED}{status}
1107Return true if the process has been stopped.
1108Availability: \UNIX{}.
1109\end{funcdesc}
1110
1111\begin{funcdesc}{WIFSIGNALED}{status}
1112Return true if the process exited due to a signal.
1113Availability: \UNIX{}.
1114\end{funcdesc}
1115
1116\begin{funcdesc}{WIFEXITED}{status}
1117Return true if the process exited using the \manpage{exit}{2} system
1118call.
1119Availability: \UNIX{}.
1120\end{funcdesc}
1121
1122\begin{funcdesc}{WEXITSTATUS}{status}
1123If \code{WIFEXITED(\var{status})} is true, return the integer
1124parameter to the \manpage{exit}{2} system call. Otherwise, the return
1125value is meaningless.
1126Availability: \UNIX{}.
1127\end{funcdesc}
1128
1129\begin{funcdesc}{WSTOPSIG}{status}
Fred Drake35c3ffd1999-03-04 14:08:10 +00001130Return the signal which caused the process to stop.
1131Availability: \UNIX{}.
1132\end{funcdesc}
1133
1134\begin{funcdesc}{WTERMSIG}{status}
Fred Drake215fe2f1999-02-02 19:02:35 +00001135Return the signal which caused the process to exit.
1136Availability: \UNIX{}.
1137\end{funcdesc}
1138
1139
Thomas Woutersf8316632000-07-16 19:01:10 +00001140\subsection{Miscellaneous System Information \label{os-path}}
Fred Drake88f6ca21999-12-15 19:39:04 +00001141
1142
1143\begin{funcdesc}{confstr}{name}
1144Return string-valued system configuration values.
1145\var{name} specifies the configuration value to retrieve; it may be a
1146string which is the name of a defined system value; these names are
Fred Drake8ee679f2001-07-14 02:50:55 +00001147specified in a number of standards (\POSIX, \UNIX 95, \UNIX 98, and
Fred Drake88f6ca21999-12-15 19:39:04 +00001148others). Some platforms define additional names as well. The names
1149known to the host operating system are given in the
1150\code{confstr_names} dictionary. For configuration variables not
1151included in that mapping, passing an integer for \var{name} is also
1152accepted.
1153Availability: \UNIX{}.
1154
1155If the configuration value specified by \var{name} isn't defined, the
1156empty string is returned.
1157
1158If \var{name} is a string and is not known, \exception{ValueError} is
1159raised. If a specific value for \var{name} is not supported by the
1160host system, even if it is included in \code{confstr_names}, an
1161\exception{OSError} is raised with \constant{errno.EINVAL} for the
1162error number.
1163\end{funcdesc}
1164
1165\begin{datadesc}{confstr_names}
1166Dictionary mapping names accepted by \function{confstr()} to the
1167integer values defined for those names by the host operating system.
1168This can be used to determine the set of names known to the system.
1169Availability: \UNIX.
1170\end{datadesc}
1171
1172\begin{funcdesc}{sysconf}{name}
1173Return integer-valued system configuration values.
1174If the configuration value specified by \var{name} isn't defined,
1175\code{-1} is returned. The comments regarding the \var{name}
1176parameter for \function{confstr()} apply here as well; the dictionary
1177that provides information on the known names is given by
1178\code{sysconf_names}.
1179Availability: \UNIX{}.
1180\end{funcdesc}
1181
1182\begin{datadesc}{sysconf_names}
1183Dictionary mapping names accepted by \function{sysconf()} to the
1184integer values defined for those names by the host operating system.
1185This can be used to determine the set of names known to the system.
1186Availability: \UNIX.
1187\end{datadesc}
1188
Fred Drake215fe2f1999-02-02 19:02:35 +00001189
1190The follow data values are used to support path manipulation
1191operations. These are defined for all platforms.
1192
1193Higher-level operations on pathnames are defined in the
1194\refmodule{os.path} module.
1195
1196
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001197\begin{datadesc}{curdir}
Fred Drake8ee679f2001-07-14 02:50:55 +00001198The constant string used by the operating system to refer to the current
1199directory.
Fred Drake907e76b2001-07-06 20:30:11 +00001200For example: \code{'.'} for \POSIX{} or \code{':'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001201\end{datadesc}
1202
1203\begin{datadesc}{pardir}
Fred Drake8ee679f2001-07-14 02:50:55 +00001204The constant string used by the operating system to refer to the parent
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}{sep}
Fred Drake8ee679f2001-07-14 02:50:55 +00001210The character used by the operating system to separate pathname components,
Fred Drake907e76b2001-07-06 20:30:11 +00001211for example, \character{/} for \POSIX{} or \character{:} for the
1212Macintosh. Note that knowing this is not sufficient to be able to
1213parse or concatenate pathnames --- use \function{os.path.split()} and
Fred Drake1a3c2a01998-08-06 15:18:23 +00001214\function{os.path.join()} --- but it is occasionally useful.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001215\end{datadesc}
1216
Guido van Rossumb2afc811997-08-29 22:37:44 +00001217\begin{datadesc}{altsep}
Fred Drake8ee679f2001-07-14 02:50:55 +00001218An alternative character used by the operating system to separate pathname
1219components, or \code{None} if only one separator character exists. This is
1220set to \character{/} on DOS and Windows systems where \code{sep} is a
1221backslash.
Guido van Rossumb2afc811997-08-29 22:37:44 +00001222\end{datadesc}
1223
Guido van Rossum470be141995-03-17 16:07:09 +00001224\begin{datadesc}{pathsep}
Fred Drake8ee679f2001-07-14 02:50:55 +00001225The character conventionally used by the operating system to separate
1226search patch components (as in \envvar{PATH}), such as \character{:} for
1227\POSIX{} or \character{;} for DOS and Windows.
Guido van Rossum9c59ce91998-06-30 15:54:27 +00001228\end{datadesc}
1229
Guido van Rossum470be141995-03-17 16:07:09 +00001230\begin{datadesc}{defpath}
Fred Drake1a3c2a01998-08-06 15:18:23 +00001231The default search path used by \function{exec*p*()} if the environment
Guido van Rossum470be141995-03-17 16:07:09 +00001232doesn't have a \code{'PATH'} key.
1233\end{datadesc}
1234
Fred Drake215fe2f1999-02-02 19:02:35 +00001235\begin{datadesc}{linesep}
1236The string used to separate (or, rather, terminate) lines on the
Fred Drake907e76b2001-07-06 20:30:11 +00001237current platform. This may be a single character, such as \code{'\e
Fred Drake8ee679f2001-07-14 02:50:55 +00001238n'} for \POSIX{} or \code{'\e r'} for the Mac OS, or multiple characters,
1239for example, \code{'\e r\e n'} for DOS and Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +00001240\end{datadesc}