【发布时间】:2011-03-09 01:16:10
【问题描述】:
我需要使用标准输入/标准输出将 C 控制台程序(作为子进程)与 Python 接口。
C 程序更是如此:
tmp = 0.0;
printf("\ninput>>");
scanf_s("%f",&tmp);
printf ("\ninput was: %f",tmp);
tmp = 0.0;
printf("\ninput>>");
scanf_s("%f",&tmp);
printf ("\ninput was: %f",tmp);
tmp = 0.0;
printf("\ninput>>");
scanf_s("%f",&tmp);
printf ("\ninput was: %f",tmp);
使用 python 子进程模块我需要从这个程序中读取数据,写一些东西,然后再次读取等等。我使用了以下代码:
>>> p=subprocess.Popen(['C:\T.exe'],stdout=subprocess.PIPE,stdin=subprocess.PIPE)
>>> o,i=communicate('123\n')
o 的输出是:
input>>
input was: 123.000000
input>>
input was: 0.000000
input>>
input was: 0.000000
我希望子进程等待输入,直到另一个 o,i=communicate() 调用。为什么它在没有任何输入的情况下进行到程序的末尾?如何解决?
【问题讨论】:
-
这在过去也曾咬过我一次。 :)
标签: python c subprocess