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