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