17.5. popen2 — Subprocesses with accessible I/O streams

Deprecated since version 2.6:

Deprecated since version 2.6: This module is obsolete. Use the subprocess module. Check especially the Replacing Older Functions with the subprocess Module section.

This module allows you to spawn processes and connect to their input/output/error pipes and obtain their return codes under Unix and Windows.

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results. Using the subprocess module is preferable to using the popen2 module.

The primary interface offered by this module is a trio of factory functions. For each of these, if bufsize is specified, it specifies the buffer size for the I/O pipes. mode, if provided, should be the string 'b' or 't'; on Windows this is needed to determine whether the file objects should be opened in binary or text mode. The default value for mode is 't'.

On Unix, cmd may be a sequence, in which case arguments will be passed directly to the program without shell intervention (as with os.spawnv()). If cmd is a string it will be passed to the shell (as with os.system()).

The only way to retrieve the return codes for the child processes is by using the poll() or wait() methods on the Popen3 and Popen4 classes; these are only available on Unix. This information is not available when using the popen2(), popen3(), and popen4() functions, or the equivalent functions in the os module. (Note that the tuples returned by the os module’s functions are in a different order from the ones returned by the popen2 module.)

popen2.popen2(cmd[, bufsize[, mode]])

Executes cmd as a sub-process. Returns the file objects (child_stdout, child_stdin).

popen2.popen3(cmd[, bufsize[, mode]])

Executes cmd as a sub-process. Returns the file objects (child_stdout, child_stdin, child_stderr).

popen2.popen4(cmd[, bufsize[, mode]])

Executes cmd as a sub-process. Returns the file objects (child_stdout_and_stderr, child_stdin).

New in version 2.0:

New in version 2.0.