blob: 70552c40070fa998367e23e4b3223afbc73bb1cc [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
Martin v. Löwis61c5edf2001-10-18 04:06:00 +0000207\begin{funcdesc}{setgroups}{groups}
208Set list of supplemental group ids associated with the current
209process to \var{groups}.
210Availability: \UNIX{}.
211\versionadded{2.2}
212\end{funcdesc}
213
Fred Drake215fe2f1999-02-02 19:02:35 +0000214\begin{funcdesc}{setpgrp}{}
215Calls the system call \cfunction{setpgrp()} or \cfunction{setpgrp(0,
2160)} depending on which version is implemented (if any). See the
217\UNIX{} manual for the semantics.
218Availability: \UNIX{}.
219\end{funcdesc}
220
221\begin{funcdesc}{setpgid}{pid, pgrp}
222Calls the system call \cfunction{setpgid()}. See the \UNIX{} manual
223for the semantics.
224Availability: \UNIX{}.
225\end{funcdesc}
226
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +0000227\begin{funcdesc}{setreuid}{ruid, euid}
228Set the current process's real and effective user ids.
229Availability: \UNIX{}.
230\end{funcdesc}
231
232\begin{funcdesc}{setregid}{rgid, egid}
233Set the current process's real and effective group ids.
234Availability: \UNIX{}.
235\end{funcdesc}
236
Fred Drake215fe2f1999-02-02 19:02:35 +0000237\begin{funcdesc}{setsid}{}
238Calls the system call \cfunction{setsid()}. See the \UNIX{} manual
239for the semantics.
240Availability: \UNIX{}.
241\end{funcdesc}
242
243\begin{funcdesc}{setuid}{uid}
Fred Drake6b330ba81999-05-25 13:42:26 +0000244\index{user!id, setting}
Fred Drake215fe2f1999-02-02 19:02:35 +0000245Set the current process' user id.
246Availability: \UNIX{}.
247\end{funcdesc}
248
249% placed in this section since it relates to errno.... a little weak ;-(
250\begin{funcdesc}{strerror}{code}
251Return the error message corresponding to the error code in
252\var{code}.
253Availability: \UNIX{}, Windows.
254\end{funcdesc}
255
256\begin{funcdesc}{umask}{mask}
257Set the current numeric umask and returns the previous umask.
258Availability: \UNIX{}, Windows.
259\end{funcdesc}
260
261\begin{funcdesc}{uname}{}
262Return a 5-tuple containing information identifying the current
263operating system. The tuple contains 5 strings:
264\code{(\var{sysname}, \var{nodename}, \var{release}, \var{version},
265\var{machine})}. Some systems truncate the nodename to 8
266characters or to the leading component; a better way to get the
267hostname is \function{socket.gethostname()}
268\withsubitem{(in module socket)}{\ttindex{gethostname()}}
269or even
270\withsubitem{(in module socket)}{\ttindex{gethostbyaddr()}}
271\code{socket.gethostbyaddr(socket.gethostname())}.
272Availability: recent flavors of \UNIX{}.
273\end{funcdesc}
274
275
276
277\subsection{File Object Creation \label{os-newstreams}}
278
279These functions create new file objects.
280
281
282\begin{funcdesc}{fdopen}{fd\optional{, mode\optional{, bufsize}}}
283Return an open file object connected to the file descriptor \var{fd}.
Fred Drake8c9fc001999-08-05 13:41:31 +0000284\index{I/O control!buffering}
Fred Drake215fe2f1999-02-02 19:02:35 +0000285The \var{mode} and \var{bufsize} arguments have the same meaning as
286the corresponding arguments to the built-in \function{open()}
287function.
288Availability: Macintosh, \UNIX{}, Windows.
289\end{funcdesc}
290
291\begin{funcdesc}{popen}{command\optional{, mode\optional{, bufsize}}}
292Open a pipe to or from \var{command}. The return value is an open
293file object connected to the pipe, which can be read or written
294depending on whether \var{mode} is \code{'r'} (default) or \code{'w'}.
295The \var{bufsize} argument has the same meaning as the corresponding
296argument to the built-in \function{open()} function. The exit status of
297the command (encoded in the format specified for \function{wait()}) is
298available as the return value of the \method{close()} method of the file
299object, except that when the exit status is zero (termination without
Fred Drake1319e3e2000-10-03 17:14:27 +0000300errors), \code{None} is returned.
Fred Drake215fe2f1999-02-02 19:02:35 +0000301Availability: \UNIX{}, Windows.
Fred Drakec71c23e2000-10-04 13:57:27 +0000302
303\versionchanged[This function worked unreliably under Windows in
304 earlier versions of Python. This was due to the use of the
305 \cfunction{_popen()} function from the libraries provided with
306 Windows. Newer versions of Python do not use the broken
307 implementation from the Windows libraries]{2.0}
Fred Drake215fe2f1999-02-02 19:02:35 +0000308\end{funcdesc}
309
Fred Drake18f7a451999-12-09 22:11:43 +0000310\begin{funcdesc}{tmpfile}{}
311Return a new file object opened in update mode (\samp{w+}). The file
312has no directory entries associated with it and will be automatically
313deleted once there are no file descriptors for the file.
Fred Drakeefaef132001-07-17 20:39:18 +0000314Availability: \UNIX{}, Windows.
Fred Drake18f7a451999-12-09 22:11:43 +0000315\end{funcdesc}
Fred Drake215fe2f1999-02-02 19:02:35 +0000316
317
Fred Drake8a9db992000-09-28 20:27:51 +0000318For each of these \function{popen()} variants, if \var{bufsize} is
319specified, it specifies the buffer size for the I/O pipes.
320\var{mode}, if provided, should be the string \code{'b'} or
321\code{'t'}; on Windows this is needed to determine whether the file
322objects should be opened in binary or text mode. The default value
323for \var{mode} is \code{'t'}.
324
Fred Drake098d7fa2001-09-11 19:56:51 +0000325These methods do not make it possible to retrieve the return code from
326the child processes. The only way to control the input and output
327streams and also retrieve the return codes is to use the
328\class{Popen3} and \class{Popen4} classes from the \refmodule{popen2}
329module; these are only available on \UNIX.
330
Fred Drake046f4d82001-06-11 15:21:48 +0000331\begin{funcdesc}{popen2}{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})}.
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}{popen3}{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}, \var{child_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
Fred Drake046f4d82001-06-11 15:21:48 +0000345\begin{funcdesc}{popen4}{cmd\optional{, mode\optional{, bufsize}}}
Fred Drake8a9db992000-09-28 20:27:51 +0000346Executes \var{cmd} as a sub-process. Returns the file objects
347\code{(\var{child_stdin}, \var{child_stdout_and_stderr})}.
Fred Drake0b9bc202001-06-11 18:25:34 +0000348Availability: \UNIX{}, Windows.
Fred Drake8a9db992000-09-28 20:27:51 +0000349\versionadded{2.0}
350\end{funcdesc}
351
352This functionality is also available in the \refmodule{popen2} module
353using functions of the same names, but the return values of those
354functions have a different order.
355
356
Fred Drake215fe2f1999-02-02 19:02:35 +0000357\subsection{File Descriptor Operations \label{os-fd-ops}}
358
359These functions operate on I/O streams referred to
360using file descriptors.
361
362
363\begin{funcdesc}{close}{fd}
364Close file descriptor \var{fd}.
365Availability: Macintosh, \UNIX{}, Windows.
366
367Note: this function is intended for low-level I/O and must be applied
368to a file descriptor as returned by \function{open()} or
369\function{pipe()}. To close a ``file object'' returned by the
370built-in function \function{open()} or by \function{popen()} or
371\function{fdopen()}, use its \method{close()} method.
372\end{funcdesc}
373
374\begin{funcdesc}{dup}{fd}
375Return a duplicate of file descriptor \var{fd}.
376Availability: Macintosh, \UNIX{}, Windows.
377\end{funcdesc}
378
379\begin{funcdesc}{dup2}{fd, fd2}
380Duplicate file descriptor \var{fd} to \var{fd2}, closing the latter
381first if necessary.
382Availability: \UNIX{}, Windows.
383\end{funcdesc}
384
Fred Drake88f6ca21999-12-15 19:39:04 +0000385\begin{funcdesc}{fpathconf}{fd, name}
Thomas Woutersf8316632000-07-16 19:01:10 +0000386Return system configuration information relevant to an open file.
Fred Drake88f6ca21999-12-15 19:39:04 +0000387\var{name} specifies the configuration value to retrieve; it may be a
388string which is the name of a defined system value; these names are
389specified in a number of standards (\POSIX.1, Unix95, Unix98, and
390others). Some platforms define additional names as well. The names
391known to the host operating system are given in the
392\code{pathconf_names} dictionary. For configuration variables not
393included in that mapping, passing an integer for \var{name} is also
394accepted.
395Availability: \UNIX{}.
396
397If \var{name} is a string and is not known, \exception{ValueError} is
398raised. If a specific value for \var{name} is not supported by the
399host system, even if it is included in \code{pathconf_names}, an
400\exception{OSError} is raised with \constant{errno.EINVAL} for the
401error number.
402\end{funcdesc}
403
Fred Drake215fe2f1999-02-02 19:02:35 +0000404\begin{funcdesc}{fstat}{fd}
405Return status for file descriptor \var{fd}, like \function{stat()}.
406Availability: \UNIX{}, Windows.
407\end{funcdesc}
408
409\begin{funcdesc}{fstatvfs}{fd}
410Return information about the filesystem containing the file associated
411with file descriptor \var{fd}, like \function{statvfs()}.
412Availability: \UNIX{}.
413\end{funcdesc}
414
415\begin{funcdesc}{ftruncate}{fd, length}
416Truncate the file corresponding to file descriptor \var{fd},
417so that it is at most \var{length} bytes in size.
418Availability: \UNIX{}.
419\end{funcdesc}
420
Skip Montanarod3725212000-07-19 17:30:58 +0000421\begin{funcdesc}{isatty}{fd}
422Return \code{1} if the file descriptor \var{fd} is open and connected to a
423tty(-like) device, else \code{0}.
424Availability: \UNIX{}
425\end{funcdesc}
426
Fred Drake215fe2f1999-02-02 19:02:35 +0000427\begin{funcdesc}{lseek}{fd, pos, how}
428Set the current position of file descriptor \var{fd} to position
429\var{pos}, modified by \var{how}: \code{0} to set the position
430relative to the beginning of the file; \code{1} to set it relative to
431the current position; \code{2} to set it relative to the end of the
432file.
433Availability: Macintosh, \UNIX{}, Windows.
434\end{funcdesc}
435
436\begin{funcdesc}{open}{file, flags\optional{, mode}}
437Open the file \var{file} and set various flags according to
438\var{flags} and possibly its mode according to \var{mode}.
439The default \var{mode} is \code{0777} (octal), and the current umask
440value is first masked out. Return the file descriptor for the newly
441opened file.
442Availability: Macintosh, \UNIX{}, Windows.
443
444For a description of the flag and mode values, see the C run-time
445documentation; flag constants (like \constant{O_RDONLY} and
446\constant{O_WRONLY}) are defined in this module too (see below).
447
448Note: this function is intended for low-level I/O. For normal usage,
449use the built-in function \function{open()}, which returns a ``file
450object'' with \method{read()} and \method{write()} methods (and many
451more).
452\end{funcdesc}
453
Fred Drakec82634c2000-06-28 17:27:48 +0000454\begin{funcdesc}{openpty}{}
455Open a new pseudo-terminal pair. Return a pair of file descriptors
456\code{(\var{master}, \var{slave})} for the pty and the tty,
457respectively. For a (slightly) more portable approach, use the
458\refmodule{pty}\refstmodindex{pty} module.
459Availability: Some flavors of \UNIX{}
460\end{funcdesc}
461
Fred Drake215fe2f1999-02-02 19:02:35 +0000462\begin{funcdesc}{pipe}{}
463Create a pipe. Return a pair of file descriptors \code{(\var{r},
464\var{w})} usable for reading and writing, respectively.
465Availability: \UNIX{}, Windows.
466\end{funcdesc}
467
468\begin{funcdesc}{read}{fd, n}
469Read at most \var{n} bytes from file descriptor \var{fd}.
470Return a string containing the bytes read.
471Availability: Macintosh, \UNIX{}, Windows.
472
473Note: this function is intended for low-level I/O and must be applied
474to a file descriptor as returned by \function{open()} or
475\function{pipe()}. To read a ``file object'' returned by the
476built-in function \function{open()} or by \function{popen()} or
477\function{fdopen()}, or \code{sys.stdin}, use its
478\method{read()} or \method{readline()} methods.
479\end{funcdesc}
480
481\begin{funcdesc}{tcgetpgrp}{fd}
482Return the process group associated with the terminal given by
483\var{fd} (an open file descriptor as returned by \function{open()}).
484Availability: \UNIX{}.
485\end{funcdesc}
486
487\begin{funcdesc}{tcsetpgrp}{fd, pg}
488Set the process group associated with the terminal given by
489\var{fd} (an open file descriptor as returned by \function{open()})
490to \var{pg}.
491Availability: \UNIX{}.
492\end{funcdesc}
493
494\begin{funcdesc}{ttyname}{fd}
495Return a string which specifies the terminal device associated with
496file-descriptor \var{fd}. If \var{fd} is not associated with a terminal
497device, an exception is raised.
498Availability: \UNIX{}.
499\end{funcdesc}
500
501\begin{funcdesc}{write}{fd, str}
502Write the string \var{str} to file descriptor \var{fd}.
503Return the number of bytes actually written.
504Availability: Macintosh, \UNIX{}, Windows.
505
506Note: this function is intended for low-level I/O and must be applied
507to a file descriptor as returned by \function{open()} or
508\function{pipe()}. To write a ``file object'' returned by the
509built-in function \function{open()} or by \function{popen()} or
510\function{fdopen()}, or \code{sys.stdout} or \code{sys.stderr}, use
511its \method{write()} method.
512\end{funcdesc}
513
514
515The following data items are available for use in constructing the
516\var{flags} parameter to the \function{open()} function.
517
518\begin{datadesc}{O_RDONLY}
519\dataline{O_WRONLY}
520\dataline{O_RDWR}
521\dataline{O_NDELAY}
522\dataline{O_NONBLOCK}
523\dataline{O_APPEND}
524\dataline{O_DSYNC}
525\dataline{O_RSYNC}
526\dataline{O_SYNC}
527\dataline{O_NOCTTY}
528\dataline{O_CREAT}
529\dataline{O_EXCL}
530\dataline{O_TRUNC}
531Options for the \var{flag} argument to the \function{open()} function.
532These can be bit-wise OR'd together.
533Availability: Macintosh, \UNIX{}, Windows.
534\end{datadesc}
535
Fred Drake3ac977e2000-08-11 20:19:51 +0000536\begin{datadesc}{O_BINARY}
537Option for the \var{flag} argument to the \function{open()} function.
538This can be bit-wise OR'd together with those listed above.
539Availability: Macintosh, Windows.
540% XXX need to check on the availability of this one.
541\end{datadesc}
542
Fred Drake215fe2f1999-02-02 19:02:35 +0000543
544\subsection{Files and Directories \label{os-file-dir}}
545
546\begin{funcdesc}{access}{path, mode}
Fred Drake38e5d272000-04-03 20:13:55 +0000547Check read/write/execute permissions for this process or existence of
548file \var{path}. \var{mode} should be \constant{F_OK} to test the
549existence of \var{path}, or it can be the inclusive OR of one or more
550of \constant{R_OK}, \constant{W_OK}, and \constant{X_OK} to test
551permissions. Return \code{1} if access is allowed, \code{0} if not.
552See the \UNIX{} man page \manpage{access}{2} for more information.
Fred Drake3ac977e2000-08-11 20:19:51 +0000553Availability: \UNIX{}, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000554\end{funcdesc}
555
Fred Drake38e5d272000-04-03 20:13:55 +0000556\begin{datadesc}{F_OK}
557 Value to pass as the \var{mode} parameter of \function{access()} to
558 test the existence of \var{path}.
559\end{datadesc}
560
561\begin{datadesc}{R_OK}
562 Value to include in the \var{mode} parameter of \function{access()}
563 to test the readability of \var{path}.
564\end{datadesc}
565
566\begin{datadesc}{W_OK}
567 Value to include in the \var{mode} parameter of \function{access()}
568 to test the writability of \var{path}.
569\end{datadesc}
570
571\begin{datadesc}{X_OK}
572 Value to include in the \var{mode} parameter of \function{access()}
573 to determine if \var{path} can be executed.
574\end{datadesc}
575
Fred Drake6db897c1999-07-12 16:49:30 +0000576\begin{funcdesc}{chdir}{path}
577\index{directory!changing}
578Change the current working directory to \var{path}.
579Availability: Macintosh, \UNIX{}, Windows.
580\end{funcdesc}
581
582\begin{funcdesc}{getcwd}{}
583Return a string representing the current working directory.
584Availability: Macintosh, \UNIX{}, Windows.
585\end{funcdesc}
586
Martin v. Löwis244edc82001-10-04 22:44:26 +0000587\begin{funcdesc}{chroot}{path}
588Change the root directory of the current process to \var{path}.
589Availability: \UNIX{}.
590\versionadded{2.2}
591\end{funcdesc}
592
Fred Drake215fe2f1999-02-02 19:02:35 +0000593\begin{funcdesc}{chmod}{path, mode}
594Change the mode of \var{path} to the numeric \var{mode}.
595Availability: \UNIX{}, Windows.
596\end{funcdesc}
597
598\begin{funcdesc}{chown}{path, uid, gid}
599Change the owner and group id of \var{path} to the numeric \var{uid}
600and \var{gid}.
601Availability: \UNIX{}.
602\end{funcdesc}
603
604\begin{funcdesc}{link}{src, dst}
605Create a hard link pointing to \var{src} named \var{dst}.
606Availability: \UNIX{}.
607\end{funcdesc}
608
609\begin{funcdesc}{listdir}{path}
610Return a list containing the names of the entries in the directory.
611The list is in arbitrary order. It does not include the special
612entries \code{'.'} and \code{'..'} even if they are present in the
613directory.
614Availability: Macintosh, \UNIX{}, Windows.
615\end{funcdesc}
616
617\begin{funcdesc}{lstat}{path}
618Like \function{stat()}, but do not follow symbolic links.
619Availability: \UNIX{}.
620\end{funcdesc}
621
622\begin{funcdesc}{mkfifo}{path\optional{, mode}}
623Create a FIFO (a named pipe) named \var{path} with numeric mode
624\var{mode}. The default \var{mode} is \code{0666} (octal). The current
625umask value is first masked out from the mode.
626Availability: \UNIX{}.
627
628FIFOs are pipes that can be accessed like regular files. FIFOs exist
629until they are deleted (for example with \function{os.unlink()}).
630Generally, FIFOs are used as rendezvous between ``client'' and
631``server'' type processes: the server opens the FIFO for reading, and
632the client opens it for writing. Note that \function{mkfifo()}
633doesn't open the FIFO --- it just creates the rendezvous point.
634\end{funcdesc}
635
636\begin{funcdesc}{mkdir}{path\optional{, mode}}
637Create a directory named \var{path} with numeric mode \var{mode}.
638The default \var{mode} is \code{0777} (octal). On some systems,
639\var{mode} is ignored. Where it is used, the current umask value is
640first masked out.
641Availability: Macintosh, \UNIX{}, Windows.
642\end{funcdesc}
643
644\begin{funcdesc}{makedirs}{path\optional{, mode}}
645\index{directory!creating}
646Recursive directory creation function. Like \function{mkdir()},
647but makes all intermediate-level directories needed to contain the
648leaf directory. Throws an \exception{error} exception if the leaf
649directory already exists or cannot be created. The default \var{mode}
Fred Drakebbf7a402001-09-28 16:14:18 +0000650is \code{0777} (octal). This function does not properly handle UNC
651paths (only relevant on Windows systems).
Fred Drake215fe2f1999-02-02 19:02:35 +0000652\versionadded{1.5.2}
653\end{funcdesc}
654
Fred Drake88f6ca21999-12-15 19:39:04 +0000655\begin{funcdesc}{pathconf}{path, name}
Thomas Woutersf8316632000-07-16 19:01:10 +0000656Return system configuration information relevant to a named file.
Fred Drake88f6ca21999-12-15 19:39:04 +0000657\var{name} specifies the configuration value to retrieve; it may be a
658string which is the name of a defined system value; these names are
Fred Drake8ee679f2001-07-14 02:50:55 +0000659specified in a number of standards (\POSIX.1, \UNIX 95, \UNIX 98, and
Fred Drake88f6ca21999-12-15 19:39:04 +0000660others). Some platforms define additional names as well. The names
661known to the host operating system are given in the
662\code{pathconf_names} dictionary. For configuration variables not
663included in that mapping, passing an integer for \var{name} is also
664accepted.
665Availability: \UNIX{}.
666
667If \var{name} is a string and is not known, \exception{ValueError} is
668raised. If a specific value for \var{name} is not supported by the
669host system, even if it is included in \code{pathconf_names}, an
670\exception{OSError} is raised with \constant{errno.EINVAL} for the
671error number.
672\end{funcdesc}
673
674\begin{datadesc}{pathconf_names}
675Dictionary mapping names accepted by \function{pathconf()} and
676\function{fpathconf()} to the integer values defined for those names
677by the host operating system. This can be used to determine the set
678of names known to the system.
679Availability: \UNIX.
680\end{datadesc}
681
Fred Drake215fe2f1999-02-02 19:02:35 +0000682\begin{funcdesc}{readlink}{path}
683Return a string representing the path to which the symbolic link
Fred Drakedc9e7e42001-05-29 18:13:06 +0000684points. The result may be either an absolute or relative pathname; if
685it is relative, it may be converted to an absolute pathname using
686\code{os.path.join(os.path.dirname(\var{path}), \var{result})}.
Fred Drake215fe2f1999-02-02 19:02:35 +0000687Availability: \UNIX{}.
688\end{funcdesc}
689
690\begin{funcdesc}{remove}{path}
Fred Drakedc9e7e42001-05-29 18:13:06 +0000691Remove the file \var{path}. If \var{path} is a directory,
692\exception{OSError} is raised; see \function{rmdir()} below to remove
693a directory. This is identical to the \function{unlink()} function
694documented below. On Windows, attempting to remove a file that is in
695use causes an exception to be raised; on \UNIX, the directory entry is
696removed but the storage allocated to the file is not made available
697until the original file is no longer in use.
Fred Drake215fe2f1999-02-02 19:02:35 +0000698Availability: Macintosh, \UNIX{}, Windows.
699\end{funcdesc}
700
701\begin{funcdesc}{removedirs}{path}
702\index{directory!deleting}
703Recursive directory removal function. Works like
704\function{rmdir()} except that, if the leaf directory is
705successfully removed, directories corresponding to rightmost path
706segments will be pruned way until either the whole path is consumed or
707an error is raised (which is ignored, because it generally means that
708a parent directory is not empty). Throws an \exception{error}
709exception if the leaf directory could not be successfully removed.
710\versionadded{1.5.2}
711\end{funcdesc}
712
713\begin{funcdesc}{rename}{src, dst}
Fred Drakedc9e7e42001-05-29 18:13:06 +0000714Rename the file or directory \var{src} to \var{dst}. If \var{dst} is
715a directory, \exception{OSError} will be raised. On \UNIX, if
716\var{dst} exists and is a file, it will be removed silently if the
717user has permission. The operation may fail on some \UNIX{} flavors
Skip Montanarob9d973d2001-06-04 15:31:17 +0000718if \var{src} and \var{dst} are on different filesystems. If
Fred Drakedc9e7e42001-05-29 18:13:06 +0000719successful, the renaming will be an atomic operation (this is a
720\POSIX{} requirement). On Windows, if \var{dst} already exists,
721\exception{OSError} will be raised even if it is a file; there may be
722no way to implement an atomic rename when \var{dst} names an existing
723file.
Fred Drake215fe2f1999-02-02 19:02:35 +0000724Availability: Macintosh, \UNIX{}, Windows.
725\end{funcdesc}
726
727\begin{funcdesc}{renames}{old, new}
728Recursive directory or file renaming function.
729Works like \function{rename()}, except creation of any intermediate
730directories needed to make the new pathname good is attempted first.
731After the rename, directories corresponding to rightmost path segments
732of the old name will be pruned away using \function{removedirs()}.
733
734Note: this function can fail with the new directory structure made if
735you lack permissions needed to remove the leaf directory or file.
736\versionadded{1.5.2}
737\end{funcdesc}
738
739\begin{funcdesc}{rmdir}{path}
740Remove the directory \var{path}.
741Availability: Macintosh, \UNIX{}, Windows.
742\end{funcdesc}
743
744\begin{funcdesc}{stat}{path}
745Perform a \cfunction{stat()} system call on the given path. The
746return value is a tuple of at least 10 integers giving the most
747important (and portable) members of the \emph{stat} structure, in the
748order
749\code{st_mode},
750\code{st_ino},
751\code{st_dev},
752\code{st_nlink},
753\code{st_uid},
754\code{st_gid},
755\code{st_size},
756\code{st_atime},
757\code{st_mtime},
758\code{st_ctime}.
Fred Drake21c9df72000-10-14 05:46:11 +0000759More items may be added at the end by some implementations. Note that
Fred Drake8ee679f2001-07-14 02:50:55 +0000760on the Mac OS, the time values are floating point values, like all
761time values on the Mac OS.
762(On Windows, some items are filled with dummy values.)
Fred Drake215fe2f1999-02-02 19:02:35 +0000763Availability: Macintosh, \UNIX{}, Windows.
764
765Note: The standard module \refmodule{stat}\refstmodindex{stat} defines
766functions and constants that are useful for extracting information
767from a \ctype{stat} structure.
768\end{funcdesc}
769
770\begin{funcdesc}{statvfs}{path}
771Perform a \cfunction{statvfs()} system call on the given path. The
Guido van Rossum0c9608c1999-02-03 16:32:37 +0000772return value is a tuple of 10 integers giving the most common
Fred Drake215fe2f1999-02-02 19:02:35 +0000773members of the \ctype{statvfs} structure, in the order
774\code{f_bsize},
775\code{f_frsize},
776\code{f_blocks},
777\code{f_bfree},
778\code{f_bavail},
779\code{f_files},
780\code{f_ffree},
781\code{f_favail},
Fred Drake215fe2f1999-02-02 19:02:35 +0000782\code{f_flag},
783\code{f_namemax}.
784Availability: \UNIX{}.
785
786Note: The standard module \module{statvfs}\refstmodindex{statvfs}
787defines constants that are useful for extracting information
788from a \ctype{statvfs} structure.
789\end{funcdesc}
790
791\begin{funcdesc}{symlink}{src, dst}
792Create a symbolic link pointing to \var{src} named \var{dst}.
793Availability: \UNIX{}.
794\end{funcdesc}
795
Fred Drake18f7a451999-12-09 22:11:43 +0000796\begin{funcdesc}{tempnam}{\optional{dir\optional{, prefix}}}
797Return a unique path name that is reasonable for creating a temporary
798file. This will be an absolute path that names a potential directory
799entry in the directory \var{dir} or a common location for temporary
800files if \var{dir} is omitted or \code{None}. If given and not
801\code{None}, \var{prefix} is used to provide a short prefix to the
802filename. Applications are responsible for properly creating and
803managing files created using paths returned by \function{tempnam()};
804no automatic cleanup is provided.
Fred Drake938a8d72001-10-09 18:07:04 +0000805\warning{Use of \function{tempnam()} is vulnerable to symlink attacks;
806consider using \function{tmpfile()} instead.}
Fred Drakeefaef132001-07-17 20:39:18 +0000807Availability: \UNIX, Windows.
Fred Drake18f7a451999-12-09 22:11:43 +0000808\end{funcdesc}
809
810\begin{funcdesc}{tmpnam}{}
811Return a unique path name that is reasonable for creating a temporary
812file. This will be an absolute path that names a potential directory
813entry in a common location for temporary files. Applications are
814responsible for properly creating and managing files created using
815paths returned by \function{tmpnam()}; no automatic cleanup is
816provided.
Fred Drake938a8d72001-10-09 18:07:04 +0000817\warning{Use of \function{tmpnam()} is vulnerable to symlink attacks;
818consider using \function{tmpfile()} instead.}
Fred Drakeefaef132001-07-17 20:39:18 +0000819Availability: \UNIX, Windows.
Fred Drake18f7a451999-12-09 22:11:43 +0000820\end{funcdesc}
821
822\begin{datadesc}{TMP_MAX}
823The maximum number of unique names that \function{tmpnam()} will
824generate before reusing names.
825\end{datadesc}
826
Fred Drake215fe2f1999-02-02 19:02:35 +0000827\begin{funcdesc}{unlink}{path}
828Remove the file \var{path}. This is the same function as
829\function{remove()}; the \function{unlink()} name is its traditional
830\UNIX{} name.
831Availability: Macintosh, \UNIX{}, Windows.
832\end{funcdesc}
833
Barry Warsaw93a8eac2000-05-01 16:18:22 +0000834\begin{funcdesc}{utime}{path, times}
835Set the access and modified times of the file specified by \var{path}.
836If \var{times} is \code{None}, then the file's access and modified
837times are set to the current time. Otherwise, \var{times} must be a
Fred Drakee06d0252000-05-02 17:29:35 +00008382-tuple of numbers, of the form \code{(\var{atime}, \var{mtime})}
839which is used to set the access and modified times, respectively.
Fred Drake4a152632000-10-19 05:33:46 +0000840\versionchanged[Added support for \code{None} for \var{times}]{2.0}
Fred Drake215fe2f1999-02-02 19:02:35 +0000841Availability: Macintosh, \UNIX{}, Windows.
842\end{funcdesc}
843
844
845\subsection{Process Management \label{os-process}}
846
Fred Drake18f7a451999-12-09 22:11:43 +0000847These functions may be used to create and manage processes.
Fred Drake215fe2f1999-02-02 19:02:35 +0000848
Fred Drake7be31152000-09-23 05:22:07 +0000849The various \function{exec*()} functions take a list of arguments for
850the new program loaded into the process. In each case, the first of
851these arguments is passed to the new program as its own name rather
852than as an argument a user may have typed on a command line. For the
853C programmer, this is the \code{argv[0]} passed to a program's
854\cfunction{main()}. For example, \samp{os.execv('/bin/echo', ['foo',
855'bar'])} will only print \samp{bar} on standard output; \samp{foo}
856will seem to be ignored.
857
Fred Drake215fe2f1999-02-02 19:02:35 +0000858
Fred Drake18f7a451999-12-09 22:11:43 +0000859\begin{funcdesc}{abort}{}
860Generate a \constant{SIGABRT} signal to the current process. On
861\UNIX, the default behavior is to produce a core dump; on Windows, the
862process immediately returns an exit code of \code{3}. Be aware that
863programs which use \function{signal.signal()} to register a handler
864for \constant{SIGABRT} will behave differently.
865Availability: \UNIX, Windows.
866\end{funcdesc}
867
Fred Drake215fe2f1999-02-02 19:02:35 +0000868\begin{funcdesc}{execl}{path, arg0, arg1, ...}
869This is equivalent to
870\samp{execv(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
871Availability: \UNIX{}, Windows.
872\end{funcdesc}
873
874\begin{funcdesc}{execle}{path, arg0, arg1, ..., env}
875This is equivalent to
876\samp{execve(\var{path}, (\var{arg0}, \var{arg1}, ...), \var{env})}.
877Availability: \UNIX{}, Windows.
878\end{funcdesc}
879
880\begin{funcdesc}{execlp}{path, arg0, arg1, ...}
881This is equivalent to
882\samp{execvp(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
883Availability: \UNIX{}, Windows.
884\end{funcdesc}
885
886\begin{funcdesc}{execv}{path, args}
887Execute the executable \var{path} with argument list \var{args},
Fred Drake907e76b2001-07-06 20:30:11 +0000888replacing the current process (the Python interpreter).
Fred Drake215fe2f1999-02-02 19:02:35 +0000889The argument list may be a tuple or list of strings.
890Availability: \UNIX{}, Windows.
891\end{funcdesc}
892
893\begin{funcdesc}{execve}{path, args, env}
894Execute the executable \var{path} with argument list \var{args},
Fred Drake907e76b2001-07-06 20:30:11 +0000895and environment \var{env}, replacing the current process (the Python
896interpreter).
Fred Drake215fe2f1999-02-02 19:02:35 +0000897The argument list may be a tuple or list of strings.
898The environment must be a dictionary mapping strings to strings.
899Availability: \UNIX{}, Windows.
900\end{funcdesc}
901
902\begin{funcdesc}{execvp}{path, args}
903This is like \samp{execv(\var{path}, \var{args})} but duplicates
904the shell's actions in searching for an executable file in a list of
905directories. The directory list is obtained from
906\code{environ['PATH']}.
907Availability: \UNIX{}, Windows.
908\end{funcdesc}
909
910\begin{funcdesc}{execvpe}{path, args, env}
911This is a cross between \function{execve()} and \function{execvp()}.
912The directory list is obtained from \code{\var{env}['PATH']}.
913Availability: \UNIX{}, Windows.
914\end{funcdesc}
915
916\begin{funcdesc}{_exit}{n}
917Exit to the system with status \var{n}, without calling cleanup
918handlers, flushing stdio buffers, etc.
919Availability: \UNIX{}, Windows.
920
921Note: the standard way to exit is \code{sys.exit(\var{n})}.
922\function{_exit()} should normally only be used in the child process
923after a \function{fork()}.
924\end{funcdesc}
925
926\begin{funcdesc}{fork}{}
927Fork a child process. Return \code{0} in the child, the child's
928process id in the parent.
929Availability: \UNIX{}.
930\end{funcdesc}
931
Fred Drakec82634c2000-06-28 17:27:48 +0000932\begin{funcdesc}{forkpty}{}
933Fork a child process, using a new pseudo-terminal as the child's
934controlling terminal. Return a pair of \code{(\var{pid}, \var{fd})},
935where \var{pid} is \code{0} in the child, the new child's process id
936in the parent, and \code{fd} is the file descriptor of the master end
937of the pseudo-terminal. For a more portable approach, use the
938\refmodule{pty} module.
939Availability: Some flavors of \UNIX{}
940\end{funcdesc}
941
Fred Drake215fe2f1999-02-02 19:02:35 +0000942\begin{funcdesc}{kill}{pid, sig}
943\index{process!killing}
944\index{process!signalling}
945Kill the process \var{pid} with signal \var{sig}.
946Availability: \UNIX{}.
947\end{funcdesc}
948
949\begin{funcdesc}{nice}{increment}
950Add \var{increment} to the process's ``niceness''. Return the new
951niceness.
952Availability: \UNIX{}.
953\end{funcdesc}
954
955\begin{funcdesc}{plock}{op}
956Lock program segments into memory. The value of \var{op}
957(defined in \code{<sys/lock.h>}) determines which segments are locked.
Fred Drake39063631999-02-26 14:05:02 +0000958Availability: \UNIX{}.
Fred Drake215fe2f1999-02-02 19:02:35 +0000959\end{funcdesc}
960
Fred Drake046f4d82001-06-11 15:21:48 +0000961\begin{funcdescni}{popen}{\unspecified}
962\funclineni{popen2}{\unspecified}
963\funclineni{popen3}{\unspecified}
964\funclineni{popen4}{\unspecified}
965Run child processes, returning opened pipes for communications. These
966functions are described in section \ref{os-newstreams}.
967\end{funcdescni}
968
Fred Drake739282d2001-08-16 21:21:28 +0000969\begin{funcdesc}{spawnl}{mode, path, \moreargs}
970\funcline{spawnle}{mode, path, \moreargs, env}
971\funcline{spawnlp}{mode, path, \moreargs}
972\funcline{spawnlpe}{mode, path, \moreargs, env}
973\funcline{spawnv}{mode, path, args}
974\funcline{spawnve}{mode, path, args, env}
975\funcline{spawnvp}{mode, path, args}
976\funcline{spawnvpe}{mode, path, args, env}
977Execute the program \var{path} in a new process. If \var{mode} is
978\constant{P_NOWAIT}, this function returns the process ID of the new
979process; it \var{mode} is \constant{P_WAIT}, returns the process's
980exit code if it exits normally, or \code{-\var{signal}}, where
981\var{signal} is the signal that killed the process.
Fred Drake215fe2f1999-02-02 19:02:35 +0000982
Fred Drake739282d2001-08-16 21:21:28 +0000983For \function{spawnle()}, \function{spawnlpe()}, \function{spawnve()},
984and \function{spawnvpe()} (note that these all end in \character{e}),
985the \var{env} parameter must be a mapping which is used to define the
986environment variables for the new process; the \function{spawnl()},
987\function{spawnlp()}, \function{spawnv()}, and \function{spawnvp()}
988all cause the new process to inherit the environment of the current
989process.
990
991The variants which include a second \character{p} near the end
992(\function{spawnlp()}, \function{spawnlpe()}, \function{spawnvp()},
993and \function{spawnvpe()}) will use the \envvar{PATH} environment
994variable to locate the program \var{path}. The other variants,
995\function{spawnl()}, \function{spawnle()}, \function{spawnv()}, and
996\function{spawnve()}, will not use the \envvar{PATH} variable to
997locate the executable.
998
999The \character{l} and \character{v} variants of the
1000\function{spawn*()} functions differ in how command-line arguments are
1001passed. The \character{l} variants are perhaps the easiest to work
1002with if the number of parameters is fixed when the code is written;
1003the individual parameters simply become additional parameters to the
1004\function{spawnl*()} functions. The \character{v} variants are good
1005when the number of parameters is variable, with the arguments being
1006passed in a list or tuple as the \var{args} parameter. In either
1007case, the arguments to the child process must start with the name of
1008the command being run.
1009
1010As an example, the following calls to \function{spawnlp()} and
1011\function{spawnvpe()} are equivalent:
1012
1013\begin{verbatim}
1014import os
1015os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1016
1017L = ['cp', 'index.html', '/dev/null']
1018os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1019\end{verbatim}
1020
Fred Drake15861b22000-02-29 05:19:38 +00001021Availability: \UNIX{}, Windows.
Fred Drake0b9bc202001-06-11 18:25:34 +00001022\versionadded{1.6}
Fred Drake215fe2f1999-02-02 19:02:35 +00001023\end{funcdesc}
1024
Fred Drake938a8d72001-10-09 18:07:04 +00001025\begin{datadesc}{P_NOWAIT}
Fred Drake9329e5e1999-02-16 19:40:19 +00001026\dataline{P_NOWAITO}
Fred Drake938a8d72001-10-09 18:07:04 +00001027Possible values for the \var{mode} parameter to the \function{spawn*()}
1028family of functions. If either of these values is given, the
1029\function{spawn*()} functions will return as soon as the new process
1030has been created, with the process ID as the return value.
Fred Drake15861b22000-02-29 05:19:38 +00001031Availability: \UNIX{}, Windows.
Fred Drake0b9bc202001-06-11 18:25:34 +00001032\versionadded{1.6}
Fred Drake15861b22000-02-29 05:19:38 +00001033\end{datadesc}
1034
Fred Drake938a8d72001-10-09 18:07:04 +00001035\begin{datadesc}{P_WAIT}
1036Possible value for the \var{mode} parameter to the \function{spawn*()}
1037family of functions. If this is given as \var{mode}, the
1038\function{spawn*()} functions will not return until the new process
1039has run to completion and will return the exit code of the process the
1040run is successful, or \code{-\var{signal}} if a signal kills the
1041process.
1042Availability: \UNIX{}, Windows.
1043\versionadded{1.6}
1044\end{datadesc}
1045
1046\begin{datadesc}{P_DETACH}
1047\dataline{P_OVERLAY}
1048Possible values for the \var{mode} parameter to the
1049\function{spawn*()} family of functions. These are less portable than
1050those listed above.
1051\constant{P_DETACH} is similar to \constant{P_NOWAIT}, but the new
1052process is detached from the console of the calling process.
1053If \constant{P_OVERLAY} is used, the current process will be replaced;
1054the \function{spawn*()} function will not return.
Fred Drake215fe2f1999-02-02 19:02:35 +00001055Availability: Windows.
Fred Drake0b9bc202001-06-11 18:25:34 +00001056\versionadded{1.6}
Fred Drake215fe2f1999-02-02 19:02:35 +00001057\end{datadesc}
1058
Fred Drake4ce4f2e2000-09-29 04:15:19 +00001059\begin{funcdesc}{startfile}{path}
1060Start a file with its associated application. This acts like
1061double-clicking the file in Windows Explorer, or giving the file name
Fred Drake8ee679f2001-07-14 02:50:55 +00001062as an argument to the \program{start} command from the interactive
1063command shell: the file is opened with whatever application (if any)
1064its extension is associated.
Fred Drake4ce4f2e2000-09-29 04:15:19 +00001065
1066\function{startfile()} returns as soon as the associated application
1067is launched. There is no option to wait for the application to close,
1068and no way to retrieve the application's exit status. The \var{path}
1069parameter is relative to the current directory. If you want to use an
1070absolute path, make sure the first character is not a slash
1071(\character{/}); the underlying Win32 \cfunction{ShellExecute()}
Fred Drake8a2adcf2001-07-23 19:20:56 +00001072function doesn't work if it is. Use the \function{os.path.normpath()}
Fred Drake4ce4f2e2000-09-29 04:15:19 +00001073function to ensure that the path is properly encoded for Win32.
1074Availability: Windows.
1075\versionadded{2.0}
1076\end{funcdesc}
1077
Fred Drake215fe2f1999-02-02 19:02:35 +00001078\begin{funcdesc}{system}{command}
1079Execute the command (a string) in a subshell. This is implemented by
1080calling the Standard C function \cfunction{system()}, and has the
Fred Drakeec6baaf1999-04-21 18:13:31 +00001081same limitations. Changes to \code{posix.environ}, \code{sys.stdin},
Fred Drake215fe2f1999-02-02 19:02:35 +00001082etc.\ are not reflected in the environment of the executed command.
1083The return value is the exit status of the process encoded in the
Fred Drake7a621281999-06-10 15:07:05 +00001084format specified for \function{wait()}, except on Windows 95 and 98,
Fred Drakea88ef001999-06-18 19:11:25 +00001085where it is always \code{0}. Note that \POSIX{} does not specify the
1086meaning of the return value of the C \cfunction{system()} function,
1087so the return value of the Python function is system-dependent.
Fred Drake215fe2f1999-02-02 19:02:35 +00001088Availability: \UNIX{}, Windows.
1089\end{funcdesc}
1090
1091\begin{funcdesc}{times}{}
Fred Drake8ee679f2001-07-14 02:50:55 +00001092Return a 5-tuple of floating point numbers indicating accumulated
1093(processor or other)
Fred Drake215fe2f1999-02-02 19:02:35 +00001094times, in seconds. The items are: user time, system time, children's
1095user time, children's system time, and elapsed real time since a fixed
Fred Drakeec6baaf1999-04-21 18:13:31 +00001096point in the past, in that order. See the \UNIX{} manual page
1097\manpage{times}{2} or the corresponding Windows Platform API
1098documentation.
Fred Drake215fe2f1999-02-02 19:02:35 +00001099Availability: \UNIX{}, Windows.
1100\end{funcdesc}
1101
1102\begin{funcdesc}{wait}{}
1103Wait for completion of a child process, and return a tuple containing
1104its pid and exit status indication: a 16-bit number, whose low byte is
1105the signal number that killed the process, and whose high byte is the
1106exit status (if the signal number is zero); the high bit of the low
1107byte is set if a core file was produced.
1108Availability: \UNIX{}.
1109\end{funcdesc}
1110
1111\begin{funcdesc}{waitpid}{pid, options}
Fred Drake31e5e371999-08-13 13:36:33 +00001112Wait for completion of a child process given by process id \var{pid},
1113and return a tuple containing its process id and exit status
1114indication (encoded as for \function{wait()}). The semantics of the
1115call are affected by the value of the integer \var{options}, which
1116should be \code{0} for normal operation.
Fred Drake215fe2f1999-02-02 19:02:35 +00001117Availability: \UNIX{}.
Fred Drake31e5e371999-08-13 13:36:33 +00001118
1119If \var{pid} is greater than \code{0}, \function{waitpid()} requests
1120status information for that specific process. If \var{pid} is
1121\code{0}, the request is for the status of any child in the process
1122group of the current process. If \var{pid} is \code{-1}, the request
1123pertains to any child of the current process. If \var{pid} is less
1124than \code{-1}, status is requested for any process in the process
1125group \code{-\var{pid}} (the absolute value of \var{pid}).
Fred Drake215fe2f1999-02-02 19:02:35 +00001126\end{funcdesc}
1127
1128\begin{datadesc}{WNOHANG}
1129The option for \function{waitpid()} to avoid hanging if no child
1130process status is available immediately.
1131Availability: \UNIX{}.
1132\end{datadesc}
1133
Fred Drake38e5d272000-04-03 20:13:55 +00001134The following functions take a process status code as returned by
1135\function{system()}, \function{wait()}, or \function{waitpid()} as a
1136parameter. They may be used to determine the disposition of a
1137process.
Fred Drake215fe2f1999-02-02 19:02:35 +00001138
1139\begin{funcdesc}{WIFSTOPPED}{status}
1140Return true if the process has been stopped.
1141Availability: \UNIX{}.
1142\end{funcdesc}
1143
1144\begin{funcdesc}{WIFSIGNALED}{status}
1145Return true if the process exited due to a signal.
1146Availability: \UNIX{}.
1147\end{funcdesc}
1148
1149\begin{funcdesc}{WIFEXITED}{status}
1150Return true if the process exited using the \manpage{exit}{2} system
1151call.
1152Availability: \UNIX{}.
1153\end{funcdesc}
1154
1155\begin{funcdesc}{WEXITSTATUS}{status}
1156If \code{WIFEXITED(\var{status})} is true, return the integer
1157parameter to the \manpage{exit}{2} system call. Otherwise, the return
1158value is meaningless.
1159Availability: \UNIX{}.
1160\end{funcdesc}
1161
1162\begin{funcdesc}{WSTOPSIG}{status}
Fred Drake35c3ffd1999-03-04 14:08:10 +00001163Return the signal which caused the process to stop.
1164Availability: \UNIX{}.
1165\end{funcdesc}
1166
1167\begin{funcdesc}{WTERMSIG}{status}
Fred Drake215fe2f1999-02-02 19:02:35 +00001168Return the signal which caused the process to exit.
1169Availability: \UNIX{}.
1170\end{funcdesc}
1171
1172
Thomas Woutersf8316632000-07-16 19:01:10 +00001173\subsection{Miscellaneous System Information \label{os-path}}
Fred Drake88f6ca21999-12-15 19:39:04 +00001174
1175
1176\begin{funcdesc}{confstr}{name}
1177Return string-valued system configuration values.
1178\var{name} specifies the configuration value to retrieve; it may be a
1179string which is the name of a defined system value; these names are
Fred Drake8ee679f2001-07-14 02:50:55 +00001180specified in a number of standards (\POSIX, \UNIX 95, \UNIX 98, and
Fred Drake88f6ca21999-12-15 19:39:04 +00001181others). Some platforms define additional names as well. The names
1182known to the host operating system are given in the
1183\code{confstr_names} dictionary. For configuration variables not
1184included in that mapping, passing an integer for \var{name} is also
1185accepted.
1186Availability: \UNIX{}.
1187
1188If the configuration value specified by \var{name} isn't defined, the
1189empty string is returned.
1190
1191If \var{name} is a string and is not known, \exception{ValueError} is
1192raised. If a specific value for \var{name} is not supported by the
1193host system, even if it is included in \code{confstr_names}, an
1194\exception{OSError} is raised with \constant{errno.EINVAL} for the
1195error number.
1196\end{funcdesc}
1197
1198\begin{datadesc}{confstr_names}
1199Dictionary mapping names accepted by \function{confstr()} to the
1200integer values defined for those names by the host operating system.
1201This can be used to determine the set of names known to the system.
1202Availability: \UNIX.
1203\end{datadesc}
1204
1205\begin{funcdesc}{sysconf}{name}
1206Return integer-valued system configuration values.
1207If the configuration value specified by \var{name} isn't defined,
1208\code{-1} is returned. The comments regarding the \var{name}
1209parameter for \function{confstr()} apply here as well; the dictionary
1210that provides information on the known names is given by
1211\code{sysconf_names}.
1212Availability: \UNIX{}.
1213\end{funcdesc}
1214
1215\begin{datadesc}{sysconf_names}
1216Dictionary mapping names accepted by \function{sysconf()} to the
1217integer values defined for those names by the host operating system.
1218This can be used to determine the set of names known to the system.
1219Availability: \UNIX.
1220\end{datadesc}
1221
Fred Drake215fe2f1999-02-02 19:02:35 +00001222
1223The follow data values are used to support path manipulation
1224operations. These are defined for all platforms.
1225
1226Higher-level operations on pathnames are defined in the
1227\refmodule{os.path} module.
1228
1229
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001230\begin{datadesc}{curdir}
Fred Drake8ee679f2001-07-14 02:50:55 +00001231The constant string used by the operating system to refer to the current
1232directory.
Fred Drake907e76b2001-07-06 20:30:11 +00001233For example: \code{'.'} for \POSIX{} or \code{':'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001234\end{datadesc}
1235
1236\begin{datadesc}{pardir}
Fred Drake8ee679f2001-07-14 02:50:55 +00001237The constant string used by the operating system to refer to the parent
1238directory.
Fred Drake907e76b2001-07-06 20:30:11 +00001239For example: \code{'..'} for \POSIX{} or \code{'::'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001240\end{datadesc}
1241
1242\begin{datadesc}{sep}
Fred Drake8ee679f2001-07-14 02:50:55 +00001243The character used by the operating system to separate pathname components,
Fred Drake907e76b2001-07-06 20:30:11 +00001244for example, \character{/} for \POSIX{} or \character{:} for the
1245Macintosh. Note that knowing this is not sufficient to be able to
1246parse or concatenate pathnames --- use \function{os.path.split()} and
Fred Drake1a3c2a01998-08-06 15:18:23 +00001247\function{os.path.join()} --- but it is occasionally useful.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001248\end{datadesc}
1249
Guido van Rossumb2afc811997-08-29 22:37:44 +00001250\begin{datadesc}{altsep}
Fred Drake8ee679f2001-07-14 02:50:55 +00001251An alternative character used by the operating system to separate pathname
1252components, or \code{None} if only one separator character exists. This is
1253set to \character{/} on DOS and Windows systems where \code{sep} is a
1254backslash.
Guido van Rossumb2afc811997-08-29 22:37:44 +00001255\end{datadesc}
1256
Guido van Rossum470be141995-03-17 16:07:09 +00001257\begin{datadesc}{pathsep}
Fred Drake8ee679f2001-07-14 02:50:55 +00001258The character conventionally used by the operating system to separate
1259search patch components (as in \envvar{PATH}), such as \character{:} for
1260\POSIX{} or \character{;} for DOS and Windows.
Guido van Rossum9c59ce91998-06-30 15:54:27 +00001261\end{datadesc}
1262
Guido van Rossum470be141995-03-17 16:07:09 +00001263\begin{datadesc}{defpath}
Fred Drake1a3c2a01998-08-06 15:18:23 +00001264The default search path used by \function{exec*p*()} if the environment
Guido van Rossum470be141995-03-17 16:07:09 +00001265doesn't have a \code{'PATH'} key.
1266\end{datadesc}
1267
Fred Drake215fe2f1999-02-02 19:02:35 +00001268\begin{datadesc}{linesep}
1269The string used to separate (or, rather, terminate) lines on the
Fred Drake907e76b2001-07-06 20:30:11 +00001270current platform. This may be a single character, such as \code{'\e
Fred Drake8ee679f2001-07-14 02:50:55 +00001271n'} for \POSIX{} or \code{'\e r'} for the Mac OS, or multiple characters,
1272for example, \code{'\e r\e n'} for DOS and Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +00001273\end{datadesc}