blob: b6e0bfa703a8111a2d24745736a07ebdae376ffa [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 Drake046f4d82001-06-11 15:21:48 +0000318\begin{funcdesc}{popen2}{cmd\optional{, mode\optional{, bufsize}}}
Fred Drake8a9db992000-09-28 20:27:51 +0000319Executes \var{cmd} as a sub-process. Returns the file objects
320\code{(\var{child_stdin}, \var{child_stdout})}.
Fred Drake0b9bc202001-06-11 18:25:34 +0000321Availability: \UNIX{}, Windows.
Fred Drake8a9db992000-09-28 20:27:51 +0000322\versionadded{2.0}
323\end{funcdesc}
324
Fred Drake046f4d82001-06-11 15:21:48 +0000325\begin{funcdesc}{popen3}{cmd\optional{, mode\optional{, bufsize}}}
Fred Drake8a9db992000-09-28 20:27:51 +0000326Executes \var{cmd} as a sub-process. Returns the file objects
327\code{(\var{child_stdin}, \var{child_stdout}, \var{child_stderr})}.
Fred Drake0b9bc202001-06-11 18:25:34 +0000328Availability: \UNIX{}, Windows.
Fred Drake8a9db992000-09-28 20:27:51 +0000329\versionadded{2.0}
330\end{funcdesc}
331
Fred Drake046f4d82001-06-11 15:21:48 +0000332\begin{funcdesc}{popen4}{cmd\optional{, mode\optional{, bufsize}}}
Fred Drake8a9db992000-09-28 20:27:51 +0000333Executes \var{cmd} as a sub-process. Returns the file objects
334\code{(\var{child_stdin}, \var{child_stdout_and_stderr})}.
Fred Drake0b9bc202001-06-11 18:25:34 +0000335Availability: \UNIX{}, Windows.
Fred Drake8a9db992000-09-28 20:27:51 +0000336\versionadded{2.0}
337\end{funcdesc}
338
339This functionality is also available in the \refmodule{popen2} module
340using functions of the same names, but the return values of those
341functions have a different order.
342
343
Fred Drake215fe2f1999-02-02 19:02:35 +0000344\subsection{File Descriptor Operations \label{os-fd-ops}}
345
346These functions operate on I/O streams referred to
347using file descriptors.
348
349
350\begin{funcdesc}{close}{fd}
351Close file descriptor \var{fd}.
352Availability: Macintosh, \UNIX{}, Windows.
353
354Note: this function is intended for low-level I/O and must be applied
355to a file descriptor as returned by \function{open()} or
356\function{pipe()}. To close a ``file object'' returned by the
357built-in function \function{open()} or by \function{popen()} or
358\function{fdopen()}, use its \method{close()} method.
359\end{funcdesc}
360
361\begin{funcdesc}{dup}{fd}
362Return a duplicate of file descriptor \var{fd}.
363Availability: Macintosh, \UNIX{}, Windows.
364\end{funcdesc}
365
366\begin{funcdesc}{dup2}{fd, fd2}
367Duplicate file descriptor \var{fd} to \var{fd2}, closing the latter
368first if necessary.
369Availability: \UNIX{}, Windows.
370\end{funcdesc}
371
Fred Drake88f6ca21999-12-15 19:39:04 +0000372\begin{funcdesc}{fpathconf}{fd, name}
Thomas Woutersf8316632000-07-16 19:01:10 +0000373Return system configuration information relevant to an open file.
Fred Drake88f6ca21999-12-15 19:39:04 +0000374\var{name} specifies the configuration value to retrieve; it may be a
375string which is the name of a defined system value; these names are
376specified in a number of standards (\POSIX.1, Unix95, Unix98, and
377others). Some platforms define additional names as well. The names
378known to the host operating system are given in the
379\code{pathconf_names} dictionary. For configuration variables not
380included in that mapping, passing an integer for \var{name} is also
381accepted.
382Availability: \UNIX{}.
383
384If \var{name} is a string and is not known, \exception{ValueError} is
385raised. If a specific value for \var{name} is not supported by the
386host system, even if it is included in \code{pathconf_names}, an
387\exception{OSError} is raised with \constant{errno.EINVAL} for the
388error number.
389\end{funcdesc}
390
Fred Drake215fe2f1999-02-02 19:02:35 +0000391\begin{funcdesc}{fstat}{fd}
392Return status for file descriptor \var{fd}, like \function{stat()}.
393Availability: \UNIX{}, Windows.
394\end{funcdesc}
395
396\begin{funcdesc}{fstatvfs}{fd}
397Return information about the filesystem containing the file associated
398with file descriptor \var{fd}, like \function{statvfs()}.
399Availability: \UNIX{}.
400\end{funcdesc}
401
402\begin{funcdesc}{ftruncate}{fd, length}
403Truncate the file corresponding to file descriptor \var{fd},
404so that it is at most \var{length} bytes in size.
405Availability: \UNIX{}.
406\end{funcdesc}
407
Skip Montanarod3725212000-07-19 17:30:58 +0000408\begin{funcdesc}{isatty}{fd}
409Return \code{1} if the file descriptor \var{fd} is open and connected to a
410tty(-like) device, else \code{0}.
411Availability: \UNIX{}
412\end{funcdesc}
413
Fred Drake215fe2f1999-02-02 19:02:35 +0000414\begin{funcdesc}{lseek}{fd, pos, how}
415Set the current position of file descriptor \var{fd} to position
416\var{pos}, modified by \var{how}: \code{0} to set the position
417relative to the beginning of the file; \code{1} to set it relative to
418the current position; \code{2} to set it relative to the end of the
419file.
420Availability: Macintosh, \UNIX{}, Windows.
421\end{funcdesc}
422
423\begin{funcdesc}{open}{file, flags\optional{, mode}}
424Open the file \var{file} and set various flags according to
425\var{flags} and possibly its mode according to \var{mode}.
426The default \var{mode} is \code{0777} (octal), and the current umask
427value is first masked out. Return the file descriptor for the newly
428opened file.
429Availability: Macintosh, \UNIX{}, Windows.
430
431For a description of the flag and mode values, see the C run-time
432documentation; flag constants (like \constant{O_RDONLY} and
433\constant{O_WRONLY}) are defined in this module too (see below).
434
435Note: this function is intended for low-level I/O. For normal usage,
436use the built-in function \function{open()}, which returns a ``file
437object'' with \method{read()} and \method{write()} methods (and many
438more).
439\end{funcdesc}
440
Fred Drakec82634c2000-06-28 17:27:48 +0000441\begin{funcdesc}{openpty}{}
442Open a new pseudo-terminal pair. Return a pair of file descriptors
443\code{(\var{master}, \var{slave})} for the pty and the tty,
444respectively. For a (slightly) more portable approach, use the
445\refmodule{pty}\refstmodindex{pty} module.
446Availability: Some flavors of \UNIX{}
447\end{funcdesc}
448
Fred Drake215fe2f1999-02-02 19:02:35 +0000449\begin{funcdesc}{pipe}{}
450Create a pipe. Return a pair of file descriptors \code{(\var{r},
451\var{w})} usable for reading and writing, respectively.
452Availability: \UNIX{}, Windows.
453\end{funcdesc}
454
455\begin{funcdesc}{read}{fd, n}
456Read at most \var{n} bytes from file descriptor \var{fd}.
457Return a string containing the bytes read.
458Availability: Macintosh, \UNIX{}, Windows.
459
460Note: this function is intended for low-level I/O and must be applied
461to a file descriptor as returned by \function{open()} or
462\function{pipe()}. To read a ``file object'' returned by the
463built-in function \function{open()} or by \function{popen()} or
464\function{fdopen()}, or \code{sys.stdin}, use its
465\method{read()} or \method{readline()} methods.
466\end{funcdesc}
467
468\begin{funcdesc}{tcgetpgrp}{fd}
469Return the process group associated with the terminal given by
470\var{fd} (an open file descriptor as returned by \function{open()}).
471Availability: \UNIX{}.
472\end{funcdesc}
473
474\begin{funcdesc}{tcsetpgrp}{fd, pg}
475Set the process group associated with the terminal given by
476\var{fd} (an open file descriptor as returned by \function{open()})
477to \var{pg}.
478Availability: \UNIX{}.
479\end{funcdesc}
480
481\begin{funcdesc}{ttyname}{fd}
482Return a string which specifies the terminal device associated with
483file-descriptor \var{fd}. If \var{fd} is not associated with a terminal
484device, an exception is raised.
485Availability: \UNIX{}.
486\end{funcdesc}
487
488\begin{funcdesc}{write}{fd, str}
489Write the string \var{str} to file descriptor \var{fd}.
490Return the number of bytes actually written.
491Availability: Macintosh, \UNIX{}, Windows.
492
493Note: this function is intended for low-level I/O and must be applied
494to a file descriptor as returned by \function{open()} or
495\function{pipe()}. To write a ``file object'' returned by the
496built-in function \function{open()} or by \function{popen()} or
497\function{fdopen()}, or \code{sys.stdout} or \code{sys.stderr}, use
498its \method{write()} method.
499\end{funcdesc}
500
501
502The following data items are available for use in constructing the
503\var{flags} parameter to the \function{open()} function.
504
505\begin{datadesc}{O_RDONLY}
506\dataline{O_WRONLY}
507\dataline{O_RDWR}
508\dataline{O_NDELAY}
509\dataline{O_NONBLOCK}
510\dataline{O_APPEND}
511\dataline{O_DSYNC}
512\dataline{O_RSYNC}
513\dataline{O_SYNC}
514\dataline{O_NOCTTY}
515\dataline{O_CREAT}
516\dataline{O_EXCL}
517\dataline{O_TRUNC}
518Options for the \var{flag} argument to the \function{open()} function.
519These can be bit-wise OR'd together.
520Availability: Macintosh, \UNIX{}, Windows.
521\end{datadesc}
522
Fred Drake3ac977e2000-08-11 20:19:51 +0000523\begin{datadesc}{O_BINARY}
524Option for the \var{flag} argument to the \function{open()} function.
525This can be bit-wise OR'd together with those listed above.
526Availability: Macintosh, Windows.
527% XXX need to check on the availability of this one.
528\end{datadesc}
529
Fred Drake215fe2f1999-02-02 19:02:35 +0000530
531\subsection{Files and Directories \label{os-file-dir}}
532
533\begin{funcdesc}{access}{path, mode}
Fred Drake38e5d272000-04-03 20:13:55 +0000534Check read/write/execute permissions for this process or existence of
535file \var{path}. \var{mode} should be \constant{F_OK} to test the
536existence of \var{path}, or it can be the inclusive OR of one or more
537of \constant{R_OK}, \constant{W_OK}, and \constant{X_OK} to test
538permissions. Return \code{1} if access is allowed, \code{0} if not.
539See the \UNIX{} man page \manpage{access}{2} for more information.
Fred Drake3ac977e2000-08-11 20:19:51 +0000540Availability: \UNIX{}, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000541\end{funcdesc}
542
Fred Drake38e5d272000-04-03 20:13:55 +0000543\begin{datadesc}{F_OK}
544 Value to pass as the \var{mode} parameter of \function{access()} to
545 test the existence of \var{path}.
546\end{datadesc}
547
548\begin{datadesc}{R_OK}
549 Value to include in the \var{mode} parameter of \function{access()}
550 to test the readability of \var{path}.
551\end{datadesc}
552
553\begin{datadesc}{W_OK}
554 Value to include in the \var{mode} parameter of \function{access()}
555 to test the writability of \var{path}.
556\end{datadesc}
557
558\begin{datadesc}{X_OK}
559 Value to include in the \var{mode} parameter of \function{access()}
560 to determine if \var{path} can be executed.
561\end{datadesc}
562
Fred Drake6db897c1999-07-12 16:49:30 +0000563\begin{funcdesc}{chdir}{path}
564\index{directory!changing}
565Change the current working directory to \var{path}.
566Availability: Macintosh, \UNIX{}, Windows.
567\end{funcdesc}
568
569\begin{funcdesc}{getcwd}{}
570Return a string representing the current working directory.
571Availability: Macintosh, \UNIX{}, Windows.
572\end{funcdesc}
573
Fred Drake215fe2f1999-02-02 19:02:35 +0000574\begin{funcdesc}{chmod}{path, mode}
575Change the mode of \var{path} to the numeric \var{mode}.
576Availability: \UNIX{}, Windows.
577\end{funcdesc}
578
579\begin{funcdesc}{chown}{path, uid, gid}
580Change the owner and group id of \var{path} to the numeric \var{uid}
581and \var{gid}.
582Availability: \UNIX{}.
583\end{funcdesc}
584
585\begin{funcdesc}{link}{src, dst}
586Create a hard link pointing to \var{src} named \var{dst}.
587Availability: \UNIX{}.
588\end{funcdesc}
589
590\begin{funcdesc}{listdir}{path}
591Return a list containing the names of the entries in the directory.
592The list is in arbitrary order. It does not include the special
593entries \code{'.'} and \code{'..'} even if they are present in the
594directory.
595Availability: Macintosh, \UNIX{}, Windows.
596\end{funcdesc}
597
598\begin{funcdesc}{lstat}{path}
599Like \function{stat()}, but do not follow symbolic links.
600Availability: \UNIX{}.
601\end{funcdesc}
602
603\begin{funcdesc}{mkfifo}{path\optional{, mode}}
604Create a FIFO (a named pipe) named \var{path} with numeric mode
605\var{mode}. The default \var{mode} is \code{0666} (octal). The current
606umask value is first masked out from the mode.
607Availability: \UNIX{}.
608
609FIFOs are pipes that can be accessed like regular files. FIFOs exist
610until they are deleted (for example with \function{os.unlink()}).
611Generally, FIFOs are used as rendezvous between ``client'' and
612``server'' type processes: the server opens the FIFO for reading, and
613the client opens it for writing. Note that \function{mkfifo()}
614doesn't open the FIFO --- it just creates the rendezvous point.
615\end{funcdesc}
616
617\begin{funcdesc}{mkdir}{path\optional{, mode}}
618Create a directory named \var{path} with numeric mode \var{mode}.
619The default \var{mode} is \code{0777} (octal). On some systems,
620\var{mode} is ignored. Where it is used, the current umask value is
621first masked out.
622Availability: Macintosh, \UNIX{}, Windows.
623\end{funcdesc}
624
625\begin{funcdesc}{makedirs}{path\optional{, mode}}
626\index{directory!creating}
627Recursive directory creation function. Like \function{mkdir()},
628but makes all intermediate-level directories needed to contain the
629leaf directory. Throws an \exception{error} exception if the leaf
630directory already exists or cannot be created. The default \var{mode}
631is \code{0777} (octal).
632\versionadded{1.5.2}
633\end{funcdesc}
634
Fred Drake88f6ca21999-12-15 19:39:04 +0000635\begin{funcdesc}{pathconf}{path, name}
Thomas Woutersf8316632000-07-16 19:01:10 +0000636Return system configuration information relevant to a named file.
Fred Drake88f6ca21999-12-15 19:39:04 +0000637\var{name} specifies the configuration value to retrieve; it may be a
638string which is the name of a defined system value; these names are
Fred Drake8ee679f2001-07-14 02:50:55 +0000639specified in a number of standards (\POSIX.1, \UNIX 95, \UNIX 98, and
Fred Drake88f6ca21999-12-15 19:39:04 +0000640others). Some platforms define additional names as well. The names
641known to the host operating system are given in the
642\code{pathconf_names} dictionary. For configuration variables not
643included in that mapping, passing an integer for \var{name} is also
644accepted.
645Availability: \UNIX{}.
646
647If \var{name} is a string and is not known, \exception{ValueError} is
648raised. If a specific value for \var{name} is not supported by the
649host system, even if it is included in \code{pathconf_names}, an
650\exception{OSError} is raised with \constant{errno.EINVAL} for the
651error number.
652\end{funcdesc}
653
654\begin{datadesc}{pathconf_names}
655Dictionary mapping names accepted by \function{pathconf()} and
656\function{fpathconf()} to the integer values defined for those names
657by the host operating system. This can be used to determine the set
658of names known to the system.
659Availability: \UNIX.
660\end{datadesc}
661
Fred Drake215fe2f1999-02-02 19:02:35 +0000662\begin{funcdesc}{readlink}{path}
663Return a string representing the path to which the symbolic link
Fred Drakedc9e7e42001-05-29 18:13:06 +0000664points. The result may be either an absolute or relative pathname; if
665it is relative, it may be converted to an absolute pathname using
666\code{os.path.join(os.path.dirname(\var{path}), \var{result})}.
Fred Drake215fe2f1999-02-02 19:02:35 +0000667Availability: \UNIX{}.
668\end{funcdesc}
669
670\begin{funcdesc}{remove}{path}
Fred Drakedc9e7e42001-05-29 18:13:06 +0000671Remove the file \var{path}. If \var{path} is a directory,
672\exception{OSError} is raised; see \function{rmdir()} below to remove
673a directory. This is identical to the \function{unlink()} function
674documented below. On Windows, attempting to remove a file that is in
675use causes an exception to be raised; on \UNIX, the directory entry is
676removed but the storage allocated to the file is not made available
677until the original file is no longer in use.
Fred Drake215fe2f1999-02-02 19:02:35 +0000678Availability: Macintosh, \UNIX{}, Windows.
679\end{funcdesc}
680
681\begin{funcdesc}{removedirs}{path}
682\index{directory!deleting}
683Recursive directory removal function. Works like
684\function{rmdir()} except that, if the leaf directory is
685successfully removed, directories corresponding to rightmost path
686segments will be pruned way until either the whole path is consumed or
687an error is raised (which is ignored, because it generally means that
688a parent directory is not empty). Throws an \exception{error}
689exception if the leaf directory could not be successfully removed.
690\versionadded{1.5.2}
691\end{funcdesc}
692
693\begin{funcdesc}{rename}{src, dst}
Fred Drakedc9e7e42001-05-29 18:13:06 +0000694Rename the file or directory \var{src} to \var{dst}. If \var{dst} is
695a directory, \exception{OSError} will be raised. On \UNIX, if
696\var{dst} exists and is a file, it will be removed silently if the
697user has permission. The operation may fail on some \UNIX{} flavors
Skip Montanarob9d973d2001-06-04 15:31:17 +0000698if \var{src} and \var{dst} are on different filesystems. If
Fred Drakedc9e7e42001-05-29 18:13:06 +0000699successful, the renaming will be an atomic operation (this is a
700\POSIX{} requirement). On Windows, if \var{dst} already exists,
701\exception{OSError} will be raised even if it is a file; there may be
702no way to implement an atomic rename when \var{dst} names an existing
703file.
Fred Drake215fe2f1999-02-02 19:02:35 +0000704Availability: Macintosh, \UNIX{}, Windows.
705\end{funcdesc}
706
707\begin{funcdesc}{renames}{old, new}
708Recursive directory or file renaming function.
709Works like \function{rename()}, except creation of any intermediate
710directories needed to make the new pathname good is attempted first.
711After the rename, directories corresponding to rightmost path segments
712of the old name will be pruned away using \function{removedirs()}.
713
714Note: this function can fail with the new directory structure made if
715you lack permissions needed to remove the leaf directory or file.
716\versionadded{1.5.2}
717\end{funcdesc}
718
719\begin{funcdesc}{rmdir}{path}
720Remove the directory \var{path}.
721Availability: Macintosh, \UNIX{}, Windows.
722\end{funcdesc}
723
724\begin{funcdesc}{stat}{path}
725Perform a \cfunction{stat()} system call on the given path. The
726return value is a tuple of at least 10 integers giving the most
727important (and portable) members of the \emph{stat} structure, in the
728order
729\code{st_mode},
730\code{st_ino},
731\code{st_dev},
732\code{st_nlink},
733\code{st_uid},
734\code{st_gid},
735\code{st_size},
736\code{st_atime},
737\code{st_mtime},
738\code{st_ctime}.
Fred Drake21c9df72000-10-14 05:46:11 +0000739More items may be added at the end by some implementations. Note that
Fred Drake8ee679f2001-07-14 02:50:55 +0000740on the Mac OS, the time values are floating point values, like all
741time values on the Mac OS.
742(On Windows, some items are filled with dummy values.)
Fred Drake215fe2f1999-02-02 19:02:35 +0000743Availability: Macintosh, \UNIX{}, Windows.
744
745Note: The standard module \refmodule{stat}\refstmodindex{stat} defines
746functions and constants that are useful for extracting information
747from a \ctype{stat} structure.
748\end{funcdesc}
749
750\begin{funcdesc}{statvfs}{path}
751Perform a \cfunction{statvfs()} system call on the given path. The
Guido van Rossum0c9608c1999-02-03 16:32:37 +0000752return value is a tuple of 10 integers giving the most common
Fred Drake215fe2f1999-02-02 19:02:35 +0000753members of the \ctype{statvfs} structure, in the order
754\code{f_bsize},
755\code{f_frsize},
756\code{f_blocks},
757\code{f_bfree},
758\code{f_bavail},
759\code{f_files},
760\code{f_ffree},
761\code{f_favail},
Fred Drake215fe2f1999-02-02 19:02:35 +0000762\code{f_flag},
763\code{f_namemax}.
764Availability: \UNIX{}.
765
766Note: The standard module \module{statvfs}\refstmodindex{statvfs}
767defines constants that are useful for extracting information
768from a \ctype{statvfs} structure.
769\end{funcdesc}
770
771\begin{funcdesc}{symlink}{src, dst}
772Create a symbolic link pointing to \var{src} named \var{dst}.
773Availability: \UNIX{}.
774\end{funcdesc}
775
Fred Drake18f7a451999-12-09 22:11:43 +0000776\begin{funcdesc}{tempnam}{\optional{dir\optional{, prefix}}}
777Return a unique path name that is reasonable for creating a temporary
778file. This will be an absolute path that names a potential directory
779entry in the directory \var{dir} or a common location for temporary
780files if \var{dir} is omitted or \code{None}. If given and not
781\code{None}, \var{prefix} is used to provide a short prefix to the
782filename. Applications are responsible for properly creating and
783managing files created using paths returned by \function{tempnam()};
784no automatic cleanup is provided.
Fred Drakeefaef132001-07-17 20:39:18 +0000785Availability: \UNIX, Windows.
Fred Drake18f7a451999-12-09 22:11:43 +0000786\end{funcdesc}
787
788\begin{funcdesc}{tmpnam}{}
789Return a unique path name that is reasonable for creating a temporary
790file. This will be an absolute path that names a potential directory
791entry in a common location for temporary files. Applications are
792responsible for properly creating and managing files created using
793paths returned by \function{tmpnam()}; no automatic cleanup is
794provided.
Fred Drakeefaef132001-07-17 20:39:18 +0000795Availability: \UNIX, Windows.
Fred Drake18f7a451999-12-09 22:11:43 +0000796\end{funcdesc}
797
798\begin{datadesc}{TMP_MAX}
799The maximum number of unique names that \function{tmpnam()} will
800generate before reusing names.
801\end{datadesc}
802
Fred Drake215fe2f1999-02-02 19:02:35 +0000803\begin{funcdesc}{unlink}{path}
804Remove the file \var{path}. This is the same function as
805\function{remove()}; the \function{unlink()} name is its traditional
806\UNIX{} name.
807Availability: Macintosh, \UNIX{}, Windows.
808\end{funcdesc}
809
Barry Warsaw93a8eac2000-05-01 16:18:22 +0000810\begin{funcdesc}{utime}{path, times}
811Set the access and modified times of the file specified by \var{path}.
812If \var{times} is \code{None}, then the file's access and modified
813times are set to the current time. Otherwise, \var{times} must be a
Fred Drakee06d0252000-05-02 17:29:35 +00008142-tuple of numbers, of the form \code{(\var{atime}, \var{mtime})}
815which is used to set the access and modified times, respectively.
Fred Drake4a152632000-10-19 05:33:46 +0000816\versionchanged[Added support for \code{None} for \var{times}]{2.0}
Fred Drake215fe2f1999-02-02 19:02:35 +0000817Availability: Macintosh, \UNIX{}, Windows.
818\end{funcdesc}
819
820
821\subsection{Process Management \label{os-process}}
822
Fred Drake18f7a451999-12-09 22:11:43 +0000823These functions may be used to create and manage processes.
Fred Drake215fe2f1999-02-02 19:02:35 +0000824
Fred Drake7be31152000-09-23 05:22:07 +0000825The various \function{exec*()} functions take a list of arguments for
826the new program loaded into the process. In each case, the first of
827these arguments is passed to the new program as its own name rather
828than as an argument a user may have typed on a command line. For the
829C programmer, this is the \code{argv[0]} passed to a program's
830\cfunction{main()}. For example, \samp{os.execv('/bin/echo', ['foo',
831'bar'])} will only print \samp{bar} on standard output; \samp{foo}
832will seem to be ignored.
833
Fred Drake215fe2f1999-02-02 19:02:35 +0000834
Fred Drake18f7a451999-12-09 22:11:43 +0000835\begin{funcdesc}{abort}{}
836Generate a \constant{SIGABRT} signal to the current process. On
837\UNIX, the default behavior is to produce a core dump; on Windows, the
838process immediately returns an exit code of \code{3}. Be aware that
839programs which use \function{signal.signal()} to register a handler
840for \constant{SIGABRT} will behave differently.
841Availability: \UNIX, Windows.
842\end{funcdesc}
843
Fred Drake215fe2f1999-02-02 19:02:35 +0000844\begin{funcdesc}{execl}{path, arg0, arg1, ...}
845This is equivalent to
846\samp{execv(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
847Availability: \UNIX{}, Windows.
848\end{funcdesc}
849
850\begin{funcdesc}{execle}{path, arg0, arg1, ..., env}
851This is equivalent to
852\samp{execve(\var{path}, (\var{arg0}, \var{arg1}, ...), \var{env})}.
853Availability: \UNIX{}, Windows.
854\end{funcdesc}
855
856\begin{funcdesc}{execlp}{path, arg0, arg1, ...}
857This is equivalent to
858\samp{execvp(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
859Availability: \UNIX{}, Windows.
860\end{funcdesc}
861
862\begin{funcdesc}{execv}{path, args}
863Execute the executable \var{path} with argument list \var{args},
Fred Drake907e76b2001-07-06 20:30:11 +0000864replacing the current process (the Python interpreter).
Fred Drake215fe2f1999-02-02 19:02:35 +0000865The argument list may be a tuple or list of strings.
866Availability: \UNIX{}, Windows.
867\end{funcdesc}
868
869\begin{funcdesc}{execve}{path, args, env}
870Execute the executable \var{path} with argument list \var{args},
Fred Drake907e76b2001-07-06 20:30:11 +0000871and environment \var{env}, replacing the current process (the Python
872interpreter).
Fred Drake215fe2f1999-02-02 19:02:35 +0000873The argument list may be a tuple or list of strings.
874The environment must be a dictionary mapping strings to strings.
875Availability: \UNIX{}, Windows.
876\end{funcdesc}
877
878\begin{funcdesc}{execvp}{path, args}
879This is like \samp{execv(\var{path}, \var{args})} but duplicates
880the shell's actions in searching for an executable file in a list of
881directories. The directory list is obtained from
882\code{environ['PATH']}.
883Availability: \UNIX{}, Windows.
884\end{funcdesc}
885
886\begin{funcdesc}{execvpe}{path, args, env}
887This is a cross between \function{execve()} and \function{execvp()}.
888The directory list is obtained from \code{\var{env}['PATH']}.
889Availability: \UNIX{}, Windows.
890\end{funcdesc}
891
892\begin{funcdesc}{_exit}{n}
893Exit to the system with status \var{n}, without calling cleanup
894handlers, flushing stdio buffers, etc.
895Availability: \UNIX{}, Windows.
896
897Note: the standard way to exit is \code{sys.exit(\var{n})}.
898\function{_exit()} should normally only be used in the child process
899after a \function{fork()}.
900\end{funcdesc}
901
902\begin{funcdesc}{fork}{}
903Fork a child process. Return \code{0} in the child, the child's
904process id in the parent.
905Availability: \UNIX{}.
906\end{funcdesc}
907
Fred Drakec82634c2000-06-28 17:27:48 +0000908\begin{funcdesc}{forkpty}{}
909Fork a child process, using a new pseudo-terminal as the child's
910controlling terminal. Return a pair of \code{(\var{pid}, \var{fd})},
911where \var{pid} is \code{0} in the child, the new child's process id
912in the parent, and \code{fd} is the file descriptor of the master end
913of the pseudo-terminal. For a more portable approach, use the
914\refmodule{pty} module.
915Availability: Some flavors of \UNIX{}
916\end{funcdesc}
917
Fred Drake215fe2f1999-02-02 19:02:35 +0000918\begin{funcdesc}{kill}{pid, sig}
919\index{process!killing}
920\index{process!signalling}
921Kill the process \var{pid} with signal \var{sig}.
922Availability: \UNIX{}.
923\end{funcdesc}
924
925\begin{funcdesc}{nice}{increment}
926Add \var{increment} to the process's ``niceness''. Return the new
927niceness.
928Availability: \UNIX{}.
929\end{funcdesc}
930
931\begin{funcdesc}{plock}{op}
932Lock program segments into memory. The value of \var{op}
933(defined in \code{<sys/lock.h>}) determines which segments are locked.
Fred Drake39063631999-02-26 14:05:02 +0000934Availability: \UNIX{}.
Fred Drake215fe2f1999-02-02 19:02:35 +0000935\end{funcdesc}
936
Fred Drake046f4d82001-06-11 15:21:48 +0000937\begin{funcdescni}{popen}{\unspecified}
938\funclineni{popen2}{\unspecified}
939\funclineni{popen3}{\unspecified}
940\funclineni{popen4}{\unspecified}
941Run child processes, returning opened pipes for communications. These
942functions are described in section \ref{os-newstreams}.
943\end{funcdescni}
944
Fred Drake215fe2f1999-02-02 19:02:35 +0000945\begin{funcdesc}{spawnv}{mode, path, args}
946Execute the program \var{path} in a new process, passing the arguments
947specified in \var{args} as command-line parameters. \var{args} may be
948a list or a tuple. \var{mode} is a magic operational constant. See
949the Visual \Cpp{} Runtime Library documentation for further
Fred Drake22702081999-07-02 14:01:03 +0000950information; the constants are exposed to the Python programmer as
951listed below.
Fred Drake15861b22000-02-29 05:19:38 +0000952Availability: \UNIX{}, Windows.
Fred Drake0b9bc202001-06-11 18:25:34 +0000953\versionadded{1.6}
Fred Drake215fe2f1999-02-02 19:02:35 +0000954\end{funcdesc}
955
956\begin{funcdesc}{spawnve}{mode, path, args, env}
957Execute the program \var{path} in a new process, passing the arguments
958specified in \var{args} as command-line parameters and the contents of
959the mapping \var{env} as the environment. \var{args} may be a list or
960a tuple. \var{mode} is a magic operational constant. See the Visual
Fred Drake22702081999-07-02 14:01:03 +0000961\Cpp{} Runtime Library documentation for further information; the
962constants are exposed to the Python programmer as listed below.
Fred Drake15861b22000-02-29 05:19:38 +0000963Availability: \UNIX{}, Windows.
Fred Drake0b9bc202001-06-11 18:25:34 +0000964\versionadded{1.6}
Fred Drake215fe2f1999-02-02 19:02:35 +0000965\end{funcdesc}
966
Fred Drake9329e5e1999-02-16 19:40:19 +0000967\begin{datadesc}{P_WAIT}
968\dataline{P_NOWAIT}
969\dataline{P_NOWAITO}
Fred Drake215fe2f1999-02-02 19:02:35 +0000970Possible values for the \var{mode} parameter to \function{spawnv()}
971and \function{spawnve()}.
Fred Drake15861b22000-02-29 05:19:38 +0000972Availability: \UNIX{}, Windows.
Fred Drake0b9bc202001-06-11 18:25:34 +0000973\versionadded{1.6}
Fred Drake15861b22000-02-29 05:19:38 +0000974\end{datadesc}
975
976\begin{datadesc}{P_OVERLAY}
977\dataline{P_DETACH}
978Possible values for the \var{mode} parameter to \function{spawnv()}
979and \function{spawnve()}. These are less portable than those listed
980above.
Fred Drake215fe2f1999-02-02 19:02:35 +0000981Availability: Windows.
Fred Drake0b9bc202001-06-11 18:25:34 +0000982\versionadded{1.6}
Fred Drake215fe2f1999-02-02 19:02:35 +0000983\end{datadesc}
984
Fred Drake4ce4f2e2000-09-29 04:15:19 +0000985\begin{funcdesc}{startfile}{path}
986Start a file with its associated application. This acts like
987double-clicking the file in Windows Explorer, or giving the file name
Fred Drake8ee679f2001-07-14 02:50:55 +0000988as an argument to the \program{start} command from the interactive
989command shell: the file is opened with whatever application (if any)
990its extension is associated.
Fred Drake4ce4f2e2000-09-29 04:15:19 +0000991
992\function{startfile()} returns as soon as the associated application
993is launched. There is no option to wait for the application to close,
994and no way to retrieve the application's exit status. The \var{path}
995parameter is relative to the current directory. If you want to use an
996absolute path, make sure the first character is not a slash
997(\character{/}); the underlying Win32 \cfunction{ShellExecute()}
998function doesn't work it is. Use the \function{os.path.normpath()}
999function to ensure that the path is properly encoded for Win32.
1000Availability: Windows.
1001\versionadded{2.0}
1002\end{funcdesc}
1003
Fred Drake215fe2f1999-02-02 19:02:35 +00001004\begin{funcdesc}{system}{command}
1005Execute the command (a string) in a subshell. This is implemented by
1006calling the Standard C function \cfunction{system()}, and has the
Fred Drakeec6baaf1999-04-21 18:13:31 +00001007same limitations. Changes to \code{posix.environ}, \code{sys.stdin},
Fred Drake215fe2f1999-02-02 19:02:35 +00001008etc.\ are not reflected in the environment of the executed command.
1009The return value is the exit status of the process encoded in the
Fred Drake7a621281999-06-10 15:07:05 +00001010format specified for \function{wait()}, except on Windows 95 and 98,
Fred Drakea88ef001999-06-18 19:11:25 +00001011where it is always \code{0}. Note that \POSIX{} does not specify the
1012meaning of the return value of the C \cfunction{system()} function,
1013so the return value of the Python function is system-dependent.
Fred Drake215fe2f1999-02-02 19:02:35 +00001014Availability: \UNIX{}, Windows.
1015\end{funcdesc}
1016
1017\begin{funcdesc}{times}{}
Fred Drake8ee679f2001-07-14 02:50:55 +00001018Return a 5-tuple of floating point numbers indicating accumulated
1019(processor or other)
Fred Drake215fe2f1999-02-02 19:02:35 +00001020times, in seconds. The items are: user time, system time, children's
1021user time, children's system time, and elapsed real time since a fixed
Fred Drakeec6baaf1999-04-21 18:13:31 +00001022point in the past, in that order. See the \UNIX{} manual page
1023\manpage{times}{2} or the corresponding Windows Platform API
1024documentation.
Fred Drake215fe2f1999-02-02 19:02:35 +00001025Availability: \UNIX{}, Windows.
1026\end{funcdesc}
1027
1028\begin{funcdesc}{wait}{}
1029Wait for completion of a child process, and return a tuple containing
1030its pid and exit status indication: a 16-bit number, whose low byte is
1031the signal number that killed the process, and whose high byte is the
1032exit status (if the signal number is zero); the high bit of the low
1033byte is set if a core file was produced.
1034Availability: \UNIX{}.
1035\end{funcdesc}
1036
1037\begin{funcdesc}{waitpid}{pid, options}
Fred Drake31e5e371999-08-13 13:36:33 +00001038Wait for completion of a child process given by process id \var{pid},
1039and return a tuple containing its process id and exit status
1040indication (encoded as for \function{wait()}). The semantics of the
1041call are affected by the value of the integer \var{options}, which
1042should be \code{0} for normal operation.
Fred Drake215fe2f1999-02-02 19:02:35 +00001043Availability: \UNIX{}.
Fred Drake31e5e371999-08-13 13:36:33 +00001044
1045If \var{pid} is greater than \code{0}, \function{waitpid()} requests
1046status information for that specific process. If \var{pid} is
1047\code{0}, the request is for the status of any child in the process
1048group of the current process. If \var{pid} is \code{-1}, the request
1049pertains to any child of the current process. If \var{pid} is less
1050than \code{-1}, status is requested for any process in the process
1051group \code{-\var{pid}} (the absolute value of \var{pid}).
Fred Drake215fe2f1999-02-02 19:02:35 +00001052\end{funcdesc}
1053
1054\begin{datadesc}{WNOHANG}
1055The option for \function{waitpid()} to avoid hanging if no child
1056process status is available immediately.
1057Availability: \UNIX{}.
1058\end{datadesc}
1059
Fred Drake38e5d272000-04-03 20:13:55 +00001060The following functions take a process status code as returned by
1061\function{system()}, \function{wait()}, or \function{waitpid()} as a
1062parameter. They may be used to determine the disposition of a
1063process.
Fred Drake215fe2f1999-02-02 19:02:35 +00001064
1065\begin{funcdesc}{WIFSTOPPED}{status}
1066Return true if the process has been stopped.
1067Availability: \UNIX{}.
1068\end{funcdesc}
1069
1070\begin{funcdesc}{WIFSIGNALED}{status}
1071Return true if the process exited due to a signal.
1072Availability: \UNIX{}.
1073\end{funcdesc}
1074
1075\begin{funcdesc}{WIFEXITED}{status}
1076Return true if the process exited using the \manpage{exit}{2} system
1077call.
1078Availability: \UNIX{}.
1079\end{funcdesc}
1080
1081\begin{funcdesc}{WEXITSTATUS}{status}
1082If \code{WIFEXITED(\var{status})} is true, return the integer
1083parameter to the \manpage{exit}{2} system call. Otherwise, the return
1084value is meaningless.
1085Availability: \UNIX{}.
1086\end{funcdesc}
1087
1088\begin{funcdesc}{WSTOPSIG}{status}
Fred Drake35c3ffd1999-03-04 14:08:10 +00001089Return the signal which caused the process to stop.
1090Availability: \UNIX{}.
1091\end{funcdesc}
1092
1093\begin{funcdesc}{WTERMSIG}{status}
Fred Drake215fe2f1999-02-02 19:02:35 +00001094Return the signal which caused the process to exit.
1095Availability: \UNIX{}.
1096\end{funcdesc}
1097
1098
Thomas Woutersf8316632000-07-16 19:01:10 +00001099\subsection{Miscellaneous System Information \label{os-path}}
Fred Drake88f6ca21999-12-15 19:39:04 +00001100
1101
1102\begin{funcdesc}{confstr}{name}
1103Return string-valued system configuration values.
1104\var{name} specifies the configuration value to retrieve; it may be a
1105string which is the name of a defined system value; these names are
Fred Drake8ee679f2001-07-14 02:50:55 +00001106specified in a number of standards (\POSIX, \UNIX 95, \UNIX 98, and
Fred Drake88f6ca21999-12-15 19:39:04 +00001107others). Some platforms define additional names as well. The names
1108known to the host operating system are given in the
1109\code{confstr_names} dictionary. For configuration variables not
1110included in that mapping, passing an integer for \var{name} is also
1111accepted.
1112Availability: \UNIX{}.
1113
1114If the configuration value specified by \var{name} isn't defined, the
1115empty string is returned.
1116
1117If \var{name} is a string and is not known, \exception{ValueError} is
1118raised. If a specific value for \var{name} is not supported by the
1119host system, even if it is included in \code{confstr_names}, an
1120\exception{OSError} is raised with \constant{errno.EINVAL} for the
1121error number.
1122\end{funcdesc}
1123
1124\begin{datadesc}{confstr_names}
1125Dictionary mapping names accepted by \function{confstr()} to the
1126integer values defined for those names by the host operating system.
1127This can be used to determine the set of names known to the system.
1128Availability: \UNIX.
1129\end{datadesc}
1130
1131\begin{funcdesc}{sysconf}{name}
1132Return integer-valued system configuration values.
1133If the configuration value specified by \var{name} isn't defined,
1134\code{-1} is returned. The comments regarding the \var{name}
1135parameter for \function{confstr()} apply here as well; the dictionary
1136that provides information on the known names is given by
1137\code{sysconf_names}.
1138Availability: \UNIX{}.
1139\end{funcdesc}
1140
1141\begin{datadesc}{sysconf_names}
1142Dictionary mapping names accepted by \function{sysconf()} to the
1143integer values defined for those names by the host operating system.
1144This can be used to determine the set of names known to the system.
1145Availability: \UNIX.
1146\end{datadesc}
1147
Fred Drake215fe2f1999-02-02 19:02:35 +00001148
1149The follow data values are used to support path manipulation
1150operations. These are defined for all platforms.
1151
1152Higher-level operations on pathnames are defined in the
1153\refmodule{os.path} module.
1154
1155
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001156\begin{datadesc}{curdir}
Fred Drake8ee679f2001-07-14 02:50:55 +00001157The constant string used by the operating system to refer to the current
1158directory.
Fred Drake907e76b2001-07-06 20:30:11 +00001159For example: \code{'.'} for \POSIX{} or \code{':'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001160\end{datadesc}
1161
1162\begin{datadesc}{pardir}
Fred Drake8ee679f2001-07-14 02:50:55 +00001163The constant string used by the operating system to refer to the parent
1164directory.
Fred Drake907e76b2001-07-06 20:30:11 +00001165For example: \code{'..'} for \POSIX{} or \code{'::'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001166\end{datadesc}
1167
1168\begin{datadesc}{sep}
Fred Drake8ee679f2001-07-14 02:50:55 +00001169The character used by the operating system to separate pathname components,
Fred Drake907e76b2001-07-06 20:30:11 +00001170for example, \character{/} for \POSIX{} or \character{:} for the
1171Macintosh. Note that knowing this is not sufficient to be able to
1172parse or concatenate pathnames --- use \function{os.path.split()} and
Fred Drake1a3c2a01998-08-06 15:18:23 +00001173\function{os.path.join()} --- but it is occasionally useful.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001174\end{datadesc}
1175
Guido van Rossumb2afc811997-08-29 22:37:44 +00001176\begin{datadesc}{altsep}
Fred Drake8ee679f2001-07-14 02:50:55 +00001177An alternative character used by the operating system to separate pathname
1178components, or \code{None} if only one separator character exists. This is
1179set to \character{/} on DOS and Windows systems where \code{sep} is a
1180backslash.
Guido van Rossumb2afc811997-08-29 22:37:44 +00001181\end{datadesc}
1182
Guido van Rossum470be141995-03-17 16:07:09 +00001183\begin{datadesc}{pathsep}
Fred Drake8ee679f2001-07-14 02:50:55 +00001184The character conventionally used by the operating system to separate
1185search patch components (as in \envvar{PATH}), such as \character{:} for
1186\POSIX{} or \character{;} for DOS and Windows.
Guido van Rossum9c59ce91998-06-30 15:54:27 +00001187\end{datadesc}
1188
Guido van Rossum470be141995-03-17 16:07:09 +00001189\begin{datadesc}{defpath}
Fred Drake1a3c2a01998-08-06 15:18:23 +00001190The default search path used by \function{exec*p*()} if the environment
Guido van Rossum470be141995-03-17 16:07:09 +00001191doesn't have a \code{'PATH'} key.
1192\end{datadesc}
1193
Fred Drake215fe2f1999-02-02 19:02:35 +00001194\begin{datadesc}{linesep}
1195The string used to separate (or, rather, terminate) lines on the
Fred Drake907e76b2001-07-06 20:30:11 +00001196current platform. This may be a single character, such as \code{'\e
Fred Drake8ee679f2001-07-14 02:50:55 +00001197n'} for \POSIX{} or \code{'\e r'} for the Mac OS, or multiple characters,
1198for example, \code{'\e r\e n'} for DOS and Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +00001199\end{datadesc}