Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1 | \section{Built-in Module \sectcode{posix}} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 2 | \bimodindex{posix} |
| 3 | |
| 4 | This module provides access to operating system functionality that is |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 5 | standardized by the C Standard and the POSIX standard (a thinly disguised |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 6 | \UNIX{} interface). |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 7 | |
| 8 | \strong{Do not import this module directly.} Instead, import the |
| 9 | module \code{os}, which provides a \emph{portable} version of this |
| 10 | interface. On \UNIX{}, the \code{os} module provides a superset of |
| 11 | the \code{posix} interface. On non-\UNIX{} operating systems the |
| 12 | \code{posix} module is not available, but a subset is always available |
| 13 | through 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 Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 18 | The descriptions below are very terse; refer to the |
| 19 | corresponding \UNIX{} manual entry for more information. |
| 20 | |
| 21 | Errors are reported as exceptions; the usual exceptions are given |
| 22 | for type errors, while errors reported by the system calls raise |
| 23 | \code{posix.error}, described below. |
| 24 | |
| 25 | Module \code{posix} defines the following data items: |
| 26 | |
| 27 | \renewcommand{\indexsubitem}{(data in module posix)} |
| 28 | \begin{datadesc}{environ} |
| 29 | A dictionary representing the string environment at the time |
| 30 | the interpreter was started. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 31 | For example, |
| 32 | \code{posix.environ['HOME']} |
| 33 | is the pathname of your home directory, equivalent to |
| 34 | \code{getenv("HOME")} |
| 35 | in C. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 36 | Modifying this dictionary does not affect the string environment |
| 37 | passed on by \code{execv()}, \code{popen()} or \code{system()}; if you |
| 38 | need to change the environment, pass \code{environ} to \code{execve()} |
| 39 | or add variable assignments and export statements to the command |
| 40 | string for \code{system()} or \code{popen()}.% |
| 41 | \footnote{The problem with automatically passing on \code{environ} is |
| 42 | that there is no portable way of changing the environment.} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 43 | \end{datadesc} |
| 44 | |
| 45 | \renewcommand{\indexsubitem}{(exception in module posix)} |
| 46 | \begin{excdesc}{error} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 47 | This exception is raised when a POSIX function returns a |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 48 | POSIX-related error (e.g., not for illegal argument types). Its |
| 49 | string value is \code{'posix.error'}. The accompanying value is a |
| 50 | pair containing the numeric error code from \code{errno} and the |
| 51 | corresponding string, as would be printed by the C function |
| 52 | \code{perror()}. |
| 53 | \end{excdesc} |
| 54 | |
Guido van Rossum | 4bbe9c0 | 1995-03-30 16:00:36 +0000 | [diff] [blame^] | 55 | It defines the following functions and constants: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 56 | |
| 57 | \renewcommand{\indexsubitem}{(in module posix)} |
| 58 | \begin{funcdesc}{chdir}{path} |
| 59 | Change the current working directory to \var{path}. |
| 60 | \end{funcdesc} |
| 61 | |
| 62 | \begin{funcdesc}{chmod}{path\, mode} |
| 63 | Change the mode of \var{path} to the numeric \var{mode}. |
| 64 | \end{funcdesc} |
| 65 | |
Guido van Rossum | 31cce97 | 1995-01-04 19:17:34 +0000 | [diff] [blame] | 66 | \begin{funcdesc}{chown}{path\, uid, gid} |
| 67 | Change the owner and group id of \var{path} to the numeric \var{uid} |
| 68 | and \var{gid}. |
| 69 | (Not on MS-DOS.) |
| 70 | \end{funcdesc} |
| 71 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 72 | \begin{funcdesc}{close}{fd} |
| 73 | Close file descriptor \var{fd}. |
Guido van Rossum | 2837970 | 1995-01-12 12:38:22 +0000 | [diff] [blame] | 74 | |
| 75 | Note: this function is intended for low-level I/O and must be applied |
| 76 | to a file descriptor as returned by \code{posix.open()} or |
| 77 | \code{posix.pipe()}. To close a ``file object'' returned by the |
| 78 | built-in function \code{open} or by \code{posix.popen} or |
| 79 | \code{posix.fdopen}, use its \code{close()} method. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 80 | \end{funcdesc} |
| 81 | |
| 82 | \begin{funcdesc}{dup}{fd} |
| 83 | Return a duplicate of file descriptor \var{fd}. |
| 84 | \end{funcdesc} |
| 85 | |
| 86 | \begin{funcdesc}{dup2}{fd\, fd2} |
| 87 | Duplicate file descriptor \var{fd} to \var{fd2}, closing the latter |
| 88 | first if necessary. Return \code{None}. |
| 89 | \end{funcdesc} |
| 90 | |
| 91 | \begin{funcdesc}{execv}{path\, args} |
| 92 | Execute the executable \var{path} with argument list \var{args}, |
| 93 | replacing the current process (i.e., the Python interpreter). |
| 94 | The argument list may be a tuple or list of strings. |
| 95 | (Not on MS-DOS.) |
| 96 | \end{funcdesc} |
| 97 | |
| 98 | \begin{funcdesc}{execve}{path\, args\, env} |
| 99 | Execute the executable \var{path} with argument list \var{args}, |
| 100 | and environment \var{env}, |
| 101 | replacing the current process (i.e., the Python interpreter). |
| 102 | The argument list may be a tuple or list of strings. |
| 103 | The environment must be a dictionary mapping strings to strings. |
| 104 | (Not on MS-DOS.) |
| 105 | \end{funcdesc} |
| 106 | |
| 107 | \begin{funcdesc}{_exit}{n} |
| 108 | Exit to the system with status \var{n}, without calling cleanup |
| 109 | handlers, flushing stdio buffers, etc. |
| 110 | (Not on MS-DOS.) |
| 111 | |
| 112 | Note: the standard way to exit is \code{sys.exit(\var{n})}. |
| 113 | \code{posix._exit()} should normally only be used in the child process |
| 114 | after a \code{fork()}. |
| 115 | \end{funcdesc} |
| 116 | |
Guido van Rossum | 2837970 | 1995-01-12 12:38:22 +0000 | [diff] [blame] | 117 | \begin{funcdesc}{fdopen}{fd\optional{\, mode\optional{\, bufsize}}} |
| 118 | Return an open file object connected to the file descriptor \var{fd}. |
| 119 | The \var{mode} and \var{bufsize} arguments have the same meaning as |
| 120 | the corresponding arguments to the built-in \code{open()} function. |
Guido van Rossum | c5c67bc | 1994-02-15 15:59:23 +0000 | [diff] [blame] | 121 | \end{funcdesc} |
| 122 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 123 | \begin{funcdesc}{fork}{} |
| 124 | Fork a child process. Return 0 in the child, the child's process id |
| 125 | in the parent. |
| 126 | (Not on MS-DOS.) |
| 127 | \end{funcdesc} |
| 128 | |
| 129 | \begin{funcdesc}{fstat}{fd} |
| 130 | Return status for file descriptor \var{fd}, like \code{stat()}. |
| 131 | \end{funcdesc} |
| 132 | |
| 133 | \begin{funcdesc}{getcwd}{} |
| 134 | Return a string representing the current working directory. |
| 135 | \end{funcdesc} |
| 136 | |
| 137 | \begin{funcdesc}{getegid}{} |
| 138 | Return the current process's effective group id. |
| 139 | (Not on MS-DOS.) |
| 140 | \end{funcdesc} |
| 141 | |
| 142 | \begin{funcdesc}{geteuid}{} |
| 143 | Return the current process's effective user id. |
| 144 | (Not on MS-DOS.) |
| 145 | \end{funcdesc} |
| 146 | |
| 147 | \begin{funcdesc}{getgid}{} |
| 148 | Return the current process's group id. |
| 149 | (Not on MS-DOS.) |
| 150 | \end{funcdesc} |
| 151 | |
| 152 | \begin{funcdesc}{getpid}{} |
| 153 | Return the current process id. |
| 154 | (Not on MS-DOS.) |
| 155 | \end{funcdesc} |
| 156 | |
| 157 | \begin{funcdesc}{getppid}{} |
| 158 | Return the parent's process id. |
| 159 | (Not on MS-DOS.) |
| 160 | \end{funcdesc} |
| 161 | |
| 162 | \begin{funcdesc}{getuid}{} |
| 163 | Return the current process's user id. |
| 164 | (Not on MS-DOS.) |
| 165 | \end{funcdesc} |
| 166 | |
| 167 | \begin{funcdesc}{kill}{pid\, sig} |
| 168 | Kill the process \var{pid} with signal \var{sig}. |
| 169 | (Not on MS-DOS.) |
| 170 | \end{funcdesc} |
| 171 | |
| 172 | \begin{funcdesc}{link}{src\, dst} |
| 173 | Create a hard link pointing to \var{src} named \var{dst}. |
| 174 | (Not on MS-DOS.) |
| 175 | \end{funcdesc} |
| 176 | |
| 177 | \begin{funcdesc}{listdir}{path} |
| 178 | Return a list containing the names of the entries in the directory. |
| 179 | The list is in arbitrary order. It includes the special entries |
| 180 | \code{'.'} and \code{'..'} if they are present in the directory. |
| 181 | \end{funcdesc} |
| 182 | |
| 183 | \begin{funcdesc}{lseek}{fd\, pos\, how} |
| 184 | Set the current position of file descriptor \var{fd} to position |
| 185 | \var{pos}, modified by \var{how}: 0 to set the position relative to |
| 186 | the beginning of the file; 1 to set it relative to the current |
| 187 | position; 2 to set it relative to the end of the file. |
| 188 | \end{funcdesc} |
| 189 | |
| 190 | \begin{funcdesc}{lstat}{path} |
| 191 | Like \code{stat()}, but do not follow symbolic links. (On systems |
| 192 | without symbolic links, this is identical to \code{posix.stat}.) |
| 193 | \end{funcdesc} |
| 194 | |
| 195 | \begin{funcdesc}{mkdir}{path\, mode} |
| 196 | Create a directory named \var{path} with numeric mode \var{mode}. |
| 197 | \end{funcdesc} |
| 198 | |
| 199 | \begin{funcdesc}{nice}{increment} |
| 200 | Add \var{incr} to the process' ``niceness''. Return the new niceness. |
| 201 | (Not on MS-DOS.) |
| 202 | \end{funcdesc} |
| 203 | |
| 204 | \begin{funcdesc}{open}{file\, flags\, mode} |
| 205 | Open the file \var{file} and set various flags according to |
| 206 | \var{flags} and possibly its mode according to \var{mode}. |
| 207 | Return the file descriptor for the newly opened file. |
Guido van Rossum | 2837970 | 1995-01-12 12:38:22 +0000 | [diff] [blame] | 208 | |
| 209 | Note: this function is intended for low-level I/O. For normal usage, |
| 210 | use the built-in function \code{open}, which returns a ``file object'' |
| 211 | with \code{read()} and \code{write()} methods (and many more). |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 212 | \end{funcdesc} |
| 213 | |
| 214 | \begin{funcdesc}{pipe}{} |
| 215 | Create a pipe. Return a pair of file descriptors \code{(r, w)} |
| 216 | usable for reading and writing, respectively. |
| 217 | (Not on MS-DOS.) |
| 218 | \end{funcdesc} |
| 219 | |
Guido van Rossum | 2837970 | 1995-01-12 12:38:22 +0000 | [diff] [blame] | 220 | \begin{funcdesc}{popen}{command\optional{\, mode\optional{\, bufsize}}} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 221 | Open a pipe to or from \var{command}. The return value is an open |
| 222 | 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] | 223 | depending on whether \var{mode} is \code{'r'} (default) or \code{'w'}. |
| 224 | The \var{bufsize} argument has the same meaning as the corresponding |
| 225 | argument to the built-in \code{open()} function. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 226 | (Not on MS-DOS.) |
| 227 | \end{funcdesc} |
| 228 | |
| 229 | \begin{funcdesc}{read}{fd\, n} |
| 230 | Read at most \var{n} bytes from file descriptor \var{fd}. |
| 231 | Return a string containing the bytes read. |
Guido van Rossum | 2837970 | 1995-01-12 12:38:22 +0000 | [diff] [blame] | 232 | |
| 233 | Note: this function is intended for low-level I/O and must be applied |
| 234 | to a file descriptor as returned by \code{posix.open()} or |
| 235 | \code{posix.pipe()}. To read a ``file object'' returned by the |
| 236 | built-in function \code{open} or by \code{posix.popen} or |
| 237 | \code{posix.fdopen}, or \code{sys.stdin}, use its |
| 238 | \code{read()} or \code{readline()} methods. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 239 | \end{funcdesc} |
| 240 | |
| 241 | \begin{funcdesc}{readlink}{path} |
| 242 | Return a string representing the path to which the symbolic link |
| 243 | points. (On systems without symbolic links, this always raises |
| 244 | \code{posix.error}.) |
| 245 | \end{funcdesc} |
| 246 | |
| 247 | \begin{funcdesc}{rename}{src\, dst} |
| 248 | Rename the file or directory \var{src} to \var{dst}. |
| 249 | \end{funcdesc} |
| 250 | |
| 251 | \begin{funcdesc}{rmdir}{path} |
| 252 | Remove the directory \var{path}. |
| 253 | \end{funcdesc} |
| 254 | |
| 255 | \begin{funcdesc}{setgid}{gid} |
| 256 | Set the current process's group id. |
| 257 | (Not on MS-DOS.) |
| 258 | \end{funcdesc} |
| 259 | |
| 260 | \begin{funcdesc}{setuid}{uid} |
| 261 | Set the current process's user id. |
| 262 | (Not on MS-DOS.) |
| 263 | \end{funcdesc} |
| 264 | |
| 265 | \begin{funcdesc}{stat}{path} |
| 266 | Perform a {\em stat} system call on the given path. The return value |
| 267 | is a tuple of at least 10 integers giving the most important (and |
| 268 | portable) members of the {\em stat} structure, in the order |
| 269 | \code{st_mode}, |
| 270 | \code{st_ino}, |
| 271 | \code{st_dev}, |
| 272 | \code{st_nlink}, |
| 273 | \code{st_uid}, |
| 274 | \code{st_gid}, |
| 275 | \code{st_size}, |
| 276 | \code{st_atime}, |
| 277 | \code{st_mtime}, |
| 278 | \code{st_ctime}. |
| 279 | More items may be added at the end by some implementations. |
| 280 | (On MS-DOS, some items are filled with dummy values.) |
| 281 | |
| 282 | Note: The standard module \code{stat} defines functions and constants |
| 283 | that are useful for extracting information from a stat structure. |
| 284 | \end{funcdesc} |
| 285 | |
| 286 | \begin{funcdesc}{symlink}{src\, dst} |
| 287 | Create a symbolic link pointing to \var{src} named \var{dst}. (On |
| 288 | systems without symbolic links, this always raises |
| 289 | \code{posix.error}.) |
| 290 | \end{funcdesc} |
| 291 | |
| 292 | \begin{funcdesc}{system}{command} |
| 293 | Execute the command (a string) in a subshell. This is implemented by |
| 294 | calling the Standard C function \code{system()}, and has the same |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 295 | limitations. Changes to \code{posix.environ}, \code{sys.stdin} etc.\ are |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 296 | not reflected in the environment of the executed command. The return |
| 297 | value is the exit status of the process as returned by Standard C |
| 298 | \code{system()}. |
| 299 | \end{funcdesc} |
| 300 | |
| 301 | \begin{funcdesc}{times}{} |
| 302 | Return a 4-tuple of floating point numbers indicating accumulated CPU |
| 303 | times, in seconds. The items are: user time, system time, children's |
| 304 | user time, and children's system time, in that order. See the \UNIX{} |
| 305 | manual page {\it times}(2). (Not on MS-DOS.) |
| 306 | \end{funcdesc} |
| 307 | |
| 308 | \begin{funcdesc}{umask}{mask} |
| 309 | Set the current numeric umask and returns the previous umask. |
| 310 | (Not on MS-DOS.) |
| 311 | \end{funcdesc} |
| 312 | |
| 313 | \begin{funcdesc}{uname}{} |
| 314 | Return a 5-tuple containing information identifying the current |
| 315 | operating system. The tuple contains 5 strings: |
| 316 | \code{(\var{sysname}, \var{nodename}, \var{release}, \var{version}, \var{machine})}. |
| 317 | Some systems truncate the nodename to 8 |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 318 | characters or to the leading component; a better way to get the |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 319 | hostname is \code{socket.gethostname()}. (Not on MS-DOS, nor on older |
| 320 | \UNIX{} systems.) |
| 321 | \end{funcdesc} |
| 322 | |
| 323 | \begin{funcdesc}{unlink}{path} |
| 324 | Unlink \var{path}. |
| 325 | \end{funcdesc} |
| 326 | |
| 327 | \begin{funcdesc}{utime}{path\, \(atime\, mtime\)} |
| 328 | Set the access and modified time of the file to the given values. |
| 329 | (The second argument is a tuple of two items.) |
| 330 | \end{funcdesc} |
| 331 | |
| 332 | \begin{funcdesc}{wait}{} |
| 333 | Wait for completion of a child process, and return a tuple containing |
| 334 | its pid and exit status indication (encoded as by \UNIX{}). |
| 335 | (Not on MS-DOS.) |
| 336 | \end{funcdesc} |
| 337 | |
| 338 | \begin{funcdesc}{waitpid}{pid\, options} |
| 339 | Wait for completion of a child process given by proces id, and return |
| 340 | a tuple containing its pid and exit status indication (encoded as by |
| 341 | \UNIX{}). The semantics of the call are affected by the value of |
| 342 | the integer options, which should be 0 for normal operation. (If the |
| 343 | system does not support waitpid(), this always raises |
| 344 | \code{posix.error}. Not on MS-DOS.) |
| 345 | \end{funcdesc} |
| 346 | |
| 347 | \begin{funcdesc}{write}{fd\, str} |
| 348 | Write the string \var{str} to file descriptor \var{fd}. |
| 349 | Return the number of bytes actually written. |
Guido van Rossum | 2837970 | 1995-01-12 12:38:22 +0000 | [diff] [blame] | 350 | |
| 351 | Note: this function is intended for low-level I/O and must be applied |
| 352 | to a file descriptor as returned by \code{posix.open()} or |
| 353 | \code{posix.pipe()}. To write a ``file object'' returned by the |
| 354 | built-in function \code{open} or by \code{posix.popen} or |
| 355 | \code{posix.fdopen}, or \code{sys.stdout} or \code{sys.stderr}, use |
| 356 | its \code{write()} method. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 357 | \end{funcdesc} |
Guido van Rossum | 4bbe9c0 | 1995-03-30 16:00:36 +0000 | [diff] [blame^] | 358 | |
| 359 | \begin{datadesc}{WNOHANG} |
| 360 | The option for \code{waitpid()} to avoid hanging if no child process |
| 361 | status is available immediately. |
| 362 | \end{datadesc} |