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