pythonのsubprocessを使って出力を読み取ってからそれを解析して入力するということがしたいのですがうまくできません。
どうやら入力してから出力結果を取り出すことはできるようなのですが、その逆は可能なのでしょうか?
c
1#include <stdio.h> 2 3int main(int argc, char const *argv[]) { 4 int ans; 5 printf("1 + 1 ="); 6 scanf("%d",&ans); 7 if(ans == 2) 8 printf("correct\n"); 9 else 10 printf("NO\n"); 11 return 0; 12} 13
sub.c
例えば上のプログラムをpythonから実行した時に「1 + 1 =」という文字列を取得してから、入力することがしたいです。試しに作ったpythonスクリプトを載せておきますので、どこをどのようにすれば思ったとおりの動きをするのか教えてください。
環境はlinuxでcのコンパイラはgcc,pythonは2.7.9です。
python
1import subprocess 2 3command = ['./sub'] 4proc = subprocess.Popen(command,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE) 5 6data = proc.stdout.read() 7print data 8proc.stdin.write('2\n')
あなたの回答
tips
プレビュー