blob: e545c7a7b4eb1bed17e657179a30dd90eb53e7ed [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Built-in Module \sectcode{posix}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-posix}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00003\bimodindex{posix}
4
5This module provides access to operating system functionality that is
Guido van Rossum6bb1adc1995-03-13 10:03:32 +00006standardized by the C Standard and the POSIX standard (a thinly disguised
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00007\UNIX{} interface).
Guido van Rossum470be141995-03-17 16:07:09 +00008
9\strong{Do not import this module directly.} Instead, import the
10module \code{os}, which provides a \emph{portable} version of this
11interface. On \UNIX{}, the \code{os} module provides a superset of
12the \code{posix} interface. On non-\UNIX{} operating systems the
13\code{posix} module is not available, but a subset is always available
14through the \code{os} interface. Once \code{os} is imported, there is
15\emph{no} performance penalty in using it instead of
16\code{posix}.
17\stmodindex{os}
18
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000019The descriptions below are very terse; refer to the
Guido van Rossum96628a91995-04-10 11:34:00 +000020corresponding \UNIX{} manual entry for more information. Arguments
21called \var{path} refer to a pathname given as a string.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000022
23Errors are reported as exceptions; the usual exceptions are given
24for type errors, while errors reported by the system calls raise
25\code{posix.error}, described below.
26
27Module \code{posix} defines the following data items:
28
29\renewcommand{\indexsubitem}{(data in module posix)}
30\begin{datadesc}{environ}
31A dictionary representing the string environment at the time
32the interpreter was started.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000033For example,
34\code{posix.environ['HOME']}
35is the pathname of your home directory, equivalent to
36\code{getenv("HOME")}
37in C.
Guido van Rossum470be141995-03-17 16:07:09 +000038Modifying this dictionary does not affect the string environment
39passed on by \code{execv()}, \code{popen()} or \code{system()}; if you
40need to change the environment, pass \code{environ} to \code{execve()}
41or add variable assignments and export statements to the command
42string for \code{system()} or \code{popen()}.%
43\footnote{The problem with automatically passing on \code{environ} is
44that there is no portable way of changing the environment.}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000045\end{datadesc}
46
47\renewcommand{\indexsubitem}{(exception in module posix)}
48\begin{excdesc}{error}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000049This exception is raised when a POSIX function returns a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000050POSIX-related error (e.g., not for illegal argument types). Its
51string value is \code{'posix.error'}. The accompanying value is a
52pair containing the numeric error code from \code{errno} and the
53corresponding string, as would be printed by the C function
54\code{perror()}.
55\end{excdesc}
56
Guido van Rossum4bbe9c01995-03-30 16:00:36 +000057It defines the following functions and constants:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000058
59\renewcommand{\indexsubitem}{(in module posix)}
60\begin{funcdesc}{chdir}{path}
61Change the current working directory to \var{path}.
62\end{funcdesc}
63
64\begin{funcdesc}{chmod}{path\, mode}
65Change the mode of \var{path} to the numeric \var{mode}.
66\end{funcdesc}
67
Guido van Rossum31cce971995-01-04 19:17:34 +000068\begin{funcdesc}{chown}{path\, uid, gid}
69Change the owner and group id of \var{path} to the numeric \var{uid}
70and \var{gid}.
71(Not on MS-DOS.)
72\end{funcdesc}
73
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000074\begin{funcdesc}{close}{fd}
75Close file descriptor \var{fd}.
Guido van Rossum28379701995-01-12 12:38:22 +000076
77Note: this function is intended for low-level I/O and must be applied
78to a file descriptor as returned by \code{posix.open()} or
79\code{posix.pipe()}. To close a ``file object'' returned by the
80built-in function \code{open} or by \code{posix.popen} or
81\code{posix.fdopen}, use its \code{close()} method.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000082\end{funcdesc}
83
84\begin{funcdesc}{dup}{fd}
85Return a duplicate of file descriptor \var{fd}.
86\end{funcdesc}
87
88\begin{funcdesc}{dup2}{fd\, fd2}
89Duplicate file descriptor \var{fd} to \var{fd2}, closing the latter
90first if necessary. Return \code{None}.
91\end{funcdesc}
92
93\begin{funcdesc}{execv}{path\, args}
94Execute the executable \var{path} with argument list \var{args},
95replacing the current process (i.e., the Python interpreter).
96The argument list may be a tuple or list of strings.
97(Not on MS-DOS.)
98\end{funcdesc}
99
100\begin{funcdesc}{execve}{path\, args\, env}
101Execute the executable \var{path} with argument list \var{args},
102and environment \var{env},
103replacing the current process (i.e., the Python interpreter).
104The argument list may be a tuple or list of strings.
105The environment must be a dictionary mapping strings to strings.
106(Not on MS-DOS.)
107\end{funcdesc}
108
109\begin{funcdesc}{_exit}{n}
110Exit to the system with status \var{n}, without calling cleanup
111handlers, flushing stdio buffers, etc.
112(Not on MS-DOS.)
113
114Note: the standard way to exit is \code{sys.exit(\var{n})}.
115\code{posix._exit()} should normally only be used in the child process
116after a \code{fork()}.
117\end{funcdesc}
118
Guido van Rossum28379701995-01-12 12:38:22 +0000119\begin{funcdesc}{fdopen}{fd\optional{\, mode\optional{\, bufsize}}}
120Return an open file object connected to the file descriptor \var{fd}.
121The \var{mode} and \var{bufsize} arguments have the same meaning as
122the corresponding arguments to the built-in \code{open()} function.
Guido van Rossumc5c67bc1994-02-15 15:59:23 +0000123\end{funcdesc}
124
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000125\begin{funcdesc}{fork}{}
126Fork a child process. Return 0 in the child, the child's process id
127in the parent.
128(Not on MS-DOS.)
129\end{funcdesc}
130
131\begin{funcdesc}{fstat}{fd}
132Return status for file descriptor \var{fd}, like \code{stat()}.
133\end{funcdesc}
134
Guido van Rossumf967bf61997-06-02 17:28:51 +0000135\begin{funcdesc}{ftruncate}{fd\, length}
136Truncate the file corresponding to file descriptor \var{fd},
137so that it is at most \var{length} bytes in size.
138\end{funcdesc}
139
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000140\begin{funcdesc}{getcwd}{}
141Return a string representing the current working directory.
142\end{funcdesc}
143
144\begin{funcdesc}{getegid}{}
145Return the current process's effective group id.
146(Not on MS-DOS.)
147\end{funcdesc}
148
149\begin{funcdesc}{geteuid}{}
150Return the current process's effective user id.
151(Not on MS-DOS.)
152\end{funcdesc}
153
154\begin{funcdesc}{getgid}{}
155Return the current process's group id.
156(Not on MS-DOS.)
157\end{funcdesc}
158
Guido van Rossum1e8b63e1996-06-26 19:22:46 +0000159\begin{funcdesc}{getpgrp}{}
160Return the current process group id.
161(Not on MS-DOS.)
162\end{funcdesc}
163
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000164\begin{funcdesc}{getpid}{}
165Return the current process id.
166(Not on MS-DOS.)
167\end{funcdesc}
168
169\begin{funcdesc}{getppid}{}
170Return the parent's process id.
171(Not on MS-DOS.)
172\end{funcdesc}
173
174\begin{funcdesc}{getuid}{}
175Return the current process's user id.
176(Not on MS-DOS.)
177\end{funcdesc}
178
179\begin{funcdesc}{kill}{pid\, sig}
180Kill the process \var{pid} with signal \var{sig}.
181(Not on MS-DOS.)
182\end{funcdesc}
183
184\begin{funcdesc}{link}{src\, dst}
185Create a hard link pointing to \var{src} named \var{dst}.
186(Not on MS-DOS.)
187\end{funcdesc}
188
189\begin{funcdesc}{listdir}{path}
190Return a list containing the names of the entries in the directory.
Guido van Rossum8c07bb41996-02-12 23:16:08 +0000191The list is in arbitrary order. It does not include the special
192entries \code{'.'} and \code{'..'} even if they are present in the
193directory.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000194\end{funcdesc}
195
196\begin{funcdesc}{lseek}{fd\, pos\, how}
197Set the current position of file descriptor \var{fd} to position
198\var{pos}, modified by \var{how}: 0 to set the position relative to
199the beginning of the file; 1 to set it relative to the current
200position; 2 to set it relative to the end of the file.
201\end{funcdesc}
202
203\begin{funcdesc}{lstat}{path}
204Like \code{stat()}, but do not follow symbolic links. (On systems
205without symbolic links, this is identical to \code{posix.stat}.)
206\end{funcdesc}
207
Guido van Rossum1e8b63e1996-06-26 19:22:46 +0000208\begin{funcdesc}{mkfifo}{path\optional{\, mode}}
209Create a FIFO (a POSIX named pipe) named \var{path} with numeric mode
210\var{mode}. The default \var{mode} is 0666 (octal). The current
211umask value is first masked out from the mode.
212(Not on MS-DOS.)
213
214FIFOs are pipes that can be accessed like regular files. FIFOs exist
215until they are deleted (for example with \code{os.unlink}).
216Generally, FIFOs are used as rendez-vous between ``client'' and
217``server'' type processes: the server opens the FIFO for reading, and
218the client opens it for writing. Note that \code{mkfifo()} doesn't
219open the FIFO -- it just creates the rendez-vous point.
220\end{funcdesc}
221
222\begin{funcdesc}{mkdir}{path\optional{\, mode}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000223Create a directory named \var{path} with numeric mode \var{mode}.
Guido van Rossum1e8b63e1996-06-26 19:22:46 +0000224The default \var{mode} is 0777 (octal). On some systems, \var{mode}
225is ignored. Where it is used, the current umask value is first
226masked out.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000227\end{funcdesc}
228
229\begin{funcdesc}{nice}{increment}
230Add \var{incr} to the process' ``niceness''. Return the new niceness.
231(Not on MS-DOS.)
232\end{funcdesc}
233
Barry Warsawfa5a6cf1996-12-19 22:38:56 +0000234\begin{funcdesc}{open}{file\, flags\optional{\, mode}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000235Open the file \var{file} and set various flags according to
236\var{flags} and possibly its mode according to \var{mode}.
Barry Warsawfa5a6cf1996-12-19 22:38:56 +0000237The default \var{mode} is 0777 (octal), and the current umask value is
238first masked out. Return the file descriptor for the newly opened
239file.
Guido van Rossum28379701995-01-12 12:38:22 +0000240
241Note: this function is intended for low-level I/O. For normal usage,
242use the built-in function \code{open}, which returns a ``file object''
243with \code{read()} and \code{write()} methods (and many more).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000244\end{funcdesc}
245
246\begin{funcdesc}{pipe}{}
247Create a pipe. Return a pair of file descriptors \code{(r, w)}
248usable for reading and writing, respectively.
249(Not on MS-DOS.)
250\end{funcdesc}
251
Guido van Rossum38e50881996-07-21 02:21:49 +0000252\begin{funcdesc}{plock}{op}
253Lock program segments into memory. The value of \var{op}
254(defined in \code{<sys/lock.h>}) determines which segments are locked.
255(Not on MS-DOS.)
256\end{funcdesc}
257
Guido van Rossum28379701995-01-12 12:38:22 +0000258\begin{funcdesc}{popen}{command\optional{\, mode\optional{\, bufsize}}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000259Open a pipe to or from \var{command}. The return value is an open
260file object connected to the pipe, which can be read or written
Guido van Rossum28379701995-01-12 12:38:22 +0000261depending on whether \var{mode} is \code{'r'} (default) or \code{'w'}.
262The \var{bufsize} argument has the same meaning as the corresponding
Guido van Rossum7e691de1997-05-09 02:22:59 +0000263argument to the built-in \code{open()} function. The exit status of
264the command (encoded in the format specified for \code{wait()}) is
265available as the return value of the \code{close()} method of the file
266object.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000267(Not on MS-DOS.)
268\end{funcdesc}
269
Guido van Rossumf967bf61997-06-02 17:28:51 +0000270\begin{funcdesc}{putenv}{varname\, value}
271Set the environment variable named \var{varname} to the string \var{value}.
272Such changes to the environment affect
273subprocesses started with \code{os.system()}, \code{os.popen()} or
274\code{os.fork()} and \code{os.execv()}. (Not on all systems.)
275
276When \code{putenv()} is
277supported, assignments to items in \code{os.environ} are automatically
278translated into corresponding calls to \code{os.putenv()}; however,
279calls to \code{os.putenv()} don't update \code{os.environ}, so it is
280actually preferable to assign to items of \code{os.environ}.
281\end{funcdesc}
282
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000283\begin{funcdesc}{read}{fd\, n}
284Read at most \var{n} bytes from file descriptor \var{fd}.
285Return a string containing the bytes read.
Guido van Rossum28379701995-01-12 12:38:22 +0000286
287Note: this function is intended for low-level I/O and must be applied
288to a file descriptor as returned by \code{posix.open()} or
289\code{posix.pipe()}. To read a ``file object'' returned by the
290built-in function \code{open} or by \code{posix.popen} or
291\code{posix.fdopen}, or \code{sys.stdin}, use its
292\code{read()} or \code{readline()} methods.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000293\end{funcdesc}
294
295\begin{funcdesc}{readlink}{path}
296Return a string representing the path to which the symbolic link
297points. (On systems without symbolic links, this always raises
298\code{posix.error}.)
299\end{funcdesc}
300
Guido van Rossum8c07bb41996-02-12 23:16:08 +0000301\begin{funcdesc}{remove}{path}
302Remove the file \var{path}. See \code{rmdir} below to remove a directory.
Guido van Rossumf967bf61997-06-02 17:28:51 +0000303This is identical to the \code{unlink} function documented below.
Guido van Rossum8c07bb41996-02-12 23:16:08 +0000304\end{funcdesc}
305
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000306\begin{funcdesc}{rename}{src\, dst}
307Rename the file or directory \var{src} to \var{dst}.
308\end{funcdesc}
309
310\begin{funcdesc}{rmdir}{path}
311Remove the directory \var{path}.
312\end{funcdesc}
313
314\begin{funcdesc}{setgid}{gid}
315Set the current process's group id.
316(Not on MS-DOS.)
317\end{funcdesc}
318
Guido van Rossum1e8b63e1996-06-26 19:22:46 +0000319\begin{funcdesc}{setpgrp}{}
320Calls the system call \code{setpgrp()} or \code{setpgrp(0, 0)}
Fred Drake4b3f0311996-12-13 22:04:31 +0000321depending on which version is implemented (if any). See the \UNIX{}
Guido van Rossum1e8b63e1996-06-26 19:22:46 +0000322manual for the semantics.
323(Not on MS-DOS.)
324\end{funcdesc}
325
326\begin{funcdesc}{setpgid}{pid\, pgrp}
Fred Drake4b3f0311996-12-13 22:04:31 +0000327Calls the system call \code{setpgid()}. See the \UNIX{} manual for
Guido van Rossum1e8b63e1996-06-26 19:22:46 +0000328the semantics.
329(Not on MS-DOS.)
330\end{funcdesc}
331
332\begin{funcdesc}{setsid}{}
Fred Drake4b3f0311996-12-13 22:04:31 +0000333Calls the system call \code{setsid()}. See the \UNIX{} manual for the
Guido van Rossum1e8b63e1996-06-26 19:22:46 +0000334semantics.
335(Not on MS-DOS.)
336\end{funcdesc}
337
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000338\begin{funcdesc}{setuid}{uid}
339Set the current process's user id.
340(Not on MS-DOS.)
341\end{funcdesc}
342
343\begin{funcdesc}{stat}{path}
344Perform a {\em stat} system call on the given path. The return value
345is a tuple of at least 10 integers giving the most important (and
346portable) members of the {\em stat} structure, in the order
347\code{st_mode},
348\code{st_ino},
349\code{st_dev},
350\code{st_nlink},
351\code{st_uid},
352\code{st_gid},
353\code{st_size},
354\code{st_atime},
355\code{st_mtime},
356\code{st_ctime}.
357More items may be added at the end by some implementations.
358(On MS-DOS, some items are filled with dummy values.)
359
360Note: The standard module \code{stat} defines functions and constants
361that are useful for extracting information from a stat structure.
362\end{funcdesc}
363
364\begin{funcdesc}{symlink}{src\, dst}
365Create a symbolic link pointing to \var{src} named \var{dst}. (On
366systems without symbolic links, this always raises
367\code{posix.error}.)
368\end{funcdesc}
369
370\begin{funcdesc}{system}{command}
371Execute the command (a string) in a subshell. This is implemented by
372calling the Standard C function \code{system()}, and has the same
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000373limitations. Changes to \code{posix.environ}, \code{sys.stdin} etc.\ are
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000374not reflected in the environment of the executed command. The return
Guido van Rossum7e691de1997-05-09 02:22:59 +0000375value is the exit status of the process encoded in the format
376specified for \code{wait()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000377\end{funcdesc}
378
Guido van Rossum1e8b63e1996-06-26 19:22:46 +0000379\begin{funcdesc}{tcgetpgrp}{fd}
380Return the process group associated with the terminal given by
381\var{fd} (an open file descriptor as returned by \code{posix.open()}).
382(Not on MS-DOS.)
383\end{funcdesc}
384
385\begin{funcdesc}{tcsetpgrp}{fd\, pg}
386Set the process group associated with the terminal given by
387\var{fd} (an open file descriptor as returned by \code{posix.open()})
388to \var{pg}.
389(Not on MS-DOS.)
390\end{funcdesc}
391
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000392\begin{funcdesc}{times}{}
Guido van Rossum1e150611995-09-13 17:36:35 +0000393Return a 5-tuple of floating point numbers indicating accumulated (CPU
394or other)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000395times, in seconds. The items are: user time, system time, children's
Guido van Rossum1e150611995-09-13 17:36:35 +0000396user time, children's system time, and elapsed real time since a fixed
397point in the past, in that order. See the \UNIX{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000398manual page {\it times}(2). (Not on MS-DOS.)
399\end{funcdesc}
400
401\begin{funcdesc}{umask}{mask}
402Set the current numeric umask and returns the previous umask.
403(Not on MS-DOS.)
404\end{funcdesc}
405
406\begin{funcdesc}{uname}{}
407Return a 5-tuple containing information identifying the current
408operating system. The tuple contains 5 strings:
409\code{(\var{sysname}, \var{nodename}, \var{release}, \var{version}, \var{machine})}.
410Some systems truncate the nodename to 8
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000411characters or to the leading component; a better way to get the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000412hostname is \code{socket.gethostname()}. (Not on MS-DOS, nor on older
413\UNIX{} systems.)
414\end{funcdesc}
415
416\begin{funcdesc}{unlink}{path}
Guido van Rossum8c07bb41996-02-12 23:16:08 +0000417Remove the file \var{path}. This is the same function as \code{remove};
418the \code{unlink} name is its traditional \UNIX{} name.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000419\end{funcdesc}
420
Fred Drake455838a1997-06-06 21:57:35 +0000421\begin{funcdesc}{utime}{path\, {\rm (}atime, mtime{\rm )}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000422Set the access and modified time of the file to the given values.
423(The second argument is a tuple of two items.)
424\end{funcdesc}
425
426\begin{funcdesc}{wait}{}
427Wait for completion of a child process, and return a tuple containing
Guido van Rossum7e691de1997-05-09 02:22:59 +0000428its pid and exit status indication: a 16-bit number, whose low byte is
429the signal number that killed the process, and whose high byte is the
430exit status (if the signal number is zero); the high bit of the low
431byte is set if a core file was produced. (Not on MS-DOS.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000432\end{funcdesc}
433
434\begin{funcdesc}{waitpid}{pid\, options}
435Wait for completion of a child process given by proces id, and return
Guido van Rossum7e691de1997-05-09 02:22:59 +0000436a tuple containing its pid and exit status indication (encoded as for
437\code{wait()}). The semantics of the call are affected by the value of
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000438the integer options, which should be 0 for normal operation. (If the
Guido van Rossum96628a91995-04-10 11:34:00 +0000439system does not support \code{waitpid()}, this always raises
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000440\code{posix.error}. Not on MS-DOS.)
441\end{funcdesc}
442
443\begin{funcdesc}{write}{fd\, str}
444Write the string \var{str} to file descriptor \var{fd}.
445Return the number of bytes actually written.
Guido van Rossum28379701995-01-12 12:38:22 +0000446
447Note: this function is intended for low-level I/O and must be applied
448to a file descriptor as returned by \code{posix.open()} or
449\code{posix.pipe()}. To write a ``file object'' returned by the
450built-in function \code{open} or by \code{posix.popen} or
451\code{posix.fdopen}, or \code{sys.stdout} or \code{sys.stderr}, use
452its \code{write()} method.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000453\end{funcdesc}
Guido van Rossum4bbe9c01995-03-30 16:00:36 +0000454
455\begin{datadesc}{WNOHANG}
456The option for \code{waitpid()} to avoid hanging if no child process
457status is available immediately.
458\end{datadesc}
Barry Warsawe5a43a41996-12-19 23:50:34 +0000459
460
461\begin{datadesc}{O_RDONLY}
462\end{datadesc}
463\begin{datadesc}{O_WRONLY}
464\end{datadesc}
465\begin{datadesc}{O_RDWR}
466\end{datadesc}
467\begin{datadesc}{O_NDELAY}
468\end{datadesc}
469\begin{datadesc}{O_NONBLOCK}
470\end{datadesc}
471\begin{datadesc}{O_APPEND}
472\end{datadesc}
473\begin{datadesc}{O_DSYNC}
474\end{datadesc}
475\begin{datadesc}{O_RSYNC}
476\end{datadesc}
477\begin{datadesc}{O_SYNC}
478\end{datadesc}
479\begin{datadesc}{O_NOCTTY}
480\end{datadesc}
481\begin{datadesc}{O_CREAT}
482\end{datadesc}
483\begin{datadesc}{O_EXCL}
484\end{datadesc}
485\begin{datadesc}{O_TRUNC}
486Options for the \code{flag} argument to the \code{open()} function.
487These can be bit-wise OR'd together.
488\end{datadesc}