我有一个产生实时数据的.exe
程序。我想在实时运行程序时提取输出,但是这是我第一次尝试,所以我需要帮助来接近这个。
我用下面的文字打开了它:
cmd = r'/Applications/StockSpy Realtime Stocks Quote.app/Contents/MacOS/StockSpy Realtime Stocks Quote'
import subprocess
with open('output.txt', 'wb') as f:
subprocess.check_call(cmd, stdout=f)
# to read line by line
with open('output.txt') as f:
for line in f:
print(line)
# output = qx(cmd)
,目的是存储输出。但是,它不保存任何输出,我得到一个空白的文本文件。
我通过以下代码保存了输出:
from subprocess import STDOUT, check_call as x
with open(os.devnull, 'rb') as DEVNULL, open('output.txt', 'wb') as f:
x(cmd, stdin=DEVNULL, stdout=f, stderr=STDOUT)
从我如何得到所有的输出从我的。exe使用子进程和Popen?
你想做的事情可以用python实现,使用这样的东西:
import subprocess
with subprocess.Popen(['/path/to/executable'], stdout=subprocess.PIPE) as proc:
data = proc.stdout.read() # the data variable will contain the
# what would usually be the output
"""Do something with data..."""