blob: c506a0507f639728bcb0e6240da12c8e8e79e1cf [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
Fred Drake88f6ca21999-12-15 19:39:04 +0000133\begin{funcdesc}{getgroups}{}
134Return list of supplemental group ids associated with the current
135process.
136Availability: \UNIX{}.
137\end{funcdesc}
138
139\begin{funcdesc}{getlogin}{}
140Return the actual login name for the current process, even if there
141are multiple login names which map to the same user id.
142Availability: \UNIX{}.
143\end{funcdesc}
144
Fred Drake215fe2f1999-02-02 19:02:35 +0000145\begin{funcdesc}{getpgrp}{}
146\index{process!group}
147Return the current process group id.
148Availability: \UNIX{}.
149\end{funcdesc}
150
151\begin{funcdesc}{getpid}{}
152\index{process!id}
153Return the current process id.
154Availability: \UNIX{}, Windows.
155\end{funcdesc}
156
157\begin{funcdesc}{getppid}{}
158\index{process!id of parent}
159Return the parent's process id.
160Availability: \UNIX{}.
161\end{funcdesc}
162
163\begin{funcdesc}{getuid}{}
Fred Drake6b330ba81999-05-25 13:42:26 +0000164\index{user!id}
Fred Drake215fe2f1999-02-02 19:02:35 +0000165Return the current process' user id.
166Availability: \UNIX{}.
167\end{funcdesc}
168
169\begin{funcdesc}{putenv}{varname, value}
170\index{environment variables!setting}
171Set the environment variable named \var{varname} to the string
172\var{value}. Such changes to the environment affect subprocesses
173started with \function{os.system()}, \function{popen()} or
174\function{fork()} and \function{execv()}.
175Availability: most flavors of \UNIX{}, Windows.
176
177When \function{putenv()} is
178supported, assignments to items in \code{os.environ} are automatically
179translated into corresponding calls to \function{putenv()}; however,
180calls to \function{putenv()} don't update \code{os.environ}, so it is
181actually preferable to assign to items of \code{os.environ}.
182\end{funcdesc}
183
184\begin{funcdesc}{setgid}{gid}
185Set the current process' group id.
186Availability: \UNIX{}.
187\end{funcdesc}
188
189\begin{funcdesc}{setpgrp}{}
190Calls the system call \cfunction{setpgrp()} or \cfunction{setpgrp(0,
1910)} depending on which version is implemented (if any). See the
192\UNIX{} manual for the semantics.
193Availability: \UNIX{}.
194\end{funcdesc}
195
196\begin{funcdesc}{setpgid}{pid, pgrp}
197Calls the system call \cfunction{setpgid()}. See the \UNIX{} manual
198for the semantics.
199Availability: \UNIX{}.
200\end{funcdesc}
201
202\begin{funcdesc}{setsid}{}
203Calls the system call \cfunction{setsid()}. See the \UNIX{} manual
204for the semantics.
205Availability: \UNIX{}.
206\end{funcdesc}
207
208\begin{funcdesc}{setuid}{uid}
Fred Drake6b330ba81999-05-25 13:42:26 +0000209\index{user!id, setting}
Fred Drake215fe2f1999-02-02 19:02:35 +0000210Set the current process' user id.
211Availability: \UNIX{}.
212\end{funcdesc}
213
214% placed in this section since it relates to errno.... a little weak ;-(
215\begin{funcdesc}{strerror}{code}
216Return the error message corresponding to the error code in
217\var{code}.
218Availability: \UNIX{}, Windows.
219\end{funcdesc}
220
221\begin{funcdesc}{umask}{mask}
222Set the current numeric umask and returns the previous umask.
223Availability: \UNIX{}, Windows.
224\end{funcdesc}
225
226\begin{funcdesc}{uname}{}
227Return a 5-tuple containing information identifying the current
228operating system. The tuple contains 5 strings:
229\code{(\var{sysname}, \var{nodename}, \var{release}, \var{version},
230\var{machine})}. Some systems truncate the nodename to 8
231characters or to the leading component; a better way to get the
232hostname is \function{socket.gethostname()}
233\withsubitem{(in module socket)}{\ttindex{gethostname()}}
234or even
235\withsubitem{(in module socket)}{\ttindex{gethostbyaddr()}}
236\code{socket.gethostbyaddr(socket.gethostname())}.
237Availability: recent flavors of \UNIX{}.
238\end{funcdesc}
239
240
241
242\subsection{File Object Creation \label{os-newstreams}}
243
244These functions create new file objects.
245
246
247\begin{funcdesc}{fdopen}{fd\optional{, mode\optional{, bufsize}}}
248Return an open file object connected to the file descriptor \var{fd}.
Fred Drake8c9fc001999-08-05 13:41:31 +0000249\index{I/O control!buffering}
Fred Drake215fe2f1999-02-02 19:02:35 +0000250The \var{mode} and \var{bufsize} arguments have the same meaning as
251the corresponding arguments to the built-in \function{open()}
252function.
253Availability: Macintosh, \UNIX{}, Windows.
254\end{funcdesc}
255
256\begin{funcdesc}{popen}{command\optional{, mode\optional{, bufsize}}}
257Open a pipe to or from \var{command}. The return value is an open
258file object connected to the pipe, which can be read or written
259depending on whether \var{mode} is \code{'r'} (default) or \code{'w'}.
260The \var{bufsize} argument has the same meaning as the corresponding
261argument to the built-in \function{open()} function. The exit status of
262the command (encoded in the format specified for \function{wait()}) is
263available as the return value of the \method{close()} method of the file
264object, except that when the exit status is zero (termination without
Fred Drake38e5d272000-04-03 20:13:55 +0000265errors), \code{None} is returned. \strong{Note:} This function
266behaves unreliably under Windows due to the native implementation of
267\cfunction{popen()}.
Fred Drake215fe2f1999-02-02 19:02:35 +0000268Availability: \UNIX{}, Windows.
269\end{funcdesc}
270
Fred Drake18f7a451999-12-09 22:11:43 +0000271\begin{funcdesc}{tmpfile}{}
272Return a new file object opened in update mode (\samp{w+}). The file
273has no directory entries associated with it and will be automatically
274deleted once there are no file descriptors for the file.
275Availability: \UNIX{}.
276\end{funcdesc}
Fred Drake215fe2f1999-02-02 19:02:35 +0000277
278
279\subsection{File Descriptor Operations \label{os-fd-ops}}
280
281These functions operate on I/O streams referred to
282using file descriptors.
283
284
285\begin{funcdesc}{close}{fd}
286Close file descriptor \var{fd}.
287Availability: Macintosh, \UNIX{}, Windows.
288
289Note: this function is intended for low-level I/O and must be applied
290to a file descriptor as returned by \function{open()} or
291\function{pipe()}. To close a ``file object'' returned by the
292built-in function \function{open()} or by \function{popen()} or
293\function{fdopen()}, use its \method{close()} method.
294\end{funcdesc}
295
296\begin{funcdesc}{dup}{fd}
297Return a duplicate of file descriptor \var{fd}.
298Availability: Macintosh, \UNIX{}, Windows.
299\end{funcdesc}
300
301\begin{funcdesc}{dup2}{fd, fd2}
302Duplicate file descriptor \var{fd} to \var{fd2}, closing the latter
303first if necessary.
304Availability: \UNIX{}, Windows.
305\end{funcdesc}
306
Fred Drake88f6ca21999-12-15 19:39:04 +0000307\begin{funcdesc}{fpathconf}{fd, name}
308Return system configration information relevant to an open file.
309\var{name} specifies the configuration value to retrieve; it may be a
310string which is the name of a defined system value; these names are
311specified in a number of standards (\POSIX.1, Unix95, Unix98, and
312others). Some platforms define additional names as well. The names
313known to the host operating system are given in the
314\code{pathconf_names} dictionary. For configuration variables not
315included in that mapping, passing an integer for \var{name} is also
316accepted.
317Availability: \UNIX{}.
318
319If \var{name} is a string and is not known, \exception{ValueError} is
320raised. If a specific value for \var{name} is not supported by the
321host system, even if it is included in \code{pathconf_names}, an
322\exception{OSError} is raised with \constant{errno.EINVAL} for the
323error number.
324\end{funcdesc}
325
Fred Drake215fe2f1999-02-02 19:02:35 +0000326\begin{funcdesc}{fstat}{fd}
327Return status for file descriptor \var{fd}, like \function{stat()}.
328Availability: \UNIX{}, Windows.
329\end{funcdesc}
330
331\begin{funcdesc}{fstatvfs}{fd}
332Return information about the filesystem containing the file associated
333with file descriptor \var{fd}, like \function{statvfs()}.
334Availability: \UNIX{}.
335\end{funcdesc}
336
337\begin{funcdesc}{ftruncate}{fd, length}
338Truncate the file corresponding to file descriptor \var{fd},
339so that it is at most \var{length} bytes in size.
340Availability: \UNIX{}.
341\end{funcdesc}
342
343\begin{funcdesc}{lseek}{fd, pos, how}
344Set the current position of file descriptor \var{fd} to position
345\var{pos}, modified by \var{how}: \code{0} to set the position
346relative to the beginning of the file; \code{1} to set it relative to
347the current position; \code{2} to set it relative to the end of the
348file.
349Availability: Macintosh, \UNIX{}, Windows.
350\end{funcdesc}
351
352\begin{funcdesc}{open}{file, flags\optional{, mode}}
353Open the file \var{file} and set various flags according to
354\var{flags} and possibly its mode according to \var{mode}.
355The default \var{mode} is \code{0777} (octal), and the current umask
356value is first masked out. Return the file descriptor for the newly
357opened file.
358Availability: Macintosh, \UNIX{}, Windows.
359
360For a description of the flag and mode values, see the C run-time
361documentation; flag constants (like \constant{O_RDONLY} and
362\constant{O_WRONLY}) are defined in this module too (see below).
363
364Note: this function is intended for low-level I/O. For normal usage,
365use the built-in function \function{open()}, which returns a ``file
366object'' with \method{read()} and \method{write()} methods (and many
367more).
368\end{funcdesc}
369
Fred Drakec82634c2000-06-28 17:27:48 +0000370\begin{funcdesc}{openpty}{}
371Open a new pseudo-terminal pair. Return a pair of file descriptors
372\code{(\var{master}, \var{slave})} for the pty and the tty,
373respectively. For a (slightly) more portable approach, use the
374\refmodule{pty}\refstmodindex{pty} module.
375Availability: Some flavors of \UNIX{}
376\end{funcdesc}
377
Fred Drake215fe2f1999-02-02 19:02:35 +0000378\begin{funcdesc}{pipe}{}
379Create a pipe. Return a pair of file descriptors \code{(\var{r},
380\var{w})} usable for reading and writing, respectively.
381Availability: \UNIX{}, Windows.
382\end{funcdesc}
383
384\begin{funcdesc}{read}{fd, n}
385Read at most \var{n} bytes from file descriptor \var{fd}.
386Return a string containing the bytes read.
387Availability: Macintosh, \UNIX{}, Windows.
388
389Note: this function is intended for low-level I/O and must be applied
390to a file descriptor as returned by \function{open()} or
391\function{pipe()}. To read a ``file object'' returned by the
392built-in function \function{open()} or by \function{popen()} or
393\function{fdopen()}, or \code{sys.stdin}, use its
394\method{read()} or \method{readline()} methods.
395\end{funcdesc}
396
397\begin{funcdesc}{tcgetpgrp}{fd}
398Return the process group associated with the terminal given by
399\var{fd} (an open file descriptor as returned by \function{open()}).
400Availability: \UNIX{}.
401\end{funcdesc}
402
403\begin{funcdesc}{tcsetpgrp}{fd, pg}
404Set the process group associated with the terminal given by
405\var{fd} (an open file descriptor as returned by \function{open()})
406to \var{pg}.
407Availability: \UNIX{}.
408\end{funcdesc}
409
410\begin{funcdesc}{ttyname}{fd}
411Return a string which specifies the terminal device associated with
412file-descriptor \var{fd}. If \var{fd} is not associated with a terminal
413device, an exception is raised.
414Availability: \UNIX{}.
415\end{funcdesc}
416
417\begin{funcdesc}{write}{fd, str}
418Write the string \var{str} to file descriptor \var{fd}.
419Return the number of bytes actually written.
420Availability: Macintosh, \UNIX{}, Windows.
421
422Note: this function is intended for low-level I/O and must be applied
423to a file descriptor as returned by \function{open()} or
424\function{pipe()}. To write a ``file object'' returned by the
425built-in function \function{open()} or by \function{popen()} or
426\function{fdopen()}, or \code{sys.stdout} or \code{sys.stderr}, use
427its \method{write()} method.
428\end{funcdesc}
429
430
431The following data items are available for use in constructing the
432\var{flags} parameter to the \function{open()} function.
433
434\begin{datadesc}{O_RDONLY}
435\dataline{O_WRONLY}
436\dataline{O_RDWR}
437\dataline{O_NDELAY}
438\dataline{O_NONBLOCK}
439\dataline{O_APPEND}
440\dataline{O_DSYNC}
441\dataline{O_RSYNC}
442\dataline{O_SYNC}
443\dataline{O_NOCTTY}
444\dataline{O_CREAT}
445\dataline{O_EXCL}
446\dataline{O_TRUNC}
447Options for the \var{flag} argument to the \function{open()} function.
448These can be bit-wise OR'd together.
449Availability: Macintosh, \UNIX{}, Windows.
450\end{datadesc}
451
452
453\subsection{Files and Directories \label{os-file-dir}}
454
455\begin{funcdesc}{access}{path, mode}
Fred Drake38e5d272000-04-03 20:13:55 +0000456Check read/write/execute permissions for this process or existence of
457file \var{path}. \var{mode} should be \constant{F_OK} to test the
458existence of \var{path}, or it can be the inclusive OR of one or more
459of \constant{R_OK}, \constant{W_OK}, and \constant{X_OK} to test
460permissions. Return \code{1} if access is allowed, \code{0} if not.
461See the \UNIX{} man page \manpage{access}{2} for more information.
Fred Drake215fe2f1999-02-02 19:02:35 +0000462Availability: \UNIX{}.
463\end{funcdesc}
464
Fred Drake38e5d272000-04-03 20:13:55 +0000465\begin{datadesc}{F_OK}
466 Value to pass as the \var{mode} parameter of \function{access()} to
467 test the existence of \var{path}.
468\end{datadesc}
469
470\begin{datadesc}{R_OK}
471 Value to include in the \var{mode} parameter of \function{access()}
472 to test the readability of \var{path}.
473\end{datadesc}
474
475\begin{datadesc}{W_OK}
476 Value to include in the \var{mode} parameter of \function{access()}
477 to test the writability of \var{path}.
478\end{datadesc}
479
480\begin{datadesc}{X_OK}
481 Value to include in the \var{mode} parameter of \function{access()}
482 to determine if \var{path} can be executed.
483\end{datadesc}
484
Fred Drake6db897c1999-07-12 16:49:30 +0000485\begin{funcdesc}{chdir}{path}
486\index{directory!changing}
487Change the current working directory to \var{path}.
488Availability: Macintosh, \UNIX{}, Windows.
489\end{funcdesc}
490
491\begin{funcdesc}{getcwd}{}
492Return a string representing the current working directory.
493Availability: Macintosh, \UNIX{}, Windows.
494\end{funcdesc}
495
Fred Drake215fe2f1999-02-02 19:02:35 +0000496\begin{funcdesc}{chmod}{path, mode}
497Change the mode of \var{path} to the numeric \var{mode}.
498Availability: \UNIX{}, Windows.
499\end{funcdesc}
500
501\begin{funcdesc}{chown}{path, uid, gid}
502Change the owner and group id of \var{path} to the numeric \var{uid}
503and \var{gid}.
504Availability: \UNIX{}.
505\end{funcdesc}
506
507\begin{funcdesc}{link}{src, dst}
508Create a hard link pointing to \var{src} named \var{dst}.
509Availability: \UNIX{}.
510\end{funcdesc}
511
512\begin{funcdesc}{listdir}{path}
513Return a list containing the names of the entries in the directory.
514The list is in arbitrary order. It does not include the special
515entries \code{'.'} and \code{'..'} even if they are present in the
516directory.
517Availability: Macintosh, \UNIX{}, Windows.
518\end{funcdesc}
519
520\begin{funcdesc}{lstat}{path}
521Like \function{stat()}, but do not follow symbolic links.
522Availability: \UNIX{}.
523\end{funcdesc}
524
525\begin{funcdesc}{mkfifo}{path\optional{, mode}}
526Create a FIFO (a named pipe) named \var{path} with numeric mode
527\var{mode}. The default \var{mode} is \code{0666} (octal). The current
528umask value is first masked out from the mode.
529Availability: \UNIX{}.
530
531FIFOs are pipes that can be accessed like regular files. FIFOs exist
532until they are deleted (for example with \function{os.unlink()}).
533Generally, FIFOs are used as rendezvous between ``client'' and
534``server'' type processes: the server opens the FIFO for reading, and
535the client opens it for writing. Note that \function{mkfifo()}
536doesn't open the FIFO --- it just creates the rendezvous point.
537\end{funcdesc}
538
539\begin{funcdesc}{mkdir}{path\optional{, mode}}
540Create a directory named \var{path} with numeric mode \var{mode}.
541The default \var{mode} is \code{0777} (octal). On some systems,
542\var{mode} is ignored. Where it is used, the current umask value is
543first masked out.
544Availability: Macintosh, \UNIX{}, Windows.
545\end{funcdesc}
546
547\begin{funcdesc}{makedirs}{path\optional{, mode}}
548\index{directory!creating}
549Recursive directory creation function. Like \function{mkdir()},
550but makes all intermediate-level directories needed to contain the
551leaf directory. Throws an \exception{error} exception if the leaf
552directory already exists or cannot be created. The default \var{mode}
553is \code{0777} (octal).
554\versionadded{1.5.2}
555\end{funcdesc}
556
Fred Drake88f6ca21999-12-15 19:39:04 +0000557\begin{funcdesc}{pathconf}{path, name}
558Return system configration information relevant to a named file.
559\var{name} specifies the configuration value to retrieve; it may be a
560string which is the name of a defined system value; these names are
561specified in a number of standards (\POSIX.1, Unix95, Unix98, and
562others). Some platforms define additional names as well. The names
563known to the host operating system are given in the
564\code{pathconf_names} dictionary. For configuration variables not
565included in that mapping, passing an integer for \var{name} is also
566accepted.
567Availability: \UNIX{}.
568
569If \var{name} is a string and is not known, \exception{ValueError} is
570raised. If a specific value for \var{name} is not supported by the
571host system, even if it is included in \code{pathconf_names}, an
572\exception{OSError} is raised with \constant{errno.EINVAL} for the
573error number.
574\end{funcdesc}
575
576\begin{datadesc}{pathconf_names}
577Dictionary mapping names accepted by \function{pathconf()} and
578\function{fpathconf()} to the integer values defined for those names
579by the host operating system. This can be used to determine the set
580of names known to the system.
581Availability: \UNIX.
582\end{datadesc}
583
Fred Drake215fe2f1999-02-02 19:02:35 +0000584\begin{funcdesc}{readlink}{path}
585Return a string representing the path to which the symbolic link
586points.
587Availability: \UNIX{}.
588\end{funcdesc}
589
590\begin{funcdesc}{remove}{path}
591Remove the file \var{path}. See \function{rmdir()} below to remove a
592directory. This is identical to the \function{unlink()} function
593documented below.
594Availability: Macintosh, \UNIX{}, Windows.
595\end{funcdesc}
596
597\begin{funcdesc}{removedirs}{path}
598\index{directory!deleting}
599Recursive directory removal function. Works like
600\function{rmdir()} except that, if the leaf directory is
601successfully removed, directories corresponding to rightmost path
602segments will be pruned way until either the whole path is consumed or
603an error is raised (which is ignored, because it generally means that
604a parent directory is not empty). Throws an \exception{error}
605exception if the leaf directory could not be successfully removed.
606\versionadded{1.5.2}
607\end{funcdesc}
608
609\begin{funcdesc}{rename}{src, dst}
610Rename the file or directory \var{src} to \var{dst}.
611Availability: Macintosh, \UNIX{}, Windows.
612\end{funcdesc}
613
614\begin{funcdesc}{renames}{old, new}
615Recursive directory or file renaming function.
616Works like \function{rename()}, except creation of any intermediate
617directories needed to make the new pathname good is attempted first.
618After the rename, directories corresponding to rightmost path segments
619of the old name will be pruned away using \function{removedirs()}.
620
621Note: this function can fail with the new directory structure made if
622you lack permissions needed to remove the leaf directory or file.
623\versionadded{1.5.2}
624\end{funcdesc}
625
626\begin{funcdesc}{rmdir}{path}
627Remove the directory \var{path}.
628Availability: Macintosh, \UNIX{}, Windows.
629\end{funcdesc}
630
631\begin{funcdesc}{stat}{path}
632Perform a \cfunction{stat()} system call on the given path. The
633return value is a tuple of at least 10 integers giving the most
634important (and portable) members of the \emph{stat} structure, in the
635order
636\code{st_mode},
637\code{st_ino},
638\code{st_dev},
639\code{st_nlink},
640\code{st_uid},
641\code{st_gid},
642\code{st_size},
643\code{st_atime},
644\code{st_mtime},
645\code{st_ctime}.
646More items may be added at the end by some implementations.
647(On MS Windows, some items are filled with dummy values.)
648Availability: Macintosh, \UNIX{}, Windows.
649
650Note: The standard module \refmodule{stat}\refstmodindex{stat} defines
651functions and constants that are useful for extracting information
652from a \ctype{stat} structure.
653\end{funcdesc}
654
655\begin{funcdesc}{statvfs}{path}
656Perform a \cfunction{statvfs()} system call on the given path. The
Guido van Rossum0c9608c1999-02-03 16:32:37 +0000657return value is a tuple of 10 integers giving the most common
Fred Drake215fe2f1999-02-02 19:02:35 +0000658members of the \ctype{statvfs} structure, in the order
659\code{f_bsize},
660\code{f_frsize},
661\code{f_blocks},
662\code{f_bfree},
663\code{f_bavail},
664\code{f_files},
665\code{f_ffree},
666\code{f_favail},
Fred Drake215fe2f1999-02-02 19:02:35 +0000667\code{f_flag},
668\code{f_namemax}.
669Availability: \UNIX{}.
670
671Note: The standard module \module{statvfs}\refstmodindex{statvfs}
672defines constants that are useful for extracting information
673from a \ctype{statvfs} structure.
674\end{funcdesc}
675
676\begin{funcdesc}{symlink}{src, dst}
677Create a symbolic link pointing to \var{src} named \var{dst}.
678Availability: \UNIX{}.
679\end{funcdesc}
680
Fred Drake18f7a451999-12-09 22:11:43 +0000681\begin{funcdesc}{tempnam}{\optional{dir\optional{, prefix}}}
682Return a unique path name that is reasonable for creating a temporary
683file. This will be an absolute path that names a potential directory
684entry in the directory \var{dir} or a common location for temporary
685files if \var{dir} is omitted or \code{None}. If given and not
686\code{None}, \var{prefix} is used to provide a short prefix to the
687filename. Applications are responsible for properly creating and
688managing files created using paths returned by \function{tempnam()};
689no automatic cleanup is provided.
690\end{funcdesc}
691
692\begin{funcdesc}{tmpnam}{}
693Return a unique path name that is reasonable for creating a temporary
694file. This will be an absolute path that names a potential directory
695entry in a common location for temporary files. Applications are
696responsible for properly creating and managing files created using
697paths returned by \function{tmpnam()}; no automatic cleanup is
698provided.
699\end{funcdesc}
700
701\begin{datadesc}{TMP_MAX}
702The maximum number of unique names that \function{tmpnam()} will
703generate before reusing names.
704\end{datadesc}
705
Fred Drake215fe2f1999-02-02 19:02:35 +0000706\begin{funcdesc}{unlink}{path}
707Remove the file \var{path}. This is the same function as
708\function{remove()}; the \function{unlink()} name is its traditional
709\UNIX{} name.
710Availability: Macintosh, \UNIX{}, Windows.
711\end{funcdesc}
712
Barry Warsaw93a8eac2000-05-01 16:18:22 +0000713\begin{funcdesc}{utime}{path, times}
714Set the access and modified times of the file specified by \var{path}.
715If \var{times} is \code{None}, then the file's access and modified
716times are set to the current time. Otherwise, \var{times} must be a
Fred Drakee06d0252000-05-02 17:29:35 +00007172-tuple of numbers, of the form \code{(\var{atime}, \var{mtime})}
718which is used to set the access and modified times, respectively.
719\versionchanged[added support for \code{None} for \var{times}]{1.6}
Fred Drake215fe2f1999-02-02 19:02:35 +0000720Availability: Macintosh, \UNIX{}, Windows.
721\end{funcdesc}
722
723
724\subsection{Process Management \label{os-process}}
725
Fred Drake18f7a451999-12-09 22:11:43 +0000726These functions may be used to create and manage processes.
Fred Drake215fe2f1999-02-02 19:02:35 +0000727
728
Fred Drake18f7a451999-12-09 22:11:43 +0000729\begin{funcdesc}{abort}{}
730Generate a \constant{SIGABRT} signal to the current process. On
731\UNIX, the default behavior is to produce a core dump; on Windows, the
732process immediately returns an exit code of \code{3}. Be aware that
733programs which use \function{signal.signal()} to register a handler
734for \constant{SIGABRT} will behave differently.
735Availability: \UNIX, Windows.
736\end{funcdesc}
737
Fred Drake215fe2f1999-02-02 19:02:35 +0000738\begin{funcdesc}{execl}{path, arg0, arg1, ...}
739This is equivalent to
740\samp{execv(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
741Availability: \UNIX{}, Windows.
742\end{funcdesc}
743
744\begin{funcdesc}{execle}{path, arg0, arg1, ..., env}
745This is equivalent to
746\samp{execve(\var{path}, (\var{arg0}, \var{arg1}, ...), \var{env})}.
747Availability: \UNIX{}, Windows.
748\end{funcdesc}
749
750\begin{funcdesc}{execlp}{path, arg0, arg1, ...}
751This is equivalent to
752\samp{execvp(\var{path}, (\var{arg0}, \var{arg1}, ...))}.
753Availability: \UNIX{}, Windows.
754\end{funcdesc}
755
756\begin{funcdesc}{execv}{path, args}
757Execute the executable \var{path} with argument list \var{args},
758replacing the current process (i.e., the Python interpreter).
759The argument list may be a tuple or list of strings.
760Availability: \UNIX{}, Windows.
761\end{funcdesc}
762
763\begin{funcdesc}{execve}{path, args, env}
764Execute the executable \var{path} with argument list \var{args},
765and environment \var{env},
766replacing the current process (i.e., the Python interpreter).
767The argument list may be a tuple or list of strings.
768The environment must be a dictionary mapping strings to strings.
769Availability: \UNIX{}, Windows.
770\end{funcdesc}
771
772\begin{funcdesc}{execvp}{path, args}
773This is like \samp{execv(\var{path}, \var{args})} but duplicates
774the shell's actions in searching for an executable file in a list of
775directories. The directory list is obtained from
776\code{environ['PATH']}.
777Availability: \UNIX{}, Windows.
778\end{funcdesc}
779
780\begin{funcdesc}{execvpe}{path, args, env}
781This is a cross between \function{execve()} and \function{execvp()}.
782The directory list is obtained from \code{\var{env}['PATH']}.
783Availability: \UNIX{}, Windows.
784\end{funcdesc}
785
786\begin{funcdesc}{_exit}{n}
787Exit to the system with status \var{n}, without calling cleanup
788handlers, flushing stdio buffers, etc.
789Availability: \UNIX{}, Windows.
790
791Note: the standard way to exit is \code{sys.exit(\var{n})}.
792\function{_exit()} should normally only be used in the child process
793after a \function{fork()}.
794\end{funcdesc}
795
796\begin{funcdesc}{fork}{}
797Fork a child process. Return \code{0} in the child, the child's
798process id in the parent.
799Availability: \UNIX{}.
800\end{funcdesc}
801
Fred Drakec82634c2000-06-28 17:27:48 +0000802\begin{funcdesc}{forkpty}{}
803Fork a child process, using a new pseudo-terminal as the child's
804controlling terminal. Return a pair of \code{(\var{pid}, \var{fd})},
805where \var{pid} is \code{0} in the child, the new child's process id
806in the parent, and \code{fd} is the file descriptor of the master end
807of the pseudo-terminal. For a more portable approach, use the
808\refmodule{pty} module.
809Availability: Some flavors of \UNIX{}
810\end{funcdesc}
811
Fred Drake215fe2f1999-02-02 19:02:35 +0000812\begin{funcdesc}{kill}{pid, sig}
813\index{process!killing}
814\index{process!signalling}
815Kill the process \var{pid} with signal \var{sig}.
816Availability: \UNIX{}.
817\end{funcdesc}
818
819\begin{funcdesc}{nice}{increment}
820Add \var{increment} to the process's ``niceness''. Return the new
821niceness.
822Availability: \UNIX{}.
823\end{funcdesc}
824
825\begin{funcdesc}{plock}{op}
826Lock program segments into memory. The value of \var{op}
827(defined in \code{<sys/lock.h>}) determines which segments are locked.
Fred Drake39063631999-02-26 14:05:02 +0000828Availability: \UNIX{}.
Fred Drake215fe2f1999-02-02 19:02:35 +0000829\end{funcdesc}
830
831\begin{funcdesc}{spawnv}{mode, path, args}
832Execute the program \var{path} in a new process, passing the arguments
833specified in \var{args} as command-line parameters. \var{args} may be
834a list or a tuple. \var{mode} is a magic operational constant. See
835the Visual \Cpp{} Runtime Library documentation for further
Fred Drake22702081999-07-02 14:01:03 +0000836information; the constants are exposed to the Python programmer as
837listed below.
Fred Drake15861b22000-02-29 05:19:38 +0000838Availability: \UNIX{}, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000839\versionadded{1.5.2}
840\end{funcdesc}
841
842\begin{funcdesc}{spawnve}{mode, path, args, env}
843Execute the program \var{path} in a new process, passing the arguments
844specified in \var{args} as command-line parameters and the contents of
845the mapping \var{env} as the environment. \var{args} may be a list or
846a tuple. \var{mode} is a magic operational constant. See the Visual
Fred Drake22702081999-07-02 14:01:03 +0000847\Cpp{} Runtime Library documentation for further information; the
848constants are exposed to the Python programmer as listed below.
Fred Drake15861b22000-02-29 05:19:38 +0000849Availability: \UNIX{}, Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +0000850\versionadded{1.5.2}
851\end{funcdesc}
852
Fred Drake9329e5e1999-02-16 19:40:19 +0000853\begin{datadesc}{P_WAIT}
854\dataline{P_NOWAIT}
855\dataline{P_NOWAITO}
Fred Drake215fe2f1999-02-02 19:02:35 +0000856Possible values for the \var{mode} parameter to \function{spawnv()}
857and \function{spawnve()}.
Fred Drake15861b22000-02-29 05:19:38 +0000858Availability: \UNIX{}, Windows.
859\versionadded{1.5.2}
860\end{datadesc}
861
862\begin{datadesc}{P_OVERLAY}
863\dataline{P_DETACH}
864Possible values for the \var{mode} parameter to \function{spawnv()}
865and \function{spawnve()}. These are less portable than those listed
866above.
Fred Drake215fe2f1999-02-02 19:02:35 +0000867Availability: Windows.
868\versionadded{1.5.2}
869\end{datadesc}
870
871\begin{funcdesc}{system}{command}
872Execute the command (a string) in a subshell. This is implemented by
873calling the Standard C function \cfunction{system()}, and has the
Fred Drakeec6baaf1999-04-21 18:13:31 +0000874same limitations. Changes to \code{posix.environ}, \code{sys.stdin},
Fred Drake215fe2f1999-02-02 19:02:35 +0000875etc.\ are not reflected in the environment of the executed command.
876The return value is the exit status of the process encoded in the
Fred Drake7a621281999-06-10 15:07:05 +0000877format specified for \function{wait()}, except on Windows 95 and 98,
Fred Drakea88ef001999-06-18 19:11:25 +0000878where it is always \code{0}. Note that \POSIX{} does not specify the
879meaning of the return value of the C \cfunction{system()} function,
880so the return value of the Python function is system-dependent.
Fred Drake215fe2f1999-02-02 19:02:35 +0000881Availability: \UNIX{}, Windows.
882\end{funcdesc}
883
884\begin{funcdesc}{times}{}
885Return a 5-tuple of floating point numbers indicating accumulated (CPU
886or other)
887times, in seconds. The items are: user time, system time, children's
888user time, children's system time, and elapsed real time since a fixed
Fred Drakeec6baaf1999-04-21 18:13:31 +0000889point in the past, in that order. See the \UNIX{} manual page
890\manpage{times}{2} or the corresponding Windows Platform API
891documentation.
Fred Drake215fe2f1999-02-02 19:02:35 +0000892Availability: \UNIX{}, Windows.
893\end{funcdesc}
894
895\begin{funcdesc}{wait}{}
896Wait for completion of a child process, and return a tuple containing
897its pid and exit status indication: a 16-bit number, whose low byte is
898the signal number that killed the process, and whose high byte is the
899exit status (if the signal number is zero); the high bit of the low
900byte is set if a core file was produced.
901Availability: \UNIX{}.
902\end{funcdesc}
903
904\begin{funcdesc}{waitpid}{pid, options}
Fred Drake31e5e371999-08-13 13:36:33 +0000905Wait for completion of a child process given by process id \var{pid},
906and return a tuple containing its process id and exit status
907indication (encoded as for \function{wait()}). The semantics of the
908call are affected by the value of the integer \var{options}, which
909should be \code{0} for normal operation.
Fred Drake215fe2f1999-02-02 19:02:35 +0000910Availability: \UNIX{}.
Fred Drake31e5e371999-08-13 13:36:33 +0000911
912If \var{pid} is greater than \code{0}, \function{waitpid()} requests
913status information for that specific process. If \var{pid} is
914\code{0}, the request is for the status of any child in the process
915group of the current process. If \var{pid} is \code{-1}, the request
916pertains to any child of the current process. If \var{pid} is less
917than \code{-1}, status is requested for any process in the process
918group \code{-\var{pid}} (the absolute value of \var{pid}).
Fred Drake215fe2f1999-02-02 19:02:35 +0000919\end{funcdesc}
920
921\begin{datadesc}{WNOHANG}
922The option for \function{waitpid()} to avoid hanging if no child
923process status is available immediately.
924Availability: \UNIX{}.
925\end{datadesc}
926
Fred Drake38e5d272000-04-03 20:13:55 +0000927The following functions take a process status code as returned by
928\function{system()}, \function{wait()}, or \function{waitpid()} as a
929parameter. They may be used to determine the disposition of a
930process.
Fred Drake215fe2f1999-02-02 19:02:35 +0000931
932\begin{funcdesc}{WIFSTOPPED}{status}
933Return true if the process has been stopped.
934Availability: \UNIX{}.
935\end{funcdesc}
936
937\begin{funcdesc}{WIFSIGNALED}{status}
938Return true if the process exited due to a signal.
939Availability: \UNIX{}.
940\end{funcdesc}
941
942\begin{funcdesc}{WIFEXITED}{status}
943Return true if the process exited using the \manpage{exit}{2} system
944call.
945Availability: \UNIX{}.
946\end{funcdesc}
947
948\begin{funcdesc}{WEXITSTATUS}{status}
949If \code{WIFEXITED(\var{status})} is true, return the integer
950parameter to the \manpage{exit}{2} system call. Otherwise, the return
951value is meaningless.
952Availability: \UNIX{}.
953\end{funcdesc}
954
955\begin{funcdesc}{WSTOPSIG}{status}
Fred Drake35c3ffd1999-03-04 14:08:10 +0000956Return the signal which caused the process to stop.
957Availability: \UNIX{}.
958\end{funcdesc}
959
960\begin{funcdesc}{WTERMSIG}{status}
Fred Drake215fe2f1999-02-02 19:02:35 +0000961Return the signal which caused the process to exit.
962Availability: \UNIX{}.
963\end{funcdesc}
964
965
Fred Drake88f6ca21999-12-15 19:39:04 +0000966\subsection{Miscellanenous System Information \label{os-path}}
967
968
969\begin{funcdesc}{confstr}{name}
970Return string-valued system configuration values.
971\var{name} specifies the configuration value to retrieve; it may be a
972string which is the name of a defined system value; these names are
973specified in a number of standards (\POSIX, Unix95, Unix98, and
974others). Some platforms define additional names as well. The names
975known to the host operating system are given in the
976\code{confstr_names} dictionary. For configuration variables not
977included in that mapping, passing an integer for \var{name} is also
978accepted.
979Availability: \UNIX{}.
980
981If the configuration value specified by \var{name} isn't defined, the
982empty string is returned.
983
984If \var{name} is a string and is not known, \exception{ValueError} is
985raised. If a specific value for \var{name} is not supported by the
986host system, even if it is included in \code{confstr_names}, an
987\exception{OSError} is raised with \constant{errno.EINVAL} for the
988error number.
989\end{funcdesc}
990
991\begin{datadesc}{confstr_names}
992Dictionary mapping names accepted by \function{confstr()} to the
993integer values defined for those names by the host operating system.
994This can be used to determine the set of names known to the system.
995Availability: \UNIX.
996\end{datadesc}
997
998\begin{funcdesc}{sysconf}{name}
999Return integer-valued system configuration values.
1000If the configuration value specified by \var{name} isn't defined,
1001\code{-1} is returned. The comments regarding the \var{name}
1002parameter for \function{confstr()} apply here as well; the dictionary
1003that provides information on the known names is given by
1004\code{sysconf_names}.
1005Availability: \UNIX{}.
1006\end{funcdesc}
1007
1008\begin{datadesc}{sysconf_names}
1009Dictionary mapping names accepted by \function{sysconf()} to the
1010integer values defined for those names by the host operating system.
1011This can be used to determine the set of names known to the system.
1012Availability: \UNIX.
1013\end{datadesc}
1014
Fred Drake215fe2f1999-02-02 19:02:35 +00001015
1016The follow data values are used to support path manipulation
1017operations. These are defined for all platforms.
1018
1019Higher-level operations on pathnames are defined in the
1020\refmodule{os.path} module.
1021
1022
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001023\begin{datadesc}{curdir}
1024The constant string used by the OS to refer to the current directory,
Fred Drake1a3c2a01998-08-06 15:18:23 +00001025e.g.\ \code{'.'} for \POSIX{} or \code{':'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001026\end{datadesc}
1027
1028\begin{datadesc}{pardir}
1029The constant string used by the OS to refer to the parent directory,
Fred Drake1a3c2a01998-08-06 15:18:23 +00001030e.g.\ \code{'..'} for \POSIX{} or \code{'::'} for the Macintosh.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001031\end{datadesc}
1032
1033\begin{datadesc}{sep}
Guido van Rossumb2afc811997-08-29 22:37:44 +00001034The character used by the OS to separate pathname components,
Fred Drake1a3c2a01998-08-06 15:18:23 +00001035e.g.\ \character{/} for \POSIX{} or \character{:} for the Macintosh.
1036Note that knowing this is not sufficient to be able to parse or
1037concatenate pathnames --- use \function{os.path.split()} and
1038\function{os.path.join()} --- but it is occasionally useful.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001039\end{datadesc}
1040
Guido van Rossumb2afc811997-08-29 22:37:44 +00001041\begin{datadesc}{altsep}
1042An alternative character used by the OS to separate pathname components,
1043or \code{None} if only one separator character exists. This is set to
Fred Drake215fe2f1999-02-02 19:02:35 +00001044\character{/} on DOS and Windows systems where \code{sep} is a backslash.
Guido van Rossumb2afc811997-08-29 22:37:44 +00001045\end{datadesc}
1046
Guido van Rossum470be141995-03-17 16:07:09 +00001047\begin{datadesc}{pathsep}
1048The character conventionally used by the OS to separate search patch
Fred Drake1a3c2a01998-08-06 15:18:23 +00001049components (as in \envvar{PATH}), e.g.\ \character{:} for \POSIX{} or
Fred Drake215fe2f1999-02-02 19:02:35 +00001050\character{;} for DOS and Windows.
Guido van Rossum9c59ce91998-06-30 15:54:27 +00001051\end{datadesc}
1052
Guido van Rossum470be141995-03-17 16:07:09 +00001053\begin{datadesc}{defpath}
Fred Drake1a3c2a01998-08-06 15:18:23 +00001054The default search path used by \function{exec*p*()} if the environment
Guido van Rossum470be141995-03-17 16:07:09 +00001055doesn't have a \code{'PATH'} key.
1056\end{datadesc}
1057
Fred Drake215fe2f1999-02-02 19:02:35 +00001058\begin{datadesc}{linesep}
1059The string used to separate (or, rather, terminate) lines on the
Fred Drakeec6baaf1999-04-21 18:13:31 +00001060current platform. This may be a single character,
1061e.g.\ \code{'\e n'} for \POSIX{} or \code{'\e r'} for MacOS, or multiple
1062characters, e.g.\ \code{'\e r\e n'} for MS-DOS and MS Windows.
Fred Drake215fe2f1999-02-02 19:02:35 +00001063\end{datadesc}