blob: 6dd733d2bf5226569ad24d9d11ae1fdec5c812a2 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{os} ---
Fred Drake215fe2f1999-02-02 19:02:35 +00002 Miscellaneous OS interfaces}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakeec6baaf1999-04-21 18:13:31 +00004\declaremodule{standard}{os}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{Miscellaneous OS interfaces.}
6
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
9(OS) dependent functionality than importing an OS dependent built-in
Fred Drake2f979011999-06-11 18:28:37 +000010module like \refmodule{posix} or \module{nt}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000011
Fred Drake215fe2f1999-02-02 19:02:35 +000012This module searches for an OS 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 Drake215fe2f1999-02-02 19:02:35 +000014as found there. The design of all Python's built-in OS dependent
15modules is such that as long as the same functionality is available,
16it uses the same interface; e.g., 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
21Extensions peculiar to a particular OS are also available through the
Fred Drakec4f15af1998-03-10 03:17:26 +000022\module{os} module, but using them is of course a threat to
23portability!
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}
27instead of directly from the OS dependent built-in module, so there
28should 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}
45This exception is raised when a function returns a
46system-related error (e.g., not for illegal argument types). This is
47also known as the built-in exception \exception{OSError}. The
48accompanying 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
58involve a file system path (e.g. \function{chdir()} or
59\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}
Guido van Rossum470be141995-03-17 16:07:09 +000068The name of the OS dependent module imported. The following names
69have currently been registered: \code{'posix'}, \code{'nt'},
Fred Drake2f979011999-06-11 18:28:37 +000070\code{'dos'}, \code{'mac'}, \code{'os2'}, \code{'ce'}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000071\end{datadesc}
72
73\begin{datadesc}{path}
74The corresponding OS dependent standard module for pathname
Fred Drake215fe2f1999-02-02 19:02:35 +000075operations, e.g., \module{posixpath} or \module{macpath}. Thus, given
76the proper imports, \code{os.path.split(\var{file})} is equivalent to but
77more portable than \code{posixpath.split(\var{file})}. Note that this
78is also a valid module: it may be imported directly as
79\refmodule{os.path}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000080\end{datadesc}
81
Fred Drake215fe2f1999-02-02 19:02:35 +000082
83
84\subsection{Process Parameters \label{os-procinfo}}
85
86These functions and data items provide information and operate on the
87current process and user.
88
Fred Drake215fe2f1999-02-02 19:02:35 +000089\begin{datadesc}{environ}
Fred Drake0e1de8b1999-04-29 12:57:32 +000090A mapping object representing the string environment. For example,
91\code{environ['HOME']} is the pathname of your home directory (on some
92platforms), and is equivalent to \code{getenv("HOME")} in C.
Fred Drake215fe2f1999-02-02 19:02:35 +000093
94If the platform supports the \function{putenv()} function, this
95mapping may be used to modify the environment as well as query the
96environment. \function{putenv()} will be called automatically when
97the mapping is modified.
98
99If \function{putenv()} is not provided, this mapping may be passed to
100the appropriate process-creation functions to cause child processes to
101use a modified environment.
102\end{datadesc}
103
Fred Drake6db897c1999-07-12 16:49:30 +0000104\begin{funcdescni}{chdir}{path}
105\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
110\begin{funcdesc}{getegid}{}
111Return the current process' effective group id.
112Availability: \UNIX{}.
113\end{funcdesc}
114
115\begin{funcdesc}{geteuid}{}
Fred Drake6b330ba81999-05-25 13:42:26 +0000116\index{user!effective id}
Fred Drake215fe2f1999-02-02 19:02:35 +0000117Return the current process' effective user id.
118Availability: \UNIX{}.
119\end{funcdesc}
120
121\begin{funcdesc}{getgid}{}
Fred Drake6b330ba81999-05-25 13:42:26 +0000122\index{process!group}
Fred Drake215fe2f1999-02-02 19:02:35 +0000123Return the current process' group id.
124Availability: \UNIX{}.
125\end{funcdesc}
126
127\begin{funcdesc}{getpgrp}{}
128\index{process!group}
129Return the current process group id.
130Availability: \UNIX{}.
131\end{funcdesc}
132
133\begin{funcdesc}{getpid}{}
134\index{process!id}
135Return the current process id.
136Availability: \UNIX{}, Windows.
137\end{funcdesc}
138
139\begin{funcdesc}{getppid}{}
140\index{process!id of parent}
141Return the parent's process id.
142Availability: \UNIX{}.
143\end{funcdesc}
144
145\begin{funcdesc}{getuid}{}
Fred Drake6b330ba81999-05-25 13:42:26 +0000146\index{user!id}
Fred Drake215fe2f1999-02-02 19:02:35 +0000147Return the current process' user id.
148Availability: \UNIX{}.
149\end{funcdesc}
150
151\begin{funcdesc}{putenv}{varname, value}
152\index{environment variables!setting}
153Set the environment variable named \var{varname} to the string
154\var{value}. Such changes to the environment affect subprocesses
155started with \function{os.system()}, \function{popen()} or
156\function{fork()} and \function{execv()}.
157Availability: most flavors of \UNIX{}, Windows.
158
159When \function{putenv()} is
160supported, assignments to items in \code{os.environ} are automatically
161translated into corresponding calls to \function{putenv()}; however,
162calls to \function{putenv()} don't update \code{os.environ}, so it is
163actually preferable to assign to items of \code{os.environ}.
164\end{funcdesc}
165
166\begin{funcdesc}{setgid}{gid}
167Set the current process' group id.
168Availability: \UNIX{}.
169\end{funcdesc}
170
171\begin{funcdesc}{setpgrp}{}
172Calls the system call \cfunction{setpgrp()} or \cfunction{setpgrp(0,
1730)} depending on which version is implemented (if any). See the
174\UNIX{} manual for the semantics.
175Availability: \UNIX{}.
176\end{funcdesc}
177
178\begin{funcdesc}{setpgid}{pid, pgrp}
179Calls the system call \cfunction{setpgid()}. See the \UNIX{} manual
180for the semantics.
181Availability: \UNIX{}.
182\end{funcdesc}
183
184\begin{funcdesc}{setsid}{}
185Calls the system call \cfunction{setsid()}. See the \UNIX{} manual
186for the semantics.
187Availability: \UNIX{}.
188\end{funcdesc}
189
190\begin{funcdesc}{setuid}{uid}
Fred Drake6b330ba81999-05-25 13:42:26 +0000191\index{user!id, setting}
Fred Drake215fe2f1999-02-02 19:02:35 +0000192Set the current process' user id.
193Availability: \UNIX{}.
194\end{funcdesc}
195
196% placed in this section since it relates to errno.... a little weak ;-(
197\begin{funcdesc}{strerror}{code}
198Return the error message corresponding to the error code in
199\var{code}.
200Availability: \UNIX{}, Windows.
201\end{funcdesc}
202
203\begin{funcdesc}{umask}{mask}
204Set the current numeric umask and returns the previous umask.
205Availability: \UNIX{}, Windows.
206\end{funcdesc}
207
208\begin{funcdesc}{uname}{}
209Return a 5-tuple containing information identifying the current
210operating system. The tuple contains 5 strings:
211\code{(\var{sysname}, \var{nodename}, \var{release}, \var{version},
212\var{machine})}. Some systems truncate the nodename to 8
213characters or to the leading component; a better way to get the
214hostname is \function{socket.gethostname()}
215\withsubitem{(in module socket)}{\ttindex{gethostname()}}
216or even
217\withsubitem{(in module socket)}{\ttindex{gethostbyaddr()}}
218\code{socket.gethostbyaddr(socket.gethostname())}.
219Availability: recent flavors of \UNIX{}.
220\end{funcdesc}
221
222
223
224\subsection{File Object Creation \label{os-newstreams}}
225
226These functions create new file objects.
227
228
229\begin{funcdesc}{fdopen}{fd\optional{, mode\optional{, bufsize}}}
230Return an open file object connected to the file descriptor \var{fd}.
Fred Drake8c9fc001999-08-05 13:41:31 +0000231\index{I/O control!buffering}
Fred Drake215fe2f1999-02-02 19:02:35 +0000232The \var{mode} and \var{bufsize} arguments have the same meaning as
233the corresponding arguments to the built-in \function{open()}
234function.
235Availability: Macintosh, \UNIX{}, Windows.
236\end{funcdesc}
237
238\begin{funcdesc}{popen}{command\optional{, mode\optional{, bufsize}}}
239Open a pipe to or from \var{command}. The return value is an open
240file object connected to the pipe, which can be read or written
241depending on whether \var{mode} is \code{'r'} (default) or \code{'w'}.
242The \var{bufsize} argument has the same meaning as the corresponding
243argument to the built-in \function{open()} function. The exit status of
244the command (encoded in the format specified for \function{wait()}) is
245available as the return value of the \method{close()} method of the file
246object, except that when the exit status is zero (termination without
247errors), \code{None} is returned.
248Availability: \UNIX{}, Windows.
249\end{funcdesc}
250
251
252
253\subsection{File Descriptor Operations \label{os-fd-ops}}
254
255These functions operate on I/O streams referred to
256using file descriptors.
257
258
259\begin{funcdesc}{close}{fd}
260Close file descriptor \var{fd}.
261Availability: Macintosh, \UNIX{}, Windows.
262
263Note: this function is intended for low-level I/O and must be applied
264to a file descriptor as returned by \function{open()} or
265\function{pipe()}. To close a ``file object'' returned by the
266built-in function \function{open()} or by \function{popen()} or
267\function{fdopen()}, use its \method{close()} method.
268\end{funcdesc}
269
270\begin{funcdesc}{dup}{fd}
271Return a duplicate of file descriptor \var{fd}.
272Availability: Macintosh, \UNIX{}, Windows.
273\end{funcdesc}
274
275\begin{funcdesc}{dup2}{fd, fd2}
276Duplicate file descriptor \var{fd} to \var{fd2}, closing the latter
277first if necessary.
278Availability: \UNIX{}, Windows.
279\end{funcdesc}
280
281\begin{funcdesc}{fstat}{fd}
282Return status for file descriptor \var{fd}, like \function{stat()}.
283Availability: \UNIX{}, Windows.
284\end{funcdesc}
285
286\begin{funcdesc}{fstatvfs}{fd}
287Return information about the filesystem containing the file associated
288with file descriptor \var{fd}, like \function{statvfs()}.
289Availability: \UNIX{}.
290\end{funcdesc}
291
292\begin{funcdesc}{ftruncate}{fd, length}
293Truncate the file corresponding to file descriptor \var{fd},
294so that it is at most \var{length} bytes in size.
295Availability: \UNIX{}.
296\end{funcdesc}
297
298\begin{funcdesc}{lseek}{fd, pos, how}
299Set the current position of file descriptor \var{fd} to position
300\var{pos}, modified by \var{how}: \code{0} to set the position
301relative to the beginning of the file; \code{1} to set it relative to
302the current position; \code{2} to set it relative to the end of the
303file.
304Availability: Macintosh, \UNIX{}, Windows.
305\end{funcdesc}
306
307\begin{funcdesc}{open}{file, flags\optional{, mode}}
308Open the file \var{file} and set various flags according to
309\var{flags} and possibly its mode according to \var{mode}.
310The default \var{mode} is \code{0777} (octal), and the current umask
311value is first masked out. Return the file descriptor for the newly
312opened file.
313Availability: Macintosh, \UNIX{}, Windows.
314
315For a description of the flag and mode values, see the C run-time
316documentation; flag constants (like \constant{O_RDONLY} and
317\constant{O_WRONLY}) are defined in this module too (see below).
318
319Note: this function is intended for low-level I/O. For normal usage,
320use the built-in function \function{open()}, which returns a ``file
321object'' with \method{read()} and \method{write()} methods (and many
322more).
323\end{funcdesc}
324
325\begin{funcdesc}{pipe}{}
326Create a pipe. Return a pair of file descriptors \code{(\var{r},
327\var{w})} usable for reading and writing, respectively.
328Availability: \UNIX{}, Windows.
329\end{funcdesc}
330
331\begin{funcdesc}{read}{fd, n}
332Read at most \var{n} bytes from file descriptor \var{fd}.
333Return a string containing the bytes read.
334Availability: Macintosh, \UNIX{}, Windows.
335
336Note: this function is intended for low-level I/O and must be applied
337to a file descriptor as returned by \function{open()} or
338\function{pipe()}. To read a ``file object'' returned by the
339built-in function \function{open()} or by \function{popen()} or
340\function{fdopen()}, or \code{sys.stdin}, use its
341\method{read()} or \method{readline()} methods.
342\end{funcdesc}
343
344\begin{funcdesc}{tcgetpgrp}{fd}
345Return the process group associated with the terminal given by
346\var{fd} (an open file descriptor as returned by \function{open()}).
347Availability: \UNIX{}.
348\end{funcdesc}
349
350\begin{funcdesc}{tcsetpgrp}{fd, pg}
351Set the process group associated with the terminal given by
352\var{fd} (an open file descriptor as returned by \function{open()})
353to \var{pg}.
354Availability: \UNIX{}.
355\end{funcdesc}
356
357\begin{funcdesc}{ttyname}{fd}
358Return a string which specifies the terminal device associated with
359file-descriptor \var{fd}. If \var{fd} is not associated with a terminal
360device, an exception is raised.
361Availability: \UNIX{}.
362\end{funcdesc}
363
364\begin{funcdesc}{write}{fd, str}
365Write the string \var{str} to file descriptor \var{fd}.
366Return the number of bytes actually written.
367Availability: Macintosh, \UNIX{}, Windows.
368
369Note: this function is intended for low-level I/O and must be applied
370to a file descriptor as returned by \function{open()} or
371\function{pipe()}. To write a ``file object'' returned by the
372built-in function \function{open()} or by \function{popen()} or
373\function{fdopen()}, or \code{sys.stdout} or \code{sys.stderr}, use
374its \method{write()} method.
375\end{funcdesc}
376
377
378The following data items are available for use in constructing the
379\var{flags} parameter to the \function{open()} function.
380
381\begin{datadesc}{O_RDONLY}
382\dataline{O_WRONLY}
383\dataline{O_RDWR}
384\dataline{O_NDELAY}
385\dataline{O_NONBLOCK}
386\dataline{O_APPEND}
387\dataline{O_DSYNC}
388\dataline{O_RSYNC}
389\dataline{O_SYNC}
390\dataline{O_NOCTTY}
391\dataline{O_CREAT}
392\dataline{O_EXCL}
393\dataline{O_TRUNC}
394Options for the \var{flag} argument to the \function{open()} function.
395These can be bit-wise OR'd together.
396Availability: Macintosh, \UNIX{}, Windows.
397\end{datadesc}
398
399
400\subsection{Files and Directories \label{os-file-dir}}
401
402\begin{funcdesc}{access}{path, mode}
403Check read/write/execute permissions for this process or extance of file
404\var{path}. Return \code{1} if access is granted, \code{0} if not.
405See the \UNIX{} manual for the semantics.
406Availability: \UNIX{}.
407\end{funcdesc}
408
Fred Drake6db897c1999-07-12 16:49:30 +0000409\begin{funcdesc}{chdir}{path}
410\index{directory!changing}
411Change the current working directory to \var{path}.
412Availability: Macintosh, \UNIX{}, Windows.
413\end{funcdesc}
414
415\begin{funcdesc}{getcwd}{}
416Return a string representing the current working directory.
417Availability: Macintosh, \UNIX{}, Windows.
418\end{funcdesc}
419
Fred Drake215fe2f1999-02-02 19:02:35 +0000420\begin{funcdesc}{chmod}{path, mode}
421Change the mode of \var{path} to the numeric \var{mode}.
422Availability: \UNIX{}, Windows.
423\end{funcdesc}
424
425\begin{funcdesc}{chown}{path, uid, gid}
426Change the owner and group id of \var{path} to the numeric \var{uid}
427and \var{gid}.
428Availability: \UNIX{}.
429\end{funcdesc}
430
431\begin{funcdesc}{link}{src, dst}
432Create a hard link pointing to \var{src} named \var{dst}.
433Availability: \UNIX{}.
434\end{funcdesc}
435
436\begin{funcdesc}{listdir}{path}
437Return a list containing the names of the entries in the directory.
438The list is in arbitrary order. It does not include the special
439entries \code{'.'} and \code{'..'} even if they are present in the
440directory.
441Availability: Macintosh, \UNIX{}, Windows.
442\end{funcdesc}
443
444\begin{funcdesc}{lstat}{path}
445Like \function{stat()}, but do not follow symbolic links.
446Availability: \UNIX{}.
447\end{funcdesc}
448
449\begin{funcdesc}{mkfifo}{path\optional{, mode}}
450Create a FIFO (a named pipe) named \var{path} with numeric mode
451\var{mode}. The default \var{mode} is \code{0666} (octal). The current
452umask value is first masked out from the mode.
453Availability: \UNIX{}.
454
455FIFOs are pipes that can be accessed like regular files. FIFOs exist
456until they are deleted (for example with \function{os.unlink()}).
457Generally, FIFOs are used as rendezvous between ``client'' and
458``server'' type processes: the server opens the FIFO for reading, and
459the client opens it for writing. Note that \function{mkfifo()}
460doesn't open the FIFO --- it just creates the rendezvous point.
461\end{funcdesc}
462
463\begin{funcdesc}{mkdir}{path\optional{, mode}}
464Create a directory named \var{path} with numeric mode \var{mode}.
465The default \var{mode} is \code{0777} (octal). On some systems,
466\var{mode} is ignored. Where it is used, the current umask value is
467first masked out.
468Availability: Macintosh, \UNIX{}, Windows.
469\end{funcdesc}
470
471\begin{funcdesc}{makedirs}{path\optional{, mode}}
472\index{directory!creating}
473Recursive directory creation function. Like \function{mkdir()},
474but makes all intermediate-level directories needed to contain the
475leaf directory. Throws an \exception{error} exception if the leaf
476directory already exists or cannot be created. The default \var{mode}
477is \code{0777} (octal).
478\versionadded{1.5.2}
479\end{funcdesc}
480
481\begin{funcdesc}{readlink}{path}
482Return a string representing the path to which the symbolic link
483points.
484Availability: \UNIX{}.
485\end{funcdesc}
486
487\begin{funcdesc}{remove}{path}
488Remove the file \var{path}. See \function{rmdir()} below to remove a
489directory. This is identical to the \function{unlink()} function
490documented below.
491Availability: Macintosh, \UNIX{}, Windows.
492\end{funcdesc}
493
494\begin{funcdesc}{removedirs}{path}
495\index{directory!deleting}
496Recursive directory removal function. Works like
497\function{rmdir()} except that, if the leaf directory is
498successfully removed, directories corresponding to rightmost path
499segments will be pruned way until either the whole path is consumed or
500an error is raised (which is ignored, because it generally means that
501a parent directory is not empty). Throws an \exception{error}
502exception if the leaf directory could not be successfully removed.
503\versionadded{1.5.2}
504\end{funcdesc}
505
506\begin{funcdesc}{rename}{src, dst}
507Rename the file or directory \var{src} to \var{dst}.
508Availability: Macintosh, \UNIX{}, Windows.
509\end{funcdesc}
510
511\begin{funcdesc}{renames}{old, new}
512Recursive directory or file renaming function.
513Works like \function{rename()}, except creation of any intermediate
514directories needed to make the new pathname good is attempted first.
515After the rename, directories corresponding to rightmost path segments
516of the old name will be pruned away using \function{removedirs()}.
517
518Note: this function can fail with the new directory structure made if
519you lack permissions needed to remove the leaf directory or file.
520\versionadded{1.5.2}
521\end{funcdesc}
522
523\begin{funcdesc}{rmdir}{path}
524Remove the directory \var{path}.
525Availability: Macintosh, \UNIX{}, Windows.
526\end{funcdesc}
527
528\begin{funcdesc}{stat}{path}
529Perform a \cfunction{stat()} system call on the given path. The
530return value is a tuple of at least 10 integers giving the most
531important (and portable) members of the \emph{stat} structure, in the
532order
533\code{st_mode},
534\code{st_ino},
535\code{st_dev},
536\code{st_nlink},
537\code{st_uid},
538\code{st_gid},
539\code{st_size},
540\code{st_atime},
541\code{st_mtime},
542\code{st_ctime}.
543More items may be added at the end by some implementations.
544(On MS Windows, some items are filled with dummy values.)
545Availability: Macintosh, \UNIX{}, Windows.
546
547Note: The standard module \refmodule{stat}\refstmodindex{stat} defines
548functions and constants that are useful for extracting information
549from a \ctype{stat} structure.
550\end{funcdesc}
551
552\begin{funcdesc}{statvfs}{path}
553Perform a \cfunction{statvfs()} system call on the given path. The
Guido van Rossum0c9608c1999-02-03 16:32:37 +0000554return value is a tuple of 10 integers giving the most common
Fred Drake215fe2f1999-02-02 19:02:35 +0000555members of the \ctype{statvfs} structure, in the order
556\code{f_bsize},
557\code{f_frsize},
558\code{f_blocks},
559\code{f_bfree},
560\code{f_bavail},
561\code{f_files},
562\code{f_ffree},
563\code{f_favail},
Fred Drake215fe2f1999-02-02 19:02:35 +0000564\code{f_flag},
565\code{f_namemax}.
566Availability: \UNIX{}.
567
568Note: The standard module \module{statvfs}\refstmodindex{statvfs}
569defines constants that are useful for extracting information
570from a \ctype{statvfs} structure.
571\end{funcdesc}
572
573\begin{funcdesc}{symlink}{src, dst}
574Create a symbolic link pointing to \var{src} named \var{dst}.
575Availability: \UNIX{}.
576\end{funcdesc}
577
578\begin{funcdesc}{unlink}{path}
579Remove the file \var{path}. This is the same function as
580\function{remove()}; the \function{unlink()} name is its traditional
581\UNIX{} name.
582Availability: Macintosh, \UNIX{}, Windows.
583\end{funcdesc}
584
585\begin{funcdesc}{utime}{path, (atime, mtime)}
586Set the access and modified time of the file to the given values.
587(The second argument is a tuple of two items.)
588Availability: Macintosh, \UNIX{}, Windows.
589\end{funcdesc}
590
591
592\subsection{Process Management \label{os-process}}
593
594These functions may be used to create and manage additional
595processes.
596
597
598\begin{funcdesc}{execl}{path, arg0, arg1, ...}
599This is equivalent to
600\samp{execv(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
601Availability: \UNIX{}, Windows.
602\end{funcdesc}
603
604\begin{funcdesc}{execle}{path, arg0, arg1, ..., env}
605This is equivalent to
606\samp{execve(\var{path}, (\var{arg0}, \var{arg1}, ...), \var{env})}.
607Availability: \UNIX{}, Windows.
608\end{funcdesc}
609
610\begin{funcdesc}{execlp}{path, arg0, arg1, ...}
611This is equivalent to
612\samp{execvp(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
613Availability: \UNIX{}, Windows.
614\end{funcdesc}
615
616\begin{funcdesc}{execv}{path, args}
617Execute the executable \var{path} with argument list \var{args},
618replacing the current process (i.e., the Python interpreter).
619The argument list may be a tuple or list of strings.
620Availability: \UNIX{}, Windows.
621\end{funcdesc}
622
623\begin{funcdesc}{execve}{path, args, env}
624Execute the executable \var{path} with argument list \var{args},
625and environment \var{env},
626replacing the current process (i.e., the Python interpreter).
627The argument list may be a tuple or list of strings.
628The environment must be a dictionary mapping strings to strings.
629Availability: \UNIX{}, Windows.
630\end{funcdesc}
631
632\begin{funcdesc}{execvp}{path, args}
633This is like \samp{execv(\var{path}, \var{args})} but duplicates
634the shell's actions in searching for an executable file in a list of
635directories. The directory list is obtained from
636\code{environ['PATH']}.
637Availability: \UNIX{}, Windows.
638\end{funcdesc}
639
640\begin{funcdesc}{execvpe}{path, args, env}
641This is a cross between \function{execve()} and \function{execvp()}.
642The directory list is obtained from \code{\var{env}['PATH']}.
643Availability: \UNIX{}, Windows.
644\end{funcdesc}
645
646\begin{funcdesc}{_exit}{n}
647Exit to the system with status \var{n}, without calling cleanup
648handlers, flushing stdio buffers, etc.
649Availability: \UNIX{}, Windows.
650
651Note: the standard way to exit is \code{sys.exit(\var{n})}.
652\function{_exit()} should normally only be used in the child process
653after a \function{fork()}.
654\end{funcdesc}
655
656\begin{funcdesc}{fork}{}
657Fork a child process. Return \code{0} in the child, the child's
658process id in the parent.
659Availability: \UNIX{}.
660\end{funcdesc}
661
662\begin{funcdesc}{kill}{pid, sig}
663\index{process!killing}
664\index{process!signalling}
665Kill the process \var{pid} with signal \var{sig}.
666Availability: \UNIX{}.
667\end{funcdesc}
668
669\begin{funcdesc}{nice}{increment}
670Add \var{increment} to the process's ``niceness''. Return the new
671niceness.
672Availability: \UNIX{}.
673\end{funcdesc}
674
675\begin{funcdesc}{plock}{op}
676Lock program segments into memory. The value of \var{op}
677(defined in \code{<sys/lock.h>}) determines which segments are locked.
Fred Drake39063631999-02-26 14:05:02 +0000678Availability: \UNIX{}.
Fred Drake215fe2f1999-02-02 19:02:35 +0000679\end{funcdesc}
680
681\begin{funcdesc}{spawnv}{mode, path, args}
682Execute the program \var{path} in a new process, passing the arguments
683specified in \var{args} as command-line parameters. \var{args} may be
684a list or a tuple. \var{mode} is a magic operational constant. See
685the Visual \Cpp{} Runtime Library documentation for further
Fred Drake22702081999-07-02 14:01:03 +0000686information; the constants are exposed to the Python programmer as
687listed below.
Fred Drake215fe2f1999-02-02 19:02:35 +0000688Availability: Windows.
689\versionadded{1.5.2}
690\end{funcdesc}
691
692\begin{funcdesc}{spawnve}{mode, path, args, env}
693Execute the program \var{path} in a new process, passing the arguments
694specified in \var{args} as command-line parameters and the contents of
695the mapping \var{env} as the environment. \var{args} may be a list or
696a tuple. \var{mode} is a magic operational constant. See the Visual
Fred Drake22702081999-07-02 14:01:03 +0000697\Cpp{} Runtime Library documentation for further information; the
698constants are exposed to the Python programmer as listed below.
Fred Drake215fe2f1999-02-02 19:02:35 +0000699Availability: Windows.
700\versionadded{1.5.2}
701\end{funcdesc}
702
Fred Drake9329e5e1999-02-16 19:40:19 +0000703\begin{datadesc}{P_WAIT}
704\dataline{P_NOWAIT}
705\dataline{P_NOWAITO}
706\dataline{P_OVERLAY}
707\dataline{P_DETACH}
Fred Drake215fe2f1999-02-02 19:02:35 +0000708Possible values for the \var{mode} parameter to \function{spawnv()}
709and \function{spawnve()}.
710Availability: Windows.
711\versionadded{1.5.2}
712\end{datadesc}
713
714\begin{funcdesc}{system}{command}
715Execute the command (a string) in a subshell. This is implemented by
716calling the Standard C function \cfunction{system()}, and has the
Fred Drakeec6baaf1999-04-21 18:13:31 +0000717same limitations. Changes to \code{posix.environ}, \code{sys.stdin},
Fred Drake215fe2f1999-02-02 19:02:35 +0000718etc.\ are not reflected in the environment of the executed command.
719The return value is the exit status of the process encoded in the
Fred Drake7a621281999-06-10 15:07:05 +0000720format specified for \function{wait()}, except on Windows 95 and 98,
Fred Drakea88ef001999-06-18 19:11:25 +0000721where it is always \code{0}. Note that \POSIX{} does not specify the
722meaning of the return value of the C \cfunction{system()} function,
723so the return value of the Python function is system-dependent.
Fred Drake215fe2f1999-02-02 19:02:35 +0000724Availability: \UNIX{}, Windows.
725\end{funcdesc}
726
727\begin{funcdesc}{times}{}
728Return a 5-tuple of floating point numbers indicating accumulated (CPU
729or other)
730times, in seconds. The items are: user time, system time, children's
731user time, children's system time, and elapsed real time since a fixed
Fred Drakeec6baaf1999-04-21 18:13:31 +0000732point in the past, in that order. See the \UNIX{} manual page
733\manpage{times}{2} or the corresponding Windows Platform API
734documentation.
Fred Drake215fe2f1999-02-02 19:02:35 +0000735Availability: \UNIX{}, Windows.
736\end{funcdesc}
737
738\begin{funcdesc}{wait}{}
739Wait for completion of a child process, and return a tuple containing
740its pid and exit status indication: a 16-bit number, whose low byte is
741the signal number that killed the process, and whose high byte is the
742exit status (if the signal number is zero); the high bit of the low
743byte is set if a core file was produced.
744Availability: \UNIX{}.
745\end{funcdesc}
746
747\begin{funcdesc}{waitpid}{pid, options}
Fred Drake6b330ba81999-05-25 13:42:26 +0000748Wait for completion of a child process given by process id, and return
Fred Drake215fe2f1999-02-02 19:02:35 +0000749a tuple containing its process id and exit status indication (encoded
750as for \function{wait()}). The semantics of the call are affected by
751the value of the integer \var{options}, which should be \code{0} for
752normal operation.
753Availability: \UNIX{}.
754\end{funcdesc}
755
756\begin{datadesc}{WNOHANG}
757The option for \function{waitpid()} to avoid hanging if no child
758process status is available immediately.
759Availability: \UNIX{}.
760\end{datadesc}
761
762The following functions take a process stats code as returned by
763\function{waitpid()} as a parameter. They may be used to determine
764the disposition of a process.
765
766\begin{funcdesc}{WIFSTOPPED}{status}
767Return true if the process has been stopped.
768Availability: \UNIX{}.
769\end{funcdesc}
770
771\begin{funcdesc}{WIFSIGNALED}{status}
772Return true if the process exited due to a signal.
773Availability: \UNIX{}.
774\end{funcdesc}
775
776\begin{funcdesc}{WIFEXITED}{status}
777Return true if the process exited using the \manpage{exit}{2} system
778call.
779Availability: \UNIX{}.
780\end{funcdesc}
781
782\begin{funcdesc}{WEXITSTATUS}{status}
783If \code{WIFEXITED(\var{status})} is true, return the integer
784parameter to the \manpage{exit}{2} system call. Otherwise, the return
785value is meaningless.
786Availability: \UNIX{}.
787\end{funcdesc}
788
789\begin{funcdesc}{WSTOPSIG}{status}
Fred Drake35c3ffd1999-03-04 14:08:10 +0000790Return the signal which caused the process to stop.
791Availability: \UNIX{}.
792\end{funcdesc}
793
794\begin{funcdesc}{WTERMSIG}{status}
Fred Drake215fe2f1999-02-02 19:02:35 +0000795Return the signal which caused the process to exit.
796Availability: \UNIX{}.
797\end{funcdesc}
798
799
800\subsection{Miscellanenous System Data \label{os-path}}
801
802The follow data values are used to support path manipulation
803operations. These are defined for all platforms.
804
805Higher-level operations on pathnames are defined in the
806\refmodule{os.path} module.
807
808
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000809\begin{datadesc}{curdir}
810The constant string used by the OS to refer to the current directory,
Fred Drake1a3c2a01998-08-06 15:18:23 +0000811e.g.\ \code{'.'} for \POSIX{} or \code{':'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000812\end{datadesc}
813
814\begin{datadesc}{pardir}
815The constant string used by the OS to refer to the parent directory,
Fred Drake1a3c2a01998-08-06 15:18:23 +0000816e.g.\ \code{'..'} for \POSIX{} or \code{'::'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000817\end{datadesc}
818
819\begin{datadesc}{sep}
Guido van Rossumb2afc811997-08-29 22:37:44 +0000820The character used by the OS to separate pathname components,
Fred Drake1a3c2a01998-08-06 15:18:23 +0000821e.g.\ \character{/} for \POSIX{} or \character{:} for the Macintosh.
822Note that knowing this is not sufficient to be able to parse or
823concatenate pathnames --- use \function{os.path.split()} and
824\function{os.path.join()} --- but it is occasionally useful.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000825\end{datadesc}
826
Guido van Rossumb2afc811997-08-29 22:37:44 +0000827\begin{datadesc}{altsep}
828An alternative character used by the OS to separate pathname components,
829or \code{None} if only one separator character exists. This is set to
Fred Drake215fe2f1999-02-02 19:02:35 +0000830\character{/} on DOS and Windows systems where \code{sep} is a backslash.
Guido van Rossumb2afc811997-08-29 22:37:44 +0000831\end{datadesc}
832
Guido van Rossum470be141995-03-17 16:07:09 +0000833\begin{datadesc}{pathsep}
834The character conventionally used by the OS to separate search patch
Fred Drake1a3c2a01998-08-06 15:18:23 +0000835components (as in \envvar{PATH}), e.g.\ \character{:} for \POSIX{} or
Fred Drake215fe2f1999-02-02 19:02:35 +0000836\character{;} for DOS and Windows.
Guido van Rossum9c59ce91998-06-30 15:54:27 +0000837\end{datadesc}
838
Guido van Rossum470be141995-03-17 16:07:09 +0000839\begin{datadesc}{defpath}
Fred Drake1a3c2a01998-08-06 15:18:23 +0000840The default search path used by \function{exec*p*()} if the environment
Guido van Rossum470be141995-03-17 16:07:09 +0000841doesn't have a \code{'PATH'} key.
842\end{datadesc}
843
Fred Drake215fe2f1999-02-02 19:02:35 +0000844\begin{datadesc}{linesep}
845The string used to separate (or, rather, terminate) lines on the
Fred Drakeec6baaf1999-04-21 18:13:31 +0000846current platform. This may be a single character,
847e.g.\ \code{'\e n'} for \POSIX{} or \code{'\e r'} for MacOS, or multiple
848characters, e.g.\ \code{'\e r\e n'} for MS-DOS and MS Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000849\end{datadesc}