blob: 64da3307ceb944a6f09feb59b8a3c65a8fcbef9e [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}
Martin v. Löwisc4051332001-10-18 14:07:12 +0000208Set the list of supplemental group ids associated with the current
209process to \var{groups}. \var{groups} must be a sequence, and each
210element must be an integer identifying a group. This operation is
211typical available only to the superuser.
Martin v. Löwis61c5edf2001-10-18 04:06:00 +0000212Availability: \UNIX{}.
213\versionadded{2.2}
214\end{funcdesc}
215
Fred Drake215fe2f1999-02-02 19:02:35 +0000216\begin{funcdesc}{setpgrp}{}
217Calls the system call \cfunction{setpgrp()} or \cfunction{setpgrp(0,
2180)} depending on which version is implemented (if any). See the
219\UNIX{} manual for the semantics.
220Availability: \UNIX{}.
221\end{funcdesc}
222
223\begin{funcdesc}{setpgid}{pid, pgrp}
224Calls the system call \cfunction{setpgid()}. See the \UNIX{} manual
225for the semantics.
226Availability: \UNIX{}.
227\end{funcdesc}
228
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +0000229\begin{funcdesc}{setreuid}{ruid, euid}
230Set the current process's real and effective user ids.
231Availability: \UNIX{}.
232\end{funcdesc}
233
234\begin{funcdesc}{setregid}{rgid, egid}
235Set the current process's real and effective group ids.
236Availability: \UNIX{}.
237\end{funcdesc}
238
Fred Drake215fe2f1999-02-02 19:02:35 +0000239\begin{funcdesc}{setsid}{}
240Calls the system call \cfunction{setsid()}. See the \UNIX{} manual
241for the semantics.
242Availability: \UNIX{}.
243\end{funcdesc}
244
245\begin{funcdesc}{setuid}{uid}
Fred Drake6b330ba81999-05-25 13:42:26 +0000246\index{user!id, setting}
Fred Drake215fe2f1999-02-02 19:02:35 +0000247Set the current process' user id.
248Availability: \UNIX{}.
249\end{funcdesc}
250
251% placed in this section since it relates to errno.... a little weak ;-(
252\begin{funcdesc}{strerror}{code}
253Return the error message corresponding to the error code in
254\var{code}.
255Availability: \UNIX{}, Windows.
256\end{funcdesc}
257
258\begin{funcdesc}{umask}{mask}
259Set the current numeric umask and returns the previous umask.
260Availability: \UNIX{}, Windows.
261\end{funcdesc}
262
263\begin{funcdesc}{uname}{}
264Return a 5-tuple containing information identifying the current
265operating system. The tuple contains 5 strings:
266\code{(\var{sysname}, \var{nodename}, \var{release}, \var{version},
267\var{machine})}. Some systems truncate the nodename to 8
268characters or to the leading component; a better way to get the
269hostname is \function{socket.gethostname()}
270\withsubitem{(in module socket)}{\ttindex{gethostname()}}
271or even
272\withsubitem{(in module socket)}{\ttindex{gethostbyaddr()}}
273\code{socket.gethostbyaddr(socket.gethostname())}.
274Availability: recent flavors of \UNIX{}.
275\end{funcdesc}
276
277
278
279\subsection{File Object Creation \label{os-newstreams}}
280
281These functions create new file objects.
282
283
284\begin{funcdesc}{fdopen}{fd\optional{, mode\optional{, bufsize}}}
285Return an open file object connected to the file descriptor \var{fd}.
Fred Drake8c9fc001999-08-05 13:41:31 +0000286\index{I/O control!buffering}
Fred Drake215fe2f1999-02-02 19:02:35 +0000287The \var{mode} and \var{bufsize} arguments have the same meaning as
288the corresponding arguments to the built-in \function{open()}
289function.
290Availability: Macintosh, \UNIX{}, Windows.
291\end{funcdesc}
292
293\begin{funcdesc}{popen}{command\optional{, mode\optional{, bufsize}}}
294Open a pipe to or from \var{command}. The return value is an open
295file object connected to the pipe, which can be read or written
296depending on whether \var{mode} is \code{'r'} (default) or \code{'w'}.
297The \var{bufsize} argument has the same meaning as the corresponding
298argument to the built-in \function{open()} function. The exit status of
299the command (encoded in the format specified for \function{wait()}) is
300available as the return value of the \method{close()} method of the file
301object, except that when the exit status is zero (termination without
Fred Drake1319e3e2000-10-03 17:14:27 +0000302errors), \code{None} is returned.
Fred Drake215fe2f1999-02-02 19:02:35 +0000303Availability: \UNIX{}, Windows.
Fred Drakec71c23e2000-10-04 13:57:27 +0000304
305\versionchanged[This function worked unreliably under Windows in
306 earlier versions of Python. This was due to the use of the
307 \cfunction{_popen()} function from the libraries provided with
308 Windows. Newer versions of Python do not use the broken
309 implementation from the Windows libraries]{2.0}
Fred Drake215fe2f1999-02-02 19:02:35 +0000310\end{funcdesc}
311
Fred Drake18f7a451999-12-09 22:11:43 +0000312\begin{funcdesc}{tmpfile}{}
313Return a new file object opened in update mode (\samp{w+}). The file
314has no directory entries associated with it and will be automatically
315deleted once there are no file descriptors for the file.
Fred Drakeefaef132001-07-17 20:39:18 +0000316Availability: \UNIX{}, Windows.
Fred Drake18f7a451999-12-09 22:11:43 +0000317\end{funcdesc}
Fred Drake215fe2f1999-02-02 19:02:35 +0000318
319
Fred Drake8a9db992000-09-28 20:27:51 +0000320For each of these \function{popen()} variants, if \var{bufsize} is
321specified, it specifies the buffer size for the I/O pipes.
322\var{mode}, if provided, should be the string \code{'b'} or
323\code{'t'}; on Windows this is needed to determine whether the file
324objects should be opened in binary or text mode. The default value
325for \var{mode} is \code{'t'}.
326
Fred Drake098d7fa2001-09-11 19:56:51 +0000327These methods do not make it possible to retrieve the return code from
328the child processes. The only way to control the input and output
329streams and also retrieve the return codes is to use the
330\class{Popen3} and \class{Popen4} classes from the \refmodule{popen2}
331module; these are only available on \UNIX.
332
Fred Drake046f4d82001-06-11 15:21:48 +0000333\begin{funcdesc}{popen2}{cmd\optional{, mode\optional{, bufsize}}}
Fred Drake8a9db992000-09-28 20:27:51 +0000334Executes \var{cmd} as a sub-process. Returns the file objects
335\code{(\var{child_stdin}, \var{child_stdout})}.
Fred Drake0b9bc202001-06-11 18:25:34 +0000336Availability: \UNIX{}, Windows.
Fred Drake8a9db992000-09-28 20:27:51 +0000337\versionadded{2.0}
338\end{funcdesc}
339
Fred Drake046f4d82001-06-11 15:21:48 +0000340\begin{funcdesc}{popen3}{cmd\optional{, mode\optional{, bufsize}}}
Fred Drake8a9db992000-09-28 20:27:51 +0000341Executes \var{cmd} as a sub-process. Returns the file objects
342\code{(\var{child_stdin}, \var{child_stdout}, \var{child_stderr})}.
Fred Drake0b9bc202001-06-11 18:25:34 +0000343Availability: \UNIX{}, Windows.
Fred Drake8a9db992000-09-28 20:27:51 +0000344\versionadded{2.0}
345\end{funcdesc}
346
Fred Drake046f4d82001-06-11 15:21:48 +0000347\begin{funcdesc}{popen4}{cmd\optional{, mode\optional{, bufsize}}}
Fred Drake8a9db992000-09-28 20:27:51 +0000348Executes \var{cmd} as a sub-process. Returns the file objects
349\code{(\var{child_stdin}, \var{child_stdout_and_stderr})}.
Fred Drake0b9bc202001-06-11 18:25:34 +0000350Availability: \UNIX{}, Windows.
Fred Drake8a9db992000-09-28 20:27:51 +0000351\versionadded{2.0}
352\end{funcdesc}
353
354This functionality is also available in the \refmodule{popen2} module
355using functions of the same names, but the return values of those
356functions have a different order.
357
358
Fred Drake215fe2f1999-02-02 19:02:35 +0000359\subsection{File Descriptor Operations \label{os-fd-ops}}
360
361These functions operate on I/O streams referred to
362using file descriptors.
363
364
365\begin{funcdesc}{close}{fd}
366Close file descriptor \var{fd}.
367Availability: Macintosh, \UNIX{}, Windows.
368
369Note: this function is intended for low-level I/O and must be applied
370to a file descriptor as returned by \function{open()} or
371\function{pipe()}. To close a ``file object'' returned by the
372built-in function \function{open()} or by \function{popen()} or
373\function{fdopen()}, use its \method{close()} method.
374\end{funcdesc}
375
376\begin{funcdesc}{dup}{fd}
377Return a duplicate of file descriptor \var{fd}.
378Availability: Macintosh, \UNIX{}, Windows.
379\end{funcdesc}
380
381\begin{funcdesc}{dup2}{fd, fd2}
382Duplicate file descriptor \var{fd} to \var{fd2}, closing the latter
383first if necessary.
384Availability: \UNIX{}, Windows.
385\end{funcdesc}
386
Fred Drake88f6ca21999-12-15 19:39:04 +0000387\begin{funcdesc}{fpathconf}{fd, name}
Thomas Woutersf8316632000-07-16 19:01:10 +0000388Return system configuration information relevant to an open file.
Fred Drake88f6ca21999-12-15 19:39:04 +0000389\var{name} specifies the configuration value to retrieve; it may be a
390string which is the name of a defined system value; these names are
391specified in a number of standards (\POSIX.1, Unix95, Unix98, and
392others). Some platforms define additional names as well. The names
393known to the host operating system are given in the
394\code{pathconf_names} dictionary. For configuration variables not
395included in that mapping, passing an integer for \var{name} is also
396accepted.
397Availability: \UNIX{}.
398
399If \var{name} is a string and is not known, \exception{ValueError} is
400raised. If a specific value for \var{name} is not supported by the
401host system, even if it is included in \code{pathconf_names}, an
402\exception{OSError} is raised with \constant{errno.EINVAL} for the
403error number.
404\end{funcdesc}
405
Fred Drake215fe2f1999-02-02 19:02:35 +0000406\begin{funcdesc}{fstat}{fd}
407Return status for file descriptor \var{fd}, like \function{stat()}.
408Availability: \UNIX{}, Windows.
409\end{funcdesc}
410
411\begin{funcdesc}{fstatvfs}{fd}
412Return information about the filesystem containing the file associated
413with file descriptor \var{fd}, like \function{statvfs()}.
414Availability: \UNIX{}.
415\end{funcdesc}
416
417\begin{funcdesc}{ftruncate}{fd, length}
418Truncate the file corresponding to file descriptor \var{fd},
419so that it is at most \var{length} bytes in size.
420Availability: \UNIX{}.
421\end{funcdesc}
422
Skip Montanarod3725212000-07-19 17:30:58 +0000423\begin{funcdesc}{isatty}{fd}
424Return \code{1} if the file descriptor \var{fd} is open and connected to a
425tty(-like) device, else \code{0}.
426Availability: \UNIX{}
427\end{funcdesc}
428
Fred Drake215fe2f1999-02-02 19:02:35 +0000429\begin{funcdesc}{lseek}{fd, pos, how}
430Set the current position of file descriptor \var{fd} to position
431\var{pos}, modified by \var{how}: \code{0} to set the position
432relative to the beginning of the file; \code{1} to set it relative to
433the current position; \code{2} to set it relative to the end of the
434file.
435Availability: Macintosh, \UNIX{}, Windows.
436\end{funcdesc}
437
438\begin{funcdesc}{open}{file, flags\optional{, mode}}
439Open the file \var{file} and set various flags according to
440\var{flags} and possibly its mode according to \var{mode}.
441The default \var{mode} is \code{0777} (octal), and the current umask
442value is first masked out. Return the file descriptor for the newly
443opened file.
444Availability: Macintosh, \UNIX{}, Windows.
445
446For a description of the flag and mode values, see the C run-time
447documentation; flag constants (like \constant{O_RDONLY} and
448\constant{O_WRONLY}) are defined in this module too (see below).
449
450Note: this function is intended for low-level I/O. For normal usage,
451use the built-in function \function{open()}, which returns a ``file
452object'' with \method{read()} and \method{write()} methods (and many
453more).
454\end{funcdesc}
455
Fred Drakec82634c2000-06-28 17:27:48 +0000456\begin{funcdesc}{openpty}{}
457Open a new pseudo-terminal pair. Return a pair of file descriptors
458\code{(\var{master}, \var{slave})} for the pty and the tty,
459respectively. For a (slightly) more portable approach, use the
460\refmodule{pty}\refstmodindex{pty} module.
461Availability: Some flavors of \UNIX{}
462\end{funcdesc}
463
Fred Drake215fe2f1999-02-02 19:02:35 +0000464\begin{funcdesc}{pipe}{}
465Create a pipe. Return a pair of file descriptors \code{(\var{r},
466\var{w})} usable for reading and writing, respectively.
467Availability: \UNIX{}, Windows.
468\end{funcdesc}
469
470\begin{funcdesc}{read}{fd, n}
471Read at most \var{n} bytes from file descriptor \var{fd}.
472Return a string containing the bytes read.
473Availability: Macintosh, \UNIX{}, Windows.
474
475Note: this function is intended for low-level I/O and must be applied
476to a file descriptor as returned by \function{open()} or
477\function{pipe()}. To read a ``file object'' returned by the
478built-in function \function{open()} or by \function{popen()} or
479\function{fdopen()}, or \code{sys.stdin}, use its
480\method{read()} or \method{readline()} methods.
481\end{funcdesc}
482
483\begin{funcdesc}{tcgetpgrp}{fd}
484Return the process group associated with the terminal given by
485\var{fd} (an open file descriptor as returned by \function{open()}).
486Availability: \UNIX{}.
487\end{funcdesc}
488
489\begin{funcdesc}{tcsetpgrp}{fd, pg}
490Set the process group associated with the terminal given by
491\var{fd} (an open file descriptor as returned by \function{open()})
492to \var{pg}.
493Availability: \UNIX{}.
494\end{funcdesc}
495
496\begin{funcdesc}{ttyname}{fd}
497Return a string which specifies the terminal device associated with
498file-descriptor \var{fd}. If \var{fd} is not associated with a terminal
499device, an exception is raised.
500Availability: \UNIX{}.
501\end{funcdesc}
502
503\begin{funcdesc}{write}{fd, str}
504Write the string \var{str} to file descriptor \var{fd}.
505Return the number of bytes actually written.
506Availability: Macintosh, \UNIX{}, Windows.
507
508Note: this function is intended for low-level I/O and must be applied
509to a file descriptor as returned by \function{open()} or
510\function{pipe()}. To write a ``file object'' returned by the
511built-in function \function{open()} or by \function{popen()} or
512\function{fdopen()}, or \code{sys.stdout} or \code{sys.stderr}, use
513its \method{write()} method.
514\end{funcdesc}
515
516
517The following data items are available for use in constructing the
518\var{flags} parameter to the \function{open()} function.
519
520\begin{datadesc}{O_RDONLY}
521\dataline{O_WRONLY}
522\dataline{O_RDWR}
523\dataline{O_NDELAY}
524\dataline{O_NONBLOCK}
525\dataline{O_APPEND}
526\dataline{O_DSYNC}
527\dataline{O_RSYNC}
528\dataline{O_SYNC}
529\dataline{O_NOCTTY}
530\dataline{O_CREAT}
531\dataline{O_EXCL}
532\dataline{O_TRUNC}
533Options for the \var{flag} argument to the \function{open()} function.
534These can be bit-wise OR'd together.
535Availability: Macintosh, \UNIX{}, Windows.
536\end{datadesc}
537
Fred Drake3ac977e2000-08-11 20:19:51 +0000538\begin{datadesc}{O_BINARY}
539Option for the \var{flag} argument to the \function{open()} function.
540This can be bit-wise OR'd together with those listed above.
541Availability: Macintosh, Windows.
542% XXX need to check on the availability of this one.
543\end{datadesc}
544
Fred Drake215fe2f1999-02-02 19:02:35 +0000545
546\subsection{Files and Directories \label{os-file-dir}}
547
548\begin{funcdesc}{access}{path, mode}
Fred Drake38e5d272000-04-03 20:13:55 +0000549Check read/write/execute permissions for this process or existence of
550file \var{path}. \var{mode} should be \constant{F_OK} to test the
551existence of \var{path}, or it can be the inclusive OR of one or more
552of \constant{R_OK}, \constant{W_OK}, and \constant{X_OK} to test
553permissions. Return \code{1} if access is allowed, \code{0} if not.
554See the \UNIX{} man page \manpage{access}{2} for more information.
Fred Drake3ac977e2000-08-11 20:19:51 +0000555Availability: \UNIX{}, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000556\end{funcdesc}
557
Fred Drake38e5d272000-04-03 20:13:55 +0000558\begin{datadesc}{F_OK}
559 Value to pass as the \var{mode} parameter of \function{access()} to
560 test the existence of \var{path}.
561\end{datadesc}
562
563\begin{datadesc}{R_OK}
564 Value to include in the \var{mode} parameter of \function{access()}
565 to test the readability of \var{path}.
566\end{datadesc}
567
568\begin{datadesc}{W_OK}
569 Value to include in the \var{mode} parameter of \function{access()}
570 to test the writability of \var{path}.
571\end{datadesc}
572
573\begin{datadesc}{X_OK}
574 Value to include in the \var{mode} parameter of \function{access()}
575 to determine if \var{path} can be executed.
576\end{datadesc}
577
Fred Drake6db897c1999-07-12 16:49:30 +0000578\begin{funcdesc}{chdir}{path}
579\index{directory!changing}
580Change the current working directory to \var{path}.
581Availability: Macintosh, \UNIX{}, Windows.
582\end{funcdesc}
583
584\begin{funcdesc}{getcwd}{}
585Return a string representing the current working directory.
586Availability: Macintosh, \UNIX{}, Windows.
587\end{funcdesc}
588
Martin v. Löwis244edc82001-10-04 22:44:26 +0000589\begin{funcdesc}{chroot}{path}
590Change the root directory of the current process to \var{path}.
591Availability: \UNIX{}.
592\versionadded{2.2}
593\end{funcdesc}
594
Fred Drake215fe2f1999-02-02 19:02:35 +0000595\begin{funcdesc}{chmod}{path, mode}
596Change the mode of \var{path} to the numeric \var{mode}.
597Availability: \UNIX{}, Windows.
598\end{funcdesc}
599
600\begin{funcdesc}{chown}{path, uid, gid}
601Change the owner and group id of \var{path} to the numeric \var{uid}
602and \var{gid}.
603Availability: \UNIX{}.
604\end{funcdesc}
605
606\begin{funcdesc}{link}{src, dst}
607Create a hard link pointing to \var{src} named \var{dst}.
608Availability: \UNIX{}.
609\end{funcdesc}
610
611\begin{funcdesc}{listdir}{path}
612Return a list containing the names of the entries in the directory.
613The list is in arbitrary order. It does not include the special
614entries \code{'.'} and \code{'..'} even if they are present in the
615directory.
616Availability: Macintosh, \UNIX{}, Windows.
617\end{funcdesc}
618
619\begin{funcdesc}{lstat}{path}
620Like \function{stat()}, but do not follow symbolic links.
621Availability: \UNIX{}.
622\end{funcdesc}
623
624\begin{funcdesc}{mkfifo}{path\optional{, mode}}
625Create a FIFO (a named pipe) named \var{path} with numeric mode
626\var{mode}. The default \var{mode} is \code{0666} (octal). The current
627umask value is first masked out from the mode.
628Availability: \UNIX{}.
629
630FIFOs are pipes that can be accessed like regular files. FIFOs exist
631until they are deleted (for example with \function{os.unlink()}).
632Generally, FIFOs are used as rendezvous between ``client'' and
633``server'' type processes: the server opens the FIFO for reading, and
634the client opens it for writing. Note that \function{mkfifo()}
635doesn't open the FIFO --- it just creates the rendezvous point.
636\end{funcdesc}
637
638\begin{funcdesc}{mkdir}{path\optional{, mode}}
639Create a directory named \var{path} with numeric mode \var{mode}.
640The default \var{mode} is \code{0777} (octal). On some systems,
641\var{mode} is ignored. Where it is used, the current umask value is
642first masked out.
643Availability: Macintosh, \UNIX{}, Windows.
644\end{funcdesc}
645
646\begin{funcdesc}{makedirs}{path\optional{, mode}}
647\index{directory!creating}
648Recursive directory creation function. Like \function{mkdir()},
649but makes all intermediate-level directories needed to contain the
650leaf directory. Throws an \exception{error} exception if the leaf
651directory already exists or cannot be created. The default \var{mode}
Fred Drakebbf7a402001-09-28 16:14:18 +0000652is \code{0777} (octal). This function does not properly handle UNC
653paths (only relevant on Windows systems).
Fred Drake215fe2f1999-02-02 19:02:35 +0000654\versionadded{1.5.2}
655\end{funcdesc}
656
Fred Drake88f6ca21999-12-15 19:39:04 +0000657\begin{funcdesc}{pathconf}{path, name}
Thomas Woutersf8316632000-07-16 19:01:10 +0000658Return system configuration information relevant to a named file.
Fred Drake88f6ca21999-12-15 19:39:04 +0000659\var{name} specifies the configuration value to retrieve; it may be a
660string which is the name of a defined system value; these names are
Fred Drake8ee679f2001-07-14 02:50:55 +0000661specified in a number of standards (\POSIX.1, \UNIX 95, \UNIX 98, and
Fred Drake88f6ca21999-12-15 19:39:04 +0000662others). Some platforms define additional names as well. The names
663known to the host operating system are given in the
664\code{pathconf_names} dictionary. For configuration variables not
665included in that mapping, passing an integer for \var{name} is also
666accepted.
667Availability: \UNIX{}.
668
669If \var{name} is a string and is not known, \exception{ValueError} is
670raised. If a specific value for \var{name} is not supported by the
671host system, even if it is included in \code{pathconf_names}, an
672\exception{OSError} is raised with \constant{errno.EINVAL} for the
673error number.
674\end{funcdesc}
675
676\begin{datadesc}{pathconf_names}
677Dictionary mapping names accepted by \function{pathconf()} and
678\function{fpathconf()} to the integer values defined for those names
679by the host operating system. This can be used to determine the set
680of names known to the system.
681Availability: \UNIX.
682\end{datadesc}
683
Fred Drake215fe2f1999-02-02 19:02:35 +0000684\begin{funcdesc}{readlink}{path}
685Return a string representing the path to which the symbolic link
Fred Drakedc9e7e42001-05-29 18:13:06 +0000686points. The result may be either an absolute or relative pathname; if
687it is relative, it may be converted to an absolute pathname using
688\code{os.path.join(os.path.dirname(\var{path}), \var{result})}.
Fred Drake215fe2f1999-02-02 19:02:35 +0000689Availability: \UNIX{}.
690\end{funcdesc}
691
692\begin{funcdesc}{remove}{path}
Fred Drakedc9e7e42001-05-29 18:13:06 +0000693Remove the file \var{path}. If \var{path} is a directory,
694\exception{OSError} is raised; see \function{rmdir()} below to remove
695a directory. This is identical to the \function{unlink()} function
696documented below. On Windows, attempting to remove a file that is in
697use causes an exception to be raised; on \UNIX, the directory entry is
698removed but the storage allocated to the file is not made available
699until the original file is no longer in use.
Fred Drake215fe2f1999-02-02 19:02:35 +0000700Availability: Macintosh, \UNIX{}, Windows.
701\end{funcdesc}
702
703\begin{funcdesc}{removedirs}{path}
704\index{directory!deleting}
705Recursive directory removal function. Works like
706\function{rmdir()} except that, if the leaf directory is
707successfully removed, directories corresponding to rightmost path
708segments will be pruned way until either the whole path is consumed or
709an error is raised (which is ignored, because it generally means that
710a parent directory is not empty). Throws an \exception{error}
711exception if the leaf directory could not be successfully removed.
712\versionadded{1.5.2}
713\end{funcdesc}
714
715\begin{funcdesc}{rename}{src, dst}
Fred Drakedc9e7e42001-05-29 18:13:06 +0000716Rename the file or directory \var{src} to \var{dst}. If \var{dst} is
717a directory, \exception{OSError} will be raised. On \UNIX, if
718\var{dst} exists and is a file, it will be removed silently if the
719user has permission. The operation may fail on some \UNIX{} flavors
Skip Montanarob9d973d2001-06-04 15:31:17 +0000720if \var{src} and \var{dst} are on different filesystems. If
Fred Drakedc9e7e42001-05-29 18:13:06 +0000721successful, the renaming will be an atomic operation (this is a
722\POSIX{} requirement). On Windows, if \var{dst} already exists,
723\exception{OSError} will be raised even if it is a file; there may be
724no way to implement an atomic rename when \var{dst} names an existing
725file.
Fred Drake215fe2f1999-02-02 19:02:35 +0000726Availability: Macintosh, \UNIX{}, Windows.
727\end{funcdesc}
728
729\begin{funcdesc}{renames}{old, new}
730Recursive directory or file renaming function.
731Works like \function{rename()}, except creation of any intermediate
732directories needed to make the new pathname good is attempted first.
733After the rename, directories corresponding to rightmost path segments
734of the old name will be pruned away using \function{removedirs()}.
735
736Note: this function can fail with the new directory structure made if
737you lack permissions needed to remove the leaf directory or file.
738\versionadded{1.5.2}
739\end{funcdesc}
740
741\begin{funcdesc}{rmdir}{path}
742Remove the directory \var{path}.
743Availability: Macintosh, \UNIX{}, Windows.
744\end{funcdesc}
745
746\begin{funcdesc}{stat}{path}
747Perform a \cfunction{stat()} system call on the given path. The
748return value is a tuple of at least 10 integers giving the most
749important (and portable) members of the \emph{stat} structure, in the
750order
751\code{st_mode},
752\code{st_ino},
753\code{st_dev},
754\code{st_nlink},
755\code{st_uid},
756\code{st_gid},
757\code{st_size},
758\code{st_atime},
759\code{st_mtime},
760\code{st_ctime}.
Fred Drake21c9df72000-10-14 05:46:11 +0000761More items may be added at the end by some implementations. Note that
Fred Drake8ee679f2001-07-14 02:50:55 +0000762on the Mac OS, the time values are floating point values, like all
763time values on the Mac OS.
764(On Windows, some items are filled with dummy values.)
Fred Drake215fe2f1999-02-02 19:02:35 +0000765Availability: Macintosh, \UNIX{}, Windows.
766
767Note: The standard module \refmodule{stat}\refstmodindex{stat} defines
768functions and constants that are useful for extracting information
769from a \ctype{stat} structure.
770\end{funcdesc}
771
772\begin{funcdesc}{statvfs}{path}
773Perform a \cfunction{statvfs()} system call on the given path. The
Guido van Rossum0c9608c1999-02-03 16:32:37 +0000774return value is a tuple of 10 integers giving the most common
Fred Drake215fe2f1999-02-02 19:02:35 +0000775members of the \ctype{statvfs} structure, in the order
776\code{f_bsize},
777\code{f_frsize},
778\code{f_blocks},
779\code{f_bfree},
780\code{f_bavail},
781\code{f_files},
782\code{f_ffree},
783\code{f_favail},
Fred Drake215fe2f1999-02-02 19:02:35 +0000784\code{f_flag},
785\code{f_namemax}.
786Availability: \UNIX{}.
787
788Note: The standard module \module{statvfs}\refstmodindex{statvfs}
789defines constants that are useful for extracting information
790from a \ctype{statvfs} structure.
791\end{funcdesc}
792
793\begin{funcdesc}{symlink}{src, dst}
794Create a symbolic link pointing to \var{src} named \var{dst}.
795Availability: \UNIX{}.
796\end{funcdesc}
797
Fred Drake18f7a451999-12-09 22:11:43 +0000798\begin{funcdesc}{tempnam}{\optional{dir\optional{, prefix}}}
799Return a unique path name that is reasonable for creating a temporary
800file. This will be an absolute path that names a potential directory
801entry in the directory \var{dir} or a common location for temporary
802files if \var{dir} is omitted or \code{None}. If given and not
803\code{None}, \var{prefix} is used to provide a short prefix to the
804filename. Applications are responsible for properly creating and
805managing files created using paths returned by \function{tempnam()};
806no automatic cleanup is provided.
Fred Drake938a8d72001-10-09 18:07:04 +0000807\warning{Use of \function{tempnam()} is vulnerable to symlink attacks;
808consider using \function{tmpfile()} instead.}
Fred Drakeefaef132001-07-17 20:39:18 +0000809Availability: \UNIX, Windows.
Fred Drake18f7a451999-12-09 22:11:43 +0000810\end{funcdesc}
811
812\begin{funcdesc}{tmpnam}{}
813Return a unique path name that is reasonable for creating a temporary
814file. This will be an absolute path that names a potential directory
815entry in a common location for temporary files. Applications are
816responsible for properly creating and managing files created using
817paths returned by \function{tmpnam()}; no automatic cleanup is
818provided.
Fred Drake938a8d72001-10-09 18:07:04 +0000819\warning{Use of \function{tmpnam()} is vulnerable to symlink attacks;
820consider using \function{tmpfile()} instead.}
Fred Drakeefaef132001-07-17 20:39:18 +0000821Availability: \UNIX, Windows.
Fred Drake18f7a451999-12-09 22:11:43 +0000822\end{funcdesc}
823
824\begin{datadesc}{TMP_MAX}
825The maximum number of unique names that \function{tmpnam()} will
826generate before reusing names.
827\end{datadesc}
828
Fred Drake215fe2f1999-02-02 19:02:35 +0000829\begin{funcdesc}{unlink}{path}
830Remove the file \var{path}. This is the same function as
831\function{remove()}; the \function{unlink()} name is its traditional
832\UNIX{} name.
833Availability: Macintosh, \UNIX{}, Windows.
834\end{funcdesc}
835
Barry Warsaw93a8eac2000-05-01 16:18:22 +0000836\begin{funcdesc}{utime}{path, times}
837Set the access and modified times of the file specified by \var{path}.
838If \var{times} is \code{None}, then the file's access and modified
839times are set to the current time. Otherwise, \var{times} must be a
Fred Drakee06d0252000-05-02 17:29:35 +00008402-tuple of numbers, of the form \code{(\var{atime}, \var{mtime})}
841which is used to set the access and modified times, respectively.
Fred Drake4a152632000-10-19 05:33:46 +0000842\versionchanged[Added support for \code{None} for \var{times}]{2.0}
Fred Drake215fe2f1999-02-02 19:02:35 +0000843Availability: Macintosh, \UNIX{}, Windows.
844\end{funcdesc}
845
846
847\subsection{Process Management \label{os-process}}
848
Fred Drake18f7a451999-12-09 22:11:43 +0000849These functions may be used to create and manage processes.
Fred Drake215fe2f1999-02-02 19:02:35 +0000850
Fred Drake7be31152000-09-23 05:22:07 +0000851The various \function{exec*()} functions take a list of arguments for
852the new program loaded into the process. In each case, the first of
853these arguments is passed to the new program as its own name rather
854than as an argument a user may have typed on a command line. For the
855C programmer, this is the \code{argv[0]} passed to a program's
856\cfunction{main()}. For example, \samp{os.execv('/bin/echo', ['foo',
857'bar'])} will only print \samp{bar} on standard output; \samp{foo}
858will seem to be ignored.
859
Fred Drake215fe2f1999-02-02 19:02:35 +0000860
Fred Drake18f7a451999-12-09 22:11:43 +0000861\begin{funcdesc}{abort}{}
862Generate a \constant{SIGABRT} signal to the current process. On
863\UNIX, the default behavior is to produce a core dump; on Windows, the
864process immediately returns an exit code of \code{3}. Be aware that
865programs which use \function{signal.signal()} to register a handler
866for \constant{SIGABRT} will behave differently.
867Availability: \UNIX, Windows.
868\end{funcdesc}
869
Fred Drakedb7287c2001-10-18 18:58:30 +0000870\begin{funcdesc}{execl}{path, arg0, arg1, \moreargs}
871\funcline{execle}{path, arg0, arg1, \moreargs, env}
872\funcline{execlp}{file, arg0, arg1, \moreargs}
873\funcline{execlpe}{file, arg0, arg1, \moreargs, env}
874\funcline{execv}{path, args}
875\funcline{execve}{path, args, env}
876\funcline{execvp}{file, args}
877\funcline{execvpe}{file, args, env}
878These functions all execute a new program, replacing the current
879process; they do not return. On \UNIX, the new executable is loaded
880into the current process, and will have the same process ID as the
881caller. Errors will be reported as \exception{OSError} exceptions.
Fred Drake215fe2f1999-02-02 19:02:35 +0000882
Fred Drakedb7287c2001-10-18 18:58:30 +0000883The \character{l} and \character{v} variants of the
884\function{exec*()} functions differ in how command-line arguments are
885passed. The \character{l} variants are perhaps the easiest to work
886with if the number of parameters is fixed when the code is written;
887the individual parameters simply become additional parameters to the
888\function{execl*()} functions. The \character{v} variants are good
889when the number of parameters is variable, with the arguments being
890passed in a list or tuple as the \var{args} parameter. In either
891case, the arguments to the child process must start with the name of
892the command being run.
Fred Drake215fe2f1999-02-02 19:02:35 +0000893
Fred Drakedb7287c2001-10-18 18:58:30 +0000894The variants which include a \character{p} near the end
895(\function{execlp()}, \function{execlpe()}, \function{execvp()},
896and \function{execvpe()}) will use the \envvar{PATH} environment
897variable to locate the program \var{file}. When the environment is
898being replaced (using one of the \function{exec*e()} variants,
899discussed in the next paragraph), the
900new environment is used as the source of the \envvar{PATH} variable.
901The other variants, \function{execl()}, \function{execle()},
902\function{execv()}, and \function{execve()}, will not use the
903\envvar{PATH} variable to locate the executable; \var{path} must
904contain an appropriate absolute or relative path.
Fred Drake215fe2f1999-02-02 19:02:35 +0000905
Fred Drakedb7287c2001-10-18 18:58:30 +0000906For \function{execle()}, \function{execlpe()}, \function{execve()},
907and \function{execvpe()} (note that these all end in \character{e}),
908the \var{env} parameter must be a mapping which is used to define the
909environment variables for the new process; the \function{execl()},
910\function{execlp()}, \function{execv()}, and \function{execvp()}
911all cause the new process to inherit the environment of the current
912process.
913Availability: \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000914\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}
Fred Drakedb7287c2001-10-18 18:58:30 +0000971\funcline{spawnlp}{mode, file, \moreargs}
972\funcline{spawnlpe}{mode, file, \moreargs, env}
Fred Drake739282d2001-08-16 21:21:28 +0000973\funcline{spawnv}{mode, path, args}
974\funcline{spawnve}{mode, path, args, env}
Fred Drakedb7287c2001-10-18 18:58:30 +0000975\funcline{spawnvp}{mode, file, args}
976\funcline{spawnvpe}{mode, file, args, env}
Fred Drake739282d2001-08-16 21:21:28 +0000977Execute 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 +0000983The \character{l} and \character{v} variants of the
984\function{spawn*()} functions differ in how command-line arguments are
985passed. The \character{l} variants are perhaps the easiest to work
986with if the number of parameters is fixed when the code is written;
987the individual parameters simply become additional parameters to the
988\function{spawnl*()} functions. The \character{v} variants are good
989when the number of parameters is variable, with the arguments being
990passed in a list or tuple as the \var{args} parameter. In either
991case, the arguments to the child process must start with the name of
992the command being run.
993
Fred Drakedb7287c2001-10-18 18:58:30 +0000994The variants which include a second \character{p} near the end
995(\function{spawnlp()}, \function{spawnlpe()}, \function{spawnvp()},
996and \function{spawnvpe()}) will use the \envvar{PATH} environment
997variable to locate the program \var{file}. When the environment is
998being replaced (using one of the \function{spawn*e()} variants,
999discussed in the next paragraph), the new environment is used as the
1000source of the \envvar{PATH} variable. The other variants,
1001\function{spawnl()}, \function{spawnle()}, \function{spawnv()}, and
1002\function{spawnve()}, will not use the \envvar{PATH} variable to
1003locate the executable; \var{path} must contain an appropriate absolute
1004or relative path.
1005
1006For \function{spawnle()}, \function{spawnlpe()}, \function{spawnve()},
1007and \function{spawnvpe()} (note that these all end in \character{e}),
1008the \var{env} parameter must be a mapping which is used to define the
1009environment variables for the new process; the \function{spawnl()},
1010\function{spawnlp()}, \function{spawnv()}, and \function{spawnvp()}
1011all cause the new process to inherit the environment of the current
1012process.
1013
Fred Drake739282d2001-08-16 21:21:28 +00001014As an example, the following calls to \function{spawnlp()} and
1015\function{spawnvpe()} are equivalent:
1016
1017\begin{verbatim}
1018import os
1019os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1020
1021L = ['cp', 'index.html', '/dev/null']
1022os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1023\end{verbatim}
1024
Fred Drakedb7287c2001-10-18 18:58:30 +00001025Availability: \UNIX, Windows. \function{spawnvp()} and
1026\function{spawnvpe()} are not available on Windows.
Fred Drake0b9bc202001-06-11 18:25:34 +00001027\versionadded{1.6}
Fred Drake215fe2f1999-02-02 19:02:35 +00001028\end{funcdesc}
1029
Fred Drake938a8d72001-10-09 18:07:04 +00001030\begin{datadesc}{P_NOWAIT}
Fred Drake9329e5e1999-02-16 19:40:19 +00001031\dataline{P_NOWAITO}
Fred Drake938a8d72001-10-09 18:07:04 +00001032Possible values for the \var{mode} parameter to the \function{spawn*()}
1033family of functions. If either of these values is given, the
1034\function{spawn*()} functions will return as soon as the new process
1035has been created, with the process ID as the return value.
Fred Drake15861b22000-02-29 05:19:38 +00001036Availability: \UNIX{}, Windows.
Fred Drake0b9bc202001-06-11 18:25:34 +00001037\versionadded{1.6}
Fred Drake15861b22000-02-29 05:19:38 +00001038\end{datadesc}
1039
Fred Drake938a8d72001-10-09 18:07:04 +00001040\begin{datadesc}{P_WAIT}
1041Possible value for the \var{mode} parameter to the \function{spawn*()}
1042family of functions. If this is given as \var{mode}, the
1043\function{spawn*()} functions will not return until the new process
1044has run to completion and will return the exit code of the process the
1045run is successful, or \code{-\var{signal}} if a signal kills the
1046process.
1047Availability: \UNIX{}, Windows.
1048\versionadded{1.6}
1049\end{datadesc}
1050
1051\begin{datadesc}{P_DETACH}
1052\dataline{P_OVERLAY}
1053Possible values for the \var{mode} parameter to the
1054\function{spawn*()} family of functions. These are less portable than
1055those listed above.
1056\constant{P_DETACH} is similar to \constant{P_NOWAIT}, but the new
1057process is detached from the console of the calling process.
1058If \constant{P_OVERLAY} is used, the current process will be replaced;
1059the \function{spawn*()} function will not return.
Fred Drake215fe2f1999-02-02 19:02:35 +00001060Availability: Windows.
Fred Drake0b9bc202001-06-11 18:25:34 +00001061\versionadded{1.6}
Fred Drake215fe2f1999-02-02 19:02:35 +00001062\end{datadesc}
1063
Fred Drake4ce4f2e2000-09-29 04:15:19 +00001064\begin{funcdesc}{startfile}{path}
1065Start a file with its associated application. This acts like
1066double-clicking the file in Windows Explorer, or giving the file name
Fred Drake8ee679f2001-07-14 02:50:55 +00001067as an argument to the \program{start} command from the interactive
1068command shell: the file is opened with whatever application (if any)
1069its extension is associated.
Fred Drake4ce4f2e2000-09-29 04:15:19 +00001070
1071\function{startfile()} returns as soon as the associated application
1072is launched. There is no option to wait for the application to close,
1073and no way to retrieve the application's exit status. The \var{path}
1074parameter is relative to the current directory. If you want to use an
1075absolute path, make sure the first character is not a slash
1076(\character{/}); the underlying Win32 \cfunction{ShellExecute()}
Fred Drake8a2adcf2001-07-23 19:20:56 +00001077function doesn't work if it is. Use the \function{os.path.normpath()}
Fred Drake4ce4f2e2000-09-29 04:15:19 +00001078function to ensure that the path is properly encoded for Win32.
1079Availability: Windows.
1080\versionadded{2.0}
1081\end{funcdesc}
1082
Fred Drake215fe2f1999-02-02 19:02:35 +00001083\begin{funcdesc}{system}{command}
1084Execute the command (a string) in a subshell. This is implemented by
1085calling the Standard C function \cfunction{system()}, and has the
Fred Drakeec6baaf1999-04-21 18:13:31 +00001086same limitations. Changes to \code{posix.environ}, \code{sys.stdin},
Fred Drake215fe2f1999-02-02 19:02:35 +00001087etc.\ are not reflected in the environment of the executed command.
1088The return value is the exit status of the process encoded in the
Fred Drake7a621281999-06-10 15:07:05 +00001089format specified for \function{wait()}, except on Windows 95 and 98,
Fred Drakea88ef001999-06-18 19:11:25 +00001090where it is always \code{0}. Note that \POSIX{} does not specify the
1091meaning of the return value of the C \cfunction{system()} function,
1092so the return value of the Python function is system-dependent.
Fred Drake215fe2f1999-02-02 19:02:35 +00001093Availability: \UNIX{}, Windows.
1094\end{funcdesc}
1095
1096\begin{funcdesc}{times}{}
Fred Drake8ee679f2001-07-14 02:50:55 +00001097Return a 5-tuple of floating point numbers indicating accumulated
1098(processor or other)
Fred Drake215fe2f1999-02-02 19:02:35 +00001099times, in seconds. The items are: user time, system time, children's
1100user time, children's system time, and elapsed real time since a fixed
Fred Drakeec6baaf1999-04-21 18:13:31 +00001101point in the past, in that order. See the \UNIX{} manual page
1102\manpage{times}{2} or the corresponding Windows Platform API
1103documentation.
Fred Drake215fe2f1999-02-02 19:02:35 +00001104Availability: \UNIX{}, Windows.
1105\end{funcdesc}
1106
1107\begin{funcdesc}{wait}{}
1108Wait for completion of a child process, and return a tuple containing
1109its pid and exit status indication: a 16-bit number, whose low byte is
1110the signal number that killed the process, and whose high byte is the
1111exit status (if the signal number is zero); the high bit of the low
1112byte is set if a core file was produced.
1113Availability: \UNIX{}.
1114\end{funcdesc}
1115
1116\begin{funcdesc}{waitpid}{pid, options}
Fred Drake31e5e371999-08-13 13:36:33 +00001117Wait for completion of a child process given by process id \var{pid},
1118and return a tuple containing its process id and exit status
1119indication (encoded as for \function{wait()}). The semantics of the
1120call are affected by the value of the integer \var{options}, which
1121should be \code{0} for normal operation.
Fred Drake215fe2f1999-02-02 19:02:35 +00001122Availability: \UNIX{}.
Fred Drake31e5e371999-08-13 13:36:33 +00001123
1124If \var{pid} is greater than \code{0}, \function{waitpid()} requests
1125status information for that specific process. If \var{pid} is
1126\code{0}, the request is for the status of any child in the process
1127group of the current process. If \var{pid} is \code{-1}, the request
1128pertains to any child of the current process. If \var{pid} is less
1129than \code{-1}, status is requested for any process in the process
1130group \code{-\var{pid}} (the absolute value of \var{pid}).
Fred Drake215fe2f1999-02-02 19:02:35 +00001131\end{funcdesc}
1132
1133\begin{datadesc}{WNOHANG}
1134The option for \function{waitpid()} to avoid hanging if no child
1135process status is available immediately.
1136Availability: \UNIX{}.
1137\end{datadesc}
1138
Fred Drake38e5d272000-04-03 20:13:55 +00001139The following functions take a process status code as returned by
1140\function{system()}, \function{wait()}, or \function{waitpid()} as a
1141parameter. They may be used to determine the disposition of a
1142process.
Fred Drake215fe2f1999-02-02 19:02:35 +00001143
1144\begin{funcdesc}{WIFSTOPPED}{status}
1145Return true if the process has been stopped.
1146Availability: \UNIX{}.
1147\end{funcdesc}
1148
1149\begin{funcdesc}{WIFSIGNALED}{status}
1150Return true if the process exited due to a signal.
1151Availability: \UNIX{}.
1152\end{funcdesc}
1153
1154\begin{funcdesc}{WIFEXITED}{status}
1155Return true if the process exited using the \manpage{exit}{2} system
1156call.
1157Availability: \UNIX{}.
1158\end{funcdesc}
1159
1160\begin{funcdesc}{WEXITSTATUS}{status}
1161If \code{WIFEXITED(\var{status})} is true, return the integer
1162parameter to the \manpage{exit}{2} system call. Otherwise, the return
1163value is meaningless.
1164Availability: \UNIX{}.
1165\end{funcdesc}
1166
1167\begin{funcdesc}{WSTOPSIG}{status}
Fred Drake35c3ffd1999-03-04 14:08:10 +00001168Return the signal which caused the process to stop.
1169Availability: \UNIX{}.
1170\end{funcdesc}
1171
1172\begin{funcdesc}{WTERMSIG}{status}
Fred Drake215fe2f1999-02-02 19:02:35 +00001173Return the signal which caused the process to exit.
1174Availability: \UNIX{}.
1175\end{funcdesc}
1176
1177
Thomas Woutersf8316632000-07-16 19:01:10 +00001178\subsection{Miscellaneous System Information \label{os-path}}
Fred Drake88f6ca21999-12-15 19:39:04 +00001179
1180
1181\begin{funcdesc}{confstr}{name}
1182Return string-valued system configuration values.
1183\var{name} specifies the configuration value to retrieve; it may be a
1184string which is the name of a defined system value; these names are
Fred Drake8ee679f2001-07-14 02:50:55 +00001185specified in a number of standards (\POSIX, \UNIX 95, \UNIX 98, and
Fred Drake88f6ca21999-12-15 19:39:04 +00001186others). Some platforms define additional names as well. The names
1187known to the host operating system are given in the
1188\code{confstr_names} dictionary. For configuration variables not
1189included in that mapping, passing an integer for \var{name} is also
1190accepted.
1191Availability: \UNIX{}.
1192
1193If the configuration value specified by \var{name} isn't defined, the
1194empty string is returned.
1195
1196If \var{name} is a string and is not known, \exception{ValueError} is
1197raised. If a specific value for \var{name} is not supported by the
1198host system, even if it is included in \code{confstr_names}, an
1199\exception{OSError} is raised with \constant{errno.EINVAL} for the
1200error number.
1201\end{funcdesc}
1202
1203\begin{datadesc}{confstr_names}
1204Dictionary mapping names accepted by \function{confstr()} to the
1205integer values defined for those names by the host operating system.
1206This can be used to determine the set of names known to the system.
1207Availability: \UNIX.
1208\end{datadesc}
1209
1210\begin{funcdesc}{sysconf}{name}
1211Return integer-valued system configuration values.
1212If the configuration value specified by \var{name} isn't defined,
1213\code{-1} is returned. The comments regarding the \var{name}
1214parameter for \function{confstr()} apply here as well; the dictionary
1215that provides information on the known names is given by
1216\code{sysconf_names}.
1217Availability: \UNIX{}.
1218\end{funcdesc}
1219
1220\begin{datadesc}{sysconf_names}
1221Dictionary mapping names accepted by \function{sysconf()} to the
1222integer values defined for those names by the host operating system.
1223This can be used to determine the set of names known to the system.
1224Availability: \UNIX.
1225\end{datadesc}
1226
Fred Drake215fe2f1999-02-02 19:02:35 +00001227
1228The follow data values are used to support path manipulation
1229operations. These are defined for all platforms.
1230
1231Higher-level operations on pathnames are defined in the
1232\refmodule{os.path} module.
1233
1234
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001235\begin{datadesc}{curdir}
Fred Drake8ee679f2001-07-14 02:50:55 +00001236The constant string used by the operating system to refer to the current
1237directory.
Fred Drake907e76b2001-07-06 20:30:11 +00001238For example: \code{'.'} for \POSIX{} or \code{':'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001239\end{datadesc}
1240
1241\begin{datadesc}{pardir}
Fred Drake8ee679f2001-07-14 02:50:55 +00001242The constant string used by the operating system to refer to the parent
1243directory.
Fred Drake907e76b2001-07-06 20:30:11 +00001244For example: \code{'..'} for \POSIX{} or \code{'::'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001245\end{datadesc}
1246
1247\begin{datadesc}{sep}
Fred Drake8ee679f2001-07-14 02:50:55 +00001248The character used by the operating system to separate pathname components,
Fred Drake907e76b2001-07-06 20:30:11 +00001249for example, \character{/} for \POSIX{} or \character{:} for the
1250Macintosh. Note that knowing this is not sufficient to be able to
1251parse or concatenate pathnames --- use \function{os.path.split()} and
Fred Drake1a3c2a01998-08-06 15:18:23 +00001252\function{os.path.join()} --- but it is occasionally useful.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001253\end{datadesc}
1254
Guido van Rossumb2afc811997-08-29 22:37:44 +00001255\begin{datadesc}{altsep}
Fred Drake8ee679f2001-07-14 02:50:55 +00001256An alternative character used by the operating system to separate pathname
1257components, or \code{None} if only one separator character exists. This is
1258set to \character{/} on DOS and Windows systems where \code{sep} is a
1259backslash.
Guido van Rossumb2afc811997-08-29 22:37:44 +00001260\end{datadesc}
1261
Guido van Rossum470be141995-03-17 16:07:09 +00001262\begin{datadesc}{pathsep}
Fred Drake8ee679f2001-07-14 02:50:55 +00001263The character conventionally used by the operating system to separate
1264search patch components (as in \envvar{PATH}), such as \character{:} for
1265\POSIX{} or \character{;} for DOS and Windows.
Guido van Rossum9c59ce91998-06-30 15:54:27 +00001266\end{datadesc}
1267
Guido van Rossum470be141995-03-17 16:07:09 +00001268\begin{datadesc}{defpath}
Fred Drake1a3c2a01998-08-06 15:18:23 +00001269The default search path used by \function{exec*p*()} if the environment
Guido van Rossum470be141995-03-17 16:07:09 +00001270doesn't have a \code{'PATH'} key.
1271\end{datadesc}
1272
Fred Drake215fe2f1999-02-02 19:02:35 +00001273\begin{datadesc}{linesep}
1274The string used to separate (or, rather, terminate) lines on the
Fred Drake907e76b2001-07-06 20:30:11 +00001275current platform. This may be a single character, such as \code{'\e
Fred Drake8ee679f2001-07-14 02:50:55 +00001276n'} for \POSIX{} or \code{'\e r'} for the Mac OS, or multiple characters,
1277for example, \code{'\e r\e n'} for DOS and Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +00001278\end{datadesc}