blob: 19f53e7fc3636ec61c524c0de2eb53356d670ff3 [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 Drake215fe2f1999-02-02 19:02:35 +0000870\begin{funcdesc}{execl}{path, arg0, arg1, ...}
871This is equivalent to
872\samp{execv(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
873Availability: \UNIX{}, Windows.
874\end{funcdesc}
875
876\begin{funcdesc}{execle}{path, arg0, arg1, ..., env}
877This is equivalent to
878\samp{execve(\var{path}, (\var{arg0}, \var{arg1}, ...), \var{env})}.
879Availability: \UNIX{}, Windows.
880\end{funcdesc}
881
882\begin{funcdesc}{execlp}{path, arg0, arg1, ...}
883This is equivalent to
884\samp{execvp(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
885Availability: \UNIX{}, Windows.
886\end{funcdesc}
887
888\begin{funcdesc}{execv}{path, args}
889Execute the executable \var{path} with argument list \var{args},
Fred Drake907e76b2001-07-06 20:30:11 +0000890replacing the current process (the Python interpreter).
Fred Drake215fe2f1999-02-02 19:02:35 +0000891The argument list may be a tuple or list of strings.
892Availability: \UNIX{}, Windows.
893\end{funcdesc}
894
895\begin{funcdesc}{execve}{path, args, env}
896Execute the executable \var{path} with argument list \var{args},
Fred Drake907e76b2001-07-06 20:30:11 +0000897and environment \var{env}, replacing the current process (the Python
898interpreter).
Fred Drake215fe2f1999-02-02 19:02:35 +0000899The argument list may be a tuple or list of strings.
900The environment must be a dictionary mapping strings to strings.
901Availability: \UNIX{}, Windows.
902\end{funcdesc}
903
904\begin{funcdesc}{execvp}{path, args}
905This is like \samp{execv(\var{path}, \var{args})} but duplicates
906the shell's actions in searching for an executable file in a list of
907directories. The directory list is obtained from
908\code{environ['PATH']}.
909Availability: \UNIX{}, Windows.
910\end{funcdesc}
911
912\begin{funcdesc}{execvpe}{path, args, env}
913This is a cross between \function{execve()} and \function{execvp()}.
914The directory list is obtained from \code{\var{env}['PATH']}.
915Availability: \UNIX{}, Windows.
916\end{funcdesc}
917
918\begin{funcdesc}{_exit}{n}
919Exit to the system with status \var{n}, without calling cleanup
920handlers, flushing stdio buffers, etc.
921Availability: \UNIX{}, Windows.
922
923Note: the standard way to exit is \code{sys.exit(\var{n})}.
924\function{_exit()} should normally only be used in the child process
925after a \function{fork()}.
926\end{funcdesc}
927
928\begin{funcdesc}{fork}{}
929Fork a child process. Return \code{0} in the child, the child's
930process id in the parent.
931Availability: \UNIX{}.
932\end{funcdesc}
933
Fred Drakec82634c2000-06-28 17:27:48 +0000934\begin{funcdesc}{forkpty}{}
935Fork a child process, using a new pseudo-terminal as the child's
936controlling terminal. Return a pair of \code{(\var{pid}, \var{fd})},
937where \var{pid} is \code{0} in the child, the new child's process id
938in the parent, and \code{fd} is the file descriptor of the master end
939of the pseudo-terminal. For a more portable approach, use the
940\refmodule{pty} module.
941Availability: Some flavors of \UNIX{}
942\end{funcdesc}
943
Fred Drake215fe2f1999-02-02 19:02:35 +0000944\begin{funcdesc}{kill}{pid, sig}
945\index{process!killing}
946\index{process!signalling}
947Kill the process \var{pid} with signal \var{sig}.
948Availability: \UNIX{}.
949\end{funcdesc}
950
951\begin{funcdesc}{nice}{increment}
952Add \var{increment} to the process's ``niceness''. Return the new
953niceness.
954Availability: \UNIX{}.
955\end{funcdesc}
956
957\begin{funcdesc}{plock}{op}
958Lock program segments into memory. The value of \var{op}
959(defined in \code{<sys/lock.h>}) determines which segments are locked.
Fred Drake39063631999-02-26 14:05:02 +0000960Availability: \UNIX{}.
Fred Drake215fe2f1999-02-02 19:02:35 +0000961\end{funcdesc}
962
Fred Drake046f4d82001-06-11 15:21:48 +0000963\begin{funcdescni}{popen}{\unspecified}
964\funclineni{popen2}{\unspecified}
965\funclineni{popen3}{\unspecified}
966\funclineni{popen4}{\unspecified}
967Run child processes, returning opened pipes for communications. These
968functions are described in section \ref{os-newstreams}.
969\end{funcdescni}
970
Fred Drake739282d2001-08-16 21:21:28 +0000971\begin{funcdesc}{spawnl}{mode, path, \moreargs}
972\funcline{spawnle}{mode, path, \moreargs, env}
973\funcline{spawnlp}{mode, path, \moreargs}
974\funcline{spawnlpe}{mode, path, \moreargs, env}
975\funcline{spawnv}{mode, path, args}
976\funcline{spawnve}{mode, path, args, env}
977\funcline{spawnvp}{mode, path, args}
978\funcline{spawnvpe}{mode, path, args, env}
979Execute the program \var{path} in a new process. If \var{mode} is
980\constant{P_NOWAIT}, this function returns the process ID of the new
981process; it \var{mode} is \constant{P_WAIT}, returns the process's
982exit code if it exits normally, or \code{-\var{signal}}, where
983\var{signal} is the signal that killed the process.
Fred Drake215fe2f1999-02-02 19:02:35 +0000984
Fred Drake739282d2001-08-16 21:21:28 +0000985For \function{spawnle()}, \function{spawnlpe()}, \function{spawnve()},
986and \function{spawnvpe()} (note that these all end in \character{e}),
987the \var{env} parameter must be a mapping which is used to define the
988environment variables for the new process; the \function{spawnl()},
989\function{spawnlp()}, \function{spawnv()}, and \function{spawnvp()}
990all cause the new process to inherit the environment of the current
991process.
992
993The variants which include a second \character{p} near the end
994(\function{spawnlp()}, \function{spawnlpe()}, \function{spawnvp()},
995and \function{spawnvpe()}) will use the \envvar{PATH} environment
996variable to locate the program \var{path}. The other variants,
997\function{spawnl()}, \function{spawnle()}, \function{spawnv()}, and
998\function{spawnve()}, will not use the \envvar{PATH} variable to
999locate the executable.
1000
1001The \character{l} and \character{v} variants of the
1002\function{spawn*()} functions differ in how command-line arguments are
1003passed. The \character{l} variants are perhaps the easiest to work
1004with if the number of parameters is fixed when the code is written;
1005the individual parameters simply become additional parameters to the
1006\function{spawnl*()} functions. The \character{v} variants are good
1007when the number of parameters is variable, with the arguments being
1008passed in a list or tuple as the \var{args} parameter. In either
1009case, the arguments to the child process must start with the name of
1010the command being run.
1011
1012As an example, the following calls to \function{spawnlp()} and
1013\function{spawnvpe()} are equivalent:
1014
1015\begin{verbatim}
1016import os
1017os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1018
1019L = ['cp', 'index.html', '/dev/null']
1020os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1021\end{verbatim}
1022
Fred Drake15861b22000-02-29 05:19:38 +00001023Availability: \UNIX{}, Windows.
Fred Drake0b9bc202001-06-11 18:25:34 +00001024\versionadded{1.6}
Fred Drake215fe2f1999-02-02 19:02:35 +00001025\end{funcdesc}
1026
Fred Drake938a8d72001-10-09 18:07:04 +00001027\begin{datadesc}{P_NOWAIT}
Fred Drake9329e5e1999-02-16 19:40:19 +00001028\dataline{P_NOWAITO}
Fred Drake938a8d72001-10-09 18:07:04 +00001029Possible values for the \var{mode} parameter to the \function{spawn*()}
1030family of functions. If either of these values is given, the
1031\function{spawn*()} functions will return as soon as the new process
1032has been created, with the process ID as the return value.
Fred Drake15861b22000-02-29 05:19:38 +00001033Availability: \UNIX{}, Windows.
Fred Drake0b9bc202001-06-11 18:25:34 +00001034\versionadded{1.6}
Fred Drake15861b22000-02-29 05:19:38 +00001035\end{datadesc}
1036
Fred Drake938a8d72001-10-09 18:07:04 +00001037\begin{datadesc}{P_WAIT}
1038Possible value for the \var{mode} parameter to the \function{spawn*()}
1039family of functions. If this is given as \var{mode}, the
1040\function{spawn*()} functions will not return until the new process
1041has run to completion and will return the exit code of the process the
1042run is successful, or \code{-\var{signal}} if a signal kills the
1043process.
1044Availability: \UNIX{}, Windows.
1045\versionadded{1.6}
1046\end{datadesc}
1047
1048\begin{datadesc}{P_DETACH}
1049\dataline{P_OVERLAY}
1050Possible values for the \var{mode} parameter to the
1051\function{spawn*()} family of functions. These are less portable than
1052those listed above.
1053\constant{P_DETACH} is similar to \constant{P_NOWAIT}, but the new
1054process is detached from the console of the calling process.
1055If \constant{P_OVERLAY} is used, the current process will be replaced;
1056the \function{spawn*()} function will not return.
Fred Drake215fe2f1999-02-02 19:02:35 +00001057Availability: Windows.
Fred Drake0b9bc202001-06-11 18:25:34 +00001058\versionadded{1.6}
Fred Drake215fe2f1999-02-02 19:02:35 +00001059\end{datadesc}
1060
Fred Drake4ce4f2e2000-09-29 04:15:19 +00001061\begin{funcdesc}{startfile}{path}
1062Start a file with its associated application. This acts like
1063double-clicking the file in Windows Explorer, or giving the file name
Fred Drake8ee679f2001-07-14 02:50:55 +00001064as an argument to the \program{start} command from the interactive
1065command shell: the file is opened with whatever application (if any)
1066its extension is associated.
Fred Drake4ce4f2e2000-09-29 04:15:19 +00001067
1068\function{startfile()} returns as soon as the associated application
1069is launched. There is no option to wait for the application to close,
1070and no way to retrieve the application's exit status. The \var{path}
1071parameter is relative to the current directory. If you want to use an
1072absolute path, make sure the first character is not a slash
1073(\character{/}); the underlying Win32 \cfunction{ShellExecute()}
Fred Drake8a2adcf2001-07-23 19:20:56 +00001074function doesn't work if it is. Use the \function{os.path.normpath()}
Fred Drake4ce4f2e2000-09-29 04:15:19 +00001075function to ensure that the path is properly encoded for Win32.
1076Availability: Windows.
1077\versionadded{2.0}
1078\end{funcdesc}
1079
Fred Drake215fe2f1999-02-02 19:02:35 +00001080\begin{funcdesc}{system}{command}
1081Execute the command (a string) in a subshell. This is implemented by
1082calling the Standard C function \cfunction{system()}, and has the
Fred Drakeec6baaf1999-04-21 18:13:31 +00001083same limitations. Changes to \code{posix.environ}, \code{sys.stdin},
Fred Drake215fe2f1999-02-02 19:02:35 +00001084etc.\ are not reflected in the environment of the executed command.
1085The return value is the exit status of the process encoded in the
Fred Drake7a621281999-06-10 15:07:05 +00001086format specified for \function{wait()}, except on Windows 95 and 98,
Fred Drakea88ef001999-06-18 19:11:25 +00001087where it is always \code{0}. Note that \POSIX{} does not specify the
1088meaning of the return value of the C \cfunction{system()} function,
1089so the return value of the Python function is system-dependent.
Fred Drake215fe2f1999-02-02 19:02:35 +00001090Availability: \UNIX{}, Windows.
1091\end{funcdesc}
1092
1093\begin{funcdesc}{times}{}
Fred Drake8ee679f2001-07-14 02:50:55 +00001094Return a 5-tuple of floating point numbers indicating accumulated
1095(processor or other)
Fred Drake215fe2f1999-02-02 19:02:35 +00001096times, in seconds. The items are: user time, system time, children's
1097user time, children's system time, and elapsed real time since a fixed
Fred Drakeec6baaf1999-04-21 18:13:31 +00001098point in the past, in that order. See the \UNIX{} manual page
1099\manpage{times}{2} or the corresponding Windows Platform API
1100documentation.
Fred Drake215fe2f1999-02-02 19:02:35 +00001101Availability: \UNIX{}, Windows.
1102\end{funcdesc}
1103
1104\begin{funcdesc}{wait}{}
1105Wait for completion of a child process, and return a tuple containing
1106its pid and exit status indication: a 16-bit number, whose low byte is
1107the signal number that killed the process, and whose high byte is the
1108exit status (if the signal number is zero); the high bit of the low
1109byte is set if a core file was produced.
1110Availability: \UNIX{}.
1111\end{funcdesc}
1112
1113\begin{funcdesc}{waitpid}{pid, options}
Fred Drake31e5e371999-08-13 13:36:33 +00001114Wait for completion of a child process given by process id \var{pid},
1115and return a tuple containing its process id and exit status
1116indication (encoded as for \function{wait()}). The semantics of the
1117call are affected by the value of the integer \var{options}, which
1118should be \code{0} for normal operation.
Fred Drake215fe2f1999-02-02 19:02:35 +00001119Availability: \UNIX{}.
Fred Drake31e5e371999-08-13 13:36:33 +00001120
1121If \var{pid} is greater than \code{0}, \function{waitpid()} requests
1122status information for that specific process. If \var{pid} is
1123\code{0}, the request is for the status of any child in the process
1124group of the current process. If \var{pid} is \code{-1}, the request
1125pertains to any child of the current process. If \var{pid} is less
1126than \code{-1}, status is requested for any process in the process
1127group \code{-\var{pid}} (the absolute value of \var{pid}).
Fred Drake215fe2f1999-02-02 19:02:35 +00001128\end{funcdesc}
1129
1130\begin{datadesc}{WNOHANG}
1131The option for \function{waitpid()} to avoid hanging if no child
1132process status is available immediately.
1133Availability: \UNIX{}.
1134\end{datadesc}
1135
Fred Drake38e5d272000-04-03 20:13:55 +00001136The following functions take a process status code as returned by
1137\function{system()}, \function{wait()}, or \function{waitpid()} as a
1138parameter. They may be used to determine the disposition of a
1139process.
Fred Drake215fe2f1999-02-02 19:02:35 +00001140
1141\begin{funcdesc}{WIFSTOPPED}{status}
1142Return true if the process has been stopped.
1143Availability: \UNIX{}.
1144\end{funcdesc}
1145
1146\begin{funcdesc}{WIFSIGNALED}{status}
1147Return true if the process exited due to a signal.
1148Availability: \UNIX{}.
1149\end{funcdesc}
1150
1151\begin{funcdesc}{WIFEXITED}{status}
1152Return true if the process exited using the \manpage{exit}{2} system
1153call.
1154Availability: \UNIX{}.
1155\end{funcdesc}
1156
1157\begin{funcdesc}{WEXITSTATUS}{status}
1158If \code{WIFEXITED(\var{status})} is true, return the integer
1159parameter to the \manpage{exit}{2} system call. Otherwise, the return
1160value is meaningless.
1161Availability: \UNIX{}.
1162\end{funcdesc}
1163
1164\begin{funcdesc}{WSTOPSIG}{status}
Fred Drake35c3ffd1999-03-04 14:08:10 +00001165Return the signal which caused the process to stop.
1166Availability: \UNIX{}.
1167\end{funcdesc}
1168
1169\begin{funcdesc}{WTERMSIG}{status}
Fred Drake215fe2f1999-02-02 19:02:35 +00001170Return the signal which caused the process to exit.
1171Availability: \UNIX{}.
1172\end{funcdesc}
1173
1174
Thomas Woutersf8316632000-07-16 19:01:10 +00001175\subsection{Miscellaneous System Information \label{os-path}}
Fred Drake88f6ca21999-12-15 19:39:04 +00001176
1177
1178\begin{funcdesc}{confstr}{name}
1179Return string-valued system configuration values.
1180\var{name} specifies the configuration value to retrieve; it may be a
1181string which is the name of a defined system value; these names are
Fred Drake8ee679f2001-07-14 02:50:55 +00001182specified in a number of standards (\POSIX, \UNIX 95, \UNIX 98, and
Fred Drake88f6ca21999-12-15 19:39:04 +00001183others). Some platforms define additional names as well. The names
1184known to the host operating system are given in the
1185\code{confstr_names} dictionary. For configuration variables not
1186included in that mapping, passing an integer for \var{name} is also
1187accepted.
1188Availability: \UNIX{}.
1189
1190If the configuration value specified by \var{name} isn't defined, the
1191empty string is returned.
1192
1193If \var{name} is a string and is not known, \exception{ValueError} is
1194raised. If a specific value for \var{name} is not supported by the
1195host system, even if it is included in \code{confstr_names}, an
1196\exception{OSError} is raised with \constant{errno.EINVAL} for the
1197error number.
1198\end{funcdesc}
1199
1200\begin{datadesc}{confstr_names}
1201Dictionary mapping names accepted by \function{confstr()} to the
1202integer values defined for those names by the host operating system.
1203This can be used to determine the set of names known to the system.
1204Availability: \UNIX.
1205\end{datadesc}
1206
1207\begin{funcdesc}{sysconf}{name}
1208Return integer-valued system configuration values.
1209If the configuration value specified by \var{name} isn't defined,
1210\code{-1} is returned. The comments regarding the \var{name}
1211parameter for \function{confstr()} apply here as well; the dictionary
1212that provides information on the known names is given by
1213\code{sysconf_names}.
1214Availability: \UNIX{}.
1215\end{funcdesc}
1216
1217\begin{datadesc}{sysconf_names}
1218Dictionary mapping names accepted by \function{sysconf()} to the
1219integer values defined for those names by the host operating system.
1220This can be used to determine the set of names known to the system.
1221Availability: \UNIX.
1222\end{datadesc}
1223
Fred Drake215fe2f1999-02-02 19:02:35 +00001224
1225The follow data values are used to support path manipulation
1226operations. These are defined for all platforms.
1227
1228Higher-level operations on pathnames are defined in the
1229\refmodule{os.path} module.
1230
1231
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001232\begin{datadesc}{curdir}
Fred Drake8ee679f2001-07-14 02:50:55 +00001233The constant string used by the operating system to refer to the current
1234directory.
Fred Drake907e76b2001-07-06 20:30:11 +00001235For example: \code{'.'} for \POSIX{} or \code{':'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001236\end{datadesc}
1237
1238\begin{datadesc}{pardir}
Fred Drake8ee679f2001-07-14 02:50:55 +00001239The constant string used by the operating system to refer to the parent
1240directory.
Fred Drake907e76b2001-07-06 20:30:11 +00001241For example: \code{'..'} for \POSIX{} or \code{'::'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001242\end{datadesc}
1243
1244\begin{datadesc}{sep}
Fred Drake8ee679f2001-07-14 02:50:55 +00001245The character used by the operating system to separate pathname components,
Fred Drake907e76b2001-07-06 20:30:11 +00001246for example, \character{/} for \POSIX{} or \character{:} for the
1247Macintosh. Note that knowing this is not sufficient to be able to
1248parse or concatenate pathnames --- use \function{os.path.split()} and
Fred Drake1a3c2a01998-08-06 15:18:23 +00001249\function{os.path.join()} --- but it is occasionally useful.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001250\end{datadesc}
1251
Guido van Rossumb2afc811997-08-29 22:37:44 +00001252\begin{datadesc}{altsep}
Fred Drake8ee679f2001-07-14 02:50:55 +00001253An alternative character used by the operating system to separate pathname
1254components, or \code{None} if only one separator character exists. This is
1255set to \character{/} on DOS and Windows systems where \code{sep} is a
1256backslash.
Guido van Rossumb2afc811997-08-29 22:37:44 +00001257\end{datadesc}
1258
Guido van Rossum470be141995-03-17 16:07:09 +00001259\begin{datadesc}{pathsep}
Fred Drake8ee679f2001-07-14 02:50:55 +00001260The character conventionally used by the operating system to separate
1261search patch components (as in \envvar{PATH}), such as \character{:} for
1262\POSIX{} or \character{;} for DOS and Windows.
Guido van Rossum9c59ce91998-06-30 15:54:27 +00001263\end{datadesc}
1264
Guido van Rossum470be141995-03-17 16:07:09 +00001265\begin{datadesc}{defpath}
Fred Drake1a3c2a01998-08-06 15:18:23 +00001266The default search path used by \function{exec*p*()} if the environment
Guido van Rossum470be141995-03-17 16:07:09 +00001267doesn't have a \code{'PATH'} key.
1268\end{datadesc}
1269
Fred Drake215fe2f1999-02-02 19:02:35 +00001270\begin{datadesc}{linesep}
1271The string used to separate (or, rather, terminate) lines on the
Fred Drake907e76b2001-07-06 20:30:11 +00001272current platform. This may be a single character, such as \code{'\e
Fred Drake8ee679f2001-07-14 02:50:55 +00001273n'} for \POSIX{} or \code{'\e r'} for the Mac OS, or multiple characters,
1274for example, \code{'\e r\e n'} for DOS and Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +00001275\end{datadesc}