Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{os} --- |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 2 | Miscellaneous OS interfaces} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | ec6baaf | 1999-04-21 18:13:31 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{os} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | \modulesynopsis{Miscellaneous OS interfaces.} |
| 6 | |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 8 | This module provides a more portable way of using operating system |
| 9 | (OS) dependent functionality than importing an OS dependent built-in |
Fred Drake | 2f97901 | 1999-06-11 18:28:37 +0000 | [diff] [blame] | 10 | module like \refmodule{posix} or \module{nt}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 11 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 12 | This module searches for an OS dependent built-in module like |
Fred Drake | 2f97901 | 1999-06-11 18:28:37 +0000 | [diff] [blame] | 13 | \module{mac} or \refmodule{posix} and exports the same functions and data |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 14 | as found there. The design of all Python's built-in OS dependent |
| 15 | modules is such that as long as the same functionality is available, |
| 16 | it uses the same interface; e.g., the function |
Fred Drake | ec6baaf | 1999-04-21 18:13:31 +0000 | [diff] [blame] | 17 | \code{os.stat(\var{path})} returns stat information about \var{path} in |
| 18 | the same format (which happens to have originated with the |
| 19 | \POSIX{} interface). |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 20 | |
| 21 | Extensions peculiar to a particular OS are also available through the |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 22 | \module{os} module, but using them is of course a threat to |
| 23 | portability! |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 24 | |
Fred Drake | c4f15af | 1998-03-10 03:17:26 +0000 | [diff] [blame] | 25 | Note that after the first time \module{os} is imported, there is |
| 26 | \emph{no} performance penalty in using functions from \module{os} |
| 27 | instead of directly from the OS dependent built-in module, so there |
| 28 | should be \emph{no} reason not to use \module{os}! |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 29 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 30 | |
| 31 | \begin{excdesc}{error} |
| 32 | This exception is raised when a function returns a |
| 33 | system-related error (e.g., not for illegal argument types). This is |
| 34 | also known as the built-in exception \exception{OSError}. The |
| 35 | accompanying value is a pair containing the numeric error code from |
| 36 | \cdata{errno} and the corresponding string, as would be printed by the |
| 37 | C function \cfunction{perror()}. See the module |
| 38 | \refmodule{errno}\refbimodindex{errno}, which contains names for the |
| 39 | error codes defined by the underlying operating system. |
| 40 | |
| 41 | When exceptions are classes, this exception carries two attributes, |
| 42 | \member{errno} and \member{strerror}. The first holds the value of |
| 43 | the C \cdata{errno} variable, and the latter holds the corresponding |
| 44 | error message from \cfunction{strerror()}. For exceptions that |
| 45 | involve a file system path (e.g. \function{chdir()} or |
| 46 | \function{unlink()}), the exception instance will contain a third |
| 47 | attribute, \member{filename}, which is the file name passed to the |
| 48 | function. |
| 49 | |
| 50 | When exceptions are strings, the string for the exception is |
| 51 | \code{'OSError'}. |
| 52 | \end{excdesc} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 54 | \begin{datadesc}{name} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 55 | The name of the OS dependent module imported. The following names |
| 56 | have currently been registered: \code{'posix'}, \code{'nt'}, |
Fred Drake | 2f97901 | 1999-06-11 18:28:37 +0000 | [diff] [blame] | 57 | \code{'dos'}, \code{'mac'}, \code{'os2'}, \code{'ce'}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 58 | \end{datadesc} |
| 59 | |
| 60 | \begin{datadesc}{path} |
| 61 | The corresponding OS dependent standard module for pathname |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 62 | operations, e.g., \module{posixpath} or \module{macpath}. Thus, given |
| 63 | the proper imports, \code{os.path.split(\var{file})} is equivalent to but |
| 64 | more portable than \code{posixpath.split(\var{file})}. Note that this |
| 65 | is also a valid module: it may be imported directly as |
| 66 | \refmodule{os.path}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 67 | \end{datadesc} |
| 68 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 69 | |
| 70 | |
| 71 | \subsection{Process Parameters \label{os-procinfo}} |
| 72 | |
| 73 | These functions and data items provide information and operate on the |
| 74 | current process and user. |
| 75 | |
| 76 | \begin{funcdesc}{chdir}{path} |
| 77 | Change the current working directory to \var{path}. |
| 78 | Availability: Macintosh, \UNIX{}, Windows. |
| 79 | \end{funcdesc} |
| 80 | |
| 81 | \begin{datadesc}{environ} |
Fred Drake | 0e1de8b | 1999-04-29 12:57:32 +0000 | [diff] [blame] | 82 | A mapping object representing the string environment. For example, |
| 83 | \code{environ['HOME']} is the pathname of your home directory (on some |
| 84 | platforms), and is equivalent to \code{getenv("HOME")} in C. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 85 | |
| 86 | If the platform supports the \function{putenv()} function, this |
| 87 | mapping may be used to modify the environment as well as query the |
| 88 | environment. \function{putenv()} will be called automatically when |
| 89 | the mapping is modified. |
| 90 | |
| 91 | If \function{putenv()} is not provided, this mapping may be passed to |
| 92 | the appropriate process-creation functions to cause child processes to |
| 93 | use a modified environment. |
| 94 | \end{datadesc} |
| 95 | |
| 96 | \begin{funcdesc}{getcwd}{} |
| 97 | Return a string representing the current working directory. |
| 98 | Availability: Macintosh, \UNIX{}, Windows. |
| 99 | \end{funcdesc} |
| 100 | |
| 101 | \begin{funcdesc}{getegid}{} |
| 102 | Return the current process' effective group id. |
| 103 | Availability: \UNIX{}. |
| 104 | \end{funcdesc} |
| 105 | |
| 106 | \begin{funcdesc}{geteuid}{} |
Fred Drake | 6b330ba8 | 1999-05-25 13:42:26 +0000 | [diff] [blame] | 107 | \index{user!effective id} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 108 | Return the current process' effective user id. |
| 109 | Availability: \UNIX{}. |
| 110 | \end{funcdesc} |
| 111 | |
| 112 | \begin{funcdesc}{getgid}{} |
Fred Drake | 6b330ba8 | 1999-05-25 13:42:26 +0000 | [diff] [blame] | 113 | \index{process!group} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 114 | Return the current process' group id. |
| 115 | Availability: \UNIX{}. |
| 116 | \end{funcdesc} |
| 117 | |
| 118 | \begin{funcdesc}{getpgrp}{} |
| 119 | \index{process!group} |
| 120 | Return the current process group id. |
| 121 | Availability: \UNIX{}. |
| 122 | \end{funcdesc} |
| 123 | |
| 124 | \begin{funcdesc}{getpid}{} |
| 125 | \index{process!id} |
| 126 | Return the current process id. |
| 127 | Availability: \UNIX{}, Windows. |
| 128 | \end{funcdesc} |
| 129 | |
| 130 | \begin{funcdesc}{getppid}{} |
| 131 | \index{process!id of parent} |
| 132 | Return the parent's process id. |
| 133 | Availability: \UNIX{}. |
| 134 | \end{funcdesc} |
| 135 | |
| 136 | \begin{funcdesc}{getuid}{} |
Fred Drake | 6b330ba8 | 1999-05-25 13:42:26 +0000 | [diff] [blame] | 137 | \index{user!id} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 138 | Return the current process' user id. |
| 139 | Availability: \UNIX{}. |
| 140 | \end{funcdesc} |
| 141 | |
| 142 | \begin{funcdesc}{putenv}{varname, value} |
| 143 | \index{environment variables!setting} |
| 144 | Set the environment variable named \var{varname} to the string |
| 145 | \var{value}. Such changes to the environment affect subprocesses |
| 146 | started with \function{os.system()}, \function{popen()} or |
| 147 | \function{fork()} and \function{execv()}. |
| 148 | Availability: most flavors of \UNIX{}, Windows. |
| 149 | |
| 150 | When \function{putenv()} is |
| 151 | supported, assignments to items in \code{os.environ} are automatically |
| 152 | translated into corresponding calls to \function{putenv()}; however, |
| 153 | calls to \function{putenv()} don't update \code{os.environ}, so it is |
| 154 | actually preferable to assign to items of \code{os.environ}. |
| 155 | \end{funcdesc} |
| 156 | |
| 157 | \begin{funcdesc}{setgid}{gid} |
| 158 | Set the current process' group id. |
| 159 | Availability: \UNIX{}. |
| 160 | \end{funcdesc} |
| 161 | |
| 162 | \begin{funcdesc}{setpgrp}{} |
| 163 | Calls the system call \cfunction{setpgrp()} or \cfunction{setpgrp(0, |
| 164 | 0)} depending on which version is implemented (if any). See the |
| 165 | \UNIX{} manual for the semantics. |
| 166 | Availability: \UNIX{}. |
| 167 | \end{funcdesc} |
| 168 | |
| 169 | \begin{funcdesc}{setpgid}{pid, pgrp} |
| 170 | Calls the system call \cfunction{setpgid()}. See the \UNIX{} manual |
| 171 | for the semantics. |
| 172 | Availability: \UNIX{}. |
| 173 | \end{funcdesc} |
| 174 | |
| 175 | \begin{funcdesc}{setsid}{} |
| 176 | Calls the system call \cfunction{setsid()}. See the \UNIX{} manual |
| 177 | for the semantics. |
| 178 | Availability: \UNIX{}. |
| 179 | \end{funcdesc} |
| 180 | |
| 181 | \begin{funcdesc}{setuid}{uid} |
Fred Drake | 6b330ba8 | 1999-05-25 13:42:26 +0000 | [diff] [blame] | 182 | \index{user!id, setting} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 183 | Set the current process' user id. |
| 184 | Availability: \UNIX{}. |
| 185 | \end{funcdesc} |
| 186 | |
| 187 | % placed in this section since it relates to errno.... a little weak ;-( |
| 188 | \begin{funcdesc}{strerror}{code} |
| 189 | Return the error message corresponding to the error code in |
| 190 | \var{code}. |
| 191 | Availability: \UNIX{}, Windows. |
| 192 | \end{funcdesc} |
| 193 | |
| 194 | \begin{funcdesc}{umask}{mask} |
| 195 | Set the current numeric umask and returns the previous umask. |
| 196 | Availability: \UNIX{}, Windows. |
| 197 | \end{funcdesc} |
| 198 | |
| 199 | \begin{funcdesc}{uname}{} |
| 200 | Return a 5-tuple containing information identifying the current |
| 201 | operating system. The tuple contains 5 strings: |
| 202 | \code{(\var{sysname}, \var{nodename}, \var{release}, \var{version}, |
| 203 | \var{machine})}. Some systems truncate the nodename to 8 |
| 204 | characters or to the leading component; a better way to get the |
| 205 | hostname is \function{socket.gethostname()} |
| 206 | \withsubitem{(in module socket)}{\ttindex{gethostname()}} |
| 207 | or even |
| 208 | \withsubitem{(in module socket)}{\ttindex{gethostbyaddr()}} |
| 209 | \code{socket.gethostbyaddr(socket.gethostname())}. |
| 210 | Availability: recent flavors of \UNIX{}. |
| 211 | \end{funcdesc} |
| 212 | |
| 213 | |
| 214 | |
| 215 | \subsection{File Object Creation \label{os-newstreams}} |
| 216 | |
| 217 | These functions create new file objects. |
| 218 | |
| 219 | |
| 220 | \begin{funcdesc}{fdopen}{fd\optional{, mode\optional{, bufsize}}} |
| 221 | Return an open file object connected to the file descriptor \var{fd}. |
| 222 | The \var{mode} and \var{bufsize} arguments have the same meaning as |
| 223 | the corresponding arguments to the built-in \function{open()} |
| 224 | function. |
| 225 | Availability: Macintosh, \UNIX{}, Windows. |
| 226 | \end{funcdesc} |
| 227 | |
| 228 | \begin{funcdesc}{popen}{command\optional{, mode\optional{, bufsize}}} |
| 229 | Open a pipe to or from \var{command}. The return value is an open |
| 230 | file object connected to the pipe, which can be read or written |
| 231 | depending on whether \var{mode} is \code{'r'} (default) or \code{'w'}. |
| 232 | The \var{bufsize} argument has the same meaning as the corresponding |
| 233 | argument to the built-in \function{open()} function. The exit status of |
| 234 | the command (encoded in the format specified for \function{wait()}) is |
| 235 | available as the return value of the \method{close()} method of the file |
| 236 | object, except that when the exit status is zero (termination without |
| 237 | errors), \code{None} is returned. |
| 238 | Availability: \UNIX{}, Windows. |
| 239 | \end{funcdesc} |
| 240 | |
| 241 | |
| 242 | |
| 243 | \subsection{File Descriptor Operations \label{os-fd-ops}} |
| 244 | |
| 245 | These functions operate on I/O streams referred to |
| 246 | using file descriptors. |
| 247 | |
| 248 | |
| 249 | \begin{funcdesc}{close}{fd} |
| 250 | Close file descriptor \var{fd}. |
| 251 | Availability: Macintosh, \UNIX{}, Windows. |
| 252 | |
| 253 | Note: this function is intended for low-level I/O and must be applied |
| 254 | to a file descriptor as returned by \function{open()} or |
| 255 | \function{pipe()}. To close a ``file object'' returned by the |
| 256 | built-in function \function{open()} or by \function{popen()} or |
| 257 | \function{fdopen()}, use its \method{close()} method. |
| 258 | \end{funcdesc} |
| 259 | |
| 260 | \begin{funcdesc}{dup}{fd} |
| 261 | Return a duplicate of file descriptor \var{fd}. |
| 262 | Availability: Macintosh, \UNIX{}, Windows. |
| 263 | \end{funcdesc} |
| 264 | |
| 265 | \begin{funcdesc}{dup2}{fd, fd2} |
| 266 | Duplicate file descriptor \var{fd} to \var{fd2}, closing the latter |
| 267 | first if necessary. |
| 268 | Availability: \UNIX{}, Windows. |
| 269 | \end{funcdesc} |
| 270 | |
| 271 | \begin{funcdesc}{fstat}{fd} |
| 272 | Return status for file descriptor \var{fd}, like \function{stat()}. |
| 273 | Availability: \UNIX{}, Windows. |
| 274 | \end{funcdesc} |
| 275 | |
| 276 | \begin{funcdesc}{fstatvfs}{fd} |
| 277 | Return information about the filesystem containing the file associated |
| 278 | with file descriptor \var{fd}, like \function{statvfs()}. |
| 279 | Availability: \UNIX{}. |
| 280 | \end{funcdesc} |
| 281 | |
| 282 | \begin{funcdesc}{ftruncate}{fd, length} |
| 283 | Truncate the file corresponding to file descriptor \var{fd}, |
| 284 | so that it is at most \var{length} bytes in size. |
| 285 | Availability: \UNIX{}. |
| 286 | \end{funcdesc} |
| 287 | |
| 288 | \begin{funcdesc}{lseek}{fd, pos, how} |
| 289 | Set the current position of file descriptor \var{fd} to position |
| 290 | \var{pos}, modified by \var{how}: \code{0} to set the position |
| 291 | relative to the beginning of the file; \code{1} to set it relative to |
| 292 | the current position; \code{2} to set it relative to the end of the |
| 293 | file. |
| 294 | Availability: Macintosh, \UNIX{}, Windows. |
| 295 | \end{funcdesc} |
| 296 | |
| 297 | \begin{funcdesc}{open}{file, flags\optional{, mode}} |
| 298 | Open the file \var{file} and set various flags according to |
| 299 | \var{flags} and possibly its mode according to \var{mode}. |
| 300 | The default \var{mode} is \code{0777} (octal), and the current umask |
| 301 | value is first masked out. Return the file descriptor for the newly |
| 302 | opened file. |
| 303 | Availability: Macintosh, \UNIX{}, Windows. |
| 304 | |
| 305 | For a description of the flag and mode values, see the C run-time |
| 306 | documentation; flag constants (like \constant{O_RDONLY} and |
| 307 | \constant{O_WRONLY}) are defined in this module too (see below). |
| 308 | |
| 309 | Note: this function is intended for low-level I/O. For normal usage, |
| 310 | use the built-in function \function{open()}, which returns a ``file |
| 311 | object'' with \method{read()} and \method{write()} methods (and many |
| 312 | more). |
| 313 | \end{funcdesc} |
| 314 | |
| 315 | \begin{funcdesc}{pipe}{} |
| 316 | Create a pipe. Return a pair of file descriptors \code{(\var{r}, |
| 317 | \var{w})} usable for reading and writing, respectively. |
| 318 | Availability: \UNIX{}, Windows. |
| 319 | \end{funcdesc} |
| 320 | |
| 321 | \begin{funcdesc}{read}{fd, n} |
| 322 | Read at most \var{n} bytes from file descriptor \var{fd}. |
| 323 | Return a string containing the bytes read. |
| 324 | Availability: Macintosh, \UNIX{}, Windows. |
| 325 | |
| 326 | Note: this function is intended for low-level I/O and must be applied |
| 327 | to a file descriptor as returned by \function{open()} or |
| 328 | \function{pipe()}. To read a ``file object'' returned by the |
| 329 | built-in function \function{open()} or by \function{popen()} or |
| 330 | \function{fdopen()}, or \code{sys.stdin}, use its |
| 331 | \method{read()} or \method{readline()} methods. |
| 332 | \end{funcdesc} |
| 333 | |
| 334 | \begin{funcdesc}{tcgetpgrp}{fd} |
| 335 | Return the process group associated with the terminal given by |
| 336 | \var{fd} (an open file descriptor as returned by \function{open()}). |
| 337 | Availability: \UNIX{}. |
| 338 | \end{funcdesc} |
| 339 | |
| 340 | \begin{funcdesc}{tcsetpgrp}{fd, pg} |
| 341 | Set the process group associated with the terminal given by |
| 342 | \var{fd} (an open file descriptor as returned by \function{open()}) |
| 343 | to \var{pg}. |
| 344 | Availability: \UNIX{}. |
| 345 | \end{funcdesc} |
| 346 | |
| 347 | \begin{funcdesc}{ttyname}{fd} |
| 348 | Return a string which specifies the terminal device associated with |
| 349 | file-descriptor \var{fd}. If \var{fd} is not associated with a terminal |
| 350 | device, an exception is raised. |
| 351 | Availability: \UNIX{}. |
| 352 | \end{funcdesc} |
| 353 | |
| 354 | \begin{funcdesc}{write}{fd, str} |
| 355 | Write the string \var{str} to file descriptor \var{fd}. |
| 356 | Return the number of bytes actually written. |
| 357 | Availability: Macintosh, \UNIX{}, Windows. |
| 358 | |
| 359 | Note: this function is intended for low-level I/O and must be applied |
| 360 | to a file descriptor as returned by \function{open()} or |
| 361 | \function{pipe()}. To write a ``file object'' returned by the |
| 362 | built-in function \function{open()} or by \function{popen()} or |
| 363 | \function{fdopen()}, or \code{sys.stdout} or \code{sys.stderr}, use |
| 364 | its \method{write()} method. |
| 365 | \end{funcdesc} |
| 366 | |
| 367 | |
| 368 | The following data items are available for use in constructing the |
| 369 | \var{flags} parameter to the \function{open()} function. |
| 370 | |
| 371 | \begin{datadesc}{O_RDONLY} |
| 372 | \dataline{O_WRONLY} |
| 373 | \dataline{O_RDWR} |
| 374 | \dataline{O_NDELAY} |
| 375 | \dataline{O_NONBLOCK} |
| 376 | \dataline{O_APPEND} |
| 377 | \dataline{O_DSYNC} |
| 378 | \dataline{O_RSYNC} |
| 379 | \dataline{O_SYNC} |
| 380 | \dataline{O_NOCTTY} |
| 381 | \dataline{O_CREAT} |
| 382 | \dataline{O_EXCL} |
| 383 | \dataline{O_TRUNC} |
| 384 | Options for the \var{flag} argument to the \function{open()} function. |
| 385 | These can be bit-wise OR'd together. |
| 386 | Availability: Macintosh, \UNIX{}, Windows. |
| 387 | \end{datadesc} |
| 388 | |
| 389 | |
| 390 | \subsection{Files and Directories \label{os-file-dir}} |
| 391 | |
| 392 | \begin{funcdesc}{access}{path, mode} |
| 393 | Check read/write/execute permissions for this process or extance of file |
| 394 | \var{path}. Return \code{1} if access is granted, \code{0} if not. |
| 395 | See the \UNIX{} manual for the semantics. |
| 396 | Availability: \UNIX{}. |
| 397 | \end{funcdesc} |
| 398 | |
| 399 | \begin{funcdesc}{chmod}{path, mode} |
| 400 | Change the mode of \var{path} to the numeric \var{mode}. |
| 401 | Availability: \UNIX{}, Windows. |
| 402 | \end{funcdesc} |
| 403 | |
| 404 | \begin{funcdesc}{chown}{path, uid, gid} |
| 405 | Change the owner and group id of \var{path} to the numeric \var{uid} |
| 406 | and \var{gid}. |
| 407 | Availability: \UNIX{}. |
| 408 | \end{funcdesc} |
| 409 | |
| 410 | \begin{funcdesc}{link}{src, dst} |
| 411 | Create a hard link pointing to \var{src} named \var{dst}. |
| 412 | Availability: \UNIX{}. |
| 413 | \end{funcdesc} |
| 414 | |
| 415 | \begin{funcdesc}{listdir}{path} |
| 416 | Return a list containing the names of the entries in the directory. |
| 417 | The list is in arbitrary order. It does not include the special |
| 418 | entries \code{'.'} and \code{'..'} even if they are present in the |
| 419 | directory. |
| 420 | Availability: Macintosh, \UNIX{}, Windows. |
| 421 | \end{funcdesc} |
| 422 | |
| 423 | \begin{funcdesc}{lstat}{path} |
| 424 | Like \function{stat()}, but do not follow symbolic links. |
| 425 | Availability: \UNIX{}. |
| 426 | \end{funcdesc} |
| 427 | |
| 428 | \begin{funcdesc}{mkfifo}{path\optional{, mode}} |
| 429 | Create a FIFO (a named pipe) named \var{path} with numeric mode |
| 430 | \var{mode}. The default \var{mode} is \code{0666} (octal). The current |
| 431 | umask value is first masked out from the mode. |
| 432 | Availability: \UNIX{}. |
| 433 | |
| 434 | FIFOs are pipes that can be accessed like regular files. FIFOs exist |
| 435 | until they are deleted (for example with \function{os.unlink()}). |
| 436 | Generally, FIFOs are used as rendezvous between ``client'' and |
| 437 | ``server'' type processes: the server opens the FIFO for reading, and |
| 438 | the client opens it for writing. Note that \function{mkfifo()} |
| 439 | doesn't open the FIFO --- it just creates the rendezvous point. |
| 440 | \end{funcdesc} |
| 441 | |
| 442 | \begin{funcdesc}{mkdir}{path\optional{, mode}} |
| 443 | Create a directory named \var{path} with numeric mode \var{mode}. |
| 444 | The default \var{mode} is \code{0777} (octal). On some systems, |
| 445 | \var{mode} is ignored. Where it is used, the current umask value is |
| 446 | first masked out. |
| 447 | Availability: Macintosh, \UNIX{}, Windows. |
| 448 | \end{funcdesc} |
| 449 | |
| 450 | \begin{funcdesc}{makedirs}{path\optional{, mode}} |
| 451 | \index{directory!creating} |
| 452 | Recursive directory creation function. Like \function{mkdir()}, |
| 453 | but makes all intermediate-level directories needed to contain the |
| 454 | leaf directory. Throws an \exception{error} exception if the leaf |
| 455 | directory already exists or cannot be created. The default \var{mode} |
| 456 | is \code{0777} (octal). |
| 457 | \versionadded{1.5.2} |
| 458 | \end{funcdesc} |
| 459 | |
| 460 | \begin{funcdesc}{readlink}{path} |
| 461 | Return a string representing the path to which the symbolic link |
| 462 | points. |
| 463 | Availability: \UNIX{}. |
| 464 | \end{funcdesc} |
| 465 | |
| 466 | \begin{funcdesc}{remove}{path} |
| 467 | Remove the file \var{path}. See \function{rmdir()} below to remove a |
| 468 | directory. This is identical to the \function{unlink()} function |
| 469 | documented below. |
| 470 | Availability: Macintosh, \UNIX{}, Windows. |
| 471 | \end{funcdesc} |
| 472 | |
| 473 | \begin{funcdesc}{removedirs}{path} |
| 474 | \index{directory!deleting} |
| 475 | Recursive directory removal function. Works like |
| 476 | \function{rmdir()} except that, if the leaf directory is |
| 477 | successfully removed, directories corresponding to rightmost path |
| 478 | segments will be pruned way until either the whole path is consumed or |
| 479 | an error is raised (which is ignored, because it generally means that |
| 480 | a parent directory is not empty). Throws an \exception{error} |
| 481 | exception if the leaf directory could not be successfully removed. |
| 482 | \versionadded{1.5.2} |
| 483 | \end{funcdesc} |
| 484 | |
| 485 | \begin{funcdesc}{rename}{src, dst} |
| 486 | Rename the file or directory \var{src} to \var{dst}. |
| 487 | Availability: Macintosh, \UNIX{}, Windows. |
| 488 | \end{funcdesc} |
| 489 | |
| 490 | \begin{funcdesc}{renames}{old, new} |
| 491 | Recursive directory or file renaming function. |
| 492 | Works like \function{rename()}, except creation of any intermediate |
| 493 | directories needed to make the new pathname good is attempted first. |
| 494 | After the rename, directories corresponding to rightmost path segments |
| 495 | of the old name will be pruned away using \function{removedirs()}. |
| 496 | |
| 497 | Note: this function can fail with the new directory structure made if |
| 498 | you lack permissions needed to remove the leaf directory or file. |
| 499 | \versionadded{1.5.2} |
| 500 | \end{funcdesc} |
| 501 | |
| 502 | \begin{funcdesc}{rmdir}{path} |
| 503 | Remove the directory \var{path}. |
| 504 | Availability: Macintosh, \UNIX{}, Windows. |
| 505 | \end{funcdesc} |
| 506 | |
| 507 | \begin{funcdesc}{stat}{path} |
| 508 | Perform a \cfunction{stat()} system call on the given path. The |
| 509 | return value is a tuple of at least 10 integers giving the most |
| 510 | important (and portable) members of the \emph{stat} structure, in the |
| 511 | order |
| 512 | \code{st_mode}, |
| 513 | \code{st_ino}, |
| 514 | \code{st_dev}, |
| 515 | \code{st_nlink}, |
| 516 | \code{st_uid}, |
| 517 | \code{st_gid}, |
| 518 | \code{st_size}, |
| 519 | \code{st_atime}, |
| 520 | \code{st_mtime}, |
| 521 | \code{st_ctime}. |
| 522 | More items may be added at the end by some implementations. |
| 523 | (On MS Windows, some items are filled with dummy values.) |
| 524 | Availability: Macintosh, \UNIX{}, Windows. |
| 525 | |
| 526 | Note: The standard module \refmodule{stat}\refstmodindex{stat} defines |
| 527 | functions and constants that are useful for extracting information |
| 528 | from a \ctype{stat} structure. |
| 529 | \end{funcdesc} |
| 530 | |
| 531 | \begin{funcdesc}{statvfs}{path} |
| 532 | Perform a \cfunction{statvfs()} system call on the given path. The |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 533 | return value is a tuple of 10 integers giving the most common |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 534 | members of the \ctype{statvfs} structure, in the order |
| 535 | \code{f_bsize}, |
| 536 | \code{f_frsize}, |
| 537 | \code{f_blocks}, |
| 538 | \code{f_bfree}, |
| 539 | \code{f_bavail}, |
| 540 | \code{f_files}, |
| 541 | \code{f_ffree}, |
| 542 | \code{f_favail}, |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 543 | \code{f_flag}, |
| 544 | \code{f_namemax}. |
| 545 | Availability: \UNIX{}. |
| 546 | |
| 547 | Note: The standard module \module{statvfs}\refstmodindex{statvfs} |
| 548 | defines constants that are useful for extracting information |
| 549 | from a \ctype{statvfs} structure. |
| 550 | \end{funcdesc} |
| 551 | |
| 552 | \begin{funcdesc}{symlink}{src, dst} |
| 553 | Create a symbolic link pointing to \var{src} named \var{dst}. |
| 554 | Availability: \UNIX{}. |
| 555 | \end{funcdesc} |
| 556 | |
| 557 | \begin{funcdesc}{unlink}{path} |
| 558 | Remove the file \var{path}. This is the same function as |
| 559 | \function{remove()}; the \function{unlink()} name is its traditional |
| 560 | \UNIX{} name. |
| 561 | Availability: Macintosh, \UNIX{}, Windows. |
| 562 | \end{funcdesc} |
| 563 | |
| 564 | \begin{funcdesc}{utime}{path, (atime, mtime)} |
| 565 | Set the access and modified time of the file to the given values. |
| 566 | (The second argument is a tuple of two items.) |
| 567 | Availability: Macintosh, \UNIX{}, Windows. |
| 568 | \end{funcdesc} |
| 569 | |
| 570 | |
| 571 | \subsection{Process Management \label{os-process}} |
| 572 | |
| 573 | These functions may be used to create and manage additional |
| 574 | processes. |
| 575 | |
| 576 | |
| 577 | \begin{funcdesc}{execl}{path, arg0, arg1, ...} |
| 578 | This is equivalent to |
| 579 | \samp{execv(\var{path}, (\var{arg0}, \var{arg1}, ...))}. |
| 580 | Availability: \UNIX{}, Windows. |
| 581 | \end{funcdesc} |
| 582 | |
| 583 | \begin{funcdesc}{execle}{path, arg0, arg1, ..., env} |
| 584 | This is equivalent to |
| 585 | \samp{execve(\var{path}, (\var{arg0}, \var{arg1}, ...), \var{env})}. |
| 586 | Availability: \UNIX{}, Windows. |
| 587 | \end{funcdesc} |
| 588 | |
| 589 | \begin{funcdesc}{execlp}{path, arg0, arg1, ...} |
| 590 | This is equivalent to |
| 591 | \samp{execvp(\var{path}, (\var{arg0}, \var{arg1}, ...))}. |
| 592 | Availability: \UNIX{}, Windows. |
| 593 | \end{funcdesc} |
| 594 | |
| 595 | \begin{funcdesc}{execv}{path, args} |
| 596 | Execute the executable \var{path} with argument list \var{args}, |
| 597 | replacing the current process (i.e., the Python interpreter). |
| 598 | The argument list may be a tuple or list of strings. |
| 599 | Availability: \UNIX{}, Windows. |
| 600 | \end{funcdesc} |
| 601 | |
| 602 | \begin{funcdesc}{execve}{path, args, env} |
| 603 | Execute the executable \var{path} with argument list \var{args}, |
| 604 | and environment \var{env}, |
| 605 | replacing the current process (i.e., the Python interpreter). |
| 606 | The argument list may be a tuple or list of strings. |
| 607 | The environment must be a dictionary mapping strings to strings. |
| 608 | Availability: \UNIX{}, Windows. |
| 609 | \end{funcdesc} |
| 610 | |
| 611 | \begin{funcdesc}{execvp}{path, args} |
| 612 | This is like \samp{execv(\var{path}, \var{args})} but duplicates |
| 613 | the shell's actions in searching for an executable file in a list of |
| 614 | directories. The directory list is obtained from |
| 615 | \code{environ['PATH']}. |
| 616 | Availability: \UNIX{}, Windows. |
| 617 | \end{funcdesc} |
| 618 | |
| 619 | \begin{funcdesc}{execvpe}{path, args, env} |
| 620 | This is a cross between \function{execve()} and \function{execvp()}. |
| 621 | The directory list is obtained from \code{\var{env}['PATH']}. |
| 622 | Availability: \UNIX{}, Windows. |
| 623 | \end{funcdesc} |
| 624 | |
| 625 | \begin{funcdesc}{_exit}{n} |
| 626 | Exit to the system with status \var{n}, without calling cleanup |
| 627 | handlers, flushing stdio buffers, etc. |
| 628 | Availability: \UNIX{}, Windows. |
| 629 | |
| 630 | Note: the standard way to exit is \code{sys.exit(\var{n})}. |
| 631 | \function{_exit()} should normally only be used in the child process |
| 632 | after a \function{fork()}. |
| 633 | \end{funcdesc} |
| 634 | |
| 635 | \begin{funcdesc}{fork}{} |
| 636 | Fork a child process. Return \code{0} in the child, the child's |
| 637 | process id in the parent. |
| 638 | Availability: \UNIX{}. |
| 639 | \end{funcdesc} |
| 640 | |
| 641 | \begin{funcdesc}{kill}{pid, sig} |
| 642 | \index{process!killing} |
| 643 | \index{process!signalling} |
| 644 | Kill the process \var{pid} with signal \var{sig}. |
| 645 | Availability: \UNIX{}. |
| 646 | \end{funcdesc} |
| 647 | |
| 648 | \begin{funcdesc}{nice}{increment} |
| 649 | Add \var{increment} to the process's ``niceness''. Return the new |
| 650 | niceness. |
| 651 | Availability: \UNIX{}. |
| 652 | \end{funcdesc} |
| 653 | |
| 654 | \begin{funcdesc}{plock}{op} |
| 655 | Lock program segments into memory. The value of \var{op} |
| 656 | (defined in \code{<sys/lock.h>}) determines which segments are locked. |
Fred Drake | 3906363 | 1999-02-26 14:05:02 +0000 | [diff] [blame] | 657 | Availability: \UNIX{}. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 658 | \end{funcdesc} |
| 659 | |
| 660 | \begin{funcdesc}{spawnv}{mode, path, args} |
| 661 | Execute the program \var{path} in a new process, passing the arguments |
| 662 | specified in \var{args} as command-line parameters. \var{args} may be |
| 663 | a list or a tuple. \var{mode} is a magic operational constant. See |
| 664 | the Visual \Cpp{} Runtime Library documentation for further |
| 665 | information. |
| 666 | Availability: Windows. |
| 667 | \versionadded{1.5.2} |
| 668 | \end{funcdesc} |
| 669 | |
| 670 | \begin{funcdesc}{spawnve}{mode, path, args, env} |
| 671 | Execute the program \var{path} in a new process, passing the arguments |
| 672 | specified in \var{args} as command-line parameters and the contents of |
| 673 | the mapping \var{env} as the environment. \var{args} may be a list or |
| 674 | a tuple. \var{mode} is a magic operational constant. See the Visual |
| 675 | \Cpp{} Runtime Library documentation for further information. |
| 676 | Availability: Windows. |
| 677 | \versionadded{1.5.2} |
| 678 | \end{funcdesc} |
| 679 | |
Fred Drake | 9329e5e | 1999-02-16 19:40:19 +0000 | [diff] [blame] | 680 | \begin{datadesc}{P_WAIT} |
| 681 | \dataline{P_NOWAIT} |
| 682 | \dataline{P_NOWAITO} |
| 683 | \dataline{P_OVERLAY} |
| 684 | \dataline{P_DETACH} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 685 | Possible values for the \var{mode} parameter to \function{spawnv()} |
| 686 | and \function{spawnve()}. |
| 687 | Availability: Windows. |
| 688 | \versionadded{1.5.2} |
| 689 | \end{datadesc} |
| 690 | |
| 691 | \begin{funcdesc}{system}{command} |
| 692 | Execute the command (a string) in a subshell. This is implemented by |
| 693 | calling the Standard C function \cfunction{system()}, and has the |
Fred Drake | ec6baaf | 1999-04-21 18:13:31 +0000 | [diff] [blame] | 694 | same limitations. Changes to \code{posix.environ}, \code{sys.stdin}, |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 695 | etc.\ are not reflected in the environment of the executed command. |
| 696 | The return value is the exit status of the process encoded in the |
Fred Drake | 7a62128 | 1999-06-10 15:07:05 +0000 | [diff] [blame] | 697 | format specified for \function{wait()}, except on Windows 95 and 98, |
Fred Drake | a88ef00 | 1999-06-18 19:11:25 +0000 | [diff] [blame^] | 698 | where it is always \code{0}. Note that \POSIX{} does not specify the |
| 699 | meaning of the return value of the C \cfunction{system()} function, |
| 700 | so the return value of the Python function is system-dependent. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 701 | Availability: \UNIX{}, Windows. |
| 702 | \end{funcdesc} |
| 703 | |
| 704 | \begin{funcdesc}{times}{} |
| 705 | Return a 5-tuple of floating point numbers indicating accumulated (CPU |
| 706 | or other) |
| 707 | times, in seconds. The items are: user time, system time, children's |
| 708 | user time, children's system time, and elapsed real time since a fixed |
Fred Drake | ec6baaf | 1999-04-21 18:13:31 +0000 | [diff] [blame] | 709 | point in the past, in that order. See the \UNIX{} manual page |
| 710 | \manpage{times}{2} or the corresponding Windows Platform API |
| 711 | documentation. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 712 | Availability: \UNIX{}, Windows. |
| 713 | \end{funcdesc} |
| 714 | |
| 715 | \begin{funcdesc}{wait}{} |
| 716 | Wait for completion of a child process, and return a tuple containing |
| 717 | its pid and exit status indication: a 16-bit number, whose low byte is |
| 718 | the signal number that killed the process, and whose high byte is the |
| 719 | exit status (if the signal number is zero); the high bit of the low |
| 720 | byte is set if a core file was produced. |
| 721 | Availability: \UNIX{}. |
| 722 | \end{funcdesc} |
| 723 | |
| 724 | \begin{funcdesc}{waitpid}{pid, options} |
Fred Drake | 6b330ba8 | 1999-05-25 13:42:26 +0000 | [diff] [blame] | 725 | Wait for completion of a child process given by process id, and return |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 726 | a tuple containing its process id and exit status indication (encoded |
| 727 | as for \function{wait()}). The semantics of the call are affected by |
| 728 | the value of the integer \var{options}, which should be \code{0} for |
| 729 | normal operation. |
| 730 | Availability: \UNIX{}. |
| 731 | \end{funcdesc} |
| 732 | |
| 733 | \begin{datadesc}{WNOHANG} |
| 734 | The option for \function{waitpid()} to avoid hanging if no child |
| 735 | process status is available immediately. |
| 736 | Availability: \UNIX{}. |
| 737 | \end{datadesc} |
| 738 | |
| 739 | The following functions take a process stats code as returned by |
| 740 | \function{waitpid()} as a parameter. They may be used to determine |
| 741 | the disposition of a process. |
| 742 | |
| 743 | \begin{funcdesc}{WIFSTOPPED}{status} |
| 744 | Return true if the process has been stopped. |
| 745 | Availability: \UNIX{}. |
| 746 | \end{funcdesc} |
| 747 | |
| 748 | \begin{funcdesc}{WIFSIGNALED}{status} |
| 749 | Return true if the process exited due to a signal. |
| 750 | Availability: \UNIX{}. |
| 751 | \end{funcdesc} |
| 752 | |
| 753 | \begin{funcdesc}{WIFEXITED}{status} |
| 754 | Return true if the process exited using the \manpage{exit}{2} system |
| 755 | call. |
| 756 | Availability: \UNIX{}. |
| 757 | \end{funcdesc} |
| 758 | |
| 759 | \begin{funcdesc}{WEXITSTATUS}{status} |
| 760 | If \code{WIFEXITED(\var{status})} is true, return the integer |
| 761 | parameter to the \manpage{exit}{2} system call. Otherwise, the return |
| 762 | value is meaningless. |
| 763 | Availability: \UNIX{}. |
| 764 | \end{funcdesc} |
| 765 | |
| 766 | \begin{funcdesc}{WSTOPSIG}{status} |
Fred Drake | 35c3ffd | 1999-03-04 14:08:10 +0000 | [diff] [blame] | 767 | Return the signal which caused the process to stop. |
| 768 | Availability: \UNIX{}. |
| 769 | \end{funcdesc} |
| 770 | |
| 771 | \begin{funcdesc}{WTERMSIG}{status} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 772 | Return the signal which caused the process to exit. |
| 773 | Availability: \UNIX{}. |
| 774 | \end{funcdesc} |
| 775 | |
| 776 | |
| 777 | \subsection{Miscellanenous System Data \label{os-path}} |
| 778 | |
| 779 | The follow data values are used to support path manipulation |
| 780 | operations. These are defined for all platforms. |
| 781 | |
| 782 | Higher-level operations on pathnames are defined in the |
| 783 | \refmodule{os.path} module. |
| 784 | |
| 785 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 786 | \begin{datadesc}{curdir} |
| 787 | The constant string used by the OS to refer to the current directory, |
Fred Drake | 1a3c2a0 | 1998-08-06 15:18:23 +0000 | [diff] [blame] | 788 | e.g.\ \code{'.'} for \POSIX{} or \code{':'} for the Macintosh. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 789 | \end{datadesc} |
| 790 | |
| 791 | \begin{datadesc}{pardir} |
| 792 | The constant string used by the OS to refer to the parent directory, |
Fred Drake | 1a3c2a0 | 1998-08-06 15:18:23 +0000 | [diff] [blame] | 793 | e.g.\ \code{'..'} for \POSIX{} or \code{'::'} for the Macintosh. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 794 | \end{datadesc} |
| 795 | |
| 796 | \begin{datadesc}{sep} |
Guido van Rossum | b2afc81 | 1997-08-29 22:37:44 +0000 | [diff] [blame] | 797 | The character used by the OS to separate pathname components, |
Fred Drake | 1a3c2a0 | 1998-08-06 15:18:23 +0000 | [diff] [blame] | 798 | e.g.\ \character{/} for \POSIX{} or \character{:} for the Macintosh. |
| 799 | Note that knowing this is not sufficient to be able to parse or |
| 800 | concatenate pathnames --- use \function{os.path.split()} and |
| 801 | \function{os.path.join()} --- but it is occasionally useful. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 802 | \end{datadesc} |
| 803 | |
Guido van Rossum | b2afc81 | 1997-08-29 22:37:44 +0000 | [diff] [blame] | 804 | \begin{datadesc}{altsep} |
| 805 | An alternative character used by the OS to separate pathname components, |
| 806 | or \code{None} if only one separator character exists. This is set to |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 807 | \character{/} on DOS and Windows systems where \code{sep} is a backslash. |
Guido van Rossum | b2afc81 | 1997-08-29 22:37:44 +0000 | [diff] [blame] | 808 | \end{datadesc} |
| 809 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 810 | \begin{datadesc}{pathsep} |
| 811 | The character conventionally used by the OS to separate search patch |
Fred Drake | 1a3c2a0 | 1998-08-06 15:18:23 +0000 | [diff] [blame] | 812 | components (as in \envvar{PATH}), e.g.\ \character{:} for \POSIX{} or |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 813 | \character{;} for DOS and Windows. |
Guido van Rossum | 9c59ce9 | 1998-06-30 15:54:27 +0000 | [diff] [blame] | 814 | \end{datadesc} |
| 815 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 816 | \begin{datadesc}{defpath} |
Fred Drake | 1a3c2a0 | 1998-08-06 15:18:23 +0000 | [diff] [blame] | 817 | The default search path used by \function{exec*p*()} if the environment |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 818 | doesn't have a \code{'PATH'} key. |
| 819 | \end{datadesc} |
| 820 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 821 | \begin{datadesc}{linesep} |
| 822 | The string used to separate (or, rather, terminate) lines on the |
Fred Drake | ec6baaf | 1999-04-21 18:13:31 +0000 | [diff] [blame] | 823 | current platform. This may be a single character, |
| 824 | e.g.\ \code{'\e n'} for \POSIX{} or \code{'\e r'} for MacOS, or multiple |
| 825 | characters, e.g.\ \code{'\e r\e n'} for MS-DOS and MS Windows. |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 826 | \end{datadesc} |