popen2, popen3, popen4



Each of these functions runs the string command cmd in a new process P, and returns a tuple of file-like objects that wrap pipes to P’s standard input and from P’s standard output and standard error. mode must be 't' to get file-like objects in text mode, or 'b' to get them in binary mode. On Windows, bufsize must be -1. On Unix, bufsize has the same meaning as for Python’s built-in open function, covered in Chapter 10.

popen2 returns a pair ( fi,fo ), where fi wraps P’s standard input (so the calling process can write to fi) and fo wraps P’s standard output (so the calling process can read from fo). popen3 returns a tuple with three items ( fi,fo,fe ), where fe wraps P’s standard error (so the calling process can read from fe). popen4 returns a pair ( fi,foe ), where foe wraps both P’s standard output and error (so the calling process can read from foe). While popen3 is in a sense the most general of the three functions, it can be difficult to coordinate your reading from fo and fe. popen2 is simpler to use than popen3 when it’s okay for cmd’s standard error to go to the same destination as your own process’s standard error, and popen4 is simpler when it’s okay for cmd’s standard error and output to be mixed with each other.

File objects fi, fo, fe, and foe are all normal ones, without the special semantics of the close method as covered for function ...

Get Python in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.