1 \section{\module{os
} ---
2 Miscellaneous OS interfaces
}
4 \declaremodule{standard
}{os
}
5 \modulesynopsis{Miscellaneous OS interfaces.
}
8 This module provides a more portable way of using operating system
9 (OS) dependent functionality than importing an OS dependent built-in
10 module like
\refmodule{posix
} or
\module{nt
}.
12 This module searches for an OS dependent built-in module like
13 \module{mac
} or
\refmodule{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
} in
18 the same format (which happens to have originated with the
21 Extensions peculiar to a particular OS are also available through the
22 \module{os
} module, but using them is of course a threat to
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
}!
31 % Frank Stajano <fstajano@uk.research.att.com> complained that it
32 % wasn't clear that the entries described in the subsections were all
33 % available at the module level (most uses of subsections are
34 % different); I think this is only a problem for the HTML version,
35 % where the relationship may not be as clear.
38 The
\module{os
} module contains many functions and data values.
39 The items below and in the following sub-sections are all available
40 directly from the
\module{os
} module.
44 \begin{excdesc
}{error
}
45 This exception is raised when a function returns a
46 system-related error (e.g., not for illegal argument types). This is
47 also known as the built-in exception
\exception{OSError
}. The
48 accompanying value is a pair containing the numeric error code from
49 \cdata{errno
} and the corresponding string, as would be printed by the
50 C function
\cfunction{perror()
}. See the module
51 \refmodule{errno
}\refbimodindex{errno
}, which contains names for the
52 error codes defined by the underlying operating system.
54 When exceptions are classes, this exception carries two attributes,
55 \member{errno
} and
\member{strerror
}. The first holds the value of
56 the C
\cdata{errno
} variable, and the latter holds the corresponding
57 error message from
\cfunction{strerror()
}. For exceptions that
58 involve a file system path (e.g.
\function{chdir()
} or
59 \function{unlink()
}), the exception instance will contain a third
60 attribute,
\member{filename
}, which is the file name passed to the
63 When exceptions are strings, the string for the exception is
67 \begin{datadesc
}{name
}
68 The name of the OS dependent module imported. The following names
69 have currently been registered:
\code{'posix'
},
\code{'nt'
},
70 \code{'dos'
},
\code{'mac'
},
\code{'os2'
},
\code{'ce'
},
\code{'java'
}.
73 \begin{datadesc
}{path
}
74 The corresponding OS dependent standard module for pathname
75 operations, e.g.,
\module{posixpath
} or
\module{macpath
}. Thus, given
76 the proper imports,
\code{os.path.split(
\var{file
})
} is equivalent to but
77 more portable than
\code{posixpath.split(
\var{file
})
}. Note that this
78 is also a valid module: it may be imported directly as
84 \subsection{Process Parameters
\label{os-procinfo
}}
86 These functions and data items provide information and operate on the
87 current process and user.
89 \begin{datadesc
}{environ
}
90 A mapping object representing the string environment. For example,
91 \code{environ
['HOME'
]} is the pathname of your home directory (on some
92 platforms), and is equivalent to
\code{getenv("HOME")
} in C.
94 If the platform supports the
\function{putenv()
} function, this
95 mapping may be used to modify the environment as well as query the
96 environment.
\function{putenv()
} will be called automatically when
97 the mapping is modified.
99 If
\function{putenv()
} is not provided, this mapping may be passed to
100 the appropriate process-creation functions to cause child processes to
101 use a modified environment.
104 \begin{funcdescni
}{chdir
}{path
}
105 \funclineni{getcwd
}{}
106 These functions are described in ``Files and Directories'' (section
110 \begin{funcdesc
}{ctermid
}{}
111 Return the filename corresponding to the controlling terminal of the
113 Availability:
\UNIX{}.
116 \begin{funcdesc
}{getegid
}{}
117 Return the current process' effective group id.
118 Availability:
\UNIX{}.
121 \begin{funcdesc
}{geteuid
}{}
122 \index{user!effective id
}
123 Return the current process' effective user id.
124 Availability:
\UNIX{}.
127 \begin{funcdesc
}{getgid
}{}
128 \index{process!group
}
129 Return the current process' group id.
130 Availability:
\UNIX{}.
133 \begin{funcdesc
}{getgroups
}{}
134 Return list of supplemental group ids associated with the current
136 Availability:
\UNIX{}.
139 \begin{funcdesc
}{getlogin
}{}
140 Return the actual login name for the current process, even if there
141 are multiple login names which map to the same user id.
142 Availability:
\UNIX{}.
145 \begin{funcdesc
}{getpgrp
}{}
146 \index{process!group
}
147 Return the current process group id.
148 Availability:
\UNIX{}.
151 \begin{funcdesc
}{getpid
}{}
153 Return the current process id.
154 Availability:
\UNIX{}, Windows.
157 \begin{funcdesc
}{getppid
}{}
158 \index{process!id of parent
}
159 Return the parent's process id.
160 Availability:
\UNIX{}.
163 \begin{funcdesc
}{getuid
}{}
165 Return the current process' user id.
166 Availability:
\UNIX{}.
169 \begin{funcdesc
}{putenv
}{varname, value
}
170 \index{environment variables!setting
}
171 Set the environment variable named
\var{varname
} to the string
172 \var{value
}. Such changes to the environment affect subprocesses
173 started with
\function{os.system()
},
\function{popen()
} or
174 \function{fork()
} and
\function{execv()
}.
175 Availability: most flavors of
\UNIX{}, Windows.
177 When
\function{putenv()
} is
178 supported, assignments to items in
\code{os.environ
} are automatically
179 translated into corresponding calls to
\function{putenv()
}; however,
180 calls to
\function{putenv()
} don't update
\code{os.environ
}, so it is
181 actually preferable to assign to items of
\code{os.environ
}.
184 \begin{funcdesc
}{setgid
}{gid
}
185 Set the current process' group id.
186 Availability:
\UNIX{}.
189 \begin{funcdesc
}{setpgrp
}{}
190 Calls the system call
\cfunction{setpgrp()
} or
\cfunction{setpgrp(
0,
191 0)
} depending on which version is implemented (if any). See the
192 \UNIX{} manual for the semantics.
193 Availability:
\UNIX{}.
196 \begin{funcdesc
}{setpgid
}{pid, pgrp
}
197 Calls the system call
\cfunction{setpgid()
}. See the
\UNIX{} manual
199 Availability:
\UNIX{}.
202 \begin{funcdesc
}{setsid
}{}
203 Calls the system call
\cfunction{setsid()
}. See the
\UNIX{} manual
205 Availability:
\UNIX{}.
208 \begin{funcdesc
}{setuid
}{uid
}
209 \index{user!id, setting
}
210 Set the current process' user id.
211 Availability:
\UNIX{}.
214 % placed in this section since it relates to errno.... a little weak ;-(
215 \begin{funcdesc
}{strerror
}{code
}
216 Return the error message corresponding to the error code in
218 Availability:
\UNIX{}, Windows.
221 \begin{funcdesc
}{umask
}{mask
}
222 Set the current numeric umask and returns the previous umask.
223 Availability:
\UNIX{}, Windows.
226 \begin{funcdesc
}{uname
}{}
227 Return a
5-tuple containing information identifying the current
228 operating system. The tuple contains
5 strings:
229 \code{(
\var{sysname
},
\var{nodename
},
\var{release
},
\var{version
},
230 \var{machine
})
}. Some systems truncate the nodename to
8
231 characters or to the leading component; a better way to get the
232 hostname is
\function{socket.gethostname()
}
233 \withsubitem{(in module socket)
}{\ttindex{gethostname()
}}
235 \withsubitem{(in module socket)
}{\ttindex{gethostbyaddr()
}}
236 \code{socket.gethostbyaddr(socket.gethostname())
}.
237 Availability: recent flavors of
\UNIX{}.
242 \subsection{File Object Creation
\label{os-newstreams
}}
244 These functions create new file objects.
247 \begin{funcdesc
}{fdopen
}{fd
\optional{, mode
\optional{, bufsize
}}}
248 Return an open file object connected to the file descriptor
\var{fd
}.
249 \index{I/O control!buffering
}
250 The
\var{mode
} and
\var{bufsize
} arguments have the same meaning as
251 the corresponding arguments to the built-in
\function{open()
}
253 Availability: Macintosh,
\UNIX{}, Windows.
256 \begin{funcdesc
}{popen
}{command
\optional{, mode
\optional{, bufsize
}}}
257 Open a pipe to or from
\var{command
}. The return value is an open
258 file object connected to the pipe, which can be read or written
259 depending on whether
\var{mode
} is
\code{'r'
} (default) or
\code{'w'
}.
260 The
\var{bufsize
} argument has the same meaning as the corresponding
261 argument to the built-in
\function{open()
} function. The exit status of
262 the command (encoded in the format specified for
\function{wait()
}) is
263 available as the return value of the
\method{close()
} method of the file
264 object, except that when the exit status is zero (termination without
265 errors),
\code{None
} is returned.
266 Availability:
\UNIX{}, Windows.
269 \begin{funcdesc
}{tmpfile
}{}
270 Return a new file object opened in update mode (
\samp{w+
}). The file
271 has no directory entries associated with it and will be automatically
272 deleted once there are no file descriptors for the file.
273 Availability:
\UNIX{}.
277 \subsection{File Descriptor Operations
\label{os-fd-ops
}}
279 These functions operate on I/O streams referred to
280 using file descriptors.
283 \begin{funcdesc
}{close
}{fd
}
284 Close file descriptor
\var{fd
}.
285 Availability: Macintosh,
\UNIX{}, Windows.
287 Note: this function is intended for low-level I/O and must be applied
288 to a file descriptor as returned by
\function{open()
} or
289 \function{pipe()
}. To close a ``file object'' returned by the
290 built-in function
\function{open()
} or by
\function{popen()
} or
291 \function{fdopen()
}, use its
\method{close()
} method.
294 \begin{funcdesc
}{dup
}{fd
}
295 Return a duplicate of file descriptor
\var{fd
}.
296 Availability: Macintosh,
\UNIX{}, Windows.
299 \begin{funcdesc
}{dup2
}{fd, fd2
}
300 Duplicate file descriptor
\var{fd
} to
\var{fd2
}, closing the latter
302 Availability:
\UNIX{}, Windows.
305 \begin{funcdesc
}{fpathconf
}{fd, name
}
306 Return system configration information relevant to an open file.
307 \var{name
} specifies the configuration value to retrieve; it may be a
308 string which is the name of a defined system value; these names are
309 specified in a number of standards (
\POSIX.1, Unix95, Unix98, and
310 others). Some platforms define additional names as well. The names
311 known to the host operating system are given in the
312 \code{pathconf_names
} dictionary. For configuration variables not
313 included in that mapping, passing an integer for
\var{name
} is also
315 Availability:
\UNIX{}.
317 If
\var{name
} is a string and is not known,
\exception{ValueError
} is
318 raised. If a specific value for
\var{name
} is not supported by the
319 host system, even if it is included in
\code{pathconf_names
}, an
320 \exception{OSError
} is raised with
\constant{errno.EINVAL
} for the
324 \begin{funcdesc
}{fstat
}{fd
}
325 Return status for file descriptor
\var{fd
}, like
\function{stat()
}.
326 Availability:
\UNIX{}, Windows.
329 \begin{funcdesc
}{fstatvfs
}{fd
}
330 Return information about the filesystem containing the file associated
331 with file descriptor
\var{fd
}, like
\function{statvfs()
}.
332 Availability:
\UNIX{}.
335 \begin{funcdesc
}{ftruncate
}{fd, length
}
336 Truncate the file corresponding to file descriptor
\var{fd
},
337 so that it is at most
\var{length
} bytes in size.
338 Availability:
\UNIX{}.
341 \begin{funcdesc
}{lseek
}{fd, pos, how
}
342 Set the current position of file descriptor
\var{fd
} to position
343 \var{pos
}, modified by
\var{how
}:
\code{0} to set the position
344 relative to the beginning of the file;
\code{1} to set it relative to
345 the current position;
\code{2} to set it relative to the end of the
347 Availability: Macintosh,
\UNIX{}, Windows.
350 \begin{funcdesc
}{open
}{file, flags
\optional{, mode
}}
351 Open the file
\var{file
} and set various flags according to
352 \var{flags
} and possibly its mode according to
\var{mode
}.
353 The default
\var{mode
} is
\code{0777} (octal), and the current umask
354 value is first masked out. Return the file descriptor for the newly
356 Availability: Macintosh,
\UNIX{}, Windows.
358 For a description of the flag and mode values, see the C run-time
359 documentation; flag constants (like
\constant{O_RDONLY
} and
360 \constant{O_WRONLY
}) are defined in this module too (see below).
362 Note: this function is intended for low-level I/O. For normal usage,
363 use the built-in function
\function{open()
}, which returns a ``file
364 object'' with
\method{read()
} and
\method{write()
} methods (and many
368 \begin{funcdesc
}{pipe
}{}
369 Create a pipe. Return a pair of file descriptors
\code{(
\var{r
},
370 \var{w
})
} usable for reading and writing, respectively.
371 Availability:
\UNIX{}, Windows.
374 \begin{funcdesc
}{read
}{fd, n
}
375 Read at most
\var{n
} bytes from file descriptor
\var{fd
}.
376 Return a string containing the bytes read.
377 Availability: Macintosh,
\UNIX{}, Windows.
379 Note: this function is intended for low-level I/O and must be applied
380 to a file descriptor as returned by
\function{open()
} or
381 \function{pipe()
}. To read a ``file object'' returned by the
382 built-in function
\function{open()
} or by
\function{popen()
} or
383 \function{fdopen()
}, or
\code{sys.stdin
}, use its
384 \method{read()
} or
\method{readline()
} methods.
387 \begin{funcdesc
}{tcgetpgrp
}{fd
}
388 Return the process group associated with the terminal given by
389 \var{fd
} (an open file descriptor as returned by
\function{open()
}).
390 Availability:
\UNIX{}.
393 \begin{funcdesc
}{tcsetpgrp
}{fd, pg
}
394 Set the process group associated with the terminal given by
395 \var{fd
} (an open file descriptor as returned by
\function{open()
})
397 Availability:
\UNIX{}.
400 \begin{funcdesc
}{ttyname
}{fd
}
401 Return a string which specifies the terminal device associated with
402 file-descriptor
\var{fd
}. If
\var{fd
} is not associated with a terminal
403 device, an exception is raised.
404 Availability:
\UNIX{}.
407 \begin{funcdesc
}{write
}{fd, str
}
408 Write the string
\var{str
} to file descriptor
\var{fd
}.
409 Return the number of bytes actually written.
410 Availability: Macintosh,
\UNIX{}, Windows.
412 Note: this function is intended for low-level I/O and must be applied
413 to a file descriptor as returned by
\function{open()
} or
414 \function{pipe()
}. To write a ``file object'' returned by the
415 built-in function
\function{open()
} or by
\function{popen()
} or
416 \function{fdopen()
}, or
\code{sys.stdout
} or
\code{sys.stderr
}, use
417 its
\method{write()
} method.
421 The following data items are available for use in constructing the
422 \var{flags
} parameter to the
\function{open()
} function.
424 \begin{datadesc
}{O_RDONLY
}
428 \dataline{O_NONBLOCK
}
437 Options for the
\var{flag
} argument to the
\function{open()
} function.
438 These can be bit-wise OR'd together.
439 Availability: Macintosh,
\UNIX{}, Windows.
443 \subsection{Files and Directories
\label{os-file-dir
}}
445 \begin{funcdesc
}{access
}{path, mode
}
446 Check read/write/execute permissions for this process or extance of file
447 \var{path
}. Return
\code{1} if access is granted,
\code{0} if not.
448 See the
\UNIX{} manual for the semantics.
449 Availability:
\UNIX{}.
452 \begin{funcdesc
}{chdir
}{path
}
453 \index{directory!changing
}
454 Change the current working directory to
\var{path
}.
455 Availability: Macintosh,
\UNIX{}, Windows.
458 \begin{funcdesc
}{getcwd
}{}
459 Return a string representing the current working directory.
460 Availability: Macintosh,
\UNIX{}, Windows.
463 \begin{funcdesc
}{chmod
}{path, mode
}
464 Change the mode of
\var{path
} to the numeric
\var{mode
}.
465 Availability:
\UNIX{}, Windows.
468 \begin{funcdesc
}{chown
}{path, uid, gid
}
469 Change the owner and group id of
\var{path
} to the numeric
\var{uid
}
471 Availability:
\UNIX{}.
474 \begin{funcdesc
}{link
}{src, dst
}
475 Create a hard link pointing to
\var{src
} named
\var{dst
}.
476 Availability:
\UNIX{}.
479 \begin{funcdesc
}{listdir
}{path
}
480 Return a list containing the names of the entries in the directory.
481 The list is in arbitrary order. It does not include the special
482 entries
\code{'.'
} and
\code{'..'
} even if they are present in the
484 Availability: Macintosh,
\UNIX{}, Windows.
487 \begin{funcdesc
}{lstat
}{path
}
488 Like
\function{stat()
}, but do not follow symbolic links.
489 Availability:
\UNIX{}.
492 \begin{funcdesc
}{mkfifo
}{path
\optional{, mode
}}
493 Create a FIFO (a named pipe) named
\var{path
} with numeric mode
494 \var{mode
}. The default
\var{mode
} is
\code{0666} (octal). The current
495 umask value is first masked out from the mode.
496 Availability:
\UNIX{}.
498 FIFOs are pipes that can be accessed like regular files. FIFOs exist
499 until they are deleted (for example with
\function{os.unlink()
}).
500 Generally, FIFOs are used as rendezvous between ``client'' and
501 ``server'' type processes: the server opens the FIFO for reading, and
502 the client opens it for writing. Note that
\function{mkfifo()
}
503 doesn't open the FIFO --- it just creates the rendezvous point.
506 \begin{funcdesc
}{mkdir
}{path
\optional{, mode
}}
507 Create a directory named
\var{path
} with numeric mode
\var{mode
}.
508 The default
\var{mode
} is
\code{0777} (octal). On some systems,
509 \var{mode
} is ignored. Where it is used, the current umask value is
511 Availability: Macintosh,
\UNIX{}, Windows.
514 \begin{funcdesc
}{makedirs
}{path
\optional{, mode
}}
515 \index{directory!creating
}
516 Recursive directory creation function. Like
\function{mkdir()
},
517 but makes all intermediate-level directories needed to contain the
518 leaf directory. Throws an
\exception{error
} exception if the leaf
519 directory already exists or cannot be created. The default
\var{mode
}
520 is
\code{0777} (octal).
524 \begin{funcdesc
}{pathconf
}{path, name
}
525 Return system configration information relevant to a named file.
526 \var{name
} specifies the configuration value to retrieve; it may be a
527 string which is the name of a defined system value; these names are
528 specified in a number of standards (
\POSIX.1, Unix95, Unix98, and
529 others). Some platforms define additional names as well. The names
530 known to the host operating system are given in the
531 \code{pathconf_names
} dictionary. For configuration variables not
532 included in that mapping, passing an integer for
\var{name
} is also
534 Availability:
\UNIX{}.
536 If
\var{name
} is a string and is not known,
\exception{ValueError
} is
537 raised. If a specific value for
\var{name
} is not supported by the
538 host system, even if it is included in
\code{pathconf_names
}, an
539 \exception{OSError
} is raised with
\constant{errno.EINVAL
} for the
543 \begin{datadesc
}{pathconf_names
}
544 Dictionary mapping names accepted by
\function{pathconf()
} and
545 \function{fpathconf()
} to the integer values defined for those names
546 by the host operating system. This can be used to determine the set
547 of names known to the system.
551 \begin{funcdesc
}{readlink
}{path
}
552 Return a string representing the path to which the symbolic link
554 Availability:
\UNIX{}.
557 \begin{funcdesc
}{remove
}{path
}
558 Remove the file
\var{path
}. See
\function{rmdir()
} below to remove a
559 directory. This is identical to the
\function{unlink()
} function
561 Availability: Macintosh,
\UNIX{}, Windows.
564 \begin{funcdesc
}{removedirs
}{path
}
565 \index{directory!deleting
}
566 Recursive directory removal function. Works like
567 \function{rmdir()
} except that, if the leaf directory is
568 successfully removed, directories corresponding to rightmost path
569 segments will be pruned way until either the whole path is consumed or
570 an error is raised (which is ignored, because it generally means that
571 a parent directory is not empty). Throws an
\exception{error
}
572 exception if the leaf directory could not be successfully removed.
576 \begin{funcdesc
}{rename
}{src, dst
}
577 Rename the file or directory
\var{src
} to
\var{dst
}.
578 Availability: Macintosh,
\UNIX{}, Windows.
581 \begin{funcdesc
}{renames
}{old, new
}
582 Recursive directory or file renaming function.
583 Works like
\function{rename()
}, except creation of any intermediate
584 directories needed to make the new pathname good is attempted first.
585 After the rename, directories corresponding to rightmost path segments
586 of the old name will be pruned away using
\function{removedirs()
}.
588 Note: this function can fail with the new directory structure made if
589 you lack permissions needed to remove the leaf directory or file.
593 \begin{funcdesc
}{rmdir
}{path
}
594 Remove the directory
\var{path
}.
595 Availability: Macintosh,
\UNIX{}, Windows.
598 \begin{funcdesc
}{stat
}{path
}
599 Perform a
\cfunction{stat()
} system call on the given path. The
600 return value is a tuple of at least
10 integers giving the most
601 important (and portable) members of the
\emph{stat
} structure, in the
613 More items may be added at the end by some implementations.
614 (On MS Windows, some items are filled with dummy values.)
615 Availability: Macintosh,
\UNIX{}, Windows.
617 Note: The standard module
\refmodule{stat
}\refstmodindex{stat
} defines
618 functions and constants that are useful for extracting information
619 from a
\ctype{stat
} structure.
622 \begin{funcdesc
}{statvfs
}{path
}
623 Perform a
\cfunction{statvfs()
} system call on the given path. The
624 return value is a tuple of
10 integers giving the most common
625 members of the
\ctype{statvfs
} structure, in the order
636 Availability:
\UNIX{}.
638 Note: The standard module
\module{statvfs
}\refstmodindex{statvfs
}
639 defines constants that are useful for extracting information
640 from a
\ctype{statvfs
} structure.
643 \begin{funcdesc
}{symlink
}{src, dst
}
644 Create a symbolic link pointing to
\var{src
} named
\var{dst
}.
645 Availability:
\UNIX{}.
648 \begin{funcdesc
}{tempnam
}{\optional{dir
\optional{, prefix
}}}
649 Return a unique path name that is reasonable for creating a temporary
650 file. This will be an absolute path that names a potential directory
651 entry in the directory
\var{dir
} or a common location for temporary
652 files if
\var{dir
} is omitted or
\code{None
}. If given and not
653 \code{None
},
\var{prefix
} is used to provide a short prefix to the
654 filename. Applications are responsible for properly creating and
655 managing files created using paths returned by
\function{tempnam()
};
656 no automatic cleanup is provided.
659 \begin{funcdesc
}{tmpnam
}{}
660 Return a unique path name that is reasonable for creating a temporary
661 file. This will be an absolute path that names a potential directory
662 entry in a common location for temporary files. Applications are
663 responsible for properly creating and managing files created using
664 paths returned by
\function{tmpnam()
}; no automatic cleanup is
668 \begin{datadesc
}{TMP_MAX
}
669 The maximum number of unique names that
\function{tmpnam()
} will
670 generate before reusing names.
673 \begin{funcdesc
}{unlink
}{path
}
674 Remove the file
\var{path
}. This is the same function as
675 \function{remove()
}; the
\function{unlink()
} name is its traditional
677 Availability: Macintosh,
\UNIX{}, Windows.
680 \begin{funcdesc
}{utime
}{path, (atime, mtime)
}
681 Set the access and modified time of the file to the given values.
682 (The second argument is a tuple of two items.)
683 Availability: Macintosh,
\UNIX{}, Windows.
687 \subsection{Process Management
\label{os-process
}}
689 These functions may be used to create and manage processes.
692 \begin{funcdesc
}{abort
}{}
693 Generate a
\constant{SIGABRT
} signal to the current process. On
694 \UNIX, the default behavior is to produce a core dump; on Windows, the
695 process immediately returns an exit code of
\code{3}. Be aware that
696 programs which use
\function{signal.signal()
} to register a handler
697 for
\constant{SIGABRT
} will behave differently.
698 Availability:
\UNIX, Windows.
701 \begin{funcdesc
}{execl
}{path, arg0, arg1, ...
}
702 This is equivalent to
703 \samp{execv(
\var{path
}, (
\var{arg0
},
\var{arg1
}, ...))
}.
704 Availability:
\UNIX{}, Windows.
707 \begin{funcdesc
}{execle
}{path, arg0, arg1, ..., env
}
708 This is equivalent to
709 \samp{execve(
\var{path
}, (
\var{arg0
},
\var{arg1
}, ...),
\var{env
})
}.
710 Availability:
\UNIX{}, Windows.
713 \begin{funcdesc
}{execlp
}{path, arg0, arg1, ...
}
714 This is equivalent to
715 \samp{execvp(
\var{path
}, (
\var{arg0
},
\var{arg1
}, ...))
}.
716 Availability:
\UNIX{}, Windows.
719 \begin{funcdesc
}{execv
}{path, args
}
720 Execute the executable
\var{path
} with argument list
\var{args
},
721 replacing the current process (i.e., the Python interpreter).
722 The argument list may be a tuple or list of strings.
723 Availability:
\UNIX{}, Windows.
726 \begin{funcdesc
}{execve
}{path, args, env
}
727 Execute the executable
\var{path
} with argument list
\var{args
},
728 and environment
\var{env
},
729 replacing the current process (i.e., the Python interpreter).
730 The argument list may be a tuple or list of strings.
731 The environment must be a dictionary mapping strings to strings.
732 Availability:
\UNIX{}, Windows.
735 \begin{funcdesc
}{execvp
}{path, args
}
736 This is like
\samp{execv(
\var{path
},
\var{args
})
} but duplicates
737 the shell's actions in searching for an executable file in a list of
738 directories. The directory list is obtained from
739 \code{environ
['PATH'
]}.
740 Availability:
\UNIX{}, Windows.
743 \begin{funcdesc
}{execvpe
}{path, args, env
}
744 This is a cross between
\function{execve()
} and
\function{execvp()
}.
745 The directory list is obtained from
\code{\var{env
}['PATH'
]}.
746 Availability:
\UNIX{}, Windows.
749 \begin{funcdesc
}{_exit
}{n
}
750 Exit to the system with status
\var{n
}, without calling cleanup
751 handlers, flushing stdio buffers, etc.
752 Availability:
\UNIX{}, Windows.
754 Note: the standard way to exit is
\code{sys.exit(
\var{n
})
}.
755 \function{_exit()
} should normally only be used in the child process
756 after a
\function{fork()
}.
759 \begin{funcdesc
}{fork
}{}
760 Fork a child process. Return
\code{0} in the child, the child's
761 process id in the parent.
762 Availability:
\UNIX{}.
765 \begin{funcdesc
}{kill
}{pid, sig
}
766 \index{process!killing
}
767 \index{process!signalling
}
768 Kill the process
\var{pid
} with signal
\var{sig
}.
769 Availability:
\UNIX{}.
772 \begin{funcdesc
}{nice
}{increment
}
773 Add
\var{increment
} to the process's ``niceness''. Return the new
775 Availability:
\UNIX{}.
778 \begin{funcdesc
}{plock
}{op
}
779 Lock program segments into memory. The value of
\var{op
}
780 (defined in
\code{<sys/lock.h>
}) determines which segments are locked.
781 Availability:
\UNIX{}.
784 \begin{funcdesc
}{spawnv
}{mode, path, args
}
785 Execute the program
\var{path
} in a new process, passing the arguments
786 specified in
\var{args
} as command-line parameters.
\var{args
} may be
787 a list or a tuple.
\var{mode
} is a magic operational constant. See
788 the Visual
\Cpp{} Runtime Library documentation for further
789 information; the constants are exposed to the Python programmer as
791 Availability:
\UNIX{}, Windows.
795 \begin{funcdesc
}{spawnve
}{mode, path, args, env
}
796 Execute the program
\var{path
} in a new process, passing the arguments
797 specified in
\var{args
} as command-line parameters and the contents of
798 the mapping
\var{env
} as the environment.
\var{args
} may be a list or
799 a tuple.
\var{mode
} is a magic operational constant. See the Visual
800 \Cpp{} Runtime Library documentation for further information; the
801 constants are exposed to the Python programmer as listed below.
802 Availability:
\UNIX{}, Windows.
806 \begin{datadesc
}{P_WAIT
}
809 Possible values for the
\var{mode
} parameter to
\function{spawnv()
}
810 and
\function{spawnve()
}.
811 Availability:
\UNIX{}, Windows.
815 \begin{datadesc
}{P_OVERLAY
}
817 Possible values for the
\var{mode
} parameter to
\function{spawnv()
}
818 and
\function{spawnve()
}. These are less portable than those listed
820 Availability: Windows.
824 \begin{funcdesc
}{system
}{command
}
825 Execute the command (a string) in a subshell. This is implemented by
826 calling the Standard C function
\cfunction{system()
}, and has the
827 same limitations. Changes to
\code{posix.environ
},
\code{sys.stdin
},
828 etc.\ are not reflected in the environment of the executed command.
829 The return value is the exit status of the process encoded in the
830 format specified for
\function{wait()
}, except on Windows
95 and
98,
831 where it is always
\code{0}. Note that
\POSIX{} does not specify the
832 meaning of the return value of the C
\cfunction{system()
} function,
833 so the return value of the Python function is system-dependent.
834 Availability:
\UNIX{}, Windows.
837 \begin{funcdesc
}{times
}{}
838 Return a
5-tuple of floating point numbers indicating accumulated (CPU
840 times, in seconds. The items are: user time, system time, children's
841 user time, children's system time, and elapsed real time since a fixed
842 point in the past, in that order. See the
\UNIX{} manual page
843 \manpage{times
}{2} or the corresponding Windows Platform API
845 Availability:
\UNIX{}, Windows.
848 \begin{funcdesc
}{wait
}{}
849 Wait for completion of a child process, and return a tuple containing
850 its pid and exit status indication: a
16-bit number, whose low byte is
851 the signal number that killed the process, and whose high byte is the
852 exit status (if the signal number is zero); the high bit of the low
853 byte is set if a core file was produced.
854 Availability:
\UNIX{}.
857 \begin{funcdesc
}{waitpid
}{pid, options
}
858 Wait for completion of a child process given by process id
\var{pid
},
859 and return a tuple containing its process id and exit status
860 indication (encoded as for
\function{wait()
}). The semantics of the
861 call are affected by the value of the integer
\var{options
}, which
862 should be
\code{0} for normal operation.
863 Availability:
\UNIX{}.
865 If
\var{pid
} is greater than
\code{0},
\function{waitpid()
} requests
866 status information for that specific process. If
\var{pid
} is
867 \code{0}, the request is for the status of any child in the process
868 group of the current process. If
\var{pid
} is
\code{-
1}, the request
869 pertains to any child of the current process. If
\var{pid
} is less
870 than
\code{-
1}, status is requested for any process in the process
871 group
\code{-
\var{pid
}} (the absolute value of
\var{pid
}).
874 \begin{datadesc
}{WNOHANG
}
875 The option for
\function{waitpid()
} to avoid hanging if no child
876 process status is available immediately.
877 Availability:
\UNIX{}.
880 The following functions take a process stats code as returned by
881 \function{waitpid()
} as a parameter. They may be used to determine
882 the disposition of a process.
884 \begin{funcdesc
}{WIFSTOPPED
}{status
}
885 Return true if the process has been stopped.
886 Availability:
\UNIX{}.
889 \begin{funcdesc
}{WIFSIGNALED
}{status
}
890 Return true if the process exited due to a signal.
891 Availability:
\UNIX{}.
894 \begin{funcdesc
}{WIFEXITED
}{status
}
895 Return true if the process exited using the
\manpage{exit
}{2} system
897 Availability:
\UNIX{}.
900 \begin{funcdesc
}{WEXITSTATUS
}{status
}
901 If
\code{WIFEXITED(
\var{status
})
} is true, return the integer
902 parameter to the
\manpage{exit
}{2} system call. Otherwise, the return
903 value is meaningless.
904 Availability:
\UNIX{}.
907 \begin{funcdesc
}{WSTOPSIG
}{status
}
908 Return the signal which caused the process to stop.
909 Availability:
\UNIX{}.
912 \begin{funcdesc
}{WTERMSIG
}{status
}
913 Return the signal which caused the process to exit.
914 Availability:
\UNIX{}.
918 \subsection{Miscellanenous System Information
\label{os-path
}}
921 \begin{funcdesc
}{confstr
}{name
}
922 Return string-valued system configuration values.
923 \var{name
} specifies the configuration value to retrieve; it may be a
924 string which is the name of a defined system value; these names are
925 specified in a number of standards (
\POSIX, Unix95, Unix98, and
926 others). Some platforms define additional names as well. The names
927 known to the host operating system are given in the
928 \code{confstr_names
} dictionary. For configuration variables not
929 included in that mapping, passing an integer for
\var{name
} is also
931 Availability:
\UNIX{}.
933 If the configuration value specified by
\var{name
} isn't defined, the
934 empty string is returned.
936 If
\var{name
} is a string and is not known,
\exception{ValueError
} is
937 raised. If a specific value for
\var{name
} is not supported by the
938 host system, even if it is included in
\code{confstr_names
}, an
939 \exception{OSError
} is raised with
\constant{errno.EINVAL
} for the
943 \begin{datadesc
}{confstr_names
}
944 Dictionary mapping names accepted by
\function{confstr()
} to the
945 integer values defined for those names by the host operating system.
946 This can be used to determine the set of names known to the system.
950 \begin{funcdesc
}{sysconf
}{name
}
951 Return integer-valued system configuration values.
952 If the configuration value specified by
\var{name
} isn't defined,
953 \code{-
1} is returned. The comments regarding the
\var{name
}
954 parameter for
\function{confstr()
} apply here as well; the dictionary
955 that provides information on the known names is given by
956 \code{sysconf_names
}.
957 Availability:
\UNIX{}.
960 \begin{datadesc
}{sysconf_names
}
961 Dictionary mapping names accepted by
\function{sysconf()
} to the
962 integer values defined for those names by the host operating system.
963 This can be used to determine the set of names known to the system.
968 The follow data values are used to support path manipulation
969 operations. These are defined for all platforms.
971 Higher-level operations on pathnames are defined in the
972 \refmodule{os.path
} module.
975 \begin{datadesc
}{curdir
}
976 The constant string used by the OS to refer to the current directory,
977 e.g.\
\code{'.'
} for
\POSIX{} or
\code{':'
} for the Macintosh.
980 \begin{datadesc
}{pardir
}
981 The constant string used by the OS to refer to the parent directory,
982 e.g.\
\code{'..'
} for
\POSIX{} or
\code{'::'
} for the Macintosh.
985 \begin{datadesc
}{sep
}
986 The character used by the OS to separate pathname components,
987 e.g.\
\character{/
} for
\POSIX{} or
\character{:
} for the Macintosh.
988 Note that knowing this is not sufficient to be able to parse or
989 concatenate pathnames --- use
\function{os.path.split()
} and
990 \function{os.path.join()
} --- but it is occasionally useful.
993 \begin{datadesc
}{altsep
}
994 An alternative character used by the OS to separate pathname components,
995 or
\code{None
} if only one separator character exists. This is set to
996 \character{/
} on DOS and Windows systems where
\code{sep
} is a backslash.
999 \begin{datadesc
}{pathsep
}
1000 The character conventionally used by the OS to separate search patch
1001 components (as in
\envvar{PATH
}), e.g.\
\character{:
} for
\POSIX{} or
1002 \character{;
} for DOS and Windows.
1005 \begin{datadesc
}{defpath
}
1006 The default search path used by
\function{exec*p*()
} if the environment
1007 doesn't have a
\code{'PATH'
} key.
1010 \begin{datadesc
}{linesep
}
1011 The string used to separate (or, rather, terminate) lines on the
1012 current platform. This may be a single character,
1013 e.g.\
\code{'
\e n'
} for
\POSIX{} or
\code{'
\e r'
} for MacOS, or multiple
1014 characters, e.g.\
\code{'
\e r
\e n'
} for MS-DOS and MS Windows.