blob: 1032b5428cc615c64db5123689fcee0a55322278 [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
Guido van Rossum1e8b63e1996-06-26 19:22:46 +0000153\begin{funcdesc}{getpgrp}{}
154Return the current process group id.
155(Not on MS-DOS.)
156\end{funcdesc}
157
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000158\begin{funcdesc}{getpid}{}
159Return the current process id.
160(Not on MS-DOS.)
161\end{funcdesc}
162
163\begin{funcdesc}{getppid}{}
164Return the parent's process id.
165(Not on MS-DOS.)
166\end{funcdesc}
167
168\begin{funcdesc}{getuid}{}
169Return the current process's user id.
170(Not on MS-DOS.)
171\end{funcdesc}
172
173\begin{funcdesc}{kill}{pid\, sig}
174Kill the process \var{pid} with signal \var{sig}.
175(Not on MS-DOS.)
176\end{funcdesc}
177
178\begin{funcdesc}{link}{src\, dst}
179Create a hard link pointing to \var{src} named \var{dst}.
180(Not on MS-DOS.)
181\end{funcdesc}
182
183\begin{funcdesc}{listdir}{path}
184Return a list containing the names of the entries in the directory.
Guido van Rossum8c07bb41996-02-12 23:16:08 +0000185The list is in arbitrary order. It does not include the special
186entries \code{'.'} and \code{'..'} even if they are present in the
187directory.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000188\end{funcdesc}
189
190\begin{funcdesc}{lseek}{fd\, pos\, how}
191Set the current position of file descriptor \var{fd} to position
192\var{pos}, modified by \var{how}: 0 to set the position relative to
193the beginning of the file; 1 to set it relative to the current
194position; 2 to set it relative to the end of the file.
195\end{funcdesc}
196
197\begin{funcdesc}{lstat}{path}
198Like \code{stat()}, but do not follow symbolic links. (On systems
199without symbolic links, this is identical to \code{posix.stat}.)
200\end{funcdesc}
201
Guido van Rossum1e8b63e1996-06-26 19:22:46 +0000202\begin{funcdesc}{mkfifo}{path\optional{\, mode}}
203Create a FIFO (a POSIX named pipe) named \var{path} with numeric mode
204\var{mode}. The default \var{mode} is 0666 (octal). The current
205umask value is first masked out from the mode.
206(Not on MS-DOS.)
207
208FIFOs are pipes that can be accessed like regular files. FIFOs exist
209until they are deleted (for example with \code{os.unlink}).
210Generally, FIFOs are used as rendez-vous between ``client'' and
211``server'' type processes: the server opens the FIFO for reading, and
212the client opens it for writing. Note that \code{mkfifo()} doesn't
213open the FIFO -- it just creates the rendez-vous point.
214\end{funcdesc}
215
216\begin{funcdesc}{mkdir}{path\optional{\, mode}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000217Create a directory named \var{path} with numeric mode \var{mode}.
Guido van Rossum1e8b63e1996-06-26 19:22:46 +0000218The default \var{mode} is 0777 (octal). On some systems, \var{mode}
219is ignored. Where it is used, the current umask value is first
220masked out.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000221\end{funcdesc}
222
223\begin{funcdesc}{nice}{increment}
224Add \var{incr} to the process' ``niceness''. Return the new niceness.
225(Not on MS-DOS.)
226\end{funcdesc}
227
Barry Warsawfa5a6cf1996-12-19 22:38:56 +0000228\begin{funcdesc}{open}{file\, flags\optional{\, mode}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000229Open the file \var{file} and set various flags according to
230\var{flags} and possibly its mode according to \var{mode}.
Barry Warsawfa5a6cf1996-12-19 22:38:56 +0000231The default \var{mode} is 0777 (octal), and the current umask value is
232first masked out. Return the file descriptor for the newly opened
233file.
Guido van Rossum28379701995-01-12 12:38:22 +0000234
235Note: this function is intended for low-level I/O. For normal usage,
236use the built-in function \code{open}, which returns a ``file object''
237with \code{read()} and \code{write()} methods (and many more).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000238\end{funcdesc}
239
240\begin{funcdesc}{pipe}{}
241Create a pipe. Return a pair of file descriptors \code{(r, w)}
242usable for reading and writing, respectively.
243(Not on MS-DOS.)
244\end{funcdesc}
245
Guido van Rossum38e50881996-07-21 02:21:49 +0000246\begin{funcdesc}{plock}{op}
247Lock program segments into memory. The value of \var{op}
248(defined in \code{<sys/lock.h>}) determines which segments are locked.
249(Not on MS-DOS.)
250\end{funcdesc}
251
Guido van Rossum28379701995-01-12 12:38:22 +0000252\begin{funcdesc}{popen}{command\optional{\, mode\optional{\, bufsize}}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000253Open a pipe to or from \var{command}. The return value is an open
254file object connected to the pipe, which can be read or written
Guido van Rossum28379701995-01-12 12:38:22 +0000255depending on whether \var{mode} is \code{'r'} (default) or \code{'w'}.
256The \var{bufsize} argument has the same meaning as the corresponding
Guido van Rossum7e691de1997-05-09 02:22:59 +0000257argument to the built-in \code{open()} function. The exit status of
258the command (encoded in the format specified for \code{wait()}) is
259available as the return value of the \code{close()} method of the file
260object.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000261(Not on MS-DOS.)
262\end{funcdesc}
263
264\begin{funcdesc}{read}{fd\, n}
265Read at most \var{n} bytes from file descriptor \var{fd}.
266Return a string containing the bytes read.
Guido van Rossum28379701995-01-12 12:38:22 +0000267
268Note: this function is intended for low-level I/O and must be applied
269to a file descriptor as returned by \code{posix.open()} or
270\code{posix.pipe()}. To read a ``file object'' returned by the
271built-in function \code{open} or by \code{posix.popen} or
272\code{posix.fdopen}, or \code{sys.stdin}, use its
273\code{read()} or \code{readline()} methods.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000274\end{funcdesc}
275
276\begin{funcdesc}{readlink}{path}
277Return a string representing the path to which the symbolic link
278points. (On systems without symbolic links, this always raises
279\code{posix.error}.)
280\end{funcdesc}
281
Guido van Rossum8c07bb41996-02-12 23:16:08 +0000282\begin{funcdesc}{remove}{path}
283Remove the file \var{path}. See \code{rmdir} below to remove a directory.
284\end{funcdesc}
285
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000286\begin{funcdesc}{rename}{src\, dst}
287Rename the file or directory \var{src} to \var{dst}.
288\end{funcdesc}
289
290\begin{funcdesc}{rmdir}{path}
291Remove the directory \var{path}.
292\end{funcdesc}
293
294\begin{funcdesc}{setgid}{gid}
295Set the current process's group id.
296(Not on MS-DOS.)
297\end{funcdesc}
298
Guido van Rossum1e8b63e1996-06-26 19:22:46 +0000299\begin{funcdesc}{setpgrp}{}
300Calls the system call \code{setpgrp()} or \code{setpgrp(0, 0)}
Fred Drake4b3f0311996-12-13 22:04:31 +0000301depending on which version is implemented (if any). See the \UNIX{}
Guido van Rossum1e8b63e1996-06-26 19:22:46 +0000302manual for the semantics.
303(Not on MS-DOS.)
304\end{funcdesc}
305
306\begin{funcdesc}{setpgid}{pid\, pgrp}
Fred Drake4b3f0311996-12-13 22:04:31 +0000307Calls the system call \code{setpgid()}. See the \UNIX{} manual for
Guido van Rossum1e8b63e1996-06-26 19:22:46 +0000308the semantics.
309(Not on MS-DOS.)
310\end{funcdesc}
311
312\begin{funcdesc}{setsid}{}
Fred Drake4b3f0311996-12-13 22:04:31 +0000313Calls the system call \code{setsid()}. See the \UNIX{} manual for the
Guido van Rossum1e8b63e1996-06-26 19:22:46 +0000314semantics.
315(Not on MS-DOS.)
316\end{funcdesc}
317
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000318\begin{funcdesc}{setuid}{uid}
319Set the current process's user id.
320(Not on MS-DOS.)
321\end{funcdesc}
322
323\begin{funcdesc}{stat}{path}
324Perform a {\em stat} system call on the given path. The return value
325is a tuple of at least 10 integers giving the most important (and
326portable) members of the {\em stat} structure, in the order
327\code{st_mode},
328\code{st_ino},
329\code{st_dev},
330\code{st_nlink},
331\code{st_uid},
332\code{st_gid},
333\code{st_size},
334\code{st_atime},
335\code{st_mtime},
336\code{st_ctime}.
337More items may be added at the end by some implementations.
338(On MS-DOS, some items are filled with dummy values.)
339
340Note: The standard module \code{stat} defines functions and constants
341that are useful for extracting information from a stat structure.
342\end{funcdesc}
343
344\begin{funcdesc}{symlink}{src\, dst}
345Create a symbolic link pointing to \var{src} named \var{dst}. (On
346systems without symbolic links, this always raises
347\code{posix.error}.)
348\end{funcdesc}
349
350\begin{funcdesc}{system}{command}
351Execute the command (a string) in a subshell. This is implemented by
352calling the Standard C function \code{system()}, and has the same
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000353limitations. Changes to \code{posix.environ}, \code{sys.stdin} etc.\ are
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000354not reflected in the environment of the executed command. The return
Guido van Rossum7e691de1997-05-09 02:22:59 +0000355value is the exit status of the process encoded in the format
356specified for \code{wait()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000357\end{funcdesc}
358
Guido van Rossum1e8b63e1996-06-26 19:22:46 +0000359\begin{funcdesc}{tcgetpgrp}{fd}
360Return the process group associated with the terminal given by
361\var{fd} (an open file descriptor as returned by \code{posix.open()}).
362(Not on MS-DOS.)
363\end{funcdesc}
364
365\begin{funcdesc}{tcsetpgrp}{fd\, pg}
366Set the process group associated with the terminal given by
367\var{fd} (an open file descriptor as returned by \code{posix.open()})
368to \var{pg}.
369(Not on MS-DOS.)
370\end{funcdesc}
371
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000372\begin{funcdesc}{times}{}
Guido van Rossum1e150611995-09-13 17:36:35 +0000373Return a 5-tuple of floating point numbers indicating accumulated (CPU
374or other)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000375times, in seconds. The items are: user time, system time, children's
Guido van Rossum1e150611995-09-13 17:36:35 +0000376user time, children's system time, and elapsed real time since a fixed
377point in the past, in that order. See the \UNIX{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000378manual page {\it times}(2). (Not on MS-DOS.)
379\end{funcdesc}
380
381\begin{funcdesc}{umask}{mask}
382Set the current numeric umask and returns the previous umask.
383(Not on MS-DOS.)
384\end{funcdesc}
385
386\begin{funcdesc}{uname}{}
387Return a 5-tuple containing information identifying the current
388operating system. The tuple contains 5 strings:
389\code{(\var{sysname}, \var{nodename}, \var{release}, \var{version}, \var{machine})}.
390Some systems truncate the nodename to 8
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000391characters or to the leading component; a better way to get the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000392hostname is \code{socket.gethostname()}. (Not on MS-DOS, nor on older
393\UNIX{} systems.)
394\end{funcdesc}
395
396\begin{funcdesc}{unlink}{path}
Guido van Rossum8c07bb41996-02-12 23:16:08 +0000397Remove the file \var{path}. This is the same function as \code{remove};
398the \code{unlink} name is its traditional \UNIX{} name.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000399\end{funcdesc}
400
401\begin{funcdesc}{utime}{path\, \(atime\, mtime\)}
402Set the access and modified time of the file to the given values.
403(The second argument is a tuple of two items.)
404\end{funcdesc}
405
406\begin{funcdesc}{wait}{}
407Wait for completion of a child process, and return a tuple containing
Guido van Rossum7e691de1997-05-09 02:22:59 +0000408its pid and exit status indication: a 16-bit number, whose low byte is
409the signal number that killed the process, and whose high byte is the
410exit status (if the signal number is zero); the high bit of the low
411byte is set if a core file was produced. (Not on MS-DOS.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000412\end{funcdesc}
413
414\begin{funcdesc}{waitpid}{pid\, options}
415Wait for completion of a child process given by proces id, and return
Guido van Rossum7e691de1997-05-09 02:22:59 +0000416a tuple containing its pid and exit status indication (encoded as for
417\code{wait()}). The semantics of the call are affected by the value of
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000418the integer options, which should be 0 for normal operation. (If the
Guido van Rossum96628a91995-04-10 11:34:00 +0000419system does not support \code{waitpid()}, this always raises
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000420\code{posix.error}. Not on MS-DOS.)
421\end{funcdesc}
422
423\begin{funcdesc}{write}{fd\, str}
424Write the string \var{str} to file descriptor \var{fd}.
425Return the number of bytes actually written.
Guido van Rossum28379701995-01-12 12:38:22 +0000426
427Note: this function is intended for low-level I/O and must be applied
428to a file descriptor as returned by \code{posix.open()} or
429\code{posix.pipe()}. To write a ``file object'' returned by the
430built-in function \code{open} or by \code{posix.popen} or
431\code{posix.fdopen}, or \code{sys.stdout} or \code{sys.stderr}, use
432its \code{write()} method.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000433\end{funcdesc}
Guido van Rossum4bbe9c01995-03-30 16:00:36 +0000434
435\begin{datadesc}{WNOHANG}
436The option for \code{waitpid()} to avoid hanging if no child process
437status is available immediately.
438\end{datadesc}
Barry Warsawe5a43a41996-12-19 23:50:34 +0000439
440
441\begin{datadesc}{O_RDONLY}
442\end{datadesc}
443\begin{datadesc}{O_WRONLY}
444\end{datadesc}
445\begin{datadesc}{O_RDWR}
446\end{datadesc}
447\begin{datadesc}{O_NDELAY}
448\end{datadesc}
449\begin{datadesc}{O_NONBLOCK}
450\end{datadesc}
451\begin{datadesc}{O_APPEND}
452\end{datadesc}
453\begin{datadesc}{O_DSYNC}
454\end{datadesc}
455\begin{datadesc}{O_RSYNC}
456\end{datadesc}
457\begin{datadesc}{O_SYNC}
458\end{datadesc}
459\begin{datadesc}{O_NOCTTY}
460\end{datadesc}
461\begin{datadesc}{O_CREAT}
462\end{datadesc}
463\begin{datadesc}{O_EXCL}
464\end{datadesc}
465\begin{datadesc}{O_TRUNC}
466Options for the \code{flag} argument to the \code{open()} function.
467These can be bit-wise OR'd together.
468\end{datadesc}