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