1

Windowsには、stdinから読み取るプログラム(prog.exe)があります。Pythonでは、文字列をそのstdinへの入力としてパイプ処理したいと思います。どうやってするか?

何かのようなもの:

subprocess.check_output("echo {0} | myprog.exe".format(mystring)) 

または(引数をリストにするため)

subprocess.check_output("echo {0} | myprog.exe".format(mystring).split())

動作しないようです。それは私に与えました:

WindowsError: [Error 2] The system cannot find the file specified

また、StringIO(ファイルのようなオブジェクト)で「stdin」キーワードargを使用しようとしました

subprocess.check_output(["myprog.exe"], stdin=StringIO(mystring))

それでも運がない-check_outputはStringIOでは機能しません。

4

1 に答える 1

7

Popencommunicateメソッド(docs)を使用する必要があります。

proc = subprocess.Popen(["myprog.exe"], stdin=subprocess.PIPE)
stdout, stderr = proc.communicate('my input')
于 2012-07-20T00:58:20.253 に答える