Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{posix} --- |
| 2 | The most common \POSIX{} system calls.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | \declaremodule{builtin}{posix} |
| 4 | |
| 5 | \modulesynopsis{The most common \POSIX{} system calls (normally used via module |
| 6 | \module{os}).} |
| 7 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 8 | |
| 9 | This module provides access to operating system functionality that is |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 10 | standardized by the \C{} Standard and the \POSIX{} standard (a thinly |
| 11 | disguised \UNIX{} interface). |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 12 | |
| 13 | \strong{Do not import this module directly.} Instead, import the |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 14 | module \module{os}, which provides a \emph{portable} version of this |
| 15 | interface. On \UNIX{}, the \module{os} module provides a superset of |
| 16 | the \module{posix} interface. On non-\UNIX{} operating systems the |
| 17 | \module{posix} module is not available, but a subset is always |
| 18 | available through the \module{os} interface. Once \module{os} is |
| 19 | imported, there is \emph{no} performance penalty in using it instead |
| 20 | of \module{posix}. In addition, \module{os} provides some additional |
| 21 | functionality, such as automatically calling \function{putenv()} |
| 22 | when an entry in \code{os.environ} is changed. |
Fred Drake | 6206394 | 1997-12-15 21:42:51 +0000 | [diff] [blame] | 23 | \refstmodindex{os} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | 282290f | 1997-08-27 14:54:25 +0000 | [diff] [blame] | 25 | The descriptions below are very terse; refer to the corresponding |
Fred Drake | 65b32f7 | 1998-02-09 20:27:12 +0000 | [diff] [blame] | 26 | \UNIX{} manual (or \POSIX{} documentation) entry for more information. |
Guido van Rossum | 282290f | 1997-08-27 14:54:25 +0000 | [diff] [blame] | 27 | Arguments called \var{path} refer to a pathname given as a string. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 28 | |
Barry Warsaw | eef2cd1 | 1998-07-23 19:50:09 +0000 | [diff] [blame] | 29 | Errors are reported as exceptions; the usual exceptions are given for |
| 30 | type errors, while errors reported by the system calls raise |
| 31 | \exception{error} (a synonym for the standard exception |
| 32 | \exception{OSError}), described |
| 33 | below. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 34 | |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 35 | Module \module{posix} defines the following data items: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 37 | \begin{datadesc}{environ} |
Guido van Rossum | 0410196 | 1998-10-24 20:16:56 +0000 | [diff] [blame] | 38 | A dictionary or dictionary look-alike representing the string |
| 39 | environment at the time the interpreter was started. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 40 | For example, |
| 41 | \code{posix.environ['HOME']} |
| 42 | is the pathname of your home directory, equivalent to |
| 43 | \code{getenv("HOME")} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 44 | in \C{}. |
Guido van Rossum | 9c43c59 | 1997-08-08 21:05:09 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 46 | Modifying this dictionary does not affect the string environment |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 47 | passed on by \function{execv()}, \function{popen()} or |
| 48 | \function{system()}; if you need to change the environment, pass |
| 49 | \code{environ} to \function{execve()} or add variable assignments and |
| 50 | export statements to the command string for \function{system()} or |
| 51 | \function{popen()}. |
Guido van Rossum | 9c43c59 | 1997-08-08 21:05:09 +0000 | [diff] [blame] | 52 | |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 53 | \emph{However:} If you are using this module via the \module{os} |
| 54 | module (as you should -- see the introduction above), \code{environ} |
| 55 | is a a mapping object that behaves almost like a dictionary but |
Fred Drake | c024c99 | 1998-10-28 18:19:16 +0000 | [diff] [blame] | 56 | invokes \function{putenv()} automatically whenever an item is changed. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 57 | \end{datadesc} |
| 58 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 59 | \begin{excdesc}{error} |
Fred Drake | 65b32f7 | 1998-02-09 20:27:12 +0000 | [diff] [blame] | 60 | This exception is raised when a \POSIX{} function returns a |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 61 | \POSIX{}-related error (e.g., not for illegal argument types). The |
| 62 | accompanying value is a pair containing the numeric error code from |
| 63 | \cdata{errno} and the corresponding string, as would be printed by the |
| 64 | \C{} function \cfunction{perror()}. See the module |
| 65 | \module{errno}\refbimodindex{errno}, which contains names for the |
| 66 | error codes defined by the underlying operating system. |
| 67 | |
| 68 | When exceptions are classes, this exception carries two attributes, |
| 69 | \member{errno} and \member{strerror}. The first holds the value of |
| 70 | the \C{} \cdata{errno} variable, and the latter holds the |
Barry Warsaw | eef2cd1 | 1998-07-23 19:50:09 +0000 | [diff] [blame] | 71 | corresponding error message from \cfunction{strerror()}. For |
| 72 | exceptions that involve a file system path (e.g. \code{chdir} or |
| 73 | \code{unlink}), the exception instance will contain a third attribute |
| 74 | \member{filename} which is the file name passed to the |
| 75 | function. |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 76 | |
| 77 | When exceptions are strings, the string for the exception is |
Barry Warsaw | eef2cd1 | 1998-07-23 19:50:09 +0000 | [diff] [blame] | 78 | \code{'OSError'}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 79 | \end{excdesc} |
| 80 | |
Guido van Rossum | 4bbe9c0 | 1995-03-30 16:00:36 +0000 | [diff] [blame] | 81 | It defines the following functions and constants: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 83 | \begin{funcdesc}{chdir}{path} |
| 84 | Change the current working directory to \var{path}. |
| 85 | \end{funcdesc} |
| 86 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 87 | \begin{funcdesc}{chmod}{path, mode} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 88 | Change the mode of \var{path} to the numeric \var{mode}. |
| 89 | \end{funcdesc} |
| 90 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 91 | \begin{funcdesc}{chown}{path, uid, gid} |
Guido van Rossum | 31cce97 | 1995-01-04 19:17:34 +0000 | [diff] [blame] | 92 | Change the owner and group id of \var{path} to the numeric \var{uid} |
| 93 | and \var{gid}. |
| 94 | (Not on MS-DOS.) |
| 95 | \end{funcdesc} |
| 96 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 97 | \begin{funcdesc}{close}{fd} |
| 98 | Close file descriptor \var{fd}. |
Guido van Rossum | 2837970 | 1995-01-12 12:38:22 +0000 | [diff] [blame] | 99 | |
| 100 | Note: this function is intended for low-level I/O and must be applied |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 101 | to a file descriptor as returned by \function{open()} or |
| 102 | \function{pipe()}. To close a ``file object'' returned by the |
| 103 | built-in function \function{open()} or by \function{popen()} or |
| 104 | \function{fdopen()}, use its \method{close()} method. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 105 | \end{funcdesc} |
| 106 | |
| 107 | \begin{funcdesc}{dup}{fd} |
| 108 | Return a duplicate of file descriptor \var{fd}. |
| 109 | \end{funcdesc} |
| 110 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 111 | \begin{funcdesc}{dup2}{fd, fd2} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 112 | Duplicate file descriptor \var{fd} to \var{fd2}, closing the latter |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 113 | first if necessary. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 114 | \end{funcdesc} |
| 115 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 116 | \begin{funcdesc}{execv}{path, args} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 117 | Execute the executable \var{path} with argument list \var{args}, |
| 118 | replacing the current process (i.e., the Python interpreter). |
| 119 | The argument list may be a tuple or list of strings. |
| 120 | (Not on MS-DOS.) |
| 121 | \end{funcdesc} |
| 122 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 123 | \begin{funcdesc}{execve}{path, args, env} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 124 | Execute the executable \var{path} with argument list \var{args}, |
| 125 | and environment \var{env}, |
| 126 | replacing the current process (i.e., the Python interpreter). |
| 127 | The argument list may be a tuple or list of strings. |
| 128 | The environment must be a dictionary mapping strings to strings. |
| 129 | (Not on MS-DOS.) |
| 130 | \end{funcdesc} |
| 131 | |
| 132 | \begin{funcdesc}{_exit}{n} |
| 133 | Exit to the system with status \var{n}, without calling cleanup |
| 134 | handlers, flushing stdio buffers, etc. |
| 135 | (Not on MS-DOS.) |
| 136 | |
| 137 | Note: the standard way to exit is \code{sys.exit(\var{n})}. |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 138 | \function{_exit()} should normally only be used in the child process |
| 139 | after a \function{fork()}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 140 | \end{funcdesc} |
| 141 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 142 | \begin{funcdesc}{fdopen}{fd\optional{, mode\optional{, bufsize}}} |
Guido van Rossum | 2837970 | 1995-01-12 12:38:22 +0000 | [diff] [blame] | 143 | Return an open file object connected to the file descriptor \var{fd}. |
| 144 | The \var{mode} and \var{bufsize} arguments have the same meaning as |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 145 | the corresponding arguments to the built-in \function{open()} function. |
Guido van Rossum | c5c67bc | 1994-02-15 15:59:23 +0000 | [diff] [blame] | 146 | \end{funcdesc} |
| 147 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 148 | \begin{funcdesc}{fork}{} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 149 | Fork a child process. Return \code{0} in the child, the child's |
| 150 | process id in the parent. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 151 | (Not on MS-DOS.) |
| 152 | \end{funcdesc} |
| 153 | |
| 154 | \begin{funcdesc}{fstat}{fd} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 155 | Return status for file descriptor \var{fd}, like \function{stat()}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 156 | \end{funcdesc} |
| 157 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 158 | \begin{funcdesc}{ftruncate}{fd, length} |
Guido van Rossum | f967bf6 | 1997-06-02 17:28:51 +0000 | [diff] [blame] | 159 | Truncate the file corresponding to file descriptor \var{fd}, |
| 160 | so that it is at most \var{length} bytes in size. |
| 161 | \end{funcdesc} |
| 162 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 163 | \begin{funcdesc}{getcwd}{} |
| 164 | Return a string representing the current working directory. |
| 165 | \end{funcdesc} |
| 166 | |
| 167 | \begin{funcdesc}{getegid}{} |
Guido van Rossum | eb0f066 | 1997-12-30 20:38:16 +0000 | [diff] [blame] | 168 | Return the current process' effective group id. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 169 | (Not on MS-DOS.) |
| 170 | \end{funcdesc} |
| 171 | |
| 172 | \begin{funcdesc}{geteuid}{} |
Guido van Rossum | eb0f066 | 1997-12-30 20:38:16 +0000 | [diff] [blame] | 173 | Return the current process' effective user id. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 174 | (Not on MS-DOS.) |
| 175 | \end{funcdesc} |
| 176 | |
| 177 | \begin{funcdesc}{getgid}{} |
Guido van Rossum | eb0f066 | 1997-12-30 20:38:16 +0000 | [diff] [blame] | 178 | Return the current process' group id. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 179 | (Not on MS-DOS.) |
| 180 | \end{funcdesc} |
| 181 | |
Guido van Rossum | 1e8b63e | 1996-06-26 19:22:46 +0000 | [diff] [blame] | 182 | \begin{funcdesc}{getpgrp}{} |
Fred Drake | 3b02ddf | 1998-12-21 18:52:53 +0000 | [diff] [blame] | 183 | \index{process!group} |
Guido van Rossum | 1e8b63e | 1996-06-26 19:22:46 +0000 | [diff] [blame] | 184 | Return the current process group id. |
| 185 | (Not on MS-DOS.) |
| 186 | \end{funcdesc} |
| 187 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 188 | \begin{funcdesc}{getpid}{} |
Fred Drake | 3b02ddf | 1998-12-21 18:52:53 +0000 | [diff] [blame] | 189 | \index{process!id} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 190 | Return the current process id. |
| 191 | (Not on MS-DOS.) |
| 192 | \end{funcdesc} |
| 193 | |
| 194 | \begin{funcdesc}{getppid}{} |
Fred Drake | 3b02ddf | 1998-12-21 18:52:53 +0000 | [diff] [blame] | 195 | \index{process!id of parent} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 196 | Return the parent's process id. |
| 197 | (Not on MS-DOS.) |
| 198 | \end{funcdesc} |
| 199 | |
| 200 | \begin{funcdesc}{getuid}{} |
Fred Drake | 3b02ddf | 1998-12-21 18:52:53 +0000 | [diff] [blame] | 201 | \index{user id} |
Guido van Rossum | eb0f066 | 1997-12-30 20:38:16 +0000 | [diff] [blame] | 202 | Return the current process' user id. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 203 | (Not on MS-DOS.) |
| 204 | \end{funcdesc} |
| 205 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 206 | \begin{funcdesc}{kill}{pid, sig} |
Fred Drake | 3b02ddf | 1998-12-21 18:52:53 +0000 | [diff] [blame] | 207 | \index{process!killing} |
| 208 | \index{process!signalling} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 209 | Kill the process \var{pid} with signal \var{sig}. |
| 210 | (Not on MS-DOS.) |
| 211 | \end{funcdesc} |
| 212 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 213 | \begin{funcdesc}{link}{src, dst} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 214 | Create a hard link pointing to \var{src} named \var{dst}. |
| 215 | (Not on MS-DOS.) |
| 216 | \end{funcdesc} |
| 217 | |
| 218 | \begin{funcdesc}{listdir}{path} |
| 219 | Return a list containing the names of the entries in the directory. |
Guido van Rossum | 8c07bb4 | 1996-02-12 23:16:08 +0000 | [diff] [blame] | 220 | The list is in arbitrary order. It does not include the special |
| 221 | entries \code{'.'} and \code{'..'} even if they are present in the |
| 222 | directory. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 223 | \end{funcdesc} |
| 224 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 225 | \begin{funcdesc}{lseek}{fd, pos, how} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 226 | Set the current position of file descriptor \var{fd} to position |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 227 | \var{pos}, modified by \var{how}: \code{0} to set the position |
| 228 | relative to the beginning of the file; \code{1} to set it relative to |
| 229 | the current position; \code{2} to set it relative to the end of the |
| 230 | file. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 231 | \end{funcdesc} |
| 232 | |
| 233 | \begin{funcdesc}{lstat}{path} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 234 | Like \function{stat()}, but do not follow symbolic links. (On systems |
| 235 | without symbolic links, this is identical to \function{stat()}.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 236 | \end{funcdesc} |
| 237 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 238 | \begin{funcdesc}{mkfifo}{path\optional{, mode}} |
Fred Drake | 65b32f7 | 1998-02-09 20:27:12 +0000 | [diff] [blame] | 239 | Create a FIFO (a \POSIX{} named pipe) named \var{path} with numeric mode |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 240 | \var{mode}. The default \var{mode} is \code{0666} (octal). The current |
Guido van Rossum | 1e8b63e | 1996-06-26 19:22:46 +0000 | [diff] [blame] | 241 | umask value is first masked out from the mode. |
| 242 | (Not on MS-DOS.) |
| 243 | |
| 244 | FIFOs are pipes that can be accessed like regular files. FIFOs exist |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 245 | until they are deleted (for example with \function{os.unlink()}). |
| 246 | Generally, FIFOs are used as rendezvous between ``client'' and |
Guido van Rossum | 1e8b63e | 1996-06-26 19:22:46 +0000 | [diff] [blame] | 247 | ``server'' type processes: the server opens the FIFO for reading, and |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 248 | the client opens it for writing. Note that \function{mkfifo()} |
| 249 | doesn't open the FIFO --- it just creates the rendezvous point. |
Guido van Rossum | 1e8b63e | 1996-06-26 19:22:46 +0000 | [diff] [blame] | 250 | \end{funcdesc} |
| 251 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 252 | \begin{funcdesc}{mkdir}{path\optional{, mode}} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 253 | Create a directory named \var{path} with numeric mode \var{mode}. |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 254 | The default \var{mode} is \code{0777} (octal). On some systems, |
| 255 | \var{mode} is ignored. Where it is used, the current umask value is |
| 256 | first masked out. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 257 | \end{funcdesc} |
| 258 | |
| 259 | \begin{funcdesc}{nice}{increment} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 260 | Add \var{increment} to the process' ``niceness''. Return the new |
| 261 | niceness. (Not on MS-DOS.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 262 | \end{funcdesc} |
| 263 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 264 | \begin{funcdesc}{open}{file, flags\optional{, mode}} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 265 | Open the file \var{file} and set various flags according to |
| 266 | \var{flags} and possibly its mode according to \var{mode}. |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 267 | The default \var{mode} is \code{0777} (octal), and the current umask |
| 268 | value is first masked out. Return the file descriptor for the newly |
| 269 | opened file. |
Guido van Rossum | 2837970 | 1995-01-12 12:38:22 +0000 | [diff] [blame] | 270 | |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 271 | For a description of the flag and mode values, see the \UNIX{} or \C{} |
| 272 | run-time documentation; flag constants (like \constant{O_RDONLY} and |
| 273 | \constant{O_WRONLY}) are defined in this module too (see below). |
Guido van Rossum | 9c43c59 | 1997-08-08 21:05:09 +0000 | [diff] [blame] | 274 | |
Guido van Rossum | 2837970 | 1995-01-12 12:38:22 +0000 | [diff] [blame] | 275 | Note: this function is intended for low-level I/O. For normal usage, |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 276 | use the built-in function \function{open()}, which returns a ``file |
| 277 | object'' with \method{read()} and \method{write()} methods (and many |
| 278 | more). |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 279 | \end{funcdesc} |
| 280 | |
| 281 | \begin{funcdesc}{pipe}{} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 282 | Create a pipe. Return a pair of file descriptors \code{(\var{r}, |
| 283 | \var{w})} usable for reading and writing, respectively. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 284 | (Not on MS-DOS.) |
| 285 | \end{funcdesc} |
| 286 | |
Guido van Rossum | 38e5088 | 1996-07-21 02:21:49 +0000 | [diff] [blame] | 287 | \begin{funcdesc}{plock}{op} |
| 288 | Lock program segments into memory. The value of \var{op} |
| 289 | (defined in \code{<sys/lock.h>}) determines which segments are locked. |
| 290 | (Not on MS-DOS.) |
| 291 | \end{funcdesc} |
| 292 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 293 | \begin{funcdesc}{popen}{command\optional{, mode\optional{, bufsize}}} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 294 | Open a pipe to or from \var{command}. The return value is an open |
| 295 | file object connected to the pipe, which can be read or written |
Guido van Rossum | 2837970 | 1995-01-12 12:38:22 +0000 | [diff] [blame] | 296 | depending on whether \var{mode} is \code{'r'} (default) or \code{'w'}. |
| 297 | The \var{bufsize} argument has the same meaning as the corresponding |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 298 | argument to the built-in \function{open()} function. The exit status of |
| 299 | the command (encoded in the format specified for \function{wait()}) is |
| 300 | available as the return value of the \method{close()} method of the file |
Guido van Rossum | f35b884 | 1998-10-15 13:28:29 +0000 | [diff] [blame] | 301 | object, except that when the exit status is zero (termination without |
| 302 | errors), \code{None} is returned. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 303 | (Not on MS-DOS.) |
| 304 | \end{funcdesc} |
| 305 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 306 | \begin{funcdesc}{putenv}{varname, value} |
Fred Drake | 52405c8 | 1998-03-16 05:21:08 +0000 | [diff] [blame] | 307 | \index{environment variables!setting} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 308 | Set the environment variable named \var{varname} to the string |
| 309 | \var{value}. Such changes to the environment affect subprocesses |
| 310 | started with \function{os.system()}, \function{os.popen()} or |
| 311 | \function{os.fork()} and \function{os.execv()}. (Not on all systems.) |
Guido van Rossum | f967bf6 | 1997-06-02 17:28:51 +0000 | [diff] [blame] | 312 | |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 313 | When \function{putenv()} is |
Guido van Rossum | f967bf6 | 1997-06-02 17:28:51 +0000 | [diff] [blame] | 314 | supported, assignments to items in \code{os.environ} are automatically |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 315 | translated into corresponding calls to \function{putenv()}; however, |
| 316 | calls to \function{putenv()} don't update \code{os.environ}, so it is |
Guido van Rossum | f967bf6 | 1997-06-02 17:28:51 +0000 | [diff] [blame] | 317 | actually preferable to assign to items of \code{os.environ}. |
| 318 | \end{funcdesc} |
| 319 | |
Guido van Rossum | 0bfd146 | 1997-10-05 18:54:52 +0000 | [diff] [blame] | 320 | \begin{funcdesc}{strerror}{code} |
| 321 | Return the error message corresponding to the error code in \var{code}. |
| 322 | \end{funcdesc} |
| 323 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 324 | \begin{funcdesc}{read}{fd, n} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 325 | Read at most \var{n} bytes from file descriptor \var{fd}. |
| 326 | Return a string containing the bytes read. |
Guido van Rossum | 2837970 | 1995-01-12 12:38:22 +0000 | [diff] [blame] | 327 | |
| 328 | Note: this function is intended for low-level I/O and must be applied |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 329 | to a file descriptor as returned by \function{open()} or |
| 330 | \function{pipe()}. To read a ``file object'' returned by the |
| 331 | built-in function \function{open()} or by \function{popen()} or |
| 332 | \function{fdopen()}, or \code{sys.stdin}, use its |
| 333 | \method{read()} or \method{readline()} methods. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 334 | \end{funcdesc} |
| 335 | |
| 336 | \begin{funcdesc}{readlink}{path} |
| 337 | Return a string representing the path to which the symbolic link |
| 338 | points. (On systems without symbolic links, this always raises |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 339 | \exception{error}.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 340 | \end{funcdesc} |
| 341 | |
Guido van Rossum | 8c07bb4 | 1996-02-12 23:16:08 +0000 | [diff] [blame] | 342 | \begin{funcdesc}{remove}{path} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 343 | Remove the file \var{path}. See \function{rmdir()} below to remove a |
| 344 | directory. This is identical to the \function{unlink()} function |
| 345 | documented below. |
Guido van Rossum | 8c07bb4 | 1996-02-12 23:16:08 +0000 | [diff] [blame] | 346 | \end{funcdesc} |
| 347 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 348 | \begin{funcdesc}{rename}{src, dst} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 349 | Rename the file or directory \var{src} to \var{dst}. |
| 350 | \end{funcdesc} |
| 351 | |
| 352 | \begin{funcdesc}{rmdir}{path} |
| 353 | Remove the directory \var{path}. |
| 354 | \end{funcdesc} |
| 355 | |
| 356 | \begin{funcdesc}{setgid}{gid} |
Guido van Rossum | eb0f066 | 1997-12-30 20:38:16 +0000 | [diff] [blame] | 357 | Set the current process' group id. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 358 | (Not on MS-DOS.) |
| 359 | \end{funcdesc} |
| 360 | |
Guido van Rossum | 1e8b63e | 1996-06-26 19:22:46 +0000 | [diff] [blame] | 361 | \begin{funcdesc}{setpgrp}{} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 362 | Calls the system call \cfunction{setpgrp()} or \cfunction{setpgrp(0, |
| 363 | 0)} depending on which version is implemented (if any). See the |
| 364 | \UNIX{} manual for the semantics. |
Guido van Rossum | 1e8b63e | 1996-06-26 19:22:46 +0000 | [diff] [blame] | 365 | (Not on MS-DOS.) |
| 366 | \end{funcdesc} |
| 367 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 368 | \begin{funcdesc}{setpgid}{pid, pgrp} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 369 | Calls the system call \cfunction{setpgid()}. See the \UNIX{} manual |
| 370 | for the semantics. |
Guido van Rossum | 1e8b63e | 1996-06-26 19:22:46 +0000 | [diff] [blame] | 371 | (Not on MS-DOS.) |
| 372 | \end{funcdesc} |
| 373 | |
| 374 | \begin{funcdesc}{setsid}{} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 375 | Calls the system call \cfunction{setsid()}. See the \UNIX{} manual |
| 376 | for the semantics. |
Guido van Rossum | 1e8b63e | 1996-06-26 19:22:46 +0000 | [diff] [blame] | 377 | (Not on MS-DOS.) |
| 378 | \end{funcdesc} |
| 379 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 380 | \begin{funcdesc}{setuid}{uid} |
Guido van Rossum | eb0f066 | 1997-12-30 20:38:16 +0000 | [diff] [blame] | 381 | Set the current process' user id. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 382 | (Not on MS-DOS.) |
| 383 | \end{funcdesc} |
| 384 | |
| 385 | \begin{funcdesc}{stat}{path} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 386 | Perform a \cfunction{stat()} system call on the given path. The |
| 387 | return value is a tuple of at least 10 integers giving the most |
| 388 | important (and portable) members of the \emph{stat} structure, in the |
| 389 | order |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 390 | \code{st_mode}, |
| 391 | \code{st_ino}, |
| 392 | \code{st_dev}, |
| 393 | \code{st_nlink}, |
| 394 | \code{st_uid}, |
| 395 | \code{st_gid}, |
| 396 | \code{st_size}, |
| 397 | \code{st_atime}, |
| 398 | \code{st_mtime}, |
| 399 | \code{st_ctime}. |
| 400 | More items may be added at the end by some implementations. |
| 401 | (On MS-DOS, some items are filled with dummy values.) |
| 402 | |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 403 | Note: The standard module \module{stat}\refstmodindex{stat} defines |
| 404 | functions and constants that are useful for extracting information |
| 405 | from a stat structure. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 406 | \end{funcdesc} |
| 407 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 408 | \begin{funcdesc}{symlink}{src, dst} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 409 | Create a symbolic link pointing to \var{src} named \var{dst}. (On |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 410 | systems without symbolic links, this always raises \exception{error}.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 411 | \end{funcdesc} |
| 412 | |
| 413 | \begin{funcdesc}{system}{command} |
| 414 | Execute the command (a string) in a subshell. This is implemented by |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 415 | calling the Standard \C{} function \cfunction{system()}, and has the |
| 416 | same limitations. Changes to \code{posix.environ}, \code{sys.stdin} |
| 417 | etc.\ are not reflected in the environment of the executed command. |
| 418 | The return value is the exit status of the process encoded in the |
| 419 | format specified for \function{wait()}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 420 | \end{funcdesc} |
| 421 | |
Guido van Rossum | 1e8b63e | 1996-06-26 19:22:46 +0000 | [diff] [blame] | 422 | \begin{funcdesc}{tcgetpgrp}{fd} |
| 423 | Return the process group associated with the terminal given by |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 424 | \var{fd} (an open file descriptor as returned by \function{open()}). |
Guido van Rossum | 1e8b63e | 1996-06-26 19:22:46 +0000 | [diff] [blame] | 425 | (Not on MS-DOS.) |
| 426 | \end{funcdesc} |
| 427 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 428 | \begin{funcdesc}{tcsetpgrp}{fd, pg} |
Guido van Rossum | 1e8b63e | 1996-06-26 19:22:46 +0000 | [diff] [blame] | 429 | Set the process group associated with the terminal given by |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 430 | \var{fd} (an open file descriptor as returned by \function{open()}) |
Guido van Rossum | 1e8b63e | 1996-06-26 19:22:46 +0000 | [diff] [blame] | 431 | to \var{pg}. |
| 432 | (Not on MS-DOS.) |
| 433 | \end{funcdesc} |
| 434 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 435 | \begin{funcdesc}{times}{} |
Guido van Rossum | 1e15061 | 1995-09-13 17:36:35 +0000 | [diff] [blame] | 436 | Return a 5-tuple of floating point numbers indicating accumulated (CPU |
| 437 | or other) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 438 | times, in seconds. The items are: user time, system time, children's |
Guido van Rossum | 1e15061 | 1995-09-13 17:36:35 +0000 | [diff] [blame] | 439 | user time, children's system time, and elapsed real time since a fixed |
| 440 | point in the past, in that order. See the \UNIX{} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 441 | manual page \manpage{times}{2}. (Not on MS-DOS.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 442 | \end{funcdesc} |
| 443 | |
| 444 | \begin{funcdesc}{umask}{mask} |
| 445 | Set the current numeric umask and returns the previous umask. |
| 446 | (Not on MS-DOS.) |
| 447 | \end{funcdesc} |
| 448 | |
| 449 | \begin{funcdesc}{uname}{} |
| 450 | Return a 5-tuple containing information identifying the current |
| 451 | operating system. The tuple contains 5 strings: |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 452 | \code{(\var{sysname}, \var{nodename}, \var{release}, \var{version}, |
| 453 | \var{machine})}. Some systems truncate the nodename to 8 |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 454 | characters or to the leading component; a better way to get the |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 455 | hostname is \function{socket.gethostname()}% |
Fred Drake | 371ecc0 | 1998-03-12 06:44:58 +0000 | [diff] [blame] | 456 | \withsubitem{(in module socket)}{\ttindex{gethostname()}} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 457 | or even |
| 458 | \code{socket.gethostbyaddr(socket.gethostname())}% |
Fred Drake | 371ecc0 | 1998-03-12 06:44:58 +0000 | [diff] [blame] | 459 | \withsubitem{(in module socket)}{\ttindex{gethostbyaddr()}}. |
Guido van Rossum | eb0f066 | 1997-12-30 20:38:16 +0000 | [diff] [blame] | 460 | (Not on MS-DOS, nor on older \UNIX{} systems.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 461 | \end{funcdesc} |
| 462 | |
| 463 | \begin{funcdesc}{unlink}{path} |
Guido van Rossum | 8c07bb4 | 1996-02-12 23:16:08 +0000 | [diff] [blame] | 464 | Remove the file \var{path}. This is the same function as \code{remove}; |
| 465 | the \code{unlink} name is its traditional \UNIX{} name. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 466 | \end{funcdesc} |
| 467 | |
Fred Drake | caa3379 | 1998-11-30 21:53:47 +0000 | [diff] [blame] | 468 | \begin{funcdesc}{utime}{path, (atime, mtime)} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 469 | Set the access and modified time of the file to the given values. |
| 470 | (The second argument is a tuple of two items.) |
| 471 | \end{funcdesc} |
| 472 | |
| 473 | \begin{funcdesc}{wait}{} |
| 474 | Wait for completion of a child process, and return a tuple containing |
Guido van Rossum | 7e691de | 1997-05-09 02:22:59 +0000 | [diff] [blame] | 475 | its pid and exit status indication: a 16-bit number, whose low byte is |
| 476 | the signal number that killed the process, and whose high byte is the |
| 477 | exit status (if the signal number is zero); the high bit of the low |
| 478 | byte is set if a core file was produced. (Not on MS-DOS.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 479 | \end{funcdesc} |
| 480 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 481 | \begin{funcdesc}{waitpid}{pid, options} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 482 | Wait for completion of a child process given by proces id, and return |
Guido van Rossum | 7e691de | 1997-05-09 02:22:59 +0000 | [diff] [blame] | 483 | a tuple containing its pid and exit status indication (encoded as for |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 484 | \function{wait()}). The semantics of the call are affected by the |
| 485 | value of the integer \var{options}, which should be \code{0} for |
| 486 | normal operation. (If the system does not support |
| 487 | \function{waitpid()}, this always raises \exception{error}. Not on |
| 488 | MS-DOS.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 489 | \end{funcdesc} |
| 490 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 491 | \begin{funcdesc}{write}{fd, str} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 492 | Write the string \var{str} to file descriptor \var{fd}. |
| 493 | Return the number of bytes actually written. |
Guido van Rossum | 2837970 | 1995-01-12 12:38:22 +0000 | [diff] [blame] | 494 | |
| 495 | Note: this function is intended for low-level I/O and must be applied |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 496 | to a file descriptor as returned by \function{open()} or |
| 497 | \function{pipe()}. To write a ``file object'' returned by the |
| 498 | built-in function \function{open()} or by \function{popen()} or |
| 499 | \function{fdopen()}, or \code{sys.stdout} or \code{sys.stderr}, use |
| 500 | its \method{write()} method. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 501 | \end{funcdesc} |
Guido van Rossum | 4bbe9c0 | 1995-03-30 16:00:36 +0000 | [diff] [blame] | 502 | |
| 503 | \begin{datadesc}{WNOHANG} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 504 | The option for \function{waitpid()} to avoid hanging if no child |
| 505 | process status is available immediately. |
Guido van Rossum | 4bbe9c0 | 1995-03-30 16:00:36 +0000 | [diff] [blame] | 506 | \end{datadesc} |
Barry Warsaw | e5a43a4 | 1996-12-19 23:50:34 +0000 | [diff] [blame] | 507 | |
| 508 | |
| 509 | \begin{datadesc}{O_RDONLY} |
Fred Drake | 86b5dce | 1998-02-13 21:55:21 +0000 | [diff] [blame] | 510 | \dataline{O_WRONLY} |
| 511 | \dataline{O_RDWR} |
| 512 | \dataline{O_NDELAY} |
| 513 | \dataline{O_NONBLOCK} |
| 514 | \dataline{O_APPEND} |
| 515 | \dataline{O_DSYNC} |
| 516 | \dataline{O_RSYNC} |
| 517 | \dataline{O_SYNC} |
| 518 | \dataline{O_NOCTTY} |
| 519 | \dataline{O_CREAT} |
| 520 | \dataline{O_EXCL} |
| 521 | \dataline{O_TRUNC} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 522 | Options for the \code{flag} argument to the \function{open()} function. |
Barry Warsaw | e5a43a4 | 1996-12-19 23:50:34 +0000 | [diff] [blame] | 523 | These can be bit-wise OR'd together. |
| 524 | \end{datadesc} |