Capturing output into a python script from another python script with subprocess

ぐ巨炮叔叔 提交于 2020-01-07 02:57:20

问题


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.

来源:https://stackoverflow.com/questions/10836162/capturing-output-into-a-python-script-from-another-python-script-with-subprocess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!