Python 2.6:从Windows控制台应用程序读取数据。 (os.system?) [英] Python 2.6: reading data from a Windows Console application. (os.system?)
本文介绍了Python 2.6:从Windows控制台应用程序读取数据。 (os.system?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Windows控制台应用程序,返回一些文本。我想在Python脚本中读取该文本。我已经尝试使用os.system阅读它,但它不能正常工作。
I have a Windows console application that returns some text. I want to read that text in a Python script. I have tried reading it by using os.system, but it is not working properly.
import os
foo = os.system('test.exe')
假设test.exe返回bar,我想将变量foo设置为bar。但是会发生什么,它在控制台上打印bar,变量foo设置为0.
Assuming that test.exe returns "bar", I want the variable foo to be set to "bar". But what happens is, it prints "bar" on the console and the variable foo is set to 0.
我需要做什么来获得我想要的行为?
What do I need to do to get the behavior I want?
推荐答案
请使用子流程
import subprocess
foo = subprocess.Popen('test.exe',stdout=subprocess.PIPE,stderr=subprocess.PIPE)
http://docs.python .org / library / subprocess.html#module-subprocess
这篇关于Python 2.6:从Windows控制台应用程序读取数据。 (os.system?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文