Name
popen2, popen3, popen4
Synopsis
popen2(cmd,mode='t',bufsize=-1) popen3(cmd,mode='t',bufsize=-1) popen4(cmd,mode='t',bufsize=-1)
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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access