I'm calling a child process using subprocess.Popen
(Python 2.x on a POSIX system). I want to be able to read the output of the child process using Python's readline()
file object function. However, the stream available in Popen.stdout
does not appear to have a readline()
method.
Using the idea from Python readline from pipe on Linux, I tried the following:
p = subprocess.Popen(
[sys.executable, "child.py"],
stdout=subprocess.PIPE)
status = os.fdopen(p.stdout.fileno())
while True:
s = status.readline()
if not s:
break
print s
However, the problem with this method is that both the p.stdout
object and the new status
object attempt to close the single file descriptor. This eventually results in:
close failed: [Errno 9] Bad file number
Is there a way to create a file object that "wraps" a previously created file-like object?