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