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}{} |
| 183 | Return the current process group id. |
| 184 | (Not on MS-DOS.) |
| 185 | \end{funcdesc} |
| 186 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 187 | \begin{funcdesc}{getpid}{} |
| 188 | Return the current process id. |
| 189 | (Not on MS-DOS.) |
| 190 | \end{funcdesc} |
| 191 | |
| 192 | \begin{funcdesc}{getppid}{} |
| 193 | Return the parent's process id. |
| 194 | (Not on MS-DOS.) |
| 195 | \end{funcdesc} |
| 196 | |
| 197 | \begin{funcdesc}{getuid}{} |
Guido van Rossum | eb0f066 | 1997-12-30 20:38:16 +0000 | [diff] [blame] | 198 | Return the current process' user id. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 199 | (Not on MS-DOS.) |
| 200 | \end{funcdesc} |
| 201 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 202 | \begin{funcdesc}{kill}{pid, sig} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 203 | Kill the process \var{pid} with signal \var{sig}. |
| 204 | (Not on MS-DOS.) |
| 205 | \end{funcdesc} |
| 206 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 207 | \begin{funcdesc}{link}{src, dst} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 208 | Create a hard link pointing to \var{src} named \var{dst}. |
| 209 | (Not on MS-DOS.) |
| 210 | \end{funcdesc} |
| 211 | |
| 212 | \begin{funcdesc}{listdir}{path} |
| 213 | 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] | 214 | The list is in arbitrary order. It does not include the special |
| 215 | entries \code{'.'} and \code{'..'} even if they are present in the |
| 216 | directory. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 217 | \end{funcdesc} |
| 218 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 219 | \begin{funcdesc}{lseek}{fd, pos, how} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 220 | Set the current position of file descriptor \var{fd} to position |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 221 | \var{pos}, modified by \var{how}: \code{0} to set the position |
| 222 | relative to the beginning of the file; \code{1} to set it relative to |
| 223 | the current position; \code{2} to set it relative to the end of the |
| 224 | file. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 225 | \end{funcdesc} |
| 226 | |
| 227 | \begin{funcdesc}{lstat}{path} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 228 | Like \function{stat()}, but do not follow symbolic links. (On systems |
| 229 | without symbolic links, this is identical to \function{stat()}.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 230 | \end{funcdesc} |
| 231 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 232 | \begin{funcdesc}{mkfifo}{path\optional{, mode}} |
Fred Drake | 65b32f7 | 1998-02-09 20:27:12 +0000 | [diff] [blame] | 233 | 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] | 234 | \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] | 235 | umask value is first masked out from the mode. |
| 236 | (Not on MS-DOS.) |
| 237 | |
| 238 | FIFOs are pipes that can be accessed like regular files. FIFOs exist |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 239 | until they are deleted (for example with \function{os.unlink()}). |
| 240 | Generally, FIFOs are used as rendezvous between ``client'' and |
Guido van Rossum | 1e8b63e | 1996-06-26 19:22:46 +0000 | [diff] [blame] | 241 | ``server'' type processes: the server opens the FIFO for reading, and |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 242 | the client opens it for writing. Note that \function{mkfifo()} |
| 243 | doesn't open the FIFO --- it just creates the rendezvous point. |
Guido van Rossum | 1e8b63e | 1996-06-26 19:22:46 +0000 | [diff] [blame] | 244 | \end{funcdesc} |
| 245 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 246 | \begin{funcdesc}{mkdir}{path\optional{, mode}} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 247 | Create a directory named \var{path} with numeric mode \var{mode}. |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 248 | The default \var{mode} is \code{0777} (octal). On some systems, |
| 249 | \var{mode} is ignored. Where it is used, the current umask value is |
| 250 | first masked out. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 251 | \end{funcdesc} |
| 252 | |
| 253 | \begin{funcdesc}{nice}{increment} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 254 | Add \var{increment} to the process' ``niceness''. Return the new |
| 255 | niceness. (Not on MS-DOS.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 256 | \end{funcdesc} |
| 257 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 258 | \begin{funcdesc}{open}{file, flags\optional{, mode}} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 259 | Open the file \var{file} and set various flags according to |
| 260 | \var{flags} and possibly its mode according to \var{mode}. |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 261 | The default \var{mode} is \code{0777} (octal), and the current umask |
| 262 | value is first masked out. Return the file descriptor for the newly |
| 263 | opened file. |
Guido van Rossum | 2837970 | 1995-01-12 12:38:22 +0000 | [diff] [blame] | 264 | |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 265 | For a description of the flag and mode values, see the \UNIX{} or \C{} |
| 266 | run-time documentation; flag constants (like \constant{O_RDONLY} and |
| 267 | \constant{O_WRONLY}) are defined in this module too (see below). |
Guido van Rossum | 9c43c59 | 1997-08-08 21:05:09 +0000 | [diff] [blame] | 268 | |
Guido van Rossum | 2837970 | 1995-01-12 12:38:22 +0000 | [diff] [blame] | 269 | 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] | 270 | use the built-in function \function{open()}, which returns a ``file |
| 271 | object'' with \method{read()} and \method{write()} methods (and many |
| 272 | more). |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 273 | \end{funcdesc} |
| 274 | |
| 275 | \begin{funcdesc}{pipe}{} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 276 | Create a pipe. Return a pair of file descriptors \code{(\var{r}, |
| 277 | \var{w})} usable for reading and writing, respectively. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 278 | (Not on MS-DOS.) |
| 279 | \end{funcdesc} |
| 280 | |
Guido van Rossum | 38e5088 | 1996-07-21 02:21:49 +0000 | [diff] [blame] | 281 | \begin{funcdesc}{plock}{op} |
| 282 | Lock program segments into memory. The value of \var{op} |
| 283 | (defined in \code{<sys/lock.h>}) determines which segments are locked. |
| 284 | (Not on MS-DOS.) |
| 285 | \end{funcdesc} |
| 286 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 287 | \begin{funcdesc}{popen}{command\optional{, mode\optional{, bufsize}}} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 288 | Open a pipe to or from \var{command}. The return value is an open |
| 289 | 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] | 290 | depending on whether \var{mode} is \code{'r'} (default) or \code{'w'}. |
| 291 | The \var{bufsize} argument has the same meaning as the corresponding |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 292 | argument to the built-in \function{open()} function. The exit status of |
| 293 | the command (encoded in the format specified for \function{wait()}) is |
| 294 | 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] | 295 | object, except that when the exit status is zero (termination without |
| 296 | errors), \code{None} is returned. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 297 | (Not on MS-DOS.) |
| 298 | \end{funcdesc} |
| 299 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 300 | \begin{funcdesc}{putenv}{varname, value} |
Fred Drake | 52405c8 | 1998-03-16 05:21:08 +0000 | [diff] [blame] | 301 | \index{environment variables!setting} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 302 | Set the environment variable named \var{varname} to the string |
| 303 | \var{value}. Such changes to the environment affect subprocesses |
| 304 | started with \function{os.system()}, \function{os.popen()} or |
| 305 | \function{os.fork()} and \function{os.execv()}. (Not on all systems.) |
Guido van Rossum | f967bf6 | 1997-06-02 17:28:51 +0000 | [diff] [blame] | 306 | |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 307 | When \function{putenv()} is |
Guido van Rossum | f967bf6 | 1997-06-02 17:28:51 +0000 | [diff] [blame] | 308 | supported, assignments to items in \code{os.environ} are automatically |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 309 | translated into corresponding calls to \function{putenv()}; however, |
| 310 | 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] | 311 | actually preferable to assign to items of \code{os.environ}. |
| 312 | \end{funcdesc} |
| 313 | |
Guido van Rossum | 0bfd146 | 1997-10-05 18:54:52 +0000 | [diff] [blame] | 314 | \begin{funcdesc}{strerror}{code} |
| 315 | Return the error message corresponding to the error code in \var{code}. |
| 316 | \end{funcdesc} |
| 317 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 318 | \begin{funcdesc}{read}{fd, n} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 319 | Read at most \var{n} bytes from file descriptor \var{fd}. |
| 320 | Return a string containing the bytes read. |
Guido van Rossum | 2837970 | 1995-01-12 12:38:22 +0000 | [diff] [blame] | 321 | |
| 322 | 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] | 323 | to a file descriptor as returned by \function{open()} or |
| 324 | \function{pipe()}. To read a ``file object'' returned by the |
| 325 | built-in function \function{open()} or by \function{popen()} or |
| 326 | \function{fdopen()}, or \code{sys.stdin}, use its |
| 327 | \method{read()} or \method{readline()} methods. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 328 | \end{funcdesc} |
| 329 | |
| 330 | \begin{funcdesc}{readlink}{path} |
| 331 | Return a string representing the path to which the symbolic link |
| 332 | points. (On systems without symbolic links, this always raises |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 333 | \exception{error}.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 334 | \end{funcdesc} |
| 335 | |
Guido van Rossum | 8c07bb4 | 1996-02-12 23:16:08 +0000 | [diff] [blame] | 336 | \begin{funcdesc}{remove}{path} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 337 | Remove the file \var{path}. See \function{rmdir()} below to remove a |
| 338 | directory. This is identical to the \function{unlink()} function |
| 339 | documented below. |
Guido van Rossum | 8c07bb4 | 1996-02-12 23:16:08 +0000 | [diff] [blame] | 340 | \end{funcdesc} |
| 341 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 342 | \begin{funcdesc}{rename}{src, dst} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 343 | Rename the file or directory \var{src} to \var{dst}. |
| 344 | \end{funcdesc} |
| 345 | |
| 346 | \begin{funcdesc}{rmdir}{path} |
| 347 | Remove the directory \var{path}. |
| 348 | \end{funcdesc} |
| 349 | |
| 350 | \begin{funcdesc}{setgid}{gid} |
Guido van Rossum | eb0f066 | 1997-12-30 20:38:16 +0000 | [diff] [blame] | 351 | Set the current process' group id. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 352 | (Not on MS-DOS.) |
| 353 | \end{funcdesc} |
| 354 | |
Guido van Rossum | 1e8b63e | 1996-06-26 19:22:46 +0000 | [diff] [blame] | 355 | \begin{funcdesc}{setpgrp}{} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 356 | Calls the system call \cfunction{setpgrp()} or \cfunction{setpgrp(0, |
| 357 | 0)} depending on which version is implemented (if any). See the |
| 358 | \UNIX{} manual for the semantics. |
Guido van Rossum | 1e8b63e | 1996-06-26 19:22:46 +0000 | [diff] [blame] | 359 | (Not on MS-DOS.) |
| 360 | \end{funcdesc} |
| 361 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 362 | \begin{funcdesc}{setpgid}{pid, pgrp} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 363 | Calls the system call \cfunction{setpgid()}. See the \UNIX{} manual |
| 364 | 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 | |
| 368 | \begin{funcdesc}{setsid}{} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 369 | Calls the system call \cfunction{setsid()}. 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 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 374 | \begin{funcdesc}{setuid}{uid} |
Guido van Rossum | eb0f066 | 1997-12-30 20:38:16 +0000 | [diff] [blame] | 375 | Set the current process' user id. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 376 | (Not on MS-DOS.) |
| 377 | \end{funcdesc} |
| 378 | |
| 379 | \begin{funcdesc}{stat}{path} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 380 | Perform a \cfunction{stat()} system call on the given path. The |
| 381 | return value is a tuple of at least 10 integers giving the most |
| 382 | important (and portable) members of the \emph{stat} structure, in the |
| 383 | order |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 384 | \code{st_mode}, |
| 385 | \code{st_ino}, |
| 386 | \code{st_dev}, |
| 387 | \code{st_nlink}, |
| 388 | \code{st_uid}, |
| 389 | \code{st_gid}, |
| 390 | \code{st_size}, |
| 391 | \code{st_atime}, |
| 392 | \code{st_mtime}, |
| 393 | \code{st_ctime}. |
| 394 | More items may be added at the end by some implementations. |
| 395 | (On MS-DOS, some items are filled with dummy values.) |
| 396 | |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 397 | Note: The standard module \module{stat}\refstmodindex{stat} defines |
| 398 | functions and constants that are useful for extracting information |
| 399 | from a stat structure. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 400 | \end{funcdesc} |
| 401 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 402 | \begin{funcdesc}{symlink}{src, dst} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 403 | Create a symbolic link pointing to \var{src} named \var{dst}. (On |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 404 | systems without symbolic links, this always raises \exception{error}.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 405 | \end{funcdesc} |
| 406 | |
| 407 | \begin{funcdesc}{system}{command} |
| 408 | Execute the command (a string) in a subshell. This is implemented by |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 409 | calling the Standard \C{} function \cfunction{system()}, and has the |
| 410 | same limitations. Changes to \code{posix.environ}, \code{sys.stdin} |
| 411 | etc.\ are not reflected in the environment of the executed command. |
| 412 | The return value is the exit status of the process encoded in the |
| 413 | format specified for \function{wait()}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 414 | \end{funcdesc} |
| 415 | |
Guido van Rossum | 1e8b63e | 1996-06-26 19:22:46 +0000 | [diff] [blame] | 416 | \begin{funcdesc}{tcgetpgrp}{fd} |
| 417 | Return the process group associated with the terminal given by |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 418 | \var{fd} (an open file descriptor as returned by \function{open()}). |
Guido van Rossum | 1e8b63e | 1996-06-26 19:22:46 +0000 | [diff] [blame] | 419 | (Not on MS-DOS.) |
| 420 | \end{funcdesc} |
| 421 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 422 | \begin{funcdesc}{tcsetpgrp}{fd, pg} |
Guido van Rossum | 1e8b63e | 1996-06-26 19:22:46 +0000 | [diff] [blame] | 423 | Set 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 | to \var{pg}. |
| 426 | (Not on MS-DOS.) |
| 427 | \end{funcdesc} |
| 428 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 429 | \begin{funcdesc}{times}{} |
Guido van Rossum | 1e15061 | 1995-09-13 17:36:35 +0000 | [diff] [blame] | 430 | Return a 5-tuple of floating point numbers indicating accumulated (CPU |
| 431 | or other) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 432 | 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] | 433 | user time, children's system time, and elapsed real time since a fixed |
| 434 | point in the past, in that order. See the \UNIX{} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 435 | manual page \manpage{times}{2}. (Not on MS-DOS.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 436 | \end{funcdesc} |
| 437 | |
| 438 | \begin{funcdesc}{umask}{mask} |
| 439 | Set the current numeric umask and returns the previous umask. |
| 440 | (Not on MS-DOS.) |
| 441 | \end{funcdesc} |
| 442 | |
| 443 | \begin{funcdesc}{uname}{} |
| 444 | Return a 5-tuple containing information identifying the current |
| 445 | operating system. The tuple contains 5 strings: |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 446 | \code{(\var{sysname}, \var{nodename}, \var{release}, \var{version}, |
| 447 | \var{machine})}. Some systems truncate the nodename to 8 |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 448 | characters or to the leading component; a better way to get the |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 449 | hostname is \function{socket.gethostname()}% |
Fred Drake | 371ecc0 | 1998-03-12 06:44:58 +0000 | [diff] [blame] | 450 | \withsubitem{(in module socket)}{\ttindex{gethostname()}} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 451 | or even |
| 452 | \code{socket.gethostbyaddr(socket.gethostname())}% |
Fred Drake | 371ecc0 | 1998-03-12 06:44:58 +0000 | [diff] [blame] | 453 | \withsubitem{(in module socket)}{\ttindex{gethostbyaddr()}}. |
Guido van Rossum | eb0f066 | 1997-12-30 20:38:16 +0000 | [diff] [blame] | 454 | (Not on MS-DOS, nor on older \UNIX{} systems.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 455 | \end{funcdesc} |
| 456 | |
| 457 | \begin{funcdesc}{unlink}{path} |
Guido van Rossum | 8c07bb4 | 1996-02-12 23:16:08 +0000 | [diff] [blame] | 458 | Remove the file \var{path}. This is the same function as \code{remove}; |
| 459 | the \code{unlink} name is its traditional \UNIX{} name. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 460 | \end{funcdesc} |
| 461 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 462 | \begin{funcdesc}{utime}{path, {\rm (}atime, mtime{\rm )}} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 463 | Set the access and modified time of the file to the given values. |
| 464 | (The second argument is a tuple of two items.) |
| 465 | \end{funcdesc} |
| 466 | |
| 467 | \begin{funcdesc}{wait}{} |
| 468 | 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] | 469 | its pid and exit status indication: a 16-bit number, whose low byte is |
| 470 | the signal number that killed the process, and whose high byte is the |
| 471 | exit status (if the signal number is zero); the high bit of the low |
| 472 | 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] | 473 | \end{funcdesc} |
| 474 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 475 | \begin{funcdesc}{waitpid}{pid, options} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 476 | 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] | 477 | a tuple containing its pid and exit status indication (encoded as for |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 478 | \function{wait()}). The semantics of the call are affected by the |
| 479 | value of the integer \var{options}, which should be \code{0} for |
| 480 | normal operation. (If the system does not support |
| 481 | \function{waitpid()}, this always raises \exception{error}. Not on |
| 482 | MS-DOS.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 483 | \end{funcdesc} |
| 484 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 485 | \begin{funcdesc}{write}{fd, str} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 486 | Write the string \var{str} to file descriptor \var{fd}. |
| 487 | Return the number of bytes actually written. |
Guido van Rossum | 2837970 | 1995-01-12 12:38:22 +0000 | [diff] [blame] | 488 | |
| 489 | 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] | 490 | to a file descriptor as returned by \function{open()} or |
| 491 | \function{pipe()}. To write a ``file object'' returned by the |
| 492 | built-in function \function{open()} or by \function{popen()} or |
| 493 | \function{fdopen()}, or \code{sys.stdout} or \code{sys.stderr}, use |
| 494 | its \method{write()} method. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 495 | \end{funcdesc} |
Guido van Rossum | 4bbe9c0 | 1995-03-30 16:00:36 +0000 | [diff] [blame] | 496 | |
| 497 | \begin{datadesc}{WNOHANG} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 498 | The option for \function{waitpid()} to avoid hanging if no child |
| 499 | process status is available immediately. |
Guido van Rossum | 4bbe9c0 | 1995-03-30 16:00:36 +0000 | [diff] [blame] | 500 | \end{datadesc} |
Barry Warsaw | e5a43a4 | 1996-12-19 23:50:34 +0000 | [diff] [blame] | 501 | |
| 502 | |
| 503 | \begin{datadesc}{O_RDONLY} |
Fred Drake | 86b5dce | 1998-02-13 21:55:21 +0000 | [diff] [blame] | 504 | \dataline{O_WRONLY} |
| 505 | \dataline{O_RDWR} |
| 506 | \dataline{O_NDELAY} |
| 507 | \dataline{O_NONBLOCK} |
| 508 | \dataline{O_APPEND} |
| 509 | \dataline{O_DSYNC} |
| 510 | \dataline{O_RSYNC} |
| 511 | \dataline{O_SYNC} |
| 512 | \dataline{O_NOCTTY} |
| 513 | \dataline{O_CREAT} |
| 514 | \dataline{O_EXCL} |
| 515 | \dataline{O_TRUNC} |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 516 | Options for the \code{flag} argument to the \function{open()} function. |
Barry Warsaw | e5a43a4 | 1996-12-19 23:50:34 +0000 | [diff] [blame] | 517 | These can be bit-wise OR'd together. |
| 518 | \end{datadesc} |