blob: aa57217832bcdc8056d4da6afc4b1fee1c3f4f4e [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.
Fred Drake215fe2f1999-02-02 19:02:35 +000062\end{excdesc}
Guido van Rossum470be141995-03-17 16:07:09 +000063
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000064\begin{datadesc}{name}
Fred Drake8ee679f2001-07-14 02:50:55 +000065The name of the operating system dependent module imported. The
Fred Drake6995bb62001-11-29 20:48:44 +000066following names have currently been registered: \code{'posix'},
Martin v. Löwis36a4d8c2002-10-10 18:24:54 +000067\code{'nt'}, \code{'mac'}, \code{'os2'}, \code{'ce'},
Fred Drake6995bb62001-11-29 20:48:44 +000068\code{'java'}, \code{'riscos'}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000069\end{datadesc}
70
71\begin{datadesc}{path}
Fred Drake8ee679f2001-07-14 02:50:55 +000072The corresponding operating system dependent standard module for pathname
Fred Drake907e76b2001-07-06 20:30:11 +000073operations, such as \module{posixpath} or \module{macpath}. Thus,
74given the proper imports, \code{os.path.split(\var{file})} is
75equivalent to but more portable than
76\code{posixpath.split(\var{file})}. Note that this is also an
77importable module: it may be imported directly as
Fred Drake215fe2f1999-02-02 19:02:35 +000078\refmodule{os.path}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000079\end{datadesc}
80
Fred Drake215fe2f1999-02-02 19:02:35 +000081
82
83\subsection{Process Parameters \label{os-procinfo}}
84
85These functions and data items provide information and operate on the
86current process and user.
87
Fred Drake215fe2f1999-02-02 19:02:35 +000088\begin{datadesc}{environ}
Fred Drake0e1de8b1999-04-29 12:57:32 +000089A mapping object representing the string environment. For example,
90\code{environ['HOME']} is the pathname of your home directory (on some
91platforms), and is equivalent to \code{getenv("HOME")} in C.
Fred Drake215fe2f1999-02-02 19:02:35 +000092
93If the platform supports the \function{putenv()} function, this
94mapping may be used to modify the environment as well as query the
95environment. \function{putenv()} will be called automatically when
96the mapping is modified.
97
98If \function{putenv()} is not provided, this mapping may be passed to
Tim Petersab034fa2002-02-01 11:27:43 +000099the appropriate process-creation functions to cause child processes to
Fred Drake215fe2f1999-02-02 19:02:35 +0000100use a modified environment.
101\end{datadesc}
102
Fred Drake6db897c1999-07-12 16:49:30 +0000103\begin{funcdescni}{chdir}{path}
Fred Drakee19a5bc2002-04-15 19:46:40 +0000104\funclineni{fchdir}{fd}
Fred Drake6db897c1999-07-12 16:49:30 +0000105\funclineni{getcwd}{}
106These functions are described in ``Files and Directories'' (section
107\ref{os-file-dir}).
108\end{funcdescni}
Fred Drake215fe2f1999-02-02 19:02:35 +0000109
Fred Drake18f7a451999-12-09 22:11:43 +0000110\begin{funcdesc}{ctermid}{}
111Return the filename corresponding to the controlling terminal of the
112process.
Fred Drakec37b65e2001-11-28 07:26:15 +0000113Availability: \UNIX.
Fred Drake18f7a451999-12-09 22:11:43 +0000114\end{funcdesc}
115
Fred Drake215fe2f1999-02-02 19:02:35 +0000116\begin{funcdesc}{getegid}{}
Fred Draked3e66782002-04-26 20:59:40 +0000117Return the effective group id of the current process. This
118corresponds to the `set id' bit on the file being executed in the
119current process.
Fred Drakec37b65e2001-11-28 07:26:15 +0000120Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000121\end{funcdesc}
122
123\begin{funcdesc}{geteuid}{}
Fred Drake6b330ba81999-05-25 13:42:26 +0000124\index{user!effective id}
Fred Drake215fe2f1999-02-02 19:02:35 +0000125Return the current process' effective user id.
Fred Drakec37b65e2001-11-28 07:26:15 +0000126Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000127\end{funcdesc}
128
129\begin{funcdesc}{getgid}{}
Fred Drake6b330ba81999-05-25 13:42:26 +0000130\index{process!group}
Fred Draked3e66782002-04-26 20:59:40 +0000131Return the real group id of the current process.
Fred Drakec37b65e2001-11-28 07:26:15 +0000132Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000133\end{funcdesc}
134
Fred Drake88f6ca21999-12-15 19:39:04 +0000135\begin{funcdesc}{getgroups}{}
136Return list of supplemental group ids associated with the current
137process.
Fred Drakec37b65e2001-11-28 07:26:15 +0000138Availability: \UNIX.
Fred Drake88f6ca21999-12-15 19:39:04 +0000139\end{funcdesc}
140
141\begin{funcdesc}{getlogin}{}
Jeremy Hylton403e3512002-07-24 15:32:25 +0000142Return the name of the user logged in on the controlling terminal of
143the process. For most purposes, it is more useful to use the
Andrew M. Kuchling4b373642003-02-03 15:36:26 +0000144environment variable \envvar{LOGNAME} to find out who the user is,
145or \code{pwd.getpwuid(os.getuid())[0]} to get the login name
146of the currently effective user ID.
Fred Drakec37b65e2001-11-28 07:26:15 +0000147Availability: \UNIX.
Fred Drake88f6ca21999-12-15 19:39:04 +0000148\end{funcdesc}
149
Martin v. Löwis606edc12002-06-13 21:09:11 +0000150\begin{funcdesc}{getpgid}{pid}
151Return the process group id of the process with process id \var{pid}.
152If \var{pid} is 0, the process group id of the current process is
153returned. Availability: \UNIX.
Neal Norwitzcc5c6942002-06-13 21:19:25 +0000154\versionadded{2.3}
Martin v. Löwis606edc12002-06-13 21:09:11 +0000155\end{funcdesc}
156
Fred Drake215fe2f1999-02-02 19:02:35 +0000157\begin{funcdesc}{getpgrp}{}
158\index{process!group}
Fred Draked3e66782002-04-26 20:59:40 +0000159Return the id of the current process group.
Fred Drakec37b65e2001-11-28 07:26:15 +0000160Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000161\end{funcdesc}
162
163\begin{funcdesc}{getpid}{}
164\index{process!id}
165Return the current process id.
Fred Drakec37b65e2001-11-28 07:26:15 +0000166Availability: \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000167\end{funcdesc}
168
169\begin{funcdesc}{getppid}{}
170\index{process!id of parent}
171Return the parent's process id.
Fred Drakec37b65e2001-11-28 07:26:15 +0000172Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000173\end{funcdesc}
174
175\begin{funcdesc}{getuid}{}
Fred Drake6b330ba81999-05-25 13:42:26 +0000176\index{user!id}
Fred Drake215fe2f1999-02-02 19:02:35 +0000177Return the current process' user id.
Fred Drakec37b65e2001-11-28 07:26:15 +0000178Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000179\end{funcdesc}
180
Fred Drake81e142b2001-05-31 20:27:46 +0000181\begin{funcdesc}{getenv}{varname\optional{, value}}
182Return the value of the environment variable \var{varname} if it
183exists, or \var{value} if it doesn't. \var{value} defaults to
184\code{None}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000185Availability: most flavors of \UNIX, Windows.
Fred Drake81e142b2001-05-31 20:27:46 +0000186\end{funcdesc}
187
Fred Drake215fe2f1999-02-02 19:02:35 +0000188\begin{funcdesc}{putenv}{varname, value}
189\index{environment variables!setting}
190Set the environment variable named \var{varname} to the string
191\var{value}. Such changes to the environment affect subprocesses
192started with \function{os.system()}, \function{popen()} or
193\function{fork()} and \function{execv()}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000194Availability: most flavors of \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000195
196When \function{putenv()} is
197supported, assignments to items in \code{os.environ} are automatically
198translated into corresponding calls to \function{putenv()}; however,
199calls to \function{putenv()} don't update \code{os.environ}, so it is
Tim Petersab034fa2002-02-01 11:27:43 +0000200actually preferable to assign to items of \code{os.environ}.
Fred Drake215fe2f1999-02-02 19:02:35 +0000201\end{funcdesc}
202
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +0000203\begin{funcdesc}{setegid}{egid}
204Set the current process's effective group id.
Fred Drakec37b65e2001-11-28 07:26:15 +0000205Availability: \UNIX.
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +0000206\end{funcdesc}
207
208\begin{funcdesc}{seteuid}{euid}
209Set the current process's effective user id.
Fred Drakec37b65e2001-11-28 07:26:15 +0000210Availability: \UNIX.
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +0000211\end{funcdesc}
212
Fred Drake215fe2f1999-02-02 19:02:35 +0000213\begin{funcdesc}{setgid}{gid}
214Set the current process' group id.
Fred Drakec37b65e2001-11-28 07:26:15 +0000215Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000216\end{funcdesc}
217
Martin v. Löwis61c5edf2001-10-18 04:06:00 +0000218\begin{funcdesc}{setgroups}{groups}
Martin v. Löwisc4051332001-10-18 14:07:12 +0000219Set the list of supplemental group ids associated with the current
220process to \var{groups}. \var{groups} must be a sequence, and each
221element must be an integer identifying a group. This operation is
222typical available only to the superuser.
Fred Drakec37b65e2001-11-28 07:26:15 +0000223Availability: \UNIX.
Martin v. Löwis61c5edf2001-10-18 04:06:00 +0000224\versionadded{2.2}
225\end{funcdesc}
226
Fred Drake215fe2f1999-02-02 19:02:35 +0000227\begin{funcdesc}{setpgrp}{}
228Calls the system call \cfunction{setpgrp()} or \cfunction{setpgrp(0,
2290)} depending on which version is implemented (if any). See the
230\UNIX{} manual for the semantics.
Fred Drakec37b65e2001-11-28 07:26:15 +0000231Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000232\end{funcdesc}
233
Fred Draked3e66782002-04-26 20:59:40 +0000234\begin{funcdesc}{setpgid}{pid, pgrp} Calls the system call
235\cfunction{setpgid()} to set the process group id of the process with
236id \var{pid} to the process group with id \var{pgrp}. See the \UNIX{}
237manual for the semantics.
Fred Drakec37b65e2001-11-28 07:26:15 +0000238Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000239\end{funcdesc}
240
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +0000241\begin{funcdesc}{setreuid}{ruid, euid}
242Set the current process's real and effective user ids.
Fred Drakec37b65e2001-11-28 07:26:15 +0000243Availability: \UNIX.
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +0000244\end{funcdesc}
245
246\begin{funcdesc}{setregid}{rgid, egid}
247Set the current process's real and effective group ids.
Fred Drakec37b65e2001-11-28 07:26:15 +0000248Availability: \UNIX.
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +0000249\end{funcdesc}
250
Fred Drake215fe2f1999-02-02 19:02:35 +0000251\begin{funcdesc}{setsid}{}
252Calls the system call \cfunction{setsid()}. See the \UNIX{} manual
253for the semantics.
Fred Drakec37b65e2001-11-28 07:26:15 +0000254Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000255\end{funcdesc}
256
257\begin{funcdesc}{setuid}{uid}
Fred Drake6b330ba81999-05-25 13:42:26 +0000258\index{user!id, setting}
Fred Drake215fe2f1999-02-02 19:02:35 +0000259Set the current process' user id.
Fred Drakec37b65e2001-11-28 07:26:15 +0000260Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000261\end{funcdesc}
262
263% placed in this section since it relates to errno.... a little weak ;-(
264\begin{funcdesc}{strerror}{code}
265Return the error message corresponding to the error code in
266\var{code}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000267Availability: \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000268\end{funcdesc}
269
270\begin{funcdesc}{umask}{mask}
271Set the current numeric umask and returns the previous umask.
Fred Drakec37b65e2001-11-28 07:26:15 +0000272Availability: \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000273\end{funcdesc}
274
275\begin{funcdesc}{uname}{}
276Return a 5-tuple containing information identifying the current
277operating system. The tuple contains 5 strings:
278\code{(\var{sysname}, \var{nodename}, \var{release}, \var{version},
279\var{machine})}. Some systems truncate the nodename to 8
280characters or to the leading component; a better way to get the
281hostname is \function{socket.gethostname()}
282\withsubitem{(in module socket)}{\ttindex{gethostname()}}
283or even
284\withsubitem{(in module socket)}{\ttindex{gethostbyaddr()}}
285\code{socket.gethostbyaddr(socket.gethostname())}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000286Availability: recent flavors of \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000287\end{funcdesc}
288
289
290
291\subsection{File Object Creation \label{os-newstreams}}
292
293These functions create new file objects.
294
295
296\begin{funcdesc}{fdopen}{fd\optional{, mode\optional{, bufsize}}}
297Return an open file object connected to the file descriptor \var{fd}.
Fred Drake8c9fc001999-08-05 13:41:31 +0000298\index{I/O control!buffering}
Fred Drake215fe2f1999-02-02 19:02:35 +0000299The \var{mode} and \var{bufsize} arguments have the same meaning as
300the corresponding arguments to the built-in \function{open()}
301function.
Fred Drakec37b65e2001-11-28 07:26:15 +0000302Availability: Macintosh, \UNIX, Windows.
Thomas Heller5b470e02002-11-07 16:33:44 +0000303
304\versionchanged[When specified, the \var{mode} argument must now start
Fred Drakeb5f41de2002-11-07 17:13:03 +0000305 with one of the letters \character{r}, \character{w}, or \character{a},
306 otherwise a \exception{ValueError} is raised]{2.3}
Fred Drake215fe2f1999-02-02 19:02:35 +0000307\end{funcdesc}
308
309\begin{funcdesc}{popen}{command\optional{, mode\optional{, bufsize}}}
310Open a pipe to or from \var{command}. The return value is an open
311file object connected to the pipe, which can be read or written
312depending on whether \var{mode} is \code{'r'} (default) or \code{'w'}.
313The \var{bufsize} argument has the same meaning as the corresponding
314argument to the built-in \function{open()} function. The exit status of
315the command (encoded in the format specified for \function{wait()}) is
316available as the return value of the \method{close()} method of the file
317object, except that when the exit status is zero (termination without
Fred Drake1319e3e2000-10-03 17:14:27 +0000318errors), \code{None} is returned.
Fred Drakec37b65e2001-11-28 07:26:15 +0000319Availability: \UNIX, Windows.
Fred Drakec71c23e2000-10-04 13:57:27 +0000320
321\versionchanged[This function worked unreliably under Windows in
322 earlier versions of Python. This was due to the use of the
323 \cfunction{_popen()} function from the libraries provided with
324 Windows. Newer versions of Python do not use the broken
325 implementation from the Windows libraries]{2.0}
Fred Drake215fe2f1999-02-02 19:02:35 +0000326\end{funcdesc}
327
Fred Drake18f7a451999-12-09 22:11:43 +0000328\begin{funcdesc}{tmpfile}{}
Guido van Rossumdb9198a2002-06-10 19:23:22 +0000329Return a new file object opened in update mode (\samp{w+b}). The file
Fred Drake18f7a451999-12-09 22:11:43 +0000330has no directory entries associated with it and will be automatically
331deleted once there are no file descriptors for the file.
Fred Drakec37b65e2001-11-28 07:26:15 +0000332Availability: \UNIX, Windows.
Fred Drake18f7a451999-12-09 22:11:43 +0000333\end{funcdesc}
Fred Drake215fe2f1999-02-02 19:02:35 +0000334
335
Fred Drake8a9db992000-09-28 20:27:51 +0000336For each of these \function{popen()} variants, if \var{bufsize} is
337specified, it specifies the buffer size for the I/O pipes.
338\var{mode}, if provided, should be the string \code{'b'} or
339\code{'t'}; on Windows this is needed to determine whether the file
340objects should be opened in binary or text mode. The default value
341for \var{mode} is \code{'t'}.
342
Fred Drake098d7fa2001-09-11 19:56:51 +0000343These methods do not make it possible to retrieve the return code from
344the child processes. The only way to control the input and output
345streams and also retrieve the return codes is to use the
346\class{Popen3} and \class{Popen4} classes from the \refmodule{popen2}
347module; these are only available on \UNIX.
348
Fred Drake08d10f92002-12-06 16:45:05 +0000349For a discussion of possible deadlock conditions related to the use
Fred Drake9ea01d42002-06-18 20:30:37 +0000350of these functions, see ``\ulink{Flow Control
351Issues}{popen2-flow-control.html}''
352(section~\ref{popen2-flow-control}).
353
Fred Drake046f4d82001-06-11 15:21:48 +0000354\begin{funcdesc}{popen2}{cmd\optional{, mode\optional{, bufsize}}}
Fred Drake8a9db992000-09-28 20:27:51 +0000355Executes \var{cmd} as a sub-process. Returns the file objects
356\code{(\var{child_stdin}, \var{child_stdout})}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000357Availability: \UNIX, Windows.
Fred Drake8a9db992000-09-28 20:27:51 +0000358\versionadded{2.0}
359\end{funcdesc}
360
Fred Drake046f4d82001-06-11 15:21:48 +0000361\begin{funcdesc}{popen3}{cmd\optional{, mode\optional{, bufsize}}}
Fred Drake8a9db992000-09-28 20:27:51 +0000362Executes \var{cmd} as a sub-process. Returns the file objects
363\code{(\var{child_stdin}, \var{child_stdout}, \var{child_stderr})}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000364Availability: \UNIX, Windows.
Fred Drake8a9db992000-09-28 20:27:51 +0000365\versionadded{2.0}
366\end{funcdesc}
367
Fred Drake046f4d82001-06-11 15:21:48 +0000368\begin{funcdesc}{popen4}{cmd\optional{, mode\optional{, bufsize}}}
Fred Drake8a9db992000-09-28 20:27:51 +0000369Executes \var{cmd} as a sub-process. Returns the file objects
370\code{(\var{child_stdin}, \var{child_stdout_and_stderr})}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000371Availability: \UNIX, Windows.
Fred Drake8a9db992000-09-28 20:27:51 +0000372\versionadded{2.0}
373\end{funcdesc}
374
375This functionality is also available in the \refmodule{popen2} module
376using functions of the same names, but the return values of those
377functions have a different order.
378
379
Fred Drake215fe2f1999-02-02 19:02:35 +0000380\subsection{File Descriptor Operations \label{os-fd-ops}}
381
382These functions operate on I/O streams referred to
383using file descriptors.
384
385
386\begin{funcdesc}{close}{fd}
387Close file descriptor \var{fd}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000388Availability: Macintosh, \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000389
390Note: this function is intended for low-level I/O and must be applied
391to a file descriptor as returned by \function{open()} or
392\function{pipe()}. To close a ``file object'' returned by the
393built-in function \function{open()} or by \function{popen()} or
394\function{fdopen()}, use its \method{close()} method.
395\end{funcdesc}
396
397\begin{funcdesc}{dup}{fd}
398Return a duplicate of file descriptor \var{fd}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000399Availability: Macintosh, \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000400\end{funcdesc}
401
402\begin{funcdesc}{dup2}{fd, fd2}
403Duplicate file descriptor \var{fd} to \var{fd2}, closing the latter
404first if necessary.
Fred Drakec37b65e2001-11-28 07:26:15 +0000405Availability: \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000406\end{funcdesc}
407
Raymond Hettinger3cfdc342002-08-07 15:48:17 +0000408\begin{funcdesc}{fdatasync}{fd}
409Force write of file with filedescriptor \var{fd} to disk.
410Does not force update of metadata.
411Availability: \UNIX.
412\end{funcdesc}
413
Fred Drake88f6ca21999-12-15 19:39:04 +0000414\begin{funcdesc}{fpathconf}{fd, name}
Thomas Woutersf8316632000-07-16 19:01:10 +0000415Return system configuration information relevant to an open file.
Fred Drake88f6ca21999-12-15 19:39:04 +0000416\var{name} specifies the configuration value to retrieve; it may be a
417string which is the name of a defined system value; these names are
Fred Drakec37b65e2001-11-28 07:26:15 +0000418specified in a number of standards (\POSIX.1, \UNIX 95, \UNIX 98, and
Fred Drake88f6ca21999-12-15 19:39:04 +0000419others). Some platforms define additional names as well. The names
420known to the host operating system are given in the
421\code{pathconf_names} dictionary. For configuration variables not
422included in that mapping, passing an integer for \var{name} is also
423accepted.
Fred Drakec37b65e2001-11-28 07:26:15 +0000424Availability: \UNIX.
Fred Drake88f6ca21999-12-15 19:39:04 +0000425
426If \var{name} is a string and is not known, \exception{ValueError} is
427raised. If a specific value for \var{name} is not supported by the
428host system, even if it is included in \code{pathconf_names}, an
429\exception{OSError} is raised with \constant{errno.EINVAL} for the
430error number.
431\end{funcdesc}
432
Fred Drake215fe2f1999-02-02 19:02:35 +0000433\begin{funcdesc}{fstat}{fd}
434Return status for file descriptor \var{fd}, like \function{stat()}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000435Availability: \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000436\end{funcdesc}
437
438\begin{funcdesc}{fstatvfs}{fd}
439Return information about the filesystem containing the file associated
440with file descriptor \var{fd}, like \function{statvfs()}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000441Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000442\end{funcdesc}
443
Raymond Hettinger3cfdc342002-08-07 15:48:17 +0000444\begin{funcdesc}{fsync}{fd}
445Force write of file with filedescriptor \var{fd} to disk.
446Availability: \UNIX.
447\end{funcdesc}
448
Fred Drake215fe2f1999-02-02 19:02:35 +0000449\begin{funcdesc}{ftruncate}{fd, length}
Tim Petersab034fa2002-02-01 11:27:43 +0000450Truncate the file corresponding to file descriptor \var{fd},
Fred Drake215fe2f1999-02-02 19:02:35 +0000451so that it is at most \var{length} bytes in size.
Fred Drakec37b65e2001-11-28 07:26:15 +0000452Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000453\end{funcdesc}
454
Skip Montanarod3725212000-07-19 17:30:58 +0000455\begin{funcdesc}{isatty}{fd}
Fred Drake106c1a02002-04-23 15:58:02 +0000456Return \code{True} if the file descriptor \var{fd} is open and
457connected to a tty(-like) device, else \code{False}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000458Availability: \UNIX.
Skip Montanarod3725212000-07-19 17:30:58 +0000459\end{funcdesc}
460
Fred Drake215fe2f1999-02-02 19:02:35 +0000461\begin{funcdesc}{lseek}{fd, pos, how}
462Set the current position of file descriptor \var{fd} to position
463\var{pos}, modified by \var{how}: \code{0} to set the position
464relative to the beginning of the file; \code{1} to set it relative to
465the current position; \code{2} to set it relative to the end of the
466file.
Fred Drakec37b65e2001-11-28 07:26:15 +0000467Availability: Macintosh, \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000468\end{funcdesc}
469
470\begin{funcdesc}{open}{file, flags\optional{, mode}}
471Open the file \var{file} and set various flags according to
472\var{flags} and possibly its mode according to \var{mode}.
473The default \var{mode} is \code{0777} (octal), and the current umask
474value is first masked out. Return the file descriptor for the newly
475opened file.
Fred Drakec37b65e2001-11-28 07:26:15 +0000476Availability: Macintosh, \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000477
478For a description of the flag and mode values, see the C run-time
479documentation; flag constants (like \constant{O_RDONLY} and
480\constant{O_WRONLY}) are defined in this module too (see below).
481
482Note: this function is intended for low-level I/O. For normal usage,
483use the built-in function \function{open()}, which returns a ``file
484object'' with \method{read()} and \method{write()} methods (and many
485more).
486\end{funcdesc}
487
Fred Drakec82634c2000-06-28 17:27:48 +0000488\begin{funcdesc}{openpty}{}
489Open a new pseudo-terminal pair. Return a pair of file descriptors
490\code{(\var{master}, \var{slave})} for the pty and the tty,
491respectively. For a (slightly) more portable approach, use the
492\refmodule{pty}\refstmodindex{pty} module.
Fred Drakec37b65e2001-11-28 07:26:15 +0000493Availability: Some flavors of \UNIX.
Fred Drakec82634c2000-06-28 17:27:48 +0000494\end{funcdesc}
495
Fred Drake215fe2f1999-02-02 19:02:35 +0000496\begin{funcdesc}{pipe}{}
497Create a pipe. Return a pair of file descriptors \code{(\var{r},
498\var{w})} usable for reading and writing, respectively.
Fred Drakec37b65e2001-11-28 07:26:15 +0000499Availability: \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000500\end{funcdesc}
501
502\begin{funcdesc}{read}{fd, n}
503Read at most \var{n} bytes from file descriptor \var{fd}.
Fred Drakea65375c2002-05-01 03:31:42 +0000504Return a string containing the bytes read. If the end of the file
505referred to by \var{fd} has been reached, an empty string is
506returned.
Fred Drakec37b65e2001-11-28 07:26:15 +0000507Availability: Macintosh, \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000508
509Note: this function is intended for low-level I/O and must be applied
510to a file descriptor as returned by \function{open()} or
511\function{pipe()}. To read a ``file object'' returned by the
512built-in function \function{open()} or by \function{popen()} or
513\function{fdopen()}, or \code{sys.stdin}, use its
514\method{read()} or \method{readline()} methods.
515\end{funcdesc}
516
517\begin{funcdesc}{tcgetpgrp}{fd}
518Return the process group associated with the terminal given by
519\var{fd} (an open file descriptor as returned by \function{open()}).
Fred Drakec37b65e2001-11-28 07:26:15 +0000520Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000521\end{funcdesc}
522
523\begin{funcdesc}{tcsetpgrp}{fd, pg}
524Set the process group associated with the terminal given by
525\var{fd} (an open file descriptor as returned by \function{open()})
526to \var{pg}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000527Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000528\end{funcdesc}
529
530\begin{funcdesc}{ttyname}{fd}
531Return a string which specifies the terminal device associated with
532file-descriptor \var{fd}. If \var{fd} is not associated with a terminal
533device, an exception is raised.
Fred Drakec37b65e2001-11-28 07:26:15 +0000534Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000535\end{funcdesc}
536
537\begin{funcdesc}{write}{fd, str}
538Write the string \var{str} to file descriptor \var{fd}.
539Return the number of bytes actually written.
Fred Drakec37b65e2001-11-28 07:26:15 +0000540Availability: Macintosh, \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000541
542Note: this function is intended for low-level I/O and must be applied
543to a file descriptor as returned by \function{open()} or
544\function{pipe()}. To write a ``file object'' returned by the
545built-in function \function{open()} or by \function{popen()} or
546\function{fdopen()}, or \code{sys.stdout} or \code{sys.stderr}, use
547its \method{write()} method.
548\end{funcdesc}
549
550
551The following data items are available for use in constructing the
552\var{flags} parameter to the \function{open()} function.
553
554\begin{datadesc}{O_RDONLY}
555\dataline{O_WRONLY}
556\dataline{O_RDWR}
557\dataline{O_NDELAY}
558\dataline{O_NONBLOCK}
559\dataline{O_APPEND}
560\dataline{O_DSYNC}
561\dataline{O_RSYNC}
562\dataline{O_SYNC}
563\dataline{O_NOCTTY}
564\dataline{O_CREAT}
565\dataline{O_EXCL}
566\dataline{O_TRUNC}
567Options for the \var{flag} argument to the \function{open()} function.
568These can be bit-wise OR'd together.
Fred Drakec37b65e2001-11-28 07:26:15 +0000569Availability: Macintosh, \UNIX, Windows.
Tim Petersc48a3ca2002-01-30 05:49:46 +0000570% XXX O_NDELAY, O_NONBLOCK, O_DSYNC, O_RSYNC, O_SYNC, O_NOCTTY are not on Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000571\end{datadesc}
572
Fred Drake3ac977e2000-08-11 20:19:51 +0000573\begin{datadesc}{O_BINARY}
574Option for the \var{flag} argument to the \function{open()} function.
575This can be bit-wise OR'd together with those listed above.
576Availability: Macintosh, Windows.
577% XXX need to check on the availability of this one.
578\end{datadesc}
579
Tim Petersc48a3ca2002-01-30 05:49:46 +0000580\begin{datadesc}{O_NOINHERIT}
581\dataline{O_SHORT_LIVED}
582\dataline{O_TEMPORARY}
583\dataline{O_RANDOM}
584\dataline{O_SEQUENTIAL}
585\dataline{O_TEXT}
586Options for the \var{flag} argument to the \function{open()} function.
587These can be bit-wise OR'd together.
588Availability: Windows.
589\end{datadesc}
Fred Drake215fe2f1999-02-02 19:02:35 +0000590
591\subsection{Files and Directories \label{os-file-dir}}
592
593\begin{funcdesc}{access}{path, mode}
Fred Drake7f591242002-06-18 16:15:51 +0000594Use the real uid/gid to test for access to \var{path}. Note that most
595operations will use the effective uid/gid, therefore this routine can
596be used in a suid/sgid environment to test if the invoking user has the
597specified access to \var{path}. \var{mode} should be \constant{F_OK}
598to test the existence of \var{path}, or it can be the inclusive OR of
599one or more of \constant{R_OK}, \constant{W_OK}, and \constant{X_OK} to
600test permissions. Return \code{1} if access is allowed, \code{0} if not.
Fred Drake38e5d272000-04-03 20:13:55 +0000601See the \UNIX{} man page \manpage{access}{2} for more information.
Fred Drakec37b65e2001-11-28 07:26:15 +0000602Availability: \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000603\end{funcdesc}
604
Fred Drake38e5d272000-04-03 20:13:55 +0000605\begin{datadesc}{F_OK}
606 Value to pass as the \var{mode} parameter of \function{access()} to
607 test the existence of \var{path}.
608\end{datadesc}
609
610\begin{datadesc}{R_OK}
611 Value to include in the \var{mode} parameter of \function{access()}
612 to test the readability of \var{path}.
613\end{datadesc}
614
615\begin{datadesc}{W_OK}
616 Value to include in the \var{mode} parameter of \function{access()}
617 to test the writability of \var{path}.
618\end{datadesc}
619
620\begin{datadesc}{X_OK}
621 Value to include in the \var{mode} parameter of \function{access()}
622 to determine if \var{path} can be executed.
623\end{datadesc}
624
Fred Drake6db897c1999-07-12 16:49:30 +0000625\begin{funcdesc}{chdir}{path}
626\index{directory!changing}
627Change the current working directory to \var{path}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000628Availability: Macintosh, \UNIX, Windows.
Fred Drake6db897c1999-07-12 16:49:30 +0000629\end{funcdesc}
630
Fred Drake15498552002-04-15 19:41:27 +0000631\begin{funcdesc}{fchdir}{fd}
632Change the current working directory to the directory represented by
633the file descriptor \var{fd}. The descriptor must refer to an opened
634directory, not an open file.
635Availability: \UNIX.
636\versionadded{2.3}
637\end{funcdesc}
638
Fred Drake6db897c1999-07-12 16:49:30 +0000639\begin{funcdesc}{getcwd}{}
640Return a string representing the current working directory.
Fred Drakec37b65e2001-11-28 07:26:15 +0000641Availability: Macintosh, \UNIX, Windows.
Fred Drake6db897c1999-07-12 16:49:30 +0000642\end{funcdesc}
643
Martin v. Löwisa844f2d2002-10-05 09:46:48 +0000644\begin{funcdesc}{getcwdu}{}
645Return a Unicode object representing the current working directory.
646Availability: \UNIX, Windows.
647\versionadded{2.3}
648\end{funcdesc}
649
Martin v. Löwis244edc82001-10-04 22:44:26 +0000650\begin{funcdesc}{chroot}{path}
651Change the root directory of the current process to \var{path}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000652Availability: \UNIX.
Martin v. Löwis244edc82001-10-04 22:44:26 +0000653\versionadded{2.2}
654\end{funcdesc}
655
Fred Drake215fe2f1999-02-02 19:02:35 +0000656\begin{funcdesc}{chmod}{path, mode}
657Change the mode of \var{path} to the numeric \var{mode}.
Raymond Hettinger9f5b07d2003-01-06 13:31:26 +0000658\var{mode} may take one of the following values:
659\begin{itemize}
660 \item \code{S_ISUID}
661 \item \code{S_ISGID}
662 \item \code{S_ENFMT}
663 \item \code{S_ISVTX}
664 \item \code{S_IREAD}
665 \item \code{S_IWRITE}
666 \item \code{S_IEXEC}
667 \item \code{S_IRWXU}
668 \item \code{S_IRUSR}
669 \item \code{S_IWUSR}
670 \item \code{S_IXUSR}
671 \item \code{S_IRWXG}
672 \item \code{S_IRGRP}
673 \item \code{S_IWGRP}
674 \item \code{S_IXGRP}
675 \item \code{S_IRWXO}
676 \item \code{S_IROTH}
677 \item \code{S_IWOTH}
678 \item \code{S_IXOTH}
679\end{itemize}
Fred Drakec37b65e2001-11-28 07:26:15 +0000680Availability: \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000681\end{funcdesc}
682
683\begin{funcdesc}{chown}{path, uid, gid}
684Change the owner and group id of \var{path} to the numeric \var{uid}
685and \var{gid}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000686Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000687\end{funcdesc}
688
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +0000689\begin{funcdesc}{lchown}{path, uid, gid}
690Change the owner and group id of \var{path} to the numeric \var{uid}
691and gid. This function will not follow symbolic links.
692Availability: \UNIX.
693\versionadded{2.3}
694\end{funcdesc}
695
Fred Drake215fe2f1999-02-02 19:02:35 +0000696\begin{funcdesc}{link}{src, dst}
697Create a hard link pointing to \var{src} named \var{dst}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000698Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000699\end{funcdesc}
700
701\begin{funcdesc}{listdir}{path}
702Return a list containing the names of the entries in the directory.
703The list is in arbitrary order. It does not include the special
704entries \code{'.'} and \code{'..'} even if they are present in the
705directory.
Fred Drakec37b65e2001-11-28 07:26:15 +0000706Availability: Macintosh, \UNIX, Windows.
Martin v. Löwisa844f2d2002-10-05 09:46:48 +0000707
708\versionadded[On Windows NT/2k/XP, if \var{path} is a Unicode object,
709the result will be a list of Unicode objects.]{2.3}
Fred Drake215fe2f1999-02-02 19:02:35 +0000710\end{funcdesc}
711
712\begin{funcdesc}{lstat}{path}
713Like \function{stat()}, but do not follow symbolic links.
Fred Drakec37b65e2001-11-28 07:26:15 +0000714Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000715\end{funcdesc}
716
717\begin{funcdesc}{mkfifo}{path\optional{, mode}}
718Create a FIFO (a named pipe) named \var{path} with numeric mode
719\var{mode}. The default \var{mode} is \code{0666} (octal). The current
720umask value is first masked out from the mode.
Fred Drakec37b65e2001-11-28 07:26:15 +0000721Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000722
723FIFOs are pipes that can be accessed like regular files. FIFOs exist
724until they are deleted (for example with \function{os.unlink()}).
725Generally, FIFOs are used as rendezvous between ``client'' and
726``server'' type processes: the server opens the FIFO for reading, and
727the client opens it for writing. Note that \function{mkfifo()}
728doesn't open the FIFO --- it just creates the rendezvous point.
729\end{funcdesc}
730
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000731\begin{funcdesc}{mknod}{path\optional{, mode=0600, device}}
Martin v. Löwis06a83e92002-04-14 10:19:44 +0000732Create a filesystem node (file, device special file or named pipe)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000733named filename. \var{mode} specifies both the permissions to use and
734the type of node to be created, being combined (bitwise OR) with one
735of S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO (those constants are
736available in \module{stat}). For S_IFCHR and S_IFBLK, \var{device}
737defines the newly created device special file (probably using
738\function{os.makedev()}), otherwise it is ignored.
739
740\versionadded{2.3}
741\end{funcdesc}
742
743\begin{funcdesc}{major}{device}
744Extracts a device major number from a raw device number.
745
746\versionadded{2.3}
747\end{funcdesc}
748
749\begin{funcdesc}{minor}{device}
750Extracts a device minor number from a raw device number.
751
752\versionadded{2.3}
753\end{funcdesc}
754
755\begin{funcdesc}{makedev}{major, minor}
756Composes a raw device number from the major and minor device numbers.
Martin v. Löwis06a83e92002-04-14 10:19:44 +0000757
758\versionadded{2.3}
759\end{funcdesc}
760
Fred Drake215fe2f1999-02-02 19:02:35 +0000761\begin{funcdesc}{mkdir}{path\optional{, mode}}
762Create a directory named \var{path} with numeric mode \var{mode}.
763The default \var{mode} is \code{0777} (octal). On some systems,
764\var{mode} is ignored. Where it is used, the current umask value is
765first masked out.
Fred Drakec37b65e2001-11-28 07:26:15 +0000766Availability: Macintosh, \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000767\end{funcdesc}
768
769\begin{funcdesc}{makedirs}{path\optional{, mode}}
770\index{directory!creating}
771Recursive directory creation function. Like \function{mkdir()},
772but makes all intermediate-level directories needed to contain the
773leaf directory. Throws an \exception{error} exception if the leaf
774directory already exists or cannot be created. The default \var{mode}
Fred Drakebbf7a402001-09-28 16:14:18 +0000775is \code{0777} (octal). This function does not properly handle UNC
776paths (only relevant on Windows systems).
Fred Drake215fe2f1999-02-02 19:02:35 +0000777\versionadded{1.5.2}
778\end{funcdesc}
779
Fred Drake88f6ca21999-12-15 19:39:04 +0000780\begin{funcdesc}{pathconf}{path, name}
Thomas Woutersf8316632000-07-16 19:01:10 +0000781Return system configuration information relevant to a named file.
Fred Drake88f6ca21999-12-15 19:39:04 +0000782\var{name} specifies the configuration value to retrieve; it may be a
783string which is the name of a defined system value; these names are
Fred Drake8ee679f2001-07-14 02:50:55 +0000784specified in a number of standards (\POSIX.1, \UNIX 95, \UNIX 98, and
Fred Drake88f6ca21999-12-15 19:39:04 +0000785others). Some platforms define additional names as well. The names
786known to the host operating system are given in the
787\code{pathconf_names} dictionary. For configuration variables not
788included in that mapping, passing an integer for \var{name} is also
789accepted.
Fred Drakec37b65e2001-11-28 07:26:15 +0000790Availability: \UNIX.
Fred Drake88f6ca21999-12-15 19:39:04 +0000791
792If \var{name} is a string and is not known, \exception{ValueError} is
793raised. If a specific value for \var{name} is not supported by the
794host system, even if it is included in \code{pathconf_names}, an
795\exception{OSError} is raised with \constant{errno.EINVAL} for the
796error number.
797\end{funcdesc}
798
799\begin{datadesc}{pathconf_names}
800Dictionary mapping names accepted by \function{pathconf()} and
801\function{fpathconf()} to the integer values defined for those names
802by the host operating system. This can be used to determine the set
803of names known to the system.
804Availability: \UNIX.
805\end{datadesc}
806
Fred Drake215fe2f1999-02-02 19:02:35 +0000807\begin{funcdesc}{readlink}{path}
808Return a string representing the path to which the symbolic link
Fred Drakedc9e7e42001-05-29 18:13:06 +0000809points. The result may be either an absolute or relative pathname; if
810it is relative, it may be converted to an absolute pathname using
811\code{os.path.join(os.path.dirname(\var{path}), \var{result})}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000812Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000813\end{funcdesc}
814
815\begin{funcdesc}{remove}{path}
Fred Drakedc9e7e42001-05-29 18:13:06 +0000816Remove the file \var{path}. If \var{path} is a directory,
817\exception{OSError} is raised; see \function{rmdir()} below to remove
818a directory. This is identical to the \function{unlink()} function
819documented below. On Windows, attempting to remove a file that is in
820use causes an exception to be raised; on \UNIX, the directory entry is
821removed but the storage allocated to the file is not made available
822until the original file is no longer in use.
Fred Drakec37b65e2001-11-28 07:26:15 +0000823Availability: Macintosh, \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000824\end{funcdesc}
825
826\begin{funcdesc}{removedirs}{path}
827\index{directory!deleting}
Fred Drake2c22e852002-07-02 21:03:49 +0000828Removes directories recursively. Works like
Fred Drake215fe2f1999-02-02 19:02:35 +0000829\function{rmdir()} except that, if the leaf directory is
830successfully removed, directories corresponding to rightmost path
831segments will be pruned way until either the whole path is consumed or
832an error is raised (which is ignored, because it generally means that
833a parent directory is not empty). Throws an \exception{error}
834exception if the leaf directory could not be successfully removed.
835\versionadded{1.5.2}
836\end{funcdesc}
837
838\begin{funcdesc}{rename}{src, dst}
Fred Drakedc9e7e42001-05-29 18:13:06 +0000839Rename the file or directory \var{src} to \var{dst}. If \var{dst} is
840a directory, \exception{OSError} will be raised. On \UNIX, if
841\var{dst} exists and is a file, it will be removed silently if the
842user has permission. The operation may fail on some \UNIX{} flavors
Skip Montanarob9d973d2001-06-04 15:31:17 +0000843if \var{src} and \var{dst} are on different filesystems. If
Fred Drakedc9e7e42001-05-29 18:13:06 +0000844successful, the renaming will be an atomic operation (this is a
845\POSIX{} requirement). On Windows, if \var{dst} already exists,
846\exception{OSError} will be raised even if it is a file; there may be
847no way to implement an atomic rename when \var{dst} names an existing
848file.
Fred Drakec37b65e2001-11-28 07:26:15 +0000849Availability: Macintosh, \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000850\end{funcdesc}
851
852\begin{funcdesc}{renames}{old, new}
853Recursive directory or file renaming function.
854Works like \function{rename()}, except creation of any intermediate
855directories needed to make the new pathname good is attempted first.
856After the rename, directories corresponding to rightmost path segments
857of the old name will be pruned away using \function{removedirs()}.
858
859Note: this function can fail with the new directory structure made if
860you lack permissions needed to remove the leaf directory or file.
861\versionadded{1.5.2}
862\end{funcdesc}
863
864\begin{funcdesc}{rmdir}{path}
865Remove the directory \var{path}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000866Availability: Macintosh, \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000867\end{funcdesc}
868
869\begin{funcdesc}{stat}{path}
870Perform a \cfunction{stat()} system call on the given path. The
Fred Drake6995bb62001-11-29 20:48:44 +0000871return value is an object whose attributes correspond to the members of
872the \ctype{stat} structure, namely:
873\member{st_mode} (protection bits),
874\member{st_ino} (inode number),
875\member{st_dev} (device),
876\member{st_nlink} (number of hard links,
877\member{st_uid} (user ID of owner),
878\member{st_gid} (group ID of owner),
879\member{st_size} (size of file, in bytes),
880\member{st_atime} (time of most recent access),
881\member{st_mtime} (time of most recent content modification),
882\member{st_ctime}
883(time of most recent content modification or metadata change).
884
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000885\versionchanged [If \function{stat_float_times} returns true, the time
886values are floats, measuring seconds. Fractions of a second may be
887reported if the system supports that. On Mac OS, the times are always
888floats. See \function{stat_float_times} for further discussion. ]{2.3}
Martin v. Löwisa32c9942002-09-09 16:17:47 +0000889
Fred Drake6995bb62001-11-29 20:48:44 +0000890On some Unix systems (such as Linux), the following attributes may
891also be available:
892\member{st_blocks} (number of blocks allocated for file),
893\member{st_blksize} (filesystem blocksize),
894\member{st_rdev} (type of device if an inode device).
895
896On Mac OS systems, the following attributes may also be available:
897\member{st_rsize},
898\member{st_creator},
899\member{st_type}.
900
901On RISCOS systems, the following attributes are also available:
902\member{st_ftype} (file type),
903\member{st_attrs} (attributes),
904\member{st_obtype} (object type).
905
906For backward compatibility, the return value of \function{stat()} is
907also accessible as a tuple of at least 10 integers giving the most
908important (and portable) members of the \ctype{stat} structure, in the
Fred Drake215fe2f1999-02-02 19:02:35 +0000909order
Fred Drake6995bb62001-11-29 20:48:44 +0000910\member{st_mode},
911\member{st_ino},
912\member{st_dev},
913\member{st_nlink},
914\member{st_uid},
915\member{st_gid},
916\member{st_size},
917\member{st_atime},
918\member{st_mtime},
919\member{st_ctime}.
Martin v. Löwisa32c9942002-09-09 16:17:47 +0000920More items may be added at the end by some implementations.
Fred Drake6995bb62001-11-29 20:48:44 +0000921The standard module \refmodule{stat}\refstmodindex{stat} defines
922functions and constants that are useful for extracting information
923from a \ctype{stat} structure.
Fred Drake8ee679f2001-07-14 02:50:55 +0000924(On Windows, some items are filled with dummy values.)
Fred Drakec37b65e2001-11-28 07:26:15 +0000925Availability: Macintosh, \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000926
Fred Drake6995bb62001-11-29 20:48:44 +0000927\versionchanged
928[Added access to values as attributes of the returned object]{2.2}
Fred Drake215fe2f1999-02-02 19:02:35 +0000929\end{funcdesc}
930
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000931\begin{funcdesc}{stat_float_times}{\optional{newvalue}}
932Determine whether \class{stat_result} represents time stamps as float
933objects. If newval is True, future calls to stat() return floats, if
934it is False, future calls return ints. If newval is omitted, return
935the current setting.
936
937For compatibility with older Python versions, accessing
938\class{stat_result} as a tuple always returns integers. For
939compatibility with Python 2.2, accessing the time stamps by field name
940also returns integers. Applications that want to determine the
941fractions of a second in a time stamp can use this function to have
942time stamps represented as floats. Whether they will actually observe
943non-zero fractions depends on the system.
944
Neal Norwitz6d23b172003-01-05 22:20:51 +0000945Future Python releases will change the default of this setting;
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000946applications that cannot deal with floating point time stamps can then
947use this function to turn the feature off.
948
949It is recommended that this setting is only changed at program startup
950time in the \var{__main__} module; libraries should never change this
951setting. If an application uses a library that works incorrectly if
952floating point time stamps are processed, this application should turn
953the feature off until the library has been corrected.
954
955\end{funcdesc}
956
Fred Drake215fe2f1999-02-02 19:02:35 +0000957\begin{funcdesc}{statvfs}{path}
958Perform a \cfunction{statvfs()} system call on the given path. The
Fred Drake6995bb62001-11-29 20:48:44 +0000959return value is an object whose attributes describe the filesystem on
960the given path, and correspond to the members of the
961\ctype{statvfs} structure, namely:
962\member{f_frsize},
963\member{f_blocks},
964\member{f_bfree},
965\member{f_bavail},
966\member{f_files},
967\member{f_ffree},
968\member{f_favail},
969\member{f_flag},
970\member{f_namemax}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000971Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000972
Fred Drake6995bb62001-11-29 20:48:44 +0000973For backward compatibility, the return value is also accessible as a
974tuple whose values correspond to the attributes, in the order given above.
975The standard module \refmodule{statvfs}\refstmodindex{statvfs}
Fred Drake215fe2f1999-02-02 19:02:35 +0000976defines constants that are useful for extracting information
Fred Drake6995bb62001-11-29 20:48:44 +0000977from a \ctype{statvfs} structure when accessing it as a sequence; this
978remains useful when writing code that needs to work with versions of
979Python that don't support accessing the fields as attributes.
980
981\versionchanged
982[Added access to values as attributes of the returned object]{2.2}
Fred Drake215fe2f1999-02-02 19:02:35 +0000983\end{funcdesc}
984
985\begin{funcdesc}{symlink}{src, dst}
986Create a symbolic link pointing to \var{src} named \var{dst}.
Fred Drakec37b65e2001-11-28 07:26:15 +0000987Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +0000988\end{funcdesc}
989
Fred Drake18f7a451999-12-09 22:11:43 +0000990\begin{funcdesc}{tempnam}{\optional{dir\optional{, prefix}}}
991Return a unique path name that is reasonable for creating a temporary
992file. This will be an absolute path that names a potential directory
993entry in the directory \var{dir} or a common location for temporary
994files if \var{dir} is omitted or \code{None}. If given and not
995\code{None}, \var{prefix} is used to provide a short prefix to the
996filename. Applications are responsible for properly creating and
997managing files created using paths returned by \function{tempnam()};
998no automatic cleanup is provided.
Fred Drake4b9ed2f2002-11-12 22:07:11 +0000999On \UNIX, the environment variable \envvar{TMPDIR} overrides
1000\var{dir}, while on Windows the \envvar{TMP} is used. The specific
1001behavior of this function depends on the C library implementation;
1002some aspects are underspecified in system documentation.
Fred Drake938a8d72001-10-09 18:07:04 +00001003\warning{Use of \function{tempnam()} is vulnerable to symlink attacks;
1004consider using \function{tmpfile()} instead.}
Fred Drakeefaef132001-07-17 20:39:18 +00001005Availability: \UNIX, Windows.
Fred Drake18f7a451999-12-09 22:11:43 +00001006\end{funcdesc}
1007
1008\begin{funcdesc}{tmpnam}{}
1009Return a unique path name that is reasonable for creating a temporary
1010file. This will be an absolute path that names a potential directory
1011entry in a common location for temporary files. Applications are
1012responsible for properly creating and managing files created using
1013paths returned by \function{tmpnam()}; no automatic cleanup is
1014provided.
Fred Drake938a8d72001-10-09 18:07:04 +00001015\warning{Use of \function{tmpnam()} is vulnerable to symlink attacks;
1016consider using \function{tmpfile()} instead.}
Fred Drakeefaef132001-07-17 20:39:18 +00001017Availability: \UNIX, Windows.
Fred Drake18f7a451999-12-09 22:11:43 +00001018\end{funcdesc}
1019
1020\begin{datadesc}{TMP_MAX}
1021The maximum number of unique names that \function{tmpnam()} will
1022generate before reusing names.
1023\end{datadesc}
1024
Fred Drake215fe2f1999-02-02 19:02:35 +00001025\begin{funcdesc}{unlink}{path}
1026Remove the file \var{path}. This is the same function as
1027\function{remove()}; the \function{unlink()} name is its traditional
1028\UNIX{} name.
Fred Drakec37b65e2001-11-28 07:26:15 +00001029Availability: Macintosh, \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +00001030\end{funcdesc}
1031
Barry Warsaw93a8eac2000-05-01 16:18:22 +00001032\begin{funcdesc}{utime}{path, times}
1033Set the access and modified times of the file specified by \var{path}.
1034If \var{times} is \code{None}, then the file's access and modified
1035times are set to the current time. Otherwise, \var{times} must be a
Fred Drakee06d0252000-05-02 17:29:35 +000010362-tuple of numbers, of the form \code{(\var{atime}, \var{mtime})}
1037which is used to set the access and modified times, respectively.
Fred Drake4a152632000-10-19 05:33:46 +00001038\versionchanged[Added support for \code{None} for \var{times}]{2.0}
Fred Drakec37b65e2001-11-28 07:26:15 +00001039Availability: Macintosh, \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +00001040\end{funcdesc}
1041
1042
1043\subsection{Process Management \label{os-process}}
1044
Fred Drake18f7a451999-12-09 22:11:43 +00001045These functions may be used to create and manage processes.
Fred Drake215fe2f1999-02-02 19:02:35 +00001046
Fred Drake7be31152000-09-23 05:22:07 +00001047The various \function{exec*()} functions take a list of arguments for
1048the new program loaded into the process. In each case, the first of
1049these arguments is passed to the new program as its own name rather
1050than as an argument a user may have typed on a command line. For the
1051C programmer, this is the \code{argv[0]} passed to a program's
1052\cfunction{main()}. For example, \samp{os.execv('/bin/echo', ['foo',
1053'bar'])} will only print \samp{bar} on standard output; \samp{foo}
1054will seem to be ignored.
1055
Fred Drake215fe2f1999-02-02 19:02:35 +00001056
Fred Drake18f7a451999-12-09 22:11:43 +00001057\begin{funcdesc}{abort}{}
1058Generate a \constant{SIGABRT} signal to the current process. On
Tim Petersab034fa2002-02-01 11:27:43 +00001059\UNIX, the default behavior is to produce a core dump; on Windows, the
Fred Drake18f7a451999-12-09 22:11:43 +00001060process immediately returns an exit code of \code{3}. Be aware that
1061programs which use \function{signal.signal()} to register a handler
1062for \constant{SIGABRT} will behave differently.
1063Availability: \UNIX, Windows.
1064\end{funcdesc}
1065
Fred Drakedb7287c2001-10-18 18:58:30 +00001066\begin{funcdesc}{execl}{path, arg0, arg1, \moreargs}
1067\funcline{execle}{path, arg0, arg1, \moreargs, env}
1068\funcline{execlp}{file, arg0, arg1, \moreargs}
1069\funcline{execlpe}{file, arg0, arg1, \moreargs, env}
1070\funcline{execv}{path, args}
1071\funcline{execve}{path, args, env}
1072\funcline{execvp}{file, args}
1073\funcline{execvpe}{file, args, env}
1074These functions all execute a new program, replacing the current
1075process; they do not return. On \UNIX, the new executable is loaded
1076into the current process, and will have the same process ID as the
1077caller. Errors will be reported as \exception{OSError} exceptions.
Fred Drake215fe2f1999-02-02 19:02:35 +00001078
Fred Drakedb7287c2001-10-18 18:58:30 +00001079The \character{l} and \character{v} variants of the
1080\function{exec*()} functions differ in how command-line arguments are
1081passed. The \character{l} variants are perhaps the easiest to work
1082with if the number of parameters is fixed when the code is written;
1083the individual parameters simply become additional parameters to the
1084\function{execl*()} functions. The \character{v} variants are good
1085when the number of parameters is variable, with the arguments being
1086passed in a list or tuple as the \var{args} parameter. In either
1087case, the arguments to the child process must start with the name of
1088the command being run.
Fred Drake215fe2f1999-02-02 19:02:35 +00001089
Fred Drakedb7287c2001-10-18 18:58:30 +00001090The variants which include a \character{p} near the end
1091(\function{execlp()}, \function{execlpe()}, \function{execvp()},
1092and \function{execvpe()}) will use the \envvar{PATH} environment
1093variable to locate the program \var{file}. When the environment is
1094being replaced (using one of the \function{exec*e()} variants,
1095discussed in the next paragraph), the
1096new environment is used as the source of the \envvar{PATH} variable.
1097The other variants, \function{execl()}, \function{execle()},
1098\function{execv()}, and \function{execve()}, will not use the
1099\envvar{PATH} variable to locate the executable; \var{path} must
1100contain an appropriate absolute or relative path.
Fred Drake215fe2f1999-02-02 19:02:35 +00001101
Fred Drakedb7287c2001-10-18 18:58:30 +00001102For \function{execle()}, \function{execlpe()}, \function{execve()},
1103and \function{execvpe()} (note that these all end in \character{e}),
1104the \var{env} parameter must be a mapping which is used to define the
1105environment variables for the new process; the \function{execl()},
1106\function{execlp()}, \function{execv()}, and \function{execvp()}
1107all cause the new process to inherit the environment of the current
1108process.
1109Availability: \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +00001110\end{funcdesc}
1111
1112\begin{funcdesc}{_exit}{n}
1113Exit to the system with status \var{n}, without calling cleanup
1114handlers, flushing stdio buffers, etc.
Fred Drakec37b65e2001-11-28 07:26:15 +00001115Availability: \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +00001116
1117Note: the standard way to exit is \code{sys.exit(\var{n})}.
1118\function{_exit()} should normally only be used in the child process
1119after a \function{fork()}.
1120\end{funcdesc}
1121
Barry Warsawb6604b32003-01-07 22:43:25 +00001122The following exit codes are a defined, and can be used with
1123\function{_exit()}, although they are not required. These are
1124typically used for system programs written in Python, such as a
1125mail server's external command delivery program.
1126
1127\begin{datadesc}{EX_OK}
1128Exit code that means no error occurred.
1129Availability: \UNIX.
1130\versionadded{2.3}
1131\end{datadesc}
1132
1133\begin{datadesc}{EX_USAGE}
1134Exit code that means the command was used incorrectly, such as when
1135the wrong number of arguments are given.
1136Availability: \UNIX.
1137\versionadded{2.3}
1138\end{datadesc}
1139
1140\begin{datadesc}{EX_DATAERR}
1141Exit code that means the input data was incorrect.
1142Availability: \UNIX.
1143\versionadded{2.3}
1144\end{datadesc}
1145
1146\begin{datadesc}{EX_NOINPUT}
1147Exit code that means an input file did not exist or was not readable.
1148Availability: \UNIX.
1149\versionadded{2.3}
1150\end{datadesc}
1151
1152\begin{datadesc}{EX_NOUSER}
1153Exit code that means a specified user did not exist.
1154Availability: \UNIX.
1155\versionadded{2.3}
1156\end{datadesc}
1157
1158\begin{datadesc}{EX_NOHOST}
1159Exit code that means a specified host did not exist.
1160Availability: \UNIX.
1161\versionadded{2.3}
1162\end{datadesc}
1163
1164\begin{datadesc}{EX_UNAVAILABLE}
1165Exit code that means that a required service is unavailable.
1166Availability: \UNIX.
1167\versionadded{2.3}
1168\end{datadesc}
1169
1170\begin{datadesc}{EX_SOFTWARE}
1171Exit code that means an internal software error was detected.
1172Availability: \UNIX.
1173\versionadded{2.3}
1174\end{datadesc}
1175
1176\begin{datadesc}{EX_OSERR}
1177Exit code that means an operating system error was detected, such as
1178the inability to fork or create a pipe.
1179Availability: \UNIX.
1180\versionadded{2.3}
1181\end{datadesc}
1182
1183\begin{datadesc}{EX_OSFILE}
1184Exit code that means some system file did not exist, could not be
1185opened, or had some other kind of error.
1186Availability: \UNIX.
1187\versionadded{2.3}
1188\end{datadesc}
1189
1190\begin{datadesc}{EX_CANTCREAT}
1191Exit code that means a user specified output file could not be created.
1192Availability: \UNIX.
1193\versionadded{2.3}
1194\end{datadesc}
1195
1196\begin{datadesc}{EX_IOERR}
1197Exit code that means that an error occurred while doing I/O on some file.
1198Availability: \UNIX.
1199\versionadded{2.3}
1200\end{datadesc}
1201
1202\begin{datadesc}{EX_TEMPFAIL}
1203Exit code that means a temporary failure occurred. This indicates
1204something that may not really be an error, such as a network
1205connection that couldn't be made during a retryable operation.
1206Availability: \UNIX.
1207\versionadded{2.3}
1208\end{datadesc}
1209
1210\begin{datadesc}{EX_PROTOCOL}
1211Exit code that means that a protocol exchange was illegal, invalid, or
1212not understood.
1213Availability: \UNIX.
1214\versionadded{2.3}
1215\end{datadesc}
1216
1217\begin{datadesc}{EX_NOPERM}
1218Exit code that means that there were insufficient permissions to
1219perform the operation (but not intended for file system problems).
1220Availability: \UNIX.
1221\versionadded{2.3}
1222\end{datadesc}
1223
1224\begin{datadesc}{EX_CONFIG}
1225Exit code that means that some kind of configuration error occurred.
1226Availability: \UNIX.
1227\versionadded{2.3}
1228\end{datadesc}
1229
1230\begin{datadesc}{EX_NOTFOUND}
1231Exit code that means something like ``an entry was not found''.
1232Availability: \UNIX.
1233\versionadded{2.3}
1234\end{datadesc}
1235
Fred Drake215fe2f1999-02-02 19:02:35 +00001236\begin{funcdesc}{fork}{}
1237Fork a child process. Return \code{0} in the child, the child's
1238process id in the parent.
Fred Drakec37b65e2001-11-28 07:26:15 +00001239Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +00001240\end{funcdesc}
1241
Fred Drakec82634c2000-06-28 17:27:48 +00001242\begin{funcdesc}{forkpty}{}
1243Fork a child process, using a new pseudo-terminal as the child's
1244controlling terminal. Return a pair of \code{(\var{pid}, \var{fd})},
1245where \var{pid} is \code{0} in the child, the new child's process id
Fred Drake6995bb62001-11-29 20:48:44 +00001246in the parent, and \var{fd} is the file descriptor of the master end
Fred Drakec82634c2000-06-28 17:27:48 +00001247of the pseudo-terminal. For a more portable approach, use the
1248\refmodule{pty} module.
Fred Drakec37b65e2001-11-28 07:26:15 +00001249Availability: Some flavors of \UNIX.
Fred Drakec82634c2000-06-28 17:27:48 +00001250\end{funcdesc}
1251
Fred Drake215fe2f1999-02-02 19:02:35 +00001252\begin{funcdesc}{kill}{pid, sig}
1253\index{process!killing}
1254\index{process!signalling}
Fred Drake5c798312001-12-21 03:58:47 +00001255Kill the process \var{pid} with signal \var{sig}. Constants for the
1256specific signals available on the host platform are defined in the
1257\refmodule{signal} module.
Fred Drakec37b65e2001-11-28 07:26:15 +00001258Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +00001259\end{funcdesc}
1260
Martin v. Löwis33e94432002-12-27 10:21:19 +00001261\begin{funcdesc}{killpg}{pgid, sig}
1262\index{process!killing}
1263\index{process!signalling}
1264Kill the process group \var{pgid} with the signal \var{sig}.
1265Availability: \UNIX.
1266\versionadded{2.3}
1267\end{funcdesc}
1268
Fred Drake215fe2f1999-02-02 19:02:35 +00001269\begin{funcdesc}{nice}{increment}
1270Add \var{increment} to the process's ``niceness''. Return the new
1271niceness.
Fred Drakec37b65e2001-11-28 07:26:15 +00001272Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +00001273\end{funcdesc}
1274
1275\begin{funcdesc}{plock}{op}
1276Lock program segments into memory. The value of \var{op}
1277(defined in \code{<sys/lock.h>}) determines which segments are locked.
Fred Drakec37b65e2001-11-28 07:26:15 +00001278Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +00001279\end{funcdesc}
1280
Fred Drake046f4d82001-06-11 15:21:48 +00001281\begin{funcdescni}{popen}{\unspecified}
1282\funclineni{popen2}{\unspecified}
1283\funclineni{popen3}{\unspecified}
1284\funclineni{popen4}{\unspecified}
1285Run child processes, returning opened pipes for communications. These
1286functions are described in section \ref{os-newstreams}.
1287\end{funcdescni}
1288
Fred Drake739282d2001-08-16 21:21:28 +00001289\begin{funcdesc}{spawnl}{mode, path, \moreargs}
1290\funcline{spawnle}{mode, path, \moreargs, env}
Fred Drakedb7287c2001-10-18 18:58:30 +00001291\funcline{spawnlp}{mode, file, \moreargs}
1292\funcline{spawnlpe}{mode, file, \moreargs, env}
Fred Drake739282d2001-08-16 21:21:28 +00001293\funcline{spawnv}{mode, path, args}
1294\funcline{spawnve}{mode, path, args, env}
Fred Drakedb7287c2001-10-18 18:58:30 +00001295\funcline{spawnvp}{mode, file, args}
1296\funcline{spawnvpe}{mode, file, args, env}
Fred Drake739282d2001-08-16 21:21:28 +00001297Execute the program \var{path} in a new process. If \var{mode} is
1298\constant{P_NOWAIT}, this function returns the process ID of the new
Tim Petersb4041452001-12-06 23:37:17 +00001299process; if \var{mode} is \constant{P_WAIT}, returns the process's
Fred Drake739282d2001-08-16 21:21:28 +00001300exit code if it exits normally, or \code{-\var{signal}}, where
Fred Drake4dfb7a82002-04-01 23:30:47 +00001301\var{signal} is the signal that killed the process. On Windows, the
1302process ID will actually be the process handle, so can be used with
1303the \function{waitpid()} function.
Fred Drake215fe2f1999-02-02 19:02:35 +00001304
Fred Drake739282d2001-08-16 21:21:28 +00001305The \character{l} and \character{v} variants of the
1306\function{spawn*()} functions differ in how command-line arguments are
1307passed. The \character{l} variants are perhaps the easiest to work
1308with if the number of parameters is fixed when the code is written;
1309the individual parameters simply become additional parameters to the
1310\function{spawnl*()} functions. The \character{v} variants are good
1311when the number of parameters is variable, with the arguments being
1312passed in a list or tuple as the \var{args} parameter. In either
1313case, the arguments to the child process must start with the name of
1314the command being run.
1315
Fred Drakedb7287c2001-10-18 18:58:30 +00001316The variants which include a second \character{p} near the end
1317(\function{spawnlp()}, \function{spawnlpe()}, \function{spawnvp()},
1318and \function{spawnvpe()}) will use the \envvar{PATH} environment
1319variable to locate the program \var{file}. When the environment is
1320being replaced (using one of the \function{spawn*e()} variants,
1321discussed in the next paragraph), the new environment is used as the
1322source of the \envvar{PATH} variable. The other variants,
1323\function{spawnl()}, \function{spawnle()}, \function{spawnv()}, and
1324\function{spawnve()}, will not use the \envvar{PATH} variable to
1325locate the executable; \var{path} must contain an appropriate absolute
1326or relative path.
1327
1328For \function{spawnle()}, \function{spawnlpe()}, \function{spawnve()},
1329and \function{spawnvpe()} (note that these all end in \character{e}),
1330the \var{env} parameter must be a mapping which is used to define the
1331environment variables for the new process; the \function{spawnl()},
1332\function{spawnlp()}, \function{spawnv()}, and \function{spawnvp()}
1333all cause the new process to inherit the environment of the current
1334process.
1335
Fred Drake739282d2001-08-16 21:21:28 +00001336As an example, the following calls to \function{spawnlp()} and
1337\function{spawnvpe()} are equivalent:
1338
1339\begin{verbatim}
1340import os
1341os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
1342
1343L = ['cp', 'index.html', '/dev/null']
1344os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
1345\end{verbatim}
1346
Fred Drake8c8e8712001-12-20 17:24:11 +00001347Availability: \UNIX, Windows. \function{spawnlp()},
1348\function{spawnlpe()}, \function{spawnvp()} and \function{spawnvpe()}
1349are not available on Windows.
Fred Drake0b9bc202001-06-11 18:25:34 +00001350\versionadded{1.6}
Fred Drake215fe2f1999-02-02 19:02:35 +00001351\end{funcdesc}
1352
Fred Drake938a8d72001-10-09 18:07:04 +00001353\begin{datadesc}{P_NOWAIT}
Fred Drake9329e5e1999-02-16 19:40:19 +00001354\dataline{P_NOWAITO}
Fred Drake938a8d72001-10-09 18:07:04 +00001355Possible values for the \var{mode} parameter to the \function{spawn*()}
1356family of functions. If either of these values is given, the
1357\function{spawn*()} functions will return as soon as the new process
1358has been created, with the process ID as the return value.
Fred Drakec37b65e2001-11-28 07:26:15 +00001359Availability: \UNIX, Windows.
Fred Drake0b9bc202001-06-11 18:25:34 +00001360\versionadded{1.6}
Fred Drake15861b22000-02-29 05:19:38 +00001361\end{datadesc}
1362
Fred Drake938a8d72001-10-09 18:07:04 +00001363\begin{datadesc}{P_WAIT}
1364Possible value for the \var{mode} parameter to the \function{spawn*()}
1365family of functions. If this is given as \var{mode}, the
1366\function{spawn*()} functions will not return until the new process
1367has run to completion and will return the exit code of the process the
1368run is successful, or \code{-\var{signal}} if a signal kills the
1369process.
Fred Drakec37b65e2001-11-28 07:26:15 +00001370Availability: \UNIX, Windows.
Fred Drake938a8d72001-10-09 18:07:04 +00001371\versionadded{1.6}
1372\end{datadesc}
1373
1374\begin{datadesc}{P_DETACH}
1375\dataline{P_OVERLAY}
1376Possible values for the \var{mode} parameter to the
1377\function{spawn*()} family of functions. These are less portable than
1378those listed above.
1379\constant{P_DETACH} is similar to \constant{P_NOWAIT}, but the new
1380process is detached from the console of the calling process.
1381If \constant{P_OVERLAY} is used, the current process will be replaced;
1382the \function{spawn*()} function will not return.
Fred Drake215fe2f1999-02-02 19:02:35 +00001383Availability: Windows.
Fred Drake0b9bc202001-06-11 18:25:34 +00001384\versionadded{1.6}
Fred Drake215fe2f1999-02-02 19:02:35 +00001385\end{datadesc}
1386
Fred Drake4ce4f2e2000-09-29 04:15:19 +00001387\begin{funcdesc}{startfile}{path}
1388Start a file with its associated application. This acts like
1389double-clicking the file in Windows Explorer, or giving the file name
Fred Drake8ee679f2001-07-14 02:50:55 +00001390as an argument to the \program{start} command from the interactive
1391command shell: the file is opened with whatever application (if any)
1392its extension is associated.
Fred Drake4ce4f2e2000-09-29 04:15:19 +00001393
1394\function{startfile()} returns as soon as the associated application
1395is launched. There is no option to wait for the application to close,
1396and no way to retrieve the application's exit status. The \var{path}
1397parameter is relative to the current directory. If you want to use an
1398absolute path, make sure the first character is not a slash
1399(\character{/}); the underlying Win32 \cfunction{ShellExecute()}
Fred Drake8a2adcf2001-07-23 19:20:56 +00001400function doesn't work if it is. Use the \function{os.path.normpath()}
Fred Drake4ce4f2e2000-09-29 04:15:19 +00001401function to ensure that the path is properly encoded for Win32.
1402Availability: Windows.
1403\versionadded{2.0}
1404\end{funcdesc}
1405
Fred Drake215fe2f1999-02-02 19:02:35 +00001406\begin{funcdesc}{system}{command}
1407Execute the command (a string) in a subshell. This is implemented by
1408calling the Standard C function \cfunction{system()}, and has the
Fred Drakeec6baaf1999-04-21 18:13:31 +00001409same limitations. Changes to \code{posix.environ}, \code{sys.stdin},
Fred Drake215fe2f1999-02-02 19:02:35 +00001410etc.\ are not reflected in the environment of the executed command.
1411The return value is the exit status of the process encoded in the
Fred Drake7a621281999-06-10 15:07:05 +00001412format specified for \function{wait()}, except on Windows 95 and 98,
Fred Drakea88ef001999-06-18 19:11:25 +00001413where it is always \code{0}. Note that \POSIX{} does not specify the
1414meaning of the return value of the C \cfunction{system()} function,
1415so the return value of the Python function is system-dependent.
Fred Drakec37b65e2001-11-28 07:26:15 +00001416Availability: \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +00001417\end{funcdesc}
1418
1419\begin{funcdesc}{times}{}
Fred Drake8ee679f2001-07-14 02:50:55 +00001420Return a 5-tuple of floating point numbers indicating accumulated
1421(processor or other)
Fred Drake215fe2f1999-02-02 19:02:35 +00001422times, in seconds. The items are: user time, system time, children's
1423user time, children's system time, and elapsed real time since a fixed
Fred Drakeec6baaf1999-04-21 18:13:31 +00001424point in the past, in that order. See the \UNIX{} manual page
1425\manpage{times}{2} or the corresponding Windows Platform API
1426documentation.
Fred Drakec37b65e2001-11-28 07:26:15 +00001427Availability: \UNIX, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +00001428\end{funcdesc}
1429
1430\begin{funcdesc}{wait}{}
1431Wait for completion of a child process, and return a tuple containing
1432its pid and exit status indication: a 16-bit number, whose low byte is
1433the signal number that killed the process, and whose high byte is the
1434exit status (if the signal number is zero); the high bit of the low
1435byte is set if a core file was produced.
Fred Drakec37b65e2001-11-28 07:26:15 +00001436Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +00001437\end{funcdesc}
1438
1439\begin{funcdesc}{waitpid}{pid, options}
Fred Drake1f89e2a2002-05-10 12:37:56 +00001440The details of this function differ on \UNIX{} and Windows.
Tim Petersab034fa2002-02-01 11:27:43 +00001441
1442On \UNIX:
Fred Drake31e5e371999-08-13 13:36:33 +00001443Wait for completion of a child process given by process id \var{pid},
1444and return a tuple containing its process id and exit status
1445indication (encoded as for \function{wait()}). The semantics of the
1446call are affected by the value of the integer \var{options}, which
1447should be \code{0} for normal operation.
Fred Drake31e5e371999-08-13 13:36:33 +00001448
1449If \var{pid} is greater than \code{0}, \function{waitpid()} requests
1450status information for that specific process. If \var{pid} is
1451\code{0}, the request is for the status of any child in the process
1452group of the current process. If \var{pid} is \code{-1}, the request
1453pertains to any child of the current process. If \var{pid} is less
1454than \code{-1}, status is requested for any process in the process
1455group \code{-\var{pid}} (the absolute value of \var{pid}).
Tim Petersab034fa2002-02-01 11:27:43 +00001456
1457On Windows:
Fred Drake4dfb7a82002-04-01 23:30:47 +00001458Wait for completion of a process given by process handle \var{pid},
Tim Petersab034fa2002-02-01 11:27:43 +00001459and return a tuple containing \var{pid},
1460and its exit status shifted left by 8 bits (shifting makes cross-platform
1461use of the function easier).
1462A \var{pid} less than or equal to \code{0} has no special meaning on
1463Windows, and raises an exception.
1464The value of integer \var{options} has no effect.
1465\var{pid} can refer to any process whose id is known, not necessarily a
1466child process.
1467The \function{spawn()} functions called with \constant{P_NOWAIT}
Fred Drake4dfb7a82002-04-01 23:30:47 +00001468return suitable process handles.
Fred Drake215fe2f1999-02-02 19:02:35 +00001469\end{funcdesc}
1470
1471\begin{datadesc}{WNOHANG}
1472The option for \function{waitpid()} to avoid hanging if no child
1473process status is available immediately.
Fred Drakec37b65e2001-11-28 07:26:15 +00001474Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +00001475\end{datadesc}
1476
Fred Drake106c1a02002-04-23 15:58:02 +00001477\begin{datadesc}{WCONTINUED}
1478This option causes child processes to be reported if they have been
1479continued from a job control stop since their status was last
1480reported.
1481Availability: Some \UNIX{} systems.
1482\versionadded{2.3}
1483\end{datadesc}
1484
1485\begin{datadesc}{WUNTRACED}
1486This option causes child processes to be reported if they have been
1487stopped but their current state has not been reported since they were
1488stopped.
1489Availability: \UNIX.
1490\versionadded{2.3}
1491\end{datadesc}
1492
Fred Drake38e5d272000-04-03 20:13:55 +00001493The following functions take a process status code as returned by
1494\function{system()}, \function{wait()}, or \function{waitpid()} as a
1495parameter. They may be used to determine the disposition of a
1496process.
Fred Drake215fe2f1999-02-02 19:02:35 +00001497
Fred Drake106c1a02002-04-23 15:58:02 +00001498\begin{funcdesc}{WCOREDUMP}{status}
1499Returns \code{True} if a core dump was generated for the process,
1500otherwise it returns \code{False}.
1501Availability: \UNIX.
1502\versionadded{2.3}
1503\end{funcdesc}
1504
1505\begin{funcdesc}{WIFCONTINUED}{status}
1506Returns \code{True} if the process has been continued from a job
1507control stop, otherwise it returns \code{False}.
1508Availability: \UNIX.
1509\versionadded{2.3}
1510\end{funcdesc}
1511
Fred Drake215fe2f1999-02-02 19:02:35 +00001512\begin{funcdesc}{WIFSTOPPED}{status}
Fred Drake106c1a02002-04-23 15:58:02 +00001513Returns \code{True} if the process has been stopped, otherwise it
1514returns \code{False}.
Fred Drakec37b65e2001-11-28 07:26:15 +00001515Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +00001516\end{funcdesc}
1517
1518\begin{funcdesc}{WIFSIGNALED}{status}
Fred Drake106c1a02002-04-23 15:58:02 +00001519Returns \code{True} if the process exited due to a signal, otherwise
1520it returns \code{False}.
Fred Drakec37b65e2001-11-28 07:26:15 +00001521Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +00001522\end{funcdesc}
1523
1524\begin{funcdesc}{WIFEXITED}{status}
Fred Drake106c1a02002-04-23 15:58:02 +00001525Returns \code{True} if the process exited using the \manpage{exit}{2}
1526system call, otherwise it returns \code{False}.
Fred Drakec37b65e2001-11-28 07:26:15 +00001527Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +00001528\end{funcdesc}
1529
1530\begin{funcdesc}{WEXITSTATUS}{status}
1531If \code{WIFEXITED(\var{status})} is true, return the integer
Tim Petersab034fa2002-02-01 11:27:43 +00001532parameter to the \manpage{exit}{2} system call. Otherwise, the return
Fred Drake215fe2f1999-02-02 19:02:35 +00001533value is meaningless.
Fred Drakec37b65e2001-11-28 07:26:15 +00001534Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +00001535\end{funcdesc}
1536
1537\begin{funcdesc}{WSTOPSIG}{status}
Fred Drake35c3ffd1999-03-04 14:08:10 +00001538Return the signal which caused the process to stop.
Fred Drakec37b65e2001-11-28 07:26:15 +00001539Availability: \UNIX.
Fred Drake35c3ffd1999-03-04 14:08:10 +00001540\end{funcdesc}
1541
1542\begin{funcdesc}{WTERMSIG}{status}
Fred Drake215fe2f1999-02-02 19:02:35 +00001543Return the signal which caused the process to exit.
Fred Drakec37b65e2001-11-28 07:26:15 +00001544Availability: \UNIX.
Fred Drake215fe2f1999-02-02 19:02:35 +00001545\end{funcdesc}
1546
1547
Thomas Woutersf8316632000-07-16 19:01:10 +00001548\subsection{Miscellaneous System Information \label{os-path}}
Fred Drake88f6ca21999-12-15 19:39:04 +00001549
1550
1551\begin{funcdesc}{confstr}{name}
1552Return string-valued system configuration values.
1553\var{name} specifies the configuration value to retrieve; it may be a
1554string which is the name of a defined system value; these names are
Fred Drake8ee679f2001-07-14 02:50:55 +00001555specified in a number of standards (\POSIX, \UNIX 95, \UNIX 98, and
Fred Drake88f6ca21999-12-15 19:39:04 +00001556others). Some platforms define additional names as well. The names
1557known to the host operating system are given in the
1558\code{confstr_names} dictionary. For configuration variables not
1559included in that mapping, passing an integer for \var{name} is also
1560accepted.
Fred Drakec37b65e2001-11-28 07:26:15 +00001561Availability: \UNIX.
Fred Drake88f6ca21999-12-15 19:39:04 +00001562
1563If the configuration value specified by \var{name} isn't defined, the
1564empty string is returned.
1565
1566If \var{name} is a string and is not known, \exception{ValueError} is
1567raised. If a specific value for \var{name} is not supported by the
1568host system, even if it is included in \code{confstr_names}, an
1569\exception{OSError} is raised with \constant{errno.EINVAL} for the
1570error number.
1571\end{funcdesc}
1572
1573\begin{datadesc}{confstr_names}
1574Dictionary mapping names accepted by \function{confstr()} to the
1575integer values defined for those names by the host operating system.
1576This can be used to determine the set of names known to the system.
1577Availability: \UNIX.
1578\end{datadesc}
1579
Martin v. Löwis438b5342002-12-27 10:16:42 +00001580\begin{funcdesc}{getloadavg}{}
1581Return the number of processes in the system run queue averaged over
1582the last 1, 5, and 15 minutes or raises OSError if the load average
1583was unobtainable.
1584
1585\versionadded{2.3}
1586\end{funcdesc}
1587
Fred Drake88f6ca21999-12-15 19:39:04 +00001588\begin{funcdesc}{sysconf}{name}
1589Return integer-valued system configuration values.
1590If the configuration value specified by \var{name} isn't defined,
1591\code{-1} is returned. The comments regarding the \var{name}
1592parameter for \function{confstr()} apply here as well; the dictionary
1593that provides information on the known names is given by
1594\code{sysconf_names}.
Fred Drakec37b65e2001-11-28 07:26:15 +00001595Availability: \UNIX.
Fred Drake88f6ca21999-12-15 19:39:04 +00001596\end{funcdesc}
1597
1598\begin{datadesc}{sysconf_names}
1599Dictionary mapping names accepted by \function{sysconf()} to the
1600integer values defined for those names by the host operating system.
1601This can be used to determine the set of names known to the system.
1602Availability: \UNIX.
1603\end{datadesc}
1604
Fred Drake215fe2f1999-02-02 19:02:35 +00001605
1606The follow data values are used to support path manipulation
1607operations. These are defined for all platforms.
1608
1609Higher-level operations on pathnames are defined in the
1610\refmodule{os.path} module.
1611
1612
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001613\begin{datadesc}{curdir}
Fred Drake8ee679f2001-07-14 02:50:55 +00001614The constant string used by the operating system to refer to the current
1615directory.
Fred Drake907e76b2001-07-06 20:30:11 +00001616For example: \code{'.'} for \POSIX{} or \code{':'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001617\end{datadesc}
1618
1619\begin{datadesc}{pardir}
Fred Drake8ee679f2001-07-14 02:50:55 +00001620The constant string used by the operating system to refer to the parent
1621directory.
Fred Drake907e76b2001-07-06 20:30:11 +00001622For example: \code{'..'} for \POSIX{} or \code{'::'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001623\end{datadesc}
1624
1625\begin{datadesc}{sep}
Fred Drake8ee679f2001-07-14 02:50:55 +00001626The character used by the operating system to separate pathname components,
Fred Drake907e76b2001-07-06 20:30:11 +00001627for example, \character{/} for \POSIX{} or \character{:} for the
1628Macintosh. Note that knowing this is not sufficient to be able to
1629parse or concatenate pathnames --- use \function{os.path.split()} and
Fred Drake1a3c2a01998-08-06 15:18:23 +00001630\function{os.path.join()} --- but it is occasionally useful.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001631\end{datadesc}
1632
Guido van Rossumb2afc811997-08-29 22:37:44 +00001633\begin{datadesc}{altsep}
Fred Drake8ee679f2001-07-14 02:50:55 +00001634An alternative character used by the operating system to separate pathname
1635components, or \code{None} if only one separator character exists. This is
Martin v. Löwis36a4d8c2002-10-10 18:24:54 +00001636set to \character{/} on Windows systems where \code{sep} is a
Fred Drake8ee679f2001-07-14 02:50:55 +00001637backslash.
Guido van Rossumb2afc811997-08-29 22:37:44 +00001638\end{datadesc}
1639
Guido van Rossum470be141995-03-17 16:07:09 +00001640\begin{datadesc}{pathsep}
Fred Drake8ee679f2001-07-14 02:50:55 +00001641The character conventionally used by the operating system to separate
1642search patch components (as in \envvar{PATH}), such as \character{:} for
Martin v. Löwis36a4d8c2002-10-10 18:24:54 +00001643\POSIX{} or \character{;} for Windows.
Guido van Rossum9c59ce91998-06-30 15:54:27 +00001644\end{datadesc}
1645
Guido van Rossum470be141995-03-17 16:07:09 +00001646\begin{datadesc}{defpath}
Fred Drake6995bb62001-11-29 20:48:44 +00001647The default search path used by \function{exec*p*()} and
1648\function{spawn*p*()} if the environment doesn't have a \code{'PATH'}
1649key.
Guido van Rossum470be141995-03-17 16:07:09 +00001650\end{datadesc}
1651
Fred Drake215fe2f1999-02-02 19:02:35 +00001652\begin{datadesc}{linesep}
1653The string used to separate (or, rather, terminate) lines on the
Fred Drake907e76b2001-07-06 20:30:11 +00001654current platform. This may be a single character, such as \code{'\e
Fred Drake6995bb62001-11-29 20:48:44 +00001655n'} for \POSIX{} or \code{'\e r'} for Mac OS, or multiple characters,
Martin v. Löwis36a4d8c2002-10-10 18:24:54 +00001656for example, \code{'\e r\e n'} for Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +00001657\end{datadesc}