blob: 8388d3f3fe9aa68dfa0b83c1ff183874748335f2 [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}.
231The \var{mode} and \var{bufsize} arguments have the same meaning as
232the corresponding arguments to the built-in \function{open()}
233function.
234Availability: Macintosh, \UNIX{}, Windows.
235\end{funcdesc}
236
237\begin{funcdesc}{popen}{command\optional{, mode\optional{, bufsize}}}
238Open a pipe to or from \var{command}. The return value is an open
239file object connected to the pipe, which can be read or written
240depending on whether \var{mode} is \code{'r'} (default) or \code{'w'}.
241The \var{bufsize} argument has the same meaning as the corresponding
242argument to the built-in \function{open()} function. The exit status of
243the command (encoded in the format specified for \function{wait()}) is
244available as the return value of the \method{close()} method of the file
245object, except that when the exit status is zero (termination without
246errors), \code{None} is returned.
247Availability: \UNIX{}, Windows.
248\end{funcdesc}
249
250
251
252\subsection{File Descriptor Operations \label{os-fd-ops}}
253
254These functions operate on I/O streams referred to
255using file descriptors.
256
257
258\begin{funcdesc}{close}{fd}
259Close file descriptor \var{fd}.
260Availability: Macintosh, \UNIX{}, Windows.
261
262Note: this function is intended for low-level I/O and must be applied
263to a file descriptor as returned by \function{open()} or
264\function{pipe()}. To close a ``file object'' returned by the
265built-in function \function{open()} or by \function{popen()} or
266\function{fdopen()}, use its \method{close()} method.
267\end{funcdesc}
268
269\begin{funcdesc}{dup}{fd}
270Return a duplicate of file descriptor \var{fd}.
271Availability: Macintosh, \UNIX{}, Windows.
272\end{funcdesc}
273
274\begin{funcdesc}{dup2}{fd, fd2}
275Duplicate file descriptor \var{fd} to \var{fd2}, closing the latter
276first if necessary.
277Availability: \UNIX{}, Windows.
278\end{funcdesc}
279
280\begin{funcdesc}{fstat}{fd}
281Return status for file descriptor \var{fd}, like \function{stat()}.
282Availability: \UNIX{}, Windows.
283\end{funcdesc}
284
285\begin{funcdesc}{fstatvfs}{fd}
286Return information about the filesystem containing the file associated
287with file descriptor \var{fd}, like \function{statvfs()}.
288Availability: \UNIX{}.
289\end{funcdesc}
290
291\begin{funcdesc}{ftruncate}{fd, length}
292Truncate the file corresponding to file descriptor \var{fd},
293so that it is at most \var{length} bytes in size.
294Availability: \UNIX{}.
295\end{funcdesc}
296
297\begin{funcdesc}{lseek}{fd, pos, how}
298Set the current position of file descriptor \var{fd} to position
299\var{pos}, modified by \var{how}: \code{0} to set the position
300relative to the beginning of the file; \code{1} to set it relative to
301the current position; \code{2} to set it relative to the end of the
302file.
303Availability: Macintosh, \UNIX{}, Windows.
304\end{funcdesc}
305
306\begin{funcdesc}{open}{file, flags\optional{, mode}}
307Open the file \var{file} and set various flags according to
308\var{flags} and possibly its mode according to \var{mode}.
309The default \var{mode} is \code{0777} (octal), and the current umask
310value is first masked out. Return the file descriptor for the newly
311opened file.
312Availability: Macintosh, \UNIX{}, Windows.
313
314For a description of the flag and mode values, see the C run-time
315documentation; flag constants (like \constant{O_RDONLY} and
316\constant{O_WRONLY}) are defined in this module too (see below).
317
318Note: this function is intended for low-level I/O. For normal usage,
319use the built-in function \function{open()}, which returns a ``file
320object'' with \method{read()} and \method{write()} methods (and many
321more).
322\end{funcdesc}
323
324\begin{funcdesc}{pipe}{}
325Create a pipe. Return a pair of file descriptors \code{(\var{r},
326\var{w})} usable for reading and writing, respectively.
327Availability: \UNIX{}, Windows.
328\end{funcdesc}
329
330\begin{funcdesc}{read}{fd, n}
331Read at most \var{n} bytes from file descriptor \var{fd}.
332Return a string containing the bytes read.
333Availability: Macintosh, \UNIX{}, Windows.
334
335Note: this function is intended for low-level I/O and must be applied
336to a file descriptor as returned by \function{open()} or
337\function{pipe()}. To read a ``file object'' returned by the
338built-in function \function{open()} or by \function{popen()} or
339\function{fdopen()}, or \code{sys.stdin}, use its
340\method{read()} or \method{readline()} methods.
341\end{funcdesc}
342
343\begin{funcdesc}{tcgetpgrp}{fd}
344Return the process group associated with the terminal given by
345\var{fd} (an open file descriptor as returned by \function{open()}).
346Availability: \UNIX{}.
347\end{funcdesc}
348
349\begin{funcdesc}{tcsetpgrp}{fd, pg}
350Set the process group associated with the terminal given by
351\var{fd} (an open file descriptor as returned by \function{open()})
352to \var{pg}.
353Availability: \UNIX{}.
354\end{funcdesc}
355
356\begin{funcdesc}{ttyname}{fd}
357Return a string which specifies the terminal device associated with
358file-descriptor \var{fd}. If \var{fd} is not associated with a terminal
359device, an exception is raised.
360Availability: \UNIX{}.
361\end{funcdesc}
362
363\begin{funcdesc}{write}{fd, str}
364Write the string \var{str} to file descriptor \var{fd}.
365Return the number of bytes actually written.
366Availability: Macintosh, \UNIX{}, Windows.
367
368Note: this function is intended for low-level I/O and must be applied
369to a file descriptor as returned by \function{open()} or
370\function{pipe()}. To write a ``file object'' returned by the
371built-in function \function{open()} or by \function{popen()} or
372\function{fdopen()}, or \code{sys.stdout} or \code{sys.stderr}, use
373its \method{write()} method.
374\end{funcdesc}
375
376
377The following data items are available for use in constructing the
378\var{flags} parameter to the \function{open()} function.
379
380\begin{datadesc}{O_RDONLY}
381\dataline{O_WRONLY}
382\dataline{O_RDWR}
383\dataline{O_NDELAY}
384\dataline{O_NONBLOCK}
385\dataline{O_APPEND}
386\dataline{O_DSYNC}
387\dataline{O_RSYNC}
388\dataline{O_SYNC}
389\dataline{O_NOCTTY}
390\dataline{O_CREAT}
391\dataline{O_EXCL}
392\dataline{O_TRUNC}
393Options for the \var{flag} argument to the \function{open()} function.
394These can be bit-wise OR'd together.
395Availability: Macintosh, \UNIX{}, Windows.
396\end{datadesc}
397
398
399\subsection{Files and Directories \label{os-file-dir}}
400
401\begin{funcdesc}{access}{path, mode}
402Check read/write/execute permissions for this process or extance of file
403\var{path}. Return \code{1} if access is granted, \code{0} if not.
404See the \UNIX{} manual for the semantics.
405Availability: \UNIX{}.
406\end{funcdesc}
407
Fred Drake6db897c1999-07-12 16:49:30 +0000408\begin{funcdesc}{chdir}{path}
409\index{directory!changing}
410Change the current working directory to \var{path}.
411Availability: Macintosh, \UNIX{}, Windows.
412\end{funcdesc}
413
414\begin{funcdesc}{getcwd}{}
415Return a string representing the current working directory.
416Availability: Macintosh, \UNIX{}, Windows.
417\end{funcdesc}
418
Fred Drake215fe2f1999-02-02 19:02:35 +0000419\begin{funcdesc}{chmod}{path, mode}
420Change the mode of \var{path} to the numeric \var{mode}.
421Availability: \UNIX{}, Windows.
422\end{funcdesc}
423
424\begin{funcdesc}{chown}{path, uid, gid}
425Change the owner and group id of \var{path} to the numeric \var{uid}
426and \var{gid}.
427Availability: \UNIX{}.
428\end{funcdesc}
429
430\begin{funcdesc}{link}{src, dst}
431Create a hard link pointing to \var{src} named \var{dst}.
432Availability: \UNIX{}.
433\end{funcdesc}
434
435\begin{funcdesc}{listdir}{path}
436Return a list containing the names of the entries in the directory.
437The list is in arbitrary order. It does not include the special
438entries \code{'.'} and \code{'..'} even if they are present in the
439directory.
440Availability: Macintosh, \UNIX{}, Windows.
441\end{funcdesc}
442
443\begin{funcdesc}{lstat}{path}
444Like \function{stat()}, but do not follow symbolic links.
445Availability: \UNIX{}.
446\end{funcdesc}
447
448\begin{funcdesc}{mkfifo}{path\optional{, mode}}
449Create a FIFO (a named pipe) named \var{path} with numeric mode
450\var{mode}. The default \var{mode} is \code{0666} (octal). The current
451umask value is first masked out from the mode.
452Availability: \UNIX{}.
453
454FIFOs are pipes that can be accessed like regular files. FIFOs exist
455until they are deleted (for example with \function{os.unlink()}).
456Generally, FIFOs are used as rendezvous between ``client'' and
457``server'' type processes: the server opens the FIFO for reading, and
458the client opens it for writing. Note that \function{mkfifo()}
459doesn't open the FIFO --- it just creates the rendezvous point.
460\end{funcdesc}
461
462\begin{funcdesc}{mkdir}{path\optional{, mode}}
463Create a directory named \var{path} with numeric mode \var{mode}.
464The default \var{mode} is \code{0777} (octal). On some systems,
465\var{mode} is ignored. Where it is used, the current umask value is
466first masked out.
467Availability: Macintosh, \UNIX{}, Windows.
468\end{funcdesc}
469
470\begin{funcdesc}{makedirs}{path\optional{, mode}}
471\index{directory!creating}
472Recursive directory creation function. Like \function{mkdir()},
473but makes all intermediate-level directories needed to contain the
474leaf directory. Throws an \exception{error} exception if the leaf
475directory already exists or cannot be created. The default \var{mode}
476is \code{0777} (octal).
477\versionadded{1.5.2}
478\end{funcdesc}
479
480\begin{funcdesc}{readlink}{path}
481Return a string representing the path to which the symbolic link
482points.
483Availability: \UNIX{}.
484\end{funcdesc}
485
486\begin{funcdesc}{remove}{path}
487Remove the file \var{path}. See \function{rmdir()} below to remove a
488directory. This is identical to the \function{unlink()} function
489documented below.
490Availability: Macintosh, \UNIX{}, Windows.
491\end{funcdesc}
492
493\begin{funcdesc}{removedirs}{path}
494\index{directory!deleting}
495Recursive directory removal function. Works like
496\function{rmdir()} except that, if the leaf directory is
497successfully removed, directories corresponding to rightmost path
498segments will be pruned way until either the whole path is consumed or
499an error is raised (which is ignored, because it generally means that
500a parent directory is not empty). Throws an \exception{error}
501exception if the leaf directory could not be successfully removed.
502\versionadded{1.5.2}
503\end{funcdesc}
504
505\begin{funcdesc}{rename}{src, dst}
506Rename the file or directory \var{src} to \var{dst}.
507Availability: Macintosh, \UNIX{}, Windows.
508\end{funcdesc}
509
510\begin{funcdesc}{renames}{old, new}
511Recursive directory or file renaming function.
512Works like \function{rename()}, except creation of any intermediate
513directories needed to make the new pathname good is attempted first.
514After the rename, directories corresponding to rightmost path segments
515of the old name will be pruned away using \function{removedirs()}.
516
517Note: this function can fail with the new directory structure made if
518you lack permissions needed to remove the leaf directory or file.
519\versionadded{1.5.2}
520\end{funcdesc}
521
522\begin{funcdesc}{rmdir}{path}
523Remove the directory \var{path}.
524Availability: Macintosh, \UNIX{}, Windows.
525\end{funcdesc}
526
527\begin{funcdesc}{stat}{path}
528Perform a \cfunction{stat()} system call on the given path. The
529return value is a tuple of at least 10 integers giving the most
530important (and portable) members of the \emph{stat} structure, in the
531order
532\code{st_mode},
533\code{st_ino},
534\code{st_dev},
535\code{st_nlink},
536\code{st_uid},
537\code{st_gid},
538\code{st_size},
539\code{st_atime},
540\code{st_mtime},
541\code{st_ctime}.
542More items may be added at the end by some implementations.
543(On MS Windows, some items are filled with dummy values.)
544Availability: Macintosh, \UNIX{}, Windows.
545
546Note: The standard module \refmodule{stat}\refstmodindex{stat} defines
547functions and constants that are useful for extracting information
548from a \ctype{stat} structure.
549\end{funcdesc}
550
551\begin{funcdesc}{statvfs}{path}
552Perform a \cfunction{statvfs()} system call on the given path. The
Guido van Rossum0c9608c1999-02-03 16:32:37 +0000553return value is a tuple of 10 integers giving the most common
Fred Drake215fe2f1999-02-02 19:02:35 +0000554members of the \ctype{statvfs} structure, in the order
555\code{f_bsize},
556\code{f_frsize},
557\code{f_blocks},
558\code{f_bfree},
559\code{f_bavail},
560\code{f_files},
561\code{f_ffree},
562\code{f_favail},
Fred Drake215fe2f1999-02-02 19:02:35 +0000563\code{f_flag},
564\code{f_namemax}.
565Availability: \UNIX{}.
566
567Note: The standard module \module{statvfs}\refstmodindex{statvfs}
568defines constants that are useful for extracting information
569from a \ctype{statvfs} structure.
570\end{funcdesc}
571
572\begin{funcdesc}{symlink}{src, dst}
573Create a symbolic link pointing to \var{src} named \var{dst}.
574Availability: \UNIX{}.
575\end{funcdesc}
576
577\begin{funcdesc}{unlink}{path}
578Remove the file \var{path}. This is the same function as
579\function{remove()}; the \function{unlink()} name is its traditional
580\UNIX{} name.
581Availability: Macintosh, \UNIX{}, Windows.
582\end{funcdesc}
583
584\begin{funcdesc}{utime}{path, (atime, mtime)}
585Set the access and modified time of the file to the given values.
586(The second argument is a tuple of two items.)
587Availability: Macintosh, \UNIX{}, Windows.
588\end{funcdesc}
589
590
591\subsection{Process Management \label{os-process}}
592
593These functions may be used to create and manage additional
594processes.
595
596
597\begin{funcdesc}{execl}{path, arg0, arg1, ...}
598This is equivalent to
599\samp{execv(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
600Availability: \UNIX{}, Windows.
601\end{funcdesc}
602
603\begin{funcdesc}{execle}{path, arg0, arg1, ..., env}
604This is equivalent to
605\samp{execve(\var{path}, (\var{arg0}, \var{arg1}, ...), \var{env})}.
606Availability: \UNIX{}, Windows.
607\end{funcdesc}
608
609\begin{funcdesc}{execlp}{path, arg0, arg1, ...}
610This is equivalent to
611\samp{execvp(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
612Availability: \UNIX{}, Windows.
613\end{funcdesc}
614
615\begin{funcdesc}{execv}{path, args}
616Execute the executable \var{path} with argument list \var{args},
617replacing the current process (i.e., the Python interpreter).
618The argument list may be a tuple or list of strings.
619Availability: \UNIX{}, Windows.
620\end{funcdesc}
621
622\begin{funcdesc}{execve}{path, args, env}
623Execute the executable \var{path} with argument list \var{args},
624and environment \var{env},
625replacing the current process (i.e., the Python interpreter).
626The argument list may be a tuple or list of strings.
627The environment must be a dictionary mapping strings to strings.
628Availability: \UNIX{}, Windows.
629\end{funcdesc}
630
631\begin{funcdesc}{execvp}{path, args}
632This is like \samp{execv(\var{path}, \var{args})} but duplicates
633the shell's actions in searching for an executable file in a list of
634directories. The directory list is obtained from
635\code{environ['PATH']}.
636Availability: \UNIX{}, Windows.
637\end{funcdesc}
638
639\begin{funcdesc}{execvpe}{path, args, env}
640This is a cross between \function{execve()} and \function{execvp()}.
641The directory list is obtained from \code{\var{env}['PATH']}.
642Availability: \UNIX{}, Windows.
643\end{funcdesc}
644
645\begin{funcdesc}{_exit}{n}
646Exit to the system with status \var{n}, without calling cleanup
647handlers, flushing stdio buffers, etc.
648Availability: \UNIX{}, Windows.
649
650Note: the standard way to exit is \code{sys.exit(\var{n})}.
651\function{_exit()} should normally only be used in the child process
652after a \function{fork()}.
653\end{funcdesc}
654
655\begin{funcdesc}{fork}{}
656Fork a child process. Return \code{0} in the child, the child's
657process id in the parent.
658Availability: \UNIX{}.
659\end{funcdesc}
660
661\begin{funcdesc}{kill}{pid, sig}
662\index{process!killing}
663\index{process!signalling}
664Kill the process \var{pid} with signal \var{sig}.
665Availability: \UNIX{}.
666\end{funcdesc}
667
668\begin{funcdesc}{nice}{increment}
669Add \var{increment} to the process's ``niceness''. Return the new
670niceness.
671Availability: \UNIX{}.
672\end{funcdesc}
673
674\begin{funcdesc}{plock}{op}
675Lock program segments into memory. The value of \var{op}
676(defined in \code{<sys/lock.h>}) determines which segments are locked.
Fred Drake39063631999-02-26 14:05:02 +0000677Availability: \UNIX{}.
Fred Drake215fe2f1999-02-02 19:02:35 +0000678\end{funcdesc}
679
680\begin{funcdesc}{spawnv}{mode, path, args}
681Execute the program \var{path} in a new process, passing the arguments
682specified in \var{args} as command-line parameters. \var{args} may be
683a list or a tuple. \var{mode} is a magic operational constant. See
684the Visual \Cpp{} Runtime Library documentation for further
Fred Drake22702081999-07-02 14:01:03 +0000685information; the constants are exposed to the Python programmer as
686listed below.
Fred Drake215fe2f1999-02-02 19:02:35 +0000687Availability: Windows.
688\versionadded{1.5.2}
689\end{funcdesc}
690
691\begin{funcdesc}{spawnve}{mode, path, args, env}
692Execute the program \var{path} in a new process, passing the arguments
693specified in \var{args} as command-line parameters and the contents of
694the mapping \var{env} as the environment. \var{args} may be a list or
695a tuple. \var{mode} is a magic operational constant. See the Visual
Fred Drake22702081999-07-02 14:01:03 +0000696\Cpp{} Runtime Library documentation for further information; the
697constants are exposed to the Python programmer as listed below.
Fred Drake215fe2f1999-02-02 19:02:35 +0000698Availability: Windows.
699\versionadded{1.5.2}
700\end{funcdesc}
701
Fred Drake9329e5e1999-02-16 19:40:19 +0000702\begin{datadesc}{P_WAIT}
703\dataline{P_NOWAIT}
704\dataline{P_NOWAITO}
705\dataline{P_OVERLAY}
706\dataline{P_DETACH}
Fred Drake215fe2f1999-02-02 19:02:35 +0000707Possible values for the \var{mode} parameter to \function{spawnv()}
708and \function{spawnve()}.
709Availability: Windows.
710\versionadded{1.5.2}
711\end{datadesc}
712
713\begin{funcdesc}{system}{command}
714Execute the command (a string) in a subshell. This is implemented by
715calling the Standard C function \cfunction{system()}, and has the
Fred Drakeec6baaf1999-04-21 18:13:31 +0000716same limitations. Changes to \code{posix.environ}, \code{sys.stdin},
Fred Drake215fe2f1999-02-02 19:02:35 +0000717etc.\ are not reflected in the environment of the executed command.
718The return value is the exit status of the process encoded in the
Fred Drake7a621281999-06-10 15:07:05 +0000719format specified for \function{wait()}, except on Windows 95 and 98,
Fred Drakea88ef001999-06-18 19:11:25 +0000720where it is always \code{0}. Note that \POSIX{} does not specify the
721meaning of the return value of the C \cfunction{system()} function,
722so the return value of the Python function is system-dependent.
Fred Drake215fe2f1999-02-02 19:02:35 +0000723Availability: \UNIX{}, Windows.
724\end{funcdesc}
725
726\begin{funcdesc}{times}{}
727Return a 5-tuple of floating point numbers indicating accumulated (CPU
728or other)
729times, in seconds. The items are: user time, system time, children's
730user time, children's system time, and elapsed real time since a fixed
Fred Drakeec6baaf1999-04-21 18:13:31 +0000731point in the past, in that order. See the \UNIX{} manual page
732\manpage{times}{2} or the corresponding Windows Platform API
733documentation.
Fred Drake215fe2f1999-02-02 19:02:35 +0000734Availability: \UNIX{}, Windows.
735\end{funcdesc}
736
737\begin{funcdesc}{wait}{}
738Wait for completion of a child process, and return a tuple containing
739its pid and exit status indication: a 16-bit number, whose low byte is
740the signal number that killed the process, and whose high byte is the
741exit status (if the signal number is zero); the high bit of the low
742byte is set if a core file was produced.
743Availability: \UNIX{}.
744\end{funcdesc}
745
746\begin{funcdesc}{waitpid}{pid, options}
Fred Drake6b330ba81999-05-25 13:42:26 +0000747Wait for completion of a child process given by process id, and return
Fred Drake215fe2f1999-02-02 19:02:35 +0000748a tuple containing its process id and exit status indication (encoded
749as for \function{wait()}). The semantics of the call are affected by
750the value of the integer \var{options}, which should be \code{0} for
751normal operation.
752Availability: \UNIX{}.
753\end{funcdesc}
754
755\begin{datadesc}{WNOHANG}
756The option for \function{waitpid()} to avoid hanging if no child
757process status is available immediately.
758Availability: \UNIX{}.
759\end{datadesc}
760
761The following functions take a process stats code as returned by
762\function{waitpid()} as a parameter. They may be used to determine
763the disposition of a process.
764
765\begin{funcdesc}{WIFSTOPPED}{status}
766Return true if the process has been stopped.
767Availability: \UNIX{}.
768\end{funcdesc}
769
770\begin{funcdesc}{WIFSIGNALED}{status}
771Return true if the process exited due to a signal.
772Availability: \UNIX{}.
773\end{funcdesc}
774
775\begin{funcdesc}{WIFEXITED}{status}
776Return true if the process exited using the \manpage{exit}{2} system
777call.
778Availability: \UNIX{}.
779\end{funcdesc}
780
781\begin{funcdesc}{WEXITSTATUS}{status}
782If \code{WIFEXITED(\var{status})} is true, return the integer
783parameter to the \manpage{exit}{2} system call. Otherwise, the return
784value is meaningless.
785Availability: \UNIX{}.
786\end{funcdesc}
787
788\begin{funcdesc}{WSTOPSIG}{status}
Fred Drake35c3ffd1999-03-04 14:08:10 +0000789Return the signal which caused the process to stop.
790Availability: \UNIX{}.
791\end{funcdesc}
792
793\begin{funcdesc}{WTERMSIG}{status}
Fred Drake215fe2f1999-02-02 19:02:35 +0000794Return the signal which caused the process to exit.
795Availability: \UNIX{}.
796\end{funcdesc}
797
798
799\subsection{Miscellanenous System Data \label{os-path}}
800
801The follow data values are used to support path manipulation
802operations. These are defined for all platforms.
803
804Higher-level operations on pathnames are defined in the
805\refmodule{os.path} module.
806
807
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000808\begin{datadesc}{curdir}
809The constant string used by the OS to refer to the current directory,
Fred Drake1a3c2a01998-08-06 15:18:23 +0000810e.g.\ \code{'.'} for \POSIX{} or \code{':'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000811\end{datadesc}
812
813\begin{datadesc}{pardir}
814The constant string used by the OS to refer to the parent directory,
Fred Drake1a3c2a01998-08-06 15:18:23 +0000815e.g.\ \code{'..'} for \POSIX{} or \code{'::'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000816\end{datadesc}
817
818\begin{datadesc}{sep}
Guido van Rossumb2afc811997-08-29 22:37:44 +0000819The character used by the OS to separate pathname components,
Fred Drake1a3c2a01998-08-06 15:18:23 +0000820e.g.\ \character{/} for \POSIX{} or \character{:} for the Macintosh.
821Note that knowing this is not sufficient to be able to parse or
822concatenate pathnames --- use \function{os.path.split()} and
823\function{os.path.join()} --- but it is occasionally useful.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000824\end{datadesc}
825
Guido van Rossumb2afc811997-08-29 22:37:44 +0000826\begin{datadesc}{altsep}
827An alternative character used by the OS to separate pathname components,
828or \code{None} if only one separator character exists. This is set to
Fred Drake215fe2f1999-02-02 19:02:35 +0000829\character{/} on DOS and Windows systems where \code{sep} is a backslash.
Guido van Rossumb2afc811997-08-29 22:37:44 +0000830\end{datadesc}
831
Guido van Rossum470be141995-03-17 16:07:09 +0000832\begin{datadesc}{pathsep}
833The character conventionally used by the OS to separate search patch
Fred Drake1a3c2a01998-08-06 15:18:23 +0000834components (as in \envvar{PATH}), e.g.\ \character{:} for \POSIX{} or
Fred Drake215fe2f1999-02-02 19:02:35 +0000835\character{;} for DOS and Windows.
Guido van Rossum9c59ce91998-06-30 15:54:27 +0000836\end{datadesc}
837
Guido van Rossum470be141995-03-17 16:07:09 +0000838\begin{datadesc}{defpath}
Fred Drake1a3c2a01998-08-06 15:18:23 +0000839The default search path used by \function{exec*p*()} if the environment
Guido van Rossum470be141995-03-17 16:07:09 +0000840doesn't have a \code{'PATH'} key.
841\end{datadesc}
842
Fred Drake215fe2f1999-02-02 19:02:35 +0000843\begin{datadesc}{linesep}
844The string used to separate (or, rather, terminate) lines on the
Fred Drakeec6baaf1999-04-21 18:13:31 +0000845current platform. This may be a single character,
846e.g.\ \code{'\e n'} for \POSIX{} or \code{'\e r'} for MacOS, or multiple
847characters, e.g.\ \code{'\e r\e n'} for MS-DOS and MS Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000848\end{datadesc}