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