blob: c22f1e06578b1295f4731a0faac1d88b853cd000 [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Built-in Module \sectcode{posix}}
2
3\bimodindex{posix}
4
5This module provides access to operating system functionality that is
6standardized by the C Standard and the POSIX standard (a thinly diguised
7\UNIX{} interface).
8It is available in all Python versions except on the Macintosh;
9the MS-DOS version does not support certain functions.
10The descriptions below are very terse; refer to the
11corresponding \UNIX{} manual entry for more information.
12
13Errors are reported as exceptions; the usual exceptions are given
14for type errors, while errors reported by the system calls raise
15\code{posix.error}, described below.
16
17Module \code{posix} defines the following data items:
18
19\renewcommand{\indexsubitem}{(data in module posix)}
20\begin{datadesc}{environ}
21A dictionary representing the string environment at the time
22the interpreter was started.
23(Modifying this dictionary does not affect the string environment of the
24interpreter.)
25For example,
26\code{posix.environ['HOME']}
27is the pathname of your home directory, equivalent to
28\code{getenv("HOME")}
29in C.
30\end{datadesc}
31
32\renewcommand{\indexsubitem}{(exception in module posix)}
33\begin{excdesc}{error}
34This exception is raised when an POSIX function returns a
35POSIX-related error (e.g., not for illegal argument types). Its
36string value is \code{'posix.error'}. The accompanying value is a
37pair containing the numeric error code from \code{errno} and the
38corresponding string, as would be printed by the C function
39\code{perror()}.
40\end{excdesc}
41
42It defines the following functions:
43
44\renewcommand{\indexsubitem}{(in module posix)}
45\begin{funcdesc}{chdir}{path}
46Change the current working directory to \var{path}.
47\end{funcdesc}
48
49\begin{funcdesc}{chmod}{path\, mode}
50Change the mode of \var{path} to the numeric \var{mode}.
51\end{funcdesc}
52
53\begin{funcdesc}{close}{fd}
54Close file descriptor \var{fd}.
55\end{funcdesc}
56
57\begin{funcdesc}{dup}{fd}
58Return a duplicate of file descriptor \var{fd}.
59\end{funcdesc}
60
61\begin{funcdesc}{dup2}{fd\, fd2}
62Duplicate file descriptor \var{fd} to \var{fd2}, closing the latter
63first if necessary. Return \code{None}.
64\end{funcdesc}
65
66\begin{funcdesc}{execv}{path\, args}
67Execute the executable \var{path} with argument list \var{args},
68replacing the current process (i.e., the Python interpreter).
69The argument list may be a tuple or list of strings.
70(Not on MS-DOS.)
71\end{funcdesc}
72
73\begin{funcdesc}{execve}{path\, args\, env}
74Execute the executable \var{path} with argument list \var{args},
75and environment \var{env},
76replacing the current process (i.e., the Python interpreter).
77The argument list may be a tuple or list of strings.
78The environment must be a dictionary mapping strings to strings.
79(Not on MS-DOS.)
80\end{funcdesc}
81
82\begin{funcdesc}{_exit}{n}
83Exit to the system with status \var{n}, without calling cleanup
84handlers, flushing stdio buffers, etc.
85(Not on MS-DOS.)
86
87Note: the standard way to exit is \code{sys.exit(\var{n})}.
88\code{posix._exit()} should normally only be used in the child process
89after a \code{fork()}.
90\end{funcdesc}
91
Guido van Rossumc5c67bc1994-02-15 15:59:23 +000092\begin{funcdesc}{fdopen}{fd\, mode}
93Return an open file object connected to the file descriptor \var{fd},
94open for reading and/or writing according to the \var{mode} string
95(which has the same meaning as the \var{mode} argument to the built-in
96\code{open()} function.
97\end{funcdesc}
98
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000099\begin{funcdesc}{fork}{}
100Fork a child process. Return 0 in the child, the child's process id
101in the parent.
102(Not on MS-DOS.)
103\end{funcdesc}
104
105\begin{funcdesc}{fstat}{fd}
106Return status for file descriptor \var{fd}, like \code{stat()}.
107\end{funcdesc}
108
109\begin{funcdesc}{getcwd}{}
110Return a string representing the current working directory.
111\end{funcdesc}
112
113\begin{funcdesc}{getegid}{}
114Return the current process's effective group id.
115(Not on MS-DOS.)
116\end{funcdesc}
117
118\begin{funcdesc}{geteuid}{}
119Return the current process's effective user id.
120(Not on MS-DOS.)
121\end{funcdesc}
122
123\begin{funcdesc}{getgid}{}
124Return the current process's group id.
125(Not on MS-DOS.)
126\end{funcdesc}
127
128\begin{funcdesc}{getpid}{}
129Return the current process id.
130(Not on MS-DOS.)
131\end{funcdesc}
132
133\begin{funcdesc}{getppid}{}
134Return the parent's process id.
135(Not on MS-DOS.)
136\end{funcdesc}
137
138\begin{funcdesc}{getuid}{}
139Return the current process's user id.
140(Not on MS-DOS.)
141\end{funcdesc}
142
143\begin{funcdesc}{kill}{pid\, sig}
144Kill the process \var{pid} with signal \var{sig}.
145(Not on MS-DOS.)
146\end{funcdesc}
147
148\begin{funcdesc}{link}{src\, dst}
149Create a hard link pointing to \var{src} named \var{dst}.
150(Not on MS-DOS.)
151\end{funcdesc}
152
153\begin{funcdesc}{listdir}{path}
154Return a list containing the names of the entries in the directory.
155The list is in arbitrary order. It includes the special entries
156\code{'.'} and \code{'..'} if they are present in the directory.
157\end{funcdesc}
158
159\begin{funcdesc}{lseek}{fd\, pos\, how}
160Set the current position of file descriptor \var{fd} to position
161\var{pos}, modified by \var{how}: 0 to set the position relative to
162the beginning of the file; 1 to set it relative to the current
163position; 2 to set it relative to the end of the file.
164\end{funcdesc}
165
166\begin{funcdesc}{lstat}{path}
167Like \code{stat()}, but do not follow symbolic links. (On systems
168without symbolic links, this is identical to \code{posix.stat}.)
169\end{funcdesc}
170
171\begin{funcdesc}{mkdir}{path\, mode}
172Create a directory named \var{path} with numeric mode \var{mode}.
173\end{funcdesc}
174
175\begin{funcdesc}{nice}{increment}
176Add \var{incr} to the process' ``niceness''. Return the new niceness.
177(Not on MS-DOS.)
178\end{funcdesc}
179
180\begin{funcdesc}{open}{file\, flags\, mode}
181Open the file \var{file} and set various flags according to
182\var{flags} and possibly its mode according to \var{mode}.
183Return the file descriptor for the newly opened file.
184\end{funcdesc}
185
186\begin{funcdesc}{pipe}{}
187Create a pipe. Return a pair of file descriptors \code{(r, w)}
188usable for reading and writing, respectively.
189(Not on MS-DOS.)
190\end{funcdesc}
191
192\begin{funcdesc}{popen}{command\, mode}
193Open a pipe to or from \var{command}. The return value is an open
194file object connected to the pipe, which can be read or written
195depending on whether \var{mode} is \code{'r'} or \code{'w'}.
196(Not on MS-DOS.)
197\end{funcdesc}
198
199\begin{funcdesc}{read}{fd\, n}
200Read at most \var{n} bytes from file descriptor \var{fd}.
201Return a string containing the bytes read.
202\end{funcdesc}
203
204\begin{funcdesc}{readlink}{path}
205Return a string representing the path to which the symbolic link
206points. (On systems without symbolic links, this always raises
207\code{posix.error}.)
208\end{funcdesc}
209
210\begin{funcdesc}{rename}{src\, dst}
211Rename the file or directory \var{src} to \var{dst}.
212\end{funcdesc}
213
214\begin{funcdesc}{rmdir}{path}
215Remove the directory \var{path}.
216\end{funcdesc}
217
218\begin{funcdesc}{setgid}{gid}
219Set the current process's group id.
220(Not on MS-DOS.)
221\end{funcdesc}
222
223\begin{funcdesc}{setuid}{uid}
224Set the current process's user id.
225(Not on MS-DOS.)
226\end{funcdesc}
227
228\begin{funcdesc}{stat}{path}
229Perform a {\em stat} system call on the given path. The return value
230is a tuple of at least 10 integers giving the most important (and
231portable) members of the {\em stat} structure, in the order
232\code{st_mode},
233\code{st_ino},
234\code{st_dev},
235\code{st_nlink},
236\code{st_uid},
237\code{st_gid},
238\code{st_size},
239\code{st_atime},
240\code{st_mtime},
241\code{st_ctime}.
242More items may be added at the end by some implementations.
243(On MS-DOS, some items are filled with dummy values.)
244
245Note: The standard module \code{stat} defines functions and constants
246that are useful for extracting information from a stat structure.
247\end{funcdesc}
248
249\begin{funcdesc}{symlink}{src\, dst}
250Create a symbolic link pointing to \var{src} named \var{dst}. (On
251systems without symbolic links, this always raises
252\code{posix.error}.)
253\end{funcdesc}
254
255\begin{funcdesc}{system}{command}
256Execute the command (a string) in a subshell. This is implemented by
257calling the Standard C function \code{system()}, and has the same
258limitations. Changes to \code{posix.environ}, \code{sys.stdin} etc. are
259not reflected in the environment of the executed command. The return
260value is the exit status of the process as returned by Standard C
261\code{system()}.
262\end{funcdesc}
263
264\begin{funcdesc}{times}{}
265Return a 4-tuple of floating point numbers indicating accumulated CPU
266times, in seconds. The items are: user time, system time, children's
267user time, and children's system time, in that order. See the \UNIX{}
268manual page {\it times}(2). (Not on MS-DOS.)
269\end{funcdesc}
270
271\begin{funcdesc}{umask}{mask}
272Set the current numeric umask and returns the previous umask.
273(Not on MS-DOS.)
274\end{funcdesc}
275
276\begin{funcdesc}{uname}{}
277Return a 5-tuple containing information identifying the current
278operating system. The tuple contains 5 strings:
279\code{(\var{sysname}, \var{nodename}, \var{release}, \var{version}, \var{machine})}.
280Some systems truncate the nodename to 8
281characters or to the leading component; an better way to get the
282hostname is \code{socket.gethostname()}. (Not on MS-DOS, nor on older
283\UNIX{} systems.)
284\end{funcdesc}
285
286\begin{funcdesc}{unlink}{path}
287Unlink \var{path}.
288\end{funcdesc}
289
290\begin{funcdesc}{utime}{path\, \(atime\, mtime\)}
291Set the access and modified time of the file to the given values.
292(The second argument is a tuple of two items.)
293\end{funcdesc}
294
295\begin{funcdesc}{wait}{}
296Wait for completion of a child process, and return a tuple containing
297its pid and exit status indication (encoded as by \UNIX{}).
298(Not on MS-DOS.)
299\end{funcdesc}
300
301\begin{funcdesc}{waitpid}{pid\, options}
302Wait for completion of a child process given by proces id, and return
303a tuple containing its pid and exit status indication (encoded as by
304\UNIX{}). The semantics of the call are affected by the value of
305the integer options, which should be 0 for normal operation. (If the
306system does not support waitpid(), this always raises
307\code{posix.error}. Not on MS-DOS.)
308\end{funcdesc}
309
310\begin{funcdesc}{write}{fd\, str}
311Write the string \var{str} to file descriptor \var{fd}.
312Return the number of bytes actually written.
313\end{funcdesc}