blob: ef07b11ad2526dce8597ac529678aa359766e07c [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 Drake933d5a71999-09-17 14:38:39 +000070\code{'dos'}, \code{'mac'}, \code{'os2'}, \code{'ce'}, \code{'java'}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000071\end{datadesc}
72
73\begin{datadesc}{path}
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
Fred Drake18f7a451999-12-09 22:11:43 +0000110\begin{funcdesc}{ctermid}{}
111Return the filename corresponding to the controlling terminal of the
112process.
113Availability: \UNIX{}.
114\end{funcdesc}
115
Fred Drake215fe2f1999-02-02 19:02:35 +0000116\begin{funcdesc}{getegid}{}
117Return the current process' effective group id.
118Availability: \UNIX{}.
119\end{funcdesc}
120
121\begin{funcdesc}{geteuid}{}
Fred Drake6b330ba81999-05-25 13:42:26 +0000122\index{user!effective id}
Fred Drake215fe2f1999-02-02 19:02:35 +0000123Return the current process' effective user id.
124Availability: \UNIX{}.
125\end{funcdesc}
126
127\begin{funcdesc}{getgid}{}
Fred Drake6b330ba81999-05-25 13:42:26 +0000128\index{process!group}
Fred Drake215fe2f1999-02-02 19:02:35 +0000129Return the current process' group id.
130Availability: \UNIX{}.
131\end{funcdesc}
132
133\begin{funcdesc}{getpgrp}{}
134\index{process!group}
135Return the current process group id.
136Availability: \UNIX{}.
137\end{funcdesc}
138
139\begin{funcdesc}{getpid}{}
140\index{process!id}
141Return the current process id.
142Availability: \UNIX{}, Windows.
143\end{funcdesc}
144
145\begin{funcdesc}{getppid}{}
146\index{process!id of parent}
147Return the parent's process id.
148Availability: \UNIX{}.
149\end{funcdesc}
150
151\begin{funcdesc}{getuid}{}
Fred Drake6b330ba81999-05-25 13:42:26 +0000152\index{user!id}
Fred Drake215fe2f1999-02-02 19:02:35 +0000153Return the current process' user id.
154Availability: \UNIX{}.
155\end{funcdesc}
156
157\begin{funcdesc}{putenv}{varname, value}
158\index{environment variables!setting}
159Set the environment variable named \var{varname} to the string
160\var{value}. Such changes to the environment affect subprocesses
161started with \function{os.system()}, \function{popen()} or
162\function{fork()} and \function{execv()}.
163Availability: most flavors of \UNIX{}, Windows.
164
165When \function{putenv()} is
166supported, assignments to items in \code{os.environ} are automatically
167translated into corresponding calls to \function{putenv()}; however,
168calls to \function{putenv()} don't update \code{os.environ}, so it is
169actually preferable to assign to items of \code{os.environ}.
170\end{funcdesc}
171
172\begin{funcdesc}{setgid}{gid}
173Set the current process' group id.
174Availability: \UNIX{}.
175\end{funcdesc}
176
177\begin{funcdesc}{setpgrp}{}
178Calls the system call \cfunction{setpgrp()} or \cfunction{setpgrp(0,
1790)} depending on which version is implemented (if any). See the
180\UNIX{} manual for the semantics.
181Availability: \UNIX{}.
182\end{funcdesc}
183
184\begin{funcdesc}{setpgid}{pid, pgrp}
185Calls the system call \cfunction{setpgid()}. See the \UNIX{} manual
186for the semantics.
187Availability: \UNIX{}.
188\end{funcdesc}
189
190\begin{funcdesc}{setsid}{}
191Calls the system call \cfunction{setsid()}. See the \UNIX{} manual
192for the semantics.
193Availability: \UNIX{}.
194\end{funcdesc}
195
196\begin{funcdesc}{setuid}{uid}
Fred Drake6b330ba81999-05-25 13:42:26 +0000197\index{user!id, setting}
Fred Drake215fe2f1999-02-02 19:02:35 +0000198Set the current process' user id.
199Availability: \UNIX{}.
200\end{funcdesc}
201
202% placed in this section since it relates to errno.... a little weak ;-(
203\begin{funcdesc}{strerror}{code}
204Return the error message corresponding to the error code in
205\var{code}.
206Availability: \UNIX{}, Windows.
207\end{funcdesc}
208
209\begin{funcdesc}{umask}{mask}
210Set the current numeric umask and returns the previous umask.
211Availability: \UNIX{}, Windows.
212\end{funcdesc}
213
214\begin{funcdesc}{uname}{}
215Return a 5-tuple containing information identifying the current
216operating system. The tuple contains 5 strings:
217\code{(\var{sysname}, \var{nodename}, \var{release}, \var{version},
218\var{machine})}. Some systems truncate the nodename to 8
219characters or to the leading component; a better way to get the
220hostname is \function{socket.gethostname()}
221\withsubitem{(in module socket)}{\ttindex{gethostname()}}
222or even
223\withsubitem{(in module socket)}{\ttindex{gethostbyaddr()}}
224\code{socket.gethostbyaddr(socket.gethostname())}.
225Availability: recent flavors of \UNIX{}.
226\end{funcdesc}
227
228
229
230\subsection{File Object Creation \label{os-newstreams}}
231
232These functions create new file objects.
233
234
235\begin{funcdesc}{fdopen}{fd\optional{, mode\optional{, bufsize}}}
236Return an open file object connected to the file descriptor \var{fd}.
Fred Drake8c9fc001999-08-05 13:41:31 +0000237\index{I/O control!buffering}
Fred Drake215fe2f1999-02-02 19:02:35 +0000238The \var{mode} and \var{bufsize} arguments have the same meaning as
239the corresponding arguments to the built-in \function{open()}
240function.
241Availability: Macintosh, \UNIX{}, Windows.
242\end{funcdesc}
243
244\begin{funcdesc}{popen}{command\optional{, mode\optional{, bufsize}}}
245Open a pipe to or from \var{command}. The return value is an open
246file object connected to the pipe, which can be read or written
247depending on whether \var{mode} is \code{'r'} (default) or \code{'w'}.
248The \var{bufsize} argument has the same meaning as the corresponding
249argument to the built-in \function{open()} function. The exit status of
250the command (encoded in the format specified for \function{wait()}) is
251available as the return value of the \method{close()} method of the file
252object, except that when the exit status is zero (termination without
253errors), \code{None} is returned.
254Availability: \UNIX{}, Windows.
255\end{funcdesc}
256
Fred Drake18f7a451999-12-09 22:11:43 +0000257\begin{funcdesc}{tmpfile}{}
258Return a new file object opened in update mode (\samp{w+}). The file
259has no directory entries associated with it and will be automatically
260deleted once there are no file descriptors for the file.
261Availability: \UNIX{}.
262\end{funcdesc}
Fred Drake215fe2f1999-02-02 19:02:35 +0000263
264
265\subsection{File Descriptor Operations \label{os-fd-ops}}
266
267These functions operate on I/O streams referred to
268using file descriptors.
269
270
271\begin{funcdesc}{close}{fd}
272Close file descriptor \var{fd}.
273Availability: Macintosh, \UNIX{}, Windows.
274
275Note: this function is intended for low-level I/O and must be applied
276to a file descriptor as returned by \function{open()} or
277\function{pipe()}. To close a ``file object'' returned by the
278built-in function \function{open()} or by \function{popen()} or
279\function{fdopen()}, use its \method{close()} method.
280\end{funcdesc}
281
282\begin{funcdesc}{dup}{fd}
283Return a duplicate of file descriptor \var{fd}.
284Availability: Macintosh, \UNIX{}, Windows.
285\end{funcdesc}
286
287\begin{funcdesc}{dup2}{fd, fd2}
288Duplicate file descriptor \var{fd} to \var{fd2}, closing the latter
289first if necessary.
290Availability: \UNIX{}, Windows.
291\end{funcdesc}
292
293\begin{funcdesc}{fstat}{fd}
294Return status for file descriptor \var{fd}, like \function{stat()}.
295Availability: \UNIX{}, Windows.
296\end{funcdesc}
297
298\begin{funcdesc}{fstatvfs}{fd}
299Return information about the filesystem containing the file associated
300with file descriptor \var{fd}, like \function{statvfs()}.
301Availability: \UNIX{}.
302\end{funcdesc}
303
304\begin{funcdesc}{ftruncate}{fd, length}
305Truncate the file corresponding to file descriptor \var{fd},
306so that it is at most \var{length} bytes in size.
307Availability: \UNIX{}.
308\end{funcdesc}
309
310\begin{funcdesc}{lseek}{fd, pos, how}
311Set the current position of file descriptor \var{fd} to position
312\var{pos}, modified by \var{how}: \code{0} to set the position
313relative to the beginning of the file; \code{1} to set it relative to
314the current position; \code{2} to set it relative to the end of the
315file.
316Availability: Macintosh, \UNIX{}, Windows.
317\end{funcdesc}
318
319\begin{funcdesc}{open}{file, flags\optional{, mode}}
320Open the file \var{file} and set various flags according to
321\var{flags} and possibly its mode according to \var{mode}.
322The default \var{mode} is \code{0777} (octal), and the current umask
323value is first masked out. Return the file descriptor for the newly
324opened file.
325Availability: Macintosh, \UNIX{}, Windows.
326
327For a description of the flag and mode values, see the C run-time
328documentation; flag constants (like \constant{O_RDONLY} and
329\constant{O_WRONLY}) are defined in this module too (see below).
330
331Note: this function is intended for low-level I/O. For normal usage,
332use the built-in function \function{open()}, which returns a ``file
333object'' with \method{read()} and \method{write()} methods (and many
334more).
335\end{funcdesc}
336
337\begin{funcdesc}{pipe}{}
338Create a pipe. Return a pair of file descriptors \code{(\var{r},
339\var{w})} usable for reading and writing, respectively.
340Availability: \UNIX{}, Windows.
341\end{funcdesc}
342
343\begin{funcdesc}{read}{fd, n}
344Read at most \var{n} bytes from file descriptor \var{fd}.
345Return a string containing the bytes read.
346Availability: Macintosh, \UNIX{}, Windows.
347
348Note: this function is intended for low-level I/O and must be applied
349to a file descriptor as returned by \function{open()} or
350\function{pipe()}. To read a ``file object'' returned by the
351built-in function \function{open()} or by \function{popen()} or
352\function{fdopen()}, or \code{sys.stdin}, use its
353\method{read()} or \method{readline()} methods.
354\end{funcdesc}
355
356\begin{funcdesc}{tcgetpgrp}{fd}
357Return the process group associated with the terminal given by
358\var{fd} (an open file descriptor as returned by \function{open()}).
359Availability: \UNIX{}.
360\end{funcdesc}
361
362\begin{funcdesc}{tcsetpgrp}{fd, pg}
363Set the process group associated with the terminal given by
364\var{fd} (an open file descriptor as returned by \function{open()})
365to \var{pg}.
366Availability: \UNIX{}.
367\end{funcdesc}
368
369\begin{funcdesc}{ttyname}{fd}
370Return a string which specifies the terminal device associated with
371file-descriptor \var{fd}. If \var{fd} is not associated with a terminal
372device, an exception is raised.
373Availability: \UNIX{}.
374\end{funcdesc}
375
376\begin{funcdesc}{write}{fd, str}
377Write the string \var{str} to file descriptor \var{fd}.
378Return the number of bytes actually written.
379Availability: Macintosh, \UNIX{}, Windows.
380
381Note: this function is intended for low-level I/O and must be applied
382to a file descriptor as returned by \function{open()} or
383\function{pipe()}. To write a ``file object'' returned by the
384built-in function \function{open()} or by \function{popen()} or
385\function{fdopen()}, or \code{sys.stdout} or \code{sys.stderr}, use
386its \method{write()} method.
387\end{funcdesc}
388
389
390The following data items are available for use in constructing the
391\var{flags} parameter to the \function{open()} function.
392
393\begin{datadesc}{O_RDONLY}
394\dataline{O_WRONLY}
395\dataline{O_RDWR}
396\dataline{O_NDELAY}
397\dataline{O_NONBLOCK}
398\dataline{O_APPEND}
399\dataline{O_DSYNC}
400\dataline{O_RSYNC}
401\dataline{O_SYNC}
402\dataline{O_NOCTTY}
403\dataline{O_CREAT}
404\dataline{O_EXCL}
405\dataline{O_TRUNC}
406Options for the \var{flag} argument to the \function{open()} function.
407These can be bit-wise OR'd together.
408Availability: Macintosh, \UNIX{}, Windows.
409\end{datadesc}
410
411
412\subsection{Files and Directories \label{os-file-dir}}
413
414\begin{funcdesc}{access}{path, mode}
415Check read/write/execute permissions for this process or extance of file
416\var{path}. Return \code{1} if access is granted, \code{0} if not.
417See the \UNIX{} manual for the semantics.
418Availability: \UNIX{}.
419\end{funcdesc}
420
Fred Drake6db897c1999-07-12 16:49:30 +0000421\begin{funcdesc}{chdir}{path}
422\index{directory!changing}
423Change the current working directory to \var{path}.
424Availability: Macintosh, \UNIX{}, Windows.
425\end{funcdesc}
426
427\begin{funcdesc}{getcwd}{}
428Return a string representing the current working directory.
429Availability: Macintosh, \UNIX{}, Windows.
430\end{funcdesc}
431
Fred Drake215fe2f1999-02-02 19:02:35 +0000432\begin{funcdesc}{chmod}{path, mode}
433Change the mode of \var{path} to the numeric \var{mode}.
434Availability: \UNIX{}, Windows.
435\end{funcdesc}
436
437\begin{funcdesc}{chown}{path, uid, gid}
438Change the owner and group id of \var{path} to the numeric \var{uid}
439and \var{gid}.
440Availability: \UNIX{}.
441\end{funcdesc}
442
443\begin{funcdesc}{link}{src, dst}
444Create a hard link pointing to \var{src} named \var{dst}.
445Availability: \UNIX{}.
446\end{funcdesc}
447
448\begin{funcdesc}{listdir}{path}
449Return a list containing the names of the entries in the directory.
450The list is in arbitrary order. It does not include the special
451entries \code{'.'} and \code{'..'} even if they are present in the
452directory.
453Availability: Macintosh, \UNIX{}, Windows.
454\end{funcdesc}
455
456\begin{funcdesc}{lstat}{path}
457Like \function{stat()}, but do not follow symbolic links.
458Availability: \UNIX{}.
459\end{funcdesc}
460
461\begin{funcdesc}{mkfifo}{path\optional{, mode}}
462Create a FIFO (a named pipe) named \var{path} with numeric mode
463\var{mode}. The default \var{mode} is \code{0666} (octal). The current
464umask value is first masked out from the mode.
465Availability: \UNIX{}.
466
467FIFOs are pipes that can be accessed like regular files. FIFOs exist
468until they are deleted (for example with \function{os.unlink()}).
469Generally, FIFOs are used as rendezvous between ``client'' and
470``server'' type processes: the server opens the FIFO for reading, and
471the client opens it for writing. Note that \function{mkfifo()}
472doesn't open the FIFO --- it just creates the rendezvous point.
473\end{funcdesc}
474
475\begin{funcdesc}{mkdir}{path\optional{, mode}}
476Create a directory named \var{path} with numeric mode \var{mode}.
477The default \var{mode} is \code{0777} (octal). On some systems,
478\var{mode} is ignored. Where it is used, the current umask value is
479first masked out.
480Availability: Macintosh, \UNIX{}, Windows.
481\end{funcdesc}
482
483\begin{funcdesc}{makedirs}{path\optional{, mode}}
484\index{directory!creating}
485Recursive directory creation function. Like \function{mkdir()},
486but makes all intermediate-level directories needed to contain the
487leaf directory. Throws an \exception{error} exception if the leaf
488directory already exists or cannot be created. The default \var{mode}
489is \code{0777} (octal).
490\versionadded{1.5.2}
491\end{funcdesc}
492
493\begin{funcdesc}{readlink}{path}
494Return a string representing the path to which the symbolic link
495points.
496Availability: \UNIX{}.
497\end{funcdesc}
498
499\begin{funcdesc}{remove}{path}
500Remove the file \var{path}. See \function{rmdir()} below to remove a
501directory. This is identical to the \function{unlink()} function
502documented below.
503Availability: Macintosh, \UNIX{}, Windows.
504\end{funcdesc}
505
506\begin{funcdesc}{removedirs}{path}
507\index{directory!deleting}
508Recursive directory removal function. Works like
509\function{rmdir()} except that, if the leaf directory is
510successfully removed, directories corresponding to rightmost path
511segments will be pruned way until either the whole path is consumed or
512an error is raised (which is ignored, because it generally means that
513a parent directory is not empty). Throws an \exception{error}
514exception if the leaf directory could not be successfully removed.
515\versionadded{1.5.2}
516\end{funcdesc}
517
518\begin{funcdesc}{rename}{src, dst}
519Rename the file or directory \var{src} to \var{dst}.
520Availability: Macintosh, \UNIX{}, Windows.
521\end{funcdesc}
522
523\begin{funcdesc}{renames}{old, new}
524Recursive directory or file renaming function.
525Works like \function{rename()}, except creation of any intermediate
526directories needed to make the new pathname good is attempted first.
527After the rename, directories corresponding to rightmost path segments
528of the old name will be pruned away using \function{removedirs()}.
529
530Note: this function can fail with the new directory structure made if
531you lack permissions needed to remove the leaf directory or file.
532\versionadded{1.5.2}
533\end{funcdesc}
534
535\begin{funcdesc}{rmdir}{path}
536Remove the directory \var{path}.
537Availability: Macintosh, \UNIX{}, Windows.
538\end{funcdesc}
539
540\begin{funcdesc}{stat}{path}
541Perform a \cfunction{stat()} system call on the given path. The
542return value is a tuple of at least 10 integers giving the most
543important (and portable) members of the \emph{stat} structure, in the
544order
545\code{st_mode},
546\code{st_ino},
547\code{st_dev},
548\code{st_nlink},
549\code{st_uid},
550\code{st_gid},
551\code{st_size},
552\code{st_atime},
553\code{st_mtime},
554\code{st_ctime}.
555More items may be added at the end by some implementations.
556(On MS Windows, some items are filled with dummy values.)
557Availability: Macintosh, \UNIX{}, Windows.
558
559Note: The standard module \refmodule{stat}\refstmodindex{stat} defines
560functions and constants that are useful for extracting information
561from a \ctype{stat} structure.
562\end{funcdesc}
563
564\begin{funcdesc}{statvfs}{path}
565Perform a \cfunction{statvfs()} system call on the given path. The
Guido van Rossum0c9608c1999-02-03 16:32:37 +0000566return value is a tuple of 10 integers giving the most common
Fred Drake215fe2f1999-02-02 19:02:35 +0000567members of the \ctype{statvfs} structure, in the order
568\code{f_bsize},
569\code{f_frsize},
570\code{f_blocks},
571\code{f_bfree},
572\code{f_bavail},
573\code{f_files},
574\code{f_ffree},
575\code{f_favail},
Fred Drake215fe2f1999-02-02 19:02:35 +0000576\code{f_flag},
577\code{f_namemax}.
578Availability: \UNIX{}.
579
580Note: The standard module \module{statvfs}\refstmodindex{statvfs}
581defines constants that are useful for extracting information
582from a \ctype{statvfs} structure.
583\end{funcdesc}
584
585\begin{funcdesc}{symlink}{src, dst}
586Create a symbolic link pointing to \var{src} named \var{dst}.
587Availability: \UNIX{}.
588\end{funcdesc}
589
Fred Drake18f7a451999-12-09 22:11:43 +0000590\begin{funcdesc}{tempnam}{\optional{dir\optional{, prefix}}}
591Return a unique path name that is reasonable for creating a temporary
592file. This will be an absolute path that names a potential directory
593entry in the directory \var{dir} or a common location for temporary
594files if \var{dir} is omitted or \code{None}. If given and not
595\code{None}, \var{prefix} is used to provide a short prefix to the
596filename. Applications are responsible for properly creating and
597managing files created using paths returned by \function{tempnam()};
598no automatic cleanup is provided.
599\end{funcdesc}
600
601\begin{funcdesc}{tmpnam}{}
602Return a unique path name that is reasonable for creating a temporary
603file. This will be an absolute path that names a potential directory
604entry in a common location for temporary files. Applications are
605responsible for properly creating and managing files created using
606paths returned by \function{tmpnam()}; no automatic cleanup is
607provided.
608\end{funcdesc}
609
610\begin{datadesc}{TMP_MAX}
611The maximum number of unique names that \function{tmpnam()} will
612generate before reusing names.
613\end{datadesc}
614
Fred Drake215fe2f1999-02-02 19:02:35 +0000615\begin{funcdesc}{unlink}{path}
616Remove the file \var{path}. This is the same function as
617\function{remove()}; the \function{unlink()} name is its traditional
618\UNIX{} name.
619Availability: Macintosh, \UNIX{}, Windows.
620\end{funcdesc}
621
622\begin{funcdesc}{utime}{path, (atime, mtime)}
623Set the access and modified time of the file to the given values.
624(The second argument is a tuple of two items.)
625Availability: Macintosh, \UNIX{}, Windows.
626\end{funcdesc}
627
628
629\subsection{Process Management \label{os-process}}
630
Fred Drake18f7a451999-12-09 22:11:43 +0000631These functions may be used to create and manage processes.
Fred Drake215fe2f1999-02-02 19:02:35 +0000632
633
Fred Drake18f7a451999-12-09 22:11:43 +0000634\begin{funcdesc}{abort}{}
635Generate a \constant{SIGABRT} signal to the current process. On
636\UNIX, the default behavior is to produce a core dump; on Windows, the
637process immediately returns an exit code of \code{3}. Be aware that
638programs which use \function{signal.signal()} to register a handler
639for \constant{SIGABRT} will behave differently.
640Availability: \UNIX, Windows.
641\end{funcdesc}
642
Fred Drake215fe2f1999-02-02 19:02:35 +0000643\begin{funcdesc}{execl}{path, arg0, arg1, ...}
644This is equivalent to
645\samp{execv(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
646Availability: \UNIX{}, Windows.
647\end{funcdesc}
648
649\begin{funcdesc}{execle}{path, arg0, arg1, ..., env}
650This is equivalent to
651\samp{execve(\var{path}, (\var{arg0}, \var{arg1}, ...), \var{env})}.
652Availability: \UNIX{}, Windows.
653\end{funcdesc}
654
655\begin{funcdesc}{execlp}{path, arg0, arg1, ...}
656This is equivalent to
657\samp{execvp(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
658Availability: \UNIX{}, Windows.
659\end{funcdesc}
660
661\begin{funcdesc}{execv}{path, args}
662Execute the executable \var{path} with argument list \var{args},
663replacing the current process (i.e., the Python interpreter).
664The argument list may be a tuple or list of strings.
665Availability: \UNIX{}, Windows.
666\end{funcdesc}
667
668\begin{funcdesc}{execve}{path, args, env}
669Execute the executable \var{path} with argument list \var{args},
670and environment \var{env},
671replacing the current process (i.e., the Python interpreter).
672The argument list may be a tuple or list of strings.
673The environment must be a dictionary mapping strings to strings.
674Availability: \UNIX{}, Windows.
675\end{funcdesc}
676
677\begin{funcdesc}{execvp}{path, args}
678This is like \samp{execv(\var{path}, \var{args})} but duplicates
679the shell's actions in searching for an executable file in a list of
680directories. The directory list is obtained from
681\code{environ['PATH']}.
682Availability: \UNIX{}, Windows.
683\end{funcdesc}
684
685\begin{funcdesc}{execvpe}{path, args, env}
686This is a cross between \function{execve()} and \function{execvp()}.
687The directory list is obtained from \code{\var{env}['PATH']}.
688Availability: \UNIX{}, Windows.
689\end{funcdesc}
690
691\begin{funcdesc}{_exit}{n}
692Exit to the system with status \var{n}, without calling cleanup
693handlers, flushing stdio buffers, etc.
694Availability: \UNIX{}, Windows.
695
696Note: the standard way to exit is \code{sys.exit(\var{n})}.
697\function{_exit()} should normally only be used in the child process
698after a \function{fork()}.
699\end{funcdesc}
700
701\begin{funcdesc}{fork}{}
702Fork a child process. Return \code{0} in the child, the child's
703process id in the parent.
704Availability: \UNIX{}.
705\end{funcdesc}
706
707\begin{funcdesc}{kill}{pid, sig}
708\index{process!killing}
709\index{process!signalling}
710Kill the process \var{pid} with signal \var{sig}.
711Availability: \UNIX{}.
712\end{funcdesc}
713
714\begin{funcdesc}{nice}{increment}
715Add \var{increment} to the process's ``niceness''. Return the new
716niceness.
717Availability: \UNIX{}.
718\end{funcdesc}
719
720\begin{funcdesc}{plock}{op}
721Lock program segments into memory. The value of \var{op}
722(defined in \code{<sys/lock.h>}) determines which segments are locked.
Fred Drake39063631999-02-26 14:05:02 +0000723Availability: \UNIX{}.
Fred Drake215fe2f1999-02-02 19:02:35 +0000724\end{funcdesc}
725
726\begin{funcdesc}{spawnv}{mode, path, args}
727Execute the program \var{path} in a new process, passing the arguments
728specified in \var{args} as command-line parameters. \var{args} may be
729a list or a tuple. \var{mode} is a magic operational constant. See
730the Visual \Cpp{} Runtime Library documentation for further
Fred Drake22702081999-07-02 14:01:03 +0000731information; the constants are exposed to the Python programmer as
732listed below.
Fred Drake215fe2f1999-02-02 19:02:35 +0000733Availability: Windows.
734\versionadded{1.5.2}
735\end{funcdesc}
736
737\begin{funcdesc}{spawnve}{mode, path, args, env}
738Execute the program \var{path} in a new process, passing the arguments
739specified in \var{args} as command-line parameters and the contents of
740the mapping \var{env} as the environment. \var{args} may be a list or
741a tuple. \var{mode} is a magic operational constant. See the Visual
Fred Drake22702081999-07-02 14:01:03 +0000742\Cpp{} Runtime Library documentation for further information; the
743constants are exposed to the Python programmer as listed below.
Fred Drake215fe2f1999-02-02 19:02:35 +0000744Availability: Windows.
745\versionadded{1.5.2}
746\end{funcdesc}
747
Fred Drake9329e5e1999-02-16 19:40:19 +0000748\begin{datadesc}{P_WAIT}
749\dataline{P_NOWAIT}
750\dataline{P_NOWAITO}
751\dataline{P_OVERLAY}
752\dataline{P_DETACH}
Fred Drake215fe2f1999-02-02 19:02:35 +0000753Possible values for the \var{mode} parameter to \function{spawnv()}
754and \function{spawnve()}.
755Availability: Windows.
756\versionadded{1.5.2}
757\end{datadesc}
758
759\begin{funcdesc}{system}{command}
760Execute the command (a string) in a subshell. This is implemented by
761calling the Standard C function \cfunction{system()}, and has the
Fred Drakeec6baaf1999-04-21 18:13:31 +0000762same limitations. Changes to \code{posix.environ}, \code{sys.stdin},
Fred Drake215fe2f1999-02-02 19:02:35 +0000763etc.\ are not reflected in the environment of the executed command.
764The return value is the exit status of the process encoded in the
Fred Drake7a621281999-06-10 15:07:05 +0000765format specified for \function{wait()}, except on Windows 95 and 98,
Fred Drakea88ef001999-06-18 19:11:25 +0000766where it is always \code{0}. Note that \POSIX{} does not specify the
767meaning of the return value of the C \cfunction{system()} function,
768so the return value of the Python function is system-dependent.
Fred Drake215fe2f1999-02-02 19:02:35 +0000769Availability: \UNIX{}, Windows.
770\end{funcdesc}
771
772\begin{funcdesc}{times}{}
773Return a 5-tuple of floating point numbers indicating accumulated (CPU
774or other)
775times, in seconds. The items are: user time, system time, children's
776user time, children's system time, and elapsed real time since a fixed
Fred Drakeec6baaf1999-04-21 18:13:31 +0000777point in the past, in that order. See the \UNIX{} manual page
778\manpage{times}{2} or the corresponding Windows Platform API
779documentation.
Fred Drake215fe2f1999-02-02 19:02:35 +0000780Availability: \UNIX{}, Windows.
781\end{funcdesc}
782
783\begin{funcdesc}{wait}{}
784Wait for completion of a child process, and return a tuple containing
785its pid and exit status indication: a 16-bit number, whose low byte is
786the signal number that killed the process, and whose high byte is the
787exit status (if the signal number is zero); the high bit of the low
788byte is set if a core file was produced.
789Availability: \UNIX{}.
790\end{funcdesc}
791
792\begin{funcdesc}{waitpid}{pid, options}
Fred Drake31e5e371999-08-13 13:36:33 +0000793Wait for completion of a child process given by process id \var{pid},
794and return a tuple containing its process id and exit status
795indication (encoded as for \function{wait()}). The semantics of the
796call are affected by the value of the integer \var{options}, which
797should be \code{0} for normal operation.
Fred Drake215fe2f1999-02-02 19:02:35 +0000798Availability: \UNIX{}.
Fred Drake31e5e371999-08-13 13:36:33 +0000799
800If \var{pid} is greater than \code{0}, \function{waitpid()} requests
801status information for that specific process. If \var{pid} is
802\code{0}, the request is for the status of any child in the process
803group of the current process. If \var{pid} is \code{-1}, the request
804pertains to any child of the current process. If \var{pid} is less
805than \code{-1}, status is requested for any process in the process
806group \code{-\var{pid}} (the absolute value of \var{pid}).
Fred Drake215fe2f1999-02-02 19:02:35 +0000807\end{funcdesc}
808
809\begin{datadesc}{WNOHANG}
810The option for \function{waitpid()} to avoid hanging if no child
811process status is available immediately.
812Availability: \UNIX{}.
813\end{datadesc}
814
815The following functions take a process stats code as returned by
816\function{waitpid()} as a parameter. They may be used to determine
817the disposition of a process.
818
819\begin{funcdesc}{WIFSTOPPED}{status}
820Return true if the process has been stopped.
821Availability: \UNIX{}.
822\end{funcdesc}
823
824\begin{funcdesc}{WIFSIGNALED}{status}
825Return true if the process exited due to a signal.
826Availability: \UNIX{}.
827\end{funcdesc}
828
829\begin{funcdesc}{WIFEXITED}{status}
830Return true if the process exited using the \manpage{exit}{2} system
831call.
832Availability: \UNIX{}.
833\end{funcdesc}
834
835\begin{funcdesc}{WEXITSTATUS}{status}
836If \code{WIFEXITED(\var{status})} is true, return the integer
837parameter to the \manpage{exit}{2} system call. Otherwise, the return
838value is meaningless.
839Availability: \UNIX{}.
840\end{funcdesc}
841
842\begin{funcdesc}{WSTOPSIG}{status}
Fred Drake35c3ffd1999-03-04 14:08:10 +0000843Return the signal which caused the process to stop.
844Availability: \UNIX{}.
845\end{funcdesc}
846
847\begin{funcdesc}{WTERMSIG}{status}
Fred Drake215fe2f1999-02-02 19:02:35 +0000848Return the signal which caused the process to exit.
849Availability: \UNIX{}.
850\end{funcdesc}
851
852
853\subsection{Miscellanenous System Data \label{os-path}}
854
855The follow data values are used to support path manipulation
856operations. These are defined for all platforms.
857
858Higher-level operations on pathnames are defined in the
859\refmodule{os.path} module.
860
861
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000862\begin{datadesc}{curdir}
863The constant string used by the OS to refer to the current directory,
Fred Drake1a3c2a01998-08-06 15:18:23 +0000864e.g.\ \code{'.'} for \POSIX{} or \code{':'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000865\end{datadesc}
866
867\begin{datadesc}{pardir}
868The constant string used by the OS to refer to the parent directory,
Fred Drake1a3c2a01998-08-06 15:18:23 +0000869e.g.\ \code{'..'} for \POSIX{} or \code{'::'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000870\end{datadesc}
871
872\begin{datadesc}{sep}
Guido van Rossumb2afc811997-08-29 22:37:44 +0000873The character used by the OS to separate pathname components,
Fred Drake1a3c2a01998-08-06 15:18:23 +0000874e.g.\ \character{/} for \POSIX{} or \character{:} for the Macintosh.
875Note that knowing this is not sufficient to be able to parse or
876concatenate pathnames --- use \function{os.path.split()} and
877\function{os.path.join()} --- but it is occasionally useful.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000878\end{datadesc}
879
Guido van Rossumb2afc811997-08-29 22:37:44 +0000880\begin{datadesc}{altsep}
881An alternative character used by the OS to separate pathname components,
882or \code{None} if only one separator character exists. This is set to
Fred Drake215fe2f1999-02-02 19:02:35 +0000883\character{/} on DOS and Windows systems where \code{sep} is a backslash.
Guido van Rossumb2afc811997-08-29 22:37:44 +0000884\end{datadesc}
885
Guido van Rossum470be141995-03-17 16:07:09 +0000886\begin{datadesc}{pathsep}
887The character conventionally used by the OS to separate search patch
Fred Drake1a3c2a01998-08-06 15:18:23 +0000888components (as in \envvar{PATH}), e.g.\ \character{:} for \POSIX{} or
Fred Drake215fe2f1999-02-02 19:02:35 +0000889\character{;} for DOS and Windows.
Guido van Rossum9c59ce91998-06-30 15:54:27 +0000890\end{datadesc}
891
Guido van Rossum470be141995-03-17 16:07:09 +0000892\begin{datadesc}{defpath}
Fred Drake1a3c2a01998-08-06 15:18:23 +0000893The default search path used by \function{exec*p*()} if the environment
Guido van Rossum470be141995-03-17 16:07:09 +0000894doesn't have a \code{'PATH'} key.
895\end{datadesc}
896
Fred Drake215fe2f1999-02-02 19:02:35 +0000897\begin{datadesc}{linesep}
898The string used to separate (or, rather, terminate) lines on the
Fred Drakeec6baaf1999-04-21 18:13:31 +0000899current platform. This may be a single character,
900e.g.\ \code{'\e n'} for \POSIX{} or \code{'\e r'} for MacOS, or multiple
901characters, e.g.\ \code{'\e r\e n'} for MS-DOS and MS Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000902\end{datadesc}