blob: fa0b42d23913af4bf839f3ed46d6f8a2bf050708 [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
19corresponding \UNIX{} manual entry for more information.
20
21Errors are reported as exceptions; the usual exceptions are given
22for type errors, while errors reported by the system calls raise
23\code{posix.error}, described below.
24
25Module \code{posix} defines the following data items:
26
27\renewcommand{\indexsubitem}{(data in module posix)}
28\begin{datadesc}{environ}
29A dictionary representing the string environment at the time
30the interpreter was started.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000031For example,
32\code{posix.environ['HOME']}
33is the pathname of your home directory, equivalent to
34\code{getenv("HOME")}
35in C.
Guido van Rossum470be141995-03-17 16:07:09 +000036Modifying this dictionary does not affect the string environment
37passed on by \code{execv()}, \code{popen()} or \code{system()}; if you
38need to change the environment, pass \code{environ} to \code{execve()}
39or add variable assignments and export statements to the command
40string for \code{system()} or \code{popen()}.%
41\footnote{The problem with automatically passing on \code{environ} is
42that there is no portable way of changing the environment.}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000043\end{datadesc}
44
45\renewcommand{\indexsubitem}{(exception in module posix)}
46\begin{excdesc}{error}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000047This exception is raised when a POSIX function returns a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000048POSIX-related error (e.g., not for illegal argument types). Its
49string value is \code{'posix.error'}. The accompanying value is a
50pair containing the numeric error code from \code{errno} and the
51corresponding string, as would be printed by the C function
52\code{perror()}.
53\end{excdesc}
54
Guido van Rossum4bbe9c01995-03-30 16:00:36 +000055It defines the following functions and constants:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000056
57\renewcommand{\indexsubitem}{(in module posix)}
58\begin{funcdesc}{chdir}{path}
59Change the current working directory to \var{path}.
60\end{funcdesc}
61
62\begin{funcdesc}{chmod}{path\, mode}
63Change the mode of \var{path} to the numeric \var{mode}.
64\end{funcdesc}
65
Guido van Rossum31cce971995-01-04 19:17:34 +000066\begin{funcdesc}{chown}{path\, uid, gid}
67Change the owner and group id of \var{path} to the numeric \var{uid}
68and \var{gid}.
69(Not on MS-DOS.)
70\end{funcdesc}
71
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000072\begin{funcdesc}{close}{fd}
73Close file descriptor \var{fd}.
Guido van Rossum28379701995-01-12 12:38:22 +000074
75Note: this function is intended for low-level I/O and must be applied
76to a file descriptor as returned by \code{posix.open()} or
77\code{posix.pipe()}. To close a ``file object'' returned by the
78built-in function \code{open} or by \code{posix.popen} or
79\code{posix.fdopen}, use its \code{close()} method.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000080\end{funcdesc}
81
82\begin{funcdesc}{dup}{fd}
83Return a duplicate of file descriptor \var{fd}.
84\end{funcdesc}
85
86\begin{funcdesc}{dup2}{fd\, fd2}
87Duplicate file descriptor \var{fd} to \var{fd2}, closing the latter
88first if necessary. Return \code{None}.
89\end{funcdesc}
90
91\begin{funcdesc}{execv}{path\, args}
92Execute the executable \var{path} with argument list \var{args},
93replacing the current process (i.e., the Python interpreter).
94The argument list may be a tuple or list of strings.
95(Not on MS-DOS.)
96\end{funcdesc}
97
98\begin{funcdesc}{execve}{path\, args\, env}
99Execute the executable \var{path} with argument list \var{args},
100and environment \var{env},
101replacing the current process (i.e., the Python interpreter).
102The argument list may be a tuple or list of strings.
103The environment must be a dictionary mapping strings to strings.
104(Not on MS-DOS.)
105\end{funcdesc}
106
107\begin{funcdesc}{_exit}{n}
108Exit to the system with status \var{n}, without calling cleanup
109handlers, flushing stdio buffers, etc.
110(Not on MS-DOS.)
111
112Note: the standard way to exit is \code{sys.exit(\var{n})}.
113\code{posix._exit()} should normally only be used in the child process
114after a \code{fork()}.
115\end{funcdesc}
116
Guido van Rossum28379701995-01-12 12:38:22 +0000117\begin{funcdesc}{fdopen}{fd\optional{\, mode\optional{\, bufsize}}}
118Return an open file object connected to the file descriptor \var{fd}.
119The \var{mode} and \var{bufsize} arguments have the same meaning as
120the corresponding arguments to the built-in \code{open()} function.
Guido van Rossumc5c67bc1994-02-15 15:59:23 +0000121\end{funcdesc}
122
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000123\begin{funcdesc}{fork}{}
124Fork a child process. Return 0 in the child, the child's process id
125in the parent.
126(Not on MS-DOS.)
127\end{funcdesc}
128
129\begin{funcdesc}{fstat}{fd}
130Return status for file descriptor \var{fd}, like \code{stat()}.
131\end{funcdesc}
132
133\begin{funcdesc}{getcwd}{}
134Return a string representing the current working directory.
135\end{funcdesc}
136
137\begin{funcdesc}{getegid}{}
138Return the current process's effective group id.
139(Not on MS-DOS.)
140\end{funcdesc}
141
142\begin{funcdesc}{geteuid}{}
143Return the current process's effective user id.
144(Not on MS-DOS.)
145\end{funcdesc}
146
147\begin{funcdesc}{getgid}{}
148Return the current process's group id.
149(Not on MS-DOS.)
150\end{funcdesc}
151
152\begin{funcdesc}{getpid}{}
153Return the current process id.
154(Not on MS-DOS.)
155\end{funcdesc}
156
157\begin{funcdesc}{getppid}{}
158Return the parent's process id.
159(Not on MS-DOS.)
160\end{funcdesc}
161
162\begin{funcdesc}{getuid}{}
163Return the current process's user id.
164(Not on MS-DOS.)
165\end{funcdesc}
166
167\begin{funcdesc}{kill}{pid\, sig}
168Kill the process \var{pid} with signal \var{sig}.
169(Not on MS-DOS.)
170\end{funcdesc}
171
172\begin{funcdesc}{link}{src\, dst}
173Create a hard link pointing to \var{src} named \var{dst}.
174(Not on MS-DOS.)
175\end{funcdesc}
176
177\begin{funcdesc}{listdir}{path}
178Return a list containing the names of the entries in the directory.
179The list is in arbitrary order. It includes the special entries
180\code{'.'} and \code{'..'} if they are present in the directory.
181\end{funcdesc}
182
183\begin{funcdesc}{lseek}{fd\, pos\, how}
184Set the current position of file descriptor \var{fd} to position
185\var{pos}, modified by \var{how}: 0 to set the position relative to
186the beginning of the file; 1 to set it relative to the current
187position; 2 to set it relative to the end of the file.
188\end{funcdesc}
189
190\begin{funcdesc}{lstat}{path}
191Like \code{stat()}, but do not follow symbolic links. (On systems
192without symbolic links, this is identical to \code{posix.stat}.)
193\end{funcdesc}
194
195\begin{funcdesc}{mkdir}{path\, mode}
196Create a directory named \var{path} with numeric mode \var{mode}.
197\end{funcdesc}
198
199\begin{funcdesc}{nice}{increment}
200Add \var{incr} to the process' ``niceness''. Return the new niceness.
201(Not on MS-DOS.)
202\end{funcdesc}
203
204\begin{funcdesc}{open}{file\, flags\, mode}
205Open the file \var{file} and set various flags according to
206\var{flags} and possibly its mode according to \var{mode}.
207Return the file descriptor for the newly opened file.
Guido van Rossum28379701995-01-12 12:38:22 +0000208
209Note: this function is intended for low-level I/O. For normal usage,
210use the built-in function \code{open}, which returns a ``file object''
211with \code{read()} and \code{write()} methods (and many more).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000212\end{funcdesc}
213
214\begin{funcdesc}{pipe}{}
215Create a pipe. Return a pair of file descriptors \code{(r, w)}
216usable for reading and writing, respectively.
217(Not on MS-DOS.)
218\end{funcdesc}
219
Guido van Rossum28379701995-01-12 12:38:22 +0000220\begin{funcdesc}{popen}{command\optional{\, mode\optional{\, bufsize}}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000221Open a pipe to or from \var{command}. The return value is an open
222file object connected to the pipe, which can be read or written
Guido van Rossum28379701995-01-12 12:38:22 +0000223depending on whether \var{mode} is \code{'r'} (default) or \code{'w'}.
224The \var{bufsize} argument has the same meaning as the corresponding
225argument to the built-in \code{open()} function.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000226(Not on MS-DOS.)
227\end{funcdesc}
228
229\begin{funcdesc}{read}{fd\, n}
230Read at most \var{n} bytes from file descriptor \var{fd}.
231Return a string containing the bytes read.
Guido van Rossum28379701995-01-12 12:38:22 +0000232
233Note: this function is intended for low-level I/O and must be applied
234to a file descriptor as returned by \code{posix.open()} or
235\code{posix.pipe()}. To read a ``file object'' returned by the
236built-in function \code{open} or by \code{posix.popen} or
237\code{posix.fdopen}, or \code{sys.stdin}, use its
238\code{read()} or \code{readline()} methods.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000239\end{funcdesc}
240
241\begin{funcdesc}{readlink}{path}
242Return a string representing the path to which the symbolic link
243points. (On systems without symbolic links, this always raises
244\code{posix.error}.)
245\end{funcdesc}
246
247\begin{funcdesc}{rename}{src\, dst}
248Rename the file or directory \var{src} to \var{dst}.
249\end{funcdesc}
250
251\begin{funcdesc}{rmdir}{path}
252Remove the directory \var{path}.
253\end{funcdesc}
254
255\begin{funcdesc}{setgid}{gid}
256Set the current process's group id.
257(Not on MS-DOS.)
258\end{funcdesc}
259
260\begin{funcdesc}{setuid}{uid}
261Set the current process's user id.
262(Not on MS-DOS.)
263\end{funcdesc}
264
265\begin{funcdesc}{stat}{path}
266Perform a {\em stat} system call on the given path. The return value
267is a tuple of at least 10 integers giving the most important (and
268portable) members of the {\em stat} structure, in the order
269\code{st_mode},
270\code{st_ino},
271\code{st_dev},
272\code{st_nlink},
273\code{st_uid},
274\code{st_gid},
275\code{st_size},
276\code{st_atime},
277\code{st_mtime},
278\code{st_ctime}.
279More items may be added at the end by some implementations.
280(On MS-DOS, some items are filled with dummy values.)
281
282Note: The standard module \code{stat} defines functions and constants
283that are useful for extracting information from a stat structure.
284\end{funcdesc}
285
286\begin{funcdesc}{symlink}{src\, dst}
287Create a symbolic link pointing to \var{src} named \var{dst}. (On
288systems without symbolic links, this always raises
289\code{posix.error}.)
290\end{funcdesc}
291
292\begin{funcdesc}{system}{command}
293Execute the command (a string) in a subshell. This is implemented by
294calling the Standard C function \code{system()}, and has the same
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000295limitations. Changes to \code{posix.environ}, \code{sys.stdin} etc.\ are
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000296not reflected in the environment of the executed command. The return
297value is the exit status of the process as returned by Standard C
298\code{system()}.
299\end{funcdesc}
300
301\begin{funcdesc}{times}{}
302Return a 4-tuple of floating point numbers indicating accumulated CPU
303times, in seconds. The items are: user time, system time, children's
304user time, and children's system time, in that order. See the \UNIX{}
305manual page {\it times}(2). (Not on MS-DOS.)
306\end{funcdesc}
307
308\begin{funcdesc}{umask}{mask}
309Set the current numeric umask and returns the previous umask.
310(Not on MS-DOS.)
311\end{funcdesc}
312
313\begin{funcdesc}{uname}{}
314Return a 5-tuple containing information identifying the current
315operating system. The tuple contains 5 strings:
316\code{(\var{sysname}, \var{nodename}, \var{release}, \var{version}, \var{machine})}.
317Some systems truncate the nodename to 8
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000318characters or to the leading component; a better way to get the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000319hostname is \code{socket.gethostname()}. (Not on MS-DOS, nor on older
320\UNIX{} systems.)
321\end{funcdesc}
322
323\begin{funcdesc}{unlink}{path}
324Unlink \var{path}.
325\end{funcdesc}
326
327\begin{funcdesc}{utime}{path\, \(atime\, mtime\)}
328Set the access and modified time of the file to the given values.
329(The second argument is a tuple of two items.)
330\end{funcdesc}
331
332\begin{funcdesc}{wait}{}
333Wait for completion of a child process, and return a tuple containing
334its pid and exit status indication (encoded as by \UNIX{}).
335(Not on MS-DOS.)
336\end{funcdesc}
337
338\begin{funcdesc}{waitpid}{pid\, options}
339Wait for completion of a child process given by proces id, and return
340a tuple containing its pid and exit status indication (encoded as by
341\UNIX{}). The semantics of the call are affected by the value of
342the integer options, which should be 0 for normal operation. (If the
343system does not support waitpid(), this always raises
344\code{posix.error}. Not on MS-DOS.)
345\end{funcdesc}
346
347\begin{funcdesc}{write}{fd\, str}
348Write the string \var{str} to file descriptor \var{fd}.
349Return the number of bytes actually written.
Guido van Rossum28379701995-01-12 12:38:22 +0000350
351Note: this function is intended for low-level I/O and must be applied
352to a file descriptor as returned by \code{posix.open()} or
353\code{posix.pipe()}. To write a ``file object'' returned by the
354built-in function \code{open} or by \code{posix.popen} or
355\code{posix.fdopen}, or \code{sys.stdout} or \code{sys.stderr}, use
356its \code{write()} method.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000357\end{funcdesc}
Guido van Rossum4bbe9c01995-03-30 16:00:36 +0000358
359\begin{datadesc}{WNOHANG}
360The option for \code{waitpid()} to avoid hanging if no child process
361status is available immediately.
362\end{datadesc}