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