Capturing output into a python script from another

2019-09-10 02:11发布

问题:

This is running on Windows Vista Home 32b with Service Pack 2 I am actively testing it on Mac, and Linux

I have one python script [A] that uses subprocess to run another python script [B]

process = subprocess.Popen(action, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                           bufsize=-1)
out = process.stdout.readline()
print out

I use [A] to run [B]

[B] comes from the command line, so can be any script what so ever

when [B] is a the following python script

#file = 'bufftest.py"
msg = "X"
x = 1024
y = 1024
z = 1
while (z < y):
    print msg * x
    z = z + 1

both running from the command line, and from [A], it produces, all the output

when [B] is a program that I didn't write, and I cannot show code for, run at the command line it produces fifteen lines of output however, when run from [A], it produces only the first line

The program I didn't write prints output like this

sys.stdout.write("Line01")
# 02-14
sys.stdout.write("Line15")

It originally printed output with print, sys,stdout.write() And I address sys.stdoutput.flush() to try to resolve this.

Thank you.