blob: 6696a28b735fd4381171495167df5998927bf17e [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Built-in Module \sectcode{posix}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00002\bimodindex{posix}
3
4This module provides access to operating system functionality that is
Guido van Rossum6bb1adc1995-03-13 10:03:32 +00005standardized by the C Standard and the POSIX standard (a thinly disguised
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00006\UNIX{} interface).
Guido van Rossum470be141995-03-17 16:07:09 +00007
8\strong{Do not import this module directly.} Instead, import the
9module \code{os}, which provides a \emph{portable} version of this
10interface. On \UNIX{}, the \code{os} module provides a superset of
11the \code{posix} interface. On non-\UNIX{} operating systems the
12\code{posix} module is not available, but a subset is always available
13through the \code{os} interface. Once \code{os} is imported, there is
14\emph{no} performance penalty in using it instead of
15\code{posix}.
16\stmodindex{os}
17
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000018The descriptions below are very terse; refer to the
Guido van Rossum96628a91995-04-10 11:34:00 +000019corresponding \UNIX{} manual entry for more information. Arguments
20called \var{path} refer to a pathname given as a string.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000021
22Errors are reported as exceptions; the usual exceptions are given
23for type errors, while errors reported by the system calls raise
24\code{posix.error}, described below.
25
26Module \code{posix} defines the following data items:
27
28\renewcommand{\indexsubitem}{(data in module posix)}
29\begin{datadesc}{environ}
30A dictionary representing the string environment at the time
31the interpreter was started.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000032For example,
33\code{posix.environ['HOME']}
34is the pathname of your home directory, equivalent to
35\code{getenv("HOME")}
36in C.
Guido van Rossum470be141995-03-17 16:07:09 +000037Modifying this dictionary does not affect the string environment
38passed on by \code{execv()}, \code{popen()} or \code{system()}; if you
39need to change the environment, pass \code{environ} to \code{execve()}
40or add variable assignments and export statements to the command
41string for \code{system()} or \code{popen()}.%
42\footnote{The problem with automatically passing on \code{environ} is
43that there is no portable way of changing the environment.}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000044\end{datadesc}
45
46\renewcommand{\indexsubitem}{(exception in module posix)}
47\begin{excdesc}{error}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000048This exception is raised when a POSIX function returns a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000049POSIX-related error (e.g., not for illegal argument types). Its
50string value is \code{'posix.error'}. The accompanying value is a
51pair containing the numeric error code from \code{errno} and the
52corresponding string, as would be printed by the C function
53\code{perror()}.
54\end{excdesc}
55
Guido van Rossum4bbe9c01995-03-30 16:00:36 +000056It defines the following functions and constants:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000057
58\renewcommand{\indexsubitem}{(in module posix)}
59\begin{funcdesc}{chdir}{path}
60Change the current working directory to \var{path}.
61\end{funcdesc}
62
63\begin{funcdesc}{chmod}{path\, mode}
64Change the mode of \var{path} to the numeric \var{mode}.
65\end{funcdesc}
66
Guido van Rossum31cce971995-01-04 19:17:34 +000067\begin{funcdesc}{chown}{path\, uid, gid}
68Change the owner and group id of \var{path} to the numeric \var{uid}
69and \var{gid}.
70(Not on MS-DOS.)
71\end{funcdesc}
72
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000073\begin{funcdesc}{close}{fd}
74Close file descriptor \var{fd}.
Guido van Rossum28379701995-01-12 12:38:22 +000075
76Note: this function is intended for low-level I/O and must be applied
77to a file descriptor as returned by \code{posix.open()} or
78\code{posix.pipe()}. To close a ``file object'' returned by the
79built-in function \code{open} or by \code{posix.popen} or
80\code{posix.fdopen}, use its \code{close()} method.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000081\end{funcdesc}
82
83\begin{funcdesc}{dup}{fd}
84Return a duplicate of file descriptor \var{fd}.
85\end{funcdesc}
86
87\begin{funcdesc}{dup2}{fd\, fd2}
88Duplicate file descriptor \var{fd} to \var{fd2}, closing the latter
89first if necessary. Return \code{None}.
90\end{funcdesc}
91
92\begin{funcdesc}{execv}{path\, args}
93Execute the executable \var{path} with argument list \var{args},
94replacing the current process (i.e., the Python interpreter).
95The argument list may be a tuple or list of strings.
96(Not on MS-DOS.)
97\end{funcdesc}
98
99\begin{funcdesc}{execve}{path\, args\, env}
100Execute the executable \var{path} with argument list \var{args},
101and environment \var{env},
102replacing the current process (i.e., the Python interpreter).
103The argument list may be a tuple or list of strings.
104The environment must be a dictionary mapping strings to strings.
105(Not on MS-DOS.)
106\end{funcdesc}
107
108\begin{funcdesc}{_exit}{n}
109Exit to the system with status \var{n}, without calling cleanup
110handlers, flushing stdio buffers, etc.
111(Not on MS-DOS.)
112
113Note: the standard way to exit is \code{sys.exit(\var{n})}.
114\code{posix._exit()} should normally only be used in the child process
115after a \code{fork()}.
116\end{funcdesc}
117
Guido van Rossum28379701995-01-12 12:38:22 +0000118\begin{funcdesc}{fdopen}{fd\optional{\, mode\optional{\, bufsize}}}
119Return an open file object connected to the file descriptor \var{fd}.
120The \var{mode} and \var{bufsize} arguments have the same meaning as
121the corresponding arguments to the built-in \code{open()} function.
Guido van Rossumc5c67bc1994-02-15 15:59:23 +0000122\end{funcdesc}
123
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000124\begin{funcdesc}{fork}{}
125Fork a child process. Return 0 in the child, the child's process id
126in the parent.
127(Not on MS-DOS.)
128\end{funcdesc}
129
130\begin{funcdesc}{fstat}{fd}
131Return status for file descriptor \var{fd}, like \code{stat()}.
132\end{funcdesc}
133
134\begin{funcdesc}{getcwd}{}
135Return a string representing the current working directory.
136\end{funcdesc}
137
138\begin{funcdesc}{getegid}{}
139Return the current process's effective group id.
140(Not on MS-DOS.)
141\end{funcdesc}
142
143\begin{funcdesc}{geteuid}{}
144Return the current process's effective user id.
145(Not on MS-DOS.)
146\end{funcdesc}
147
148\begin{funcdesc}{getgid}{}
149Return the current process's group id.
150(Not on MS-DOS.)
151\end{funcdesc}
152
153\begin{funcdesc}{getpid}{}
154Return the current process id.
155(Not on MS-DOS.)
156\end{funcdesc}
157
158\begin{funcdesc}{getppid}{}
159Return the parent's process id.
160(Not on MS-DOS.)
161\end{funcdesc}
162
163\begin{funcdesc}{getuid}{}
164Return the current process's user id.
165(Not on MS-DOS.)
166\end{funcdesc}
167
168\begin{funcdesc}{kill}{pid\, sig}
169Kill the process \var{pid} with signal \var{sig}.
170(Not on MS-DOS.)
171\end{funcdesc}
172
173\begin{funcdesc}{link}{src\, dst}
174Create a hard link pointing to \var{src} named \var{dst}.
175(Not on MS-DOS.)
176\end{funcdesc}
177
178\begin{funcdesc}{listdir}{path}
179Return a list containing the names of the entries in the directory.
Guido van Rossum8c07bb41996-02-12 23:16:08 +0000180The list is in arbitrary order. It does not include the special
181entries \code{'.'} and \code{'..'} even if they are present in the
182directory.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000183\end{funcdesc}
184
185\begin{funcdesc}{lseek}{fd\, pos\, how}
186Set the current position of file descriptor \var{fd} to position
187\var{pos}, modified by \var{how}: 0 to set the position relative to
188the beginning of the file; 1 to set it relative to the current
189position; 2 to set it relative to the end of the file.
190\end{funcdesc}
191
192\begin{funcdesc}{lstat}{path}
193Like \code{stat()}, but do not follow symbolic links. (On systems
194without symbolic links, this is identical to \code{posix.stat}.)
195\end{funcdesc}
196
197\begin{funcdesc}{mkdir}{path\, mode}
198Create a directory named \var{path} with numeric mode \var{mode}.
199\end{funcdesc}
200
201\begin{funcdesc}{nice}{increment}
202Add \var{incr} to the process' ``niceness''. Return the new niceness.
203(Not on MS-DOS.)
204\end{funcdesc}
205
206\begin{funcdesc}{open}{file\, flags\, mode}
207Open the file \var{file} and set various flags according to
208\var{flags} and possibly its mode according to \var{mode}.
209Return the file descriptor for the newly opened file.
Guido van Rossum28379701995-01-12 12:38:22 +0000210
211Note: this function is intended for low-level I/O. For normal usage,
212use the built-in function \code{open}, which returns a ``file object''
213with \code{read()} and \code{write()} methods (and many more).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000214\end{funcdesc}
215
216\begin{funcdesc}{pipe}{}
217Create a pipe. Return a pair of file descriptors \code{(r, w)}
218usable for reading and writing, respectively.
219(Not on MS-DOS.)
220\end{funcdesc}
221
Guido van Rossum28379701995-01-12 12:38:22 +0000222\begin{funcdesc}{popen}{command\optional{\, mode\optional{\, bufsize}}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000223Open a pipe to or from \var{command}. The return value is an open
224file object connected to the pipe, which can be read or written
Guido van Rossum28379701995-01-12 12:38:22 +0000225depending on whether \var{mode} is \code{'r'} (default) or \code{'w'}.
226The \var{bufsize} argument has the same meaning as the corresponding
227argument to the built-in \code{open()} function.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000228(Not on MS-DOS.)
229\end{funcdesc}
230
231\begin{funcdesc}{read}{fd\, n}
232Read at most \var{n} bytes from file descriptor \var{fd}.
233Return a string containing the bytes read.
Guido van Rossum28379701995-01-12 12:38:22 +0000234
235Note: this function is intended for low-level I/O and must be applied
236to a file descriptor as returned by \code{posix.open()} or
237\code{posix.pipe()}. To read a ``file object'' returned by the
238built-in function \code{open} or by \code{posix.popen} or
239\code{posix.fdopen}, or \code{sys.stdin}, use its
240\code{read()} or \code{readline()} methods.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000241\end{funcdesc}
242
243\begin{funcdesc}{readlink}{path}
244Return a string representing the path to which the symbolic link
245points. (On systems without symbolic links, this always raises
246\code{posix.error}.)
247\end{funcdesc}
248
Guido van Rossum8c07bb41996-02-12 23:16:08 +0000249\begin{funcdesc}{remove}{path}
250Remove the file \var{path}. See \code{rmdir} below to remove a directory.
251\end{funcdesc}
252
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000253\begin{funcdesc}{rename}{src\, dst}
254Rename the file or directory \var{src} to \var{dst}.
255\end{funcdesc}
256
257\begin{funcdesc}{rmdir}{path}
258Remove the directory \var{path}.
259\end{funcdesc}
260
261\begin{funcdesc}{setgid}{gid}
262Set the current process's group id.
263(Not on MS-DOS.)
264\end{funcdesc}
265
266\begin{funcdesc}{setuid}{uid}
267Set the current process's user id.
268(Not on MS-DOS.)
269\end{funcdesc}
270
271\begin{funcdesc}{stat}{path}
272Perform a {\em stat} system call on the given path. The return value
273is a tuple of at least 10 integers giving the most important (and
274portable) members of the {\em stat} structure, in the order
275\code{st_mode},
276\code{st_ino},
277\code{st_dev},
278\code{st_nlink},
279\code{st_uid},
280\code{st_gid},
281\code{st_size},
282\code{st_atime},
283\code{st_mtime},
284\code{st_ctime}.
285More items may be added at the end by some implementations.
286(On MS-DOS, some items are filled with dummy values.)
287
288Note: The standard module \code{stat} defines functions and constants
289that are useful for extracting information from a stat structure.
290\end{funcdesc}
291
292\begin{funcdesc}{symlink}{src\, dst}
293Create a symbolic link pointing to \var{src} named \var{dst}. (On
294systems without symbolic links, this always raises
295\code{posix.error}.)
296\end{funcdesc}
297
298\begin{funcdesc}{system}{command}
299Execute the command (a string) in a subshell. This is implemented by
300calling the Standard C function \code{system()}, and has the same
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000301limitations. Changes to \code{posix.environ}, \code{sys.stdin} etc.\ are
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000302not reflected in the environment of the executed command. The return
303value is the exit status of the process as returned by Standard C
304\code{system()}.
305\end{funcdesc}
306
307\begin{funcdesc}{times}{}
Guido van Rossum1e150611995-09-13 17:36:35 +0000308Return a 5-tuple of floating point numbers indicating accumulated (CPU
309or other)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000310times, in seconds. The items are: user time, system time, children's
Guido van Rossum1e150611995-09-13 17:36:35 +0000311user time, children's system time, and elapsed real time since a fixed
312point in the past, in that order. See the \UNIX{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000313manual page {\it times}(2). (Not on MS-DOS.)
314\end{funcdesc}
315
316\begin{funcdesc}{umask}{mask}
317Set the current numeric umask and returns the previous umask.
318(Not on MS-DOS.)
319\end{funcdesc}
320
321\begin{funcdesc}{uname}{}
322Return a 5-tuple containing information identifying the current
323operating system. The tuple contains 5 strings:
324\code{(\var{sysname}, \var{nodename}, \var{release}, \var{version}, \var{machine})}.
325Some systems truncate the nodename to 8
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000326characters or to the leading component; a better way to get the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000327hostname is \code{socket.gethostname()}. (Not on MS-DOS, nor on older
328\UNIX{} systems.)
329\end{funcdesc}
330
331\begin{funcdesc}{unlink}{path}
Guido van Rossum8c07bb41996-02-12 23:16:08 +0000332Remove the file \var{path}. This is the same function as \code{remove};
333the \code{unlink} name is its traditional \UNIX{} name.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000334\end{funcdesc}
335
336\begin{funcdesc}{utime}{path\, \(atime\, mtime\)}
337Set the access and modified time of the file to the given values.
338(The second argument is a tuple of two items.)
339\end{funcdesc}
340
341\begin{funcdesc}{wait}{}
342Wait for completion of a child process, and return a tuple containing
343its pid and exit status indication (encoded as by \UNIX{}).
344(Not on MS-DOS.)
345\end{funcdesc}
346
347\begin{funcdesc}{waitpid}{pid\, options}
348Wait for completion of a child process given by proces id, and return
349a tuple containing its pid and exit status indication (encoded as by
350\UNIX{}). The semantics of the call are affected by the value of
351the integer options, which should be 0 for normal operation. (If the
Guido van Rossum96628a91995-04-10 11:34:00 +0000352system does not support \code{waitpid()}, this always raises
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000353\code{posix.error}. Not on MS-DOS.)
354\end{funcdesc}
355
356\begin{funcdesc}{write}{fd\, str}
357Write the string \var{str} to file descriptor \var{fd}.
358Return the number of bytes actually written.
Guido van Rossum28379701995-01-12 12:38:22 +0000359
360Note: this function is intended for low-level I/O and must be applied
361to a file descriptor as returned by \code{posix.open()} or
362\code{posix.pipe()}. To write a ``file object'' returned by the
363built-in function \code{open} or by \code{posix.popen} or
364\code{posix.fdopen}, or \code{sys.stdout} or \code{sys.stderr}, use
365its \code{write()} method.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000366\end{funcdesc}
Guido van Rossum4bbe9c01995-03-30 16:00:36 +0000367
368\begin{datadesc}{WNOHANG}
369The option for \code{waitpid()} to avoid hanging if no child process
370status is available immediately.
371\end{datadesc}