Python中可能有这样的东西吗?

时间:2014-07-18 21:45:30

标签: python windows redirect io system

我有一个朋友给我的可执行文件(hello.exe)。此可执行文件创建另一个exe文件(test.exe)并通过另一个命令行窗口运行可执行文件。我想尝试编写一个运行hello.exe的脚本,并抓取test.exe的输出。我对python很新,熟悉系统调用和重定向输出,但这是一个特例,因为我没有直接访问test.exe。我面临的另一个问题是hello.exe在执行任何其他操作之前等待来自test.exe的键盘响应。我是否可以运行我的hello.exe脚本并从同一脚本中获取test.exe的输出?

import sys, string, os

os.system(hello.exe) > "test.txt" //Something like this?? Grab output from test.exe and put in test.txt
print "test" //this doesn't print until keystroke from terminal running test.exe

1 个答案:

答案 0 :(得分:2)

也许

os.system("hello.exe > test.txt")

或者

subprocess.Popen("hello.exe",stdout=open("test.txt","w")).communicate("\n")