9

I've got a main process in which I run a subprocess, which stdin is what I want to pipe. I know I can do it using files:

import subprocess
subprocess.call('shell command', stdin=open('somefile','mode'))

Is there any option to use a custom stdin pipe WITHOUT actual hard drive files? Is there any option, for example, to use string list (each list element would be a newline)?

I know that python subprocess calls .readline() on the pipe object.

4

1 回答 1

5

首先,使用subprocess.Popen-.call只是它的一个快捷方式,您需要访问该Popen实例才能写入管道。然后将subprocess.PIPEflag 作为stdinkwarg 传递。就像是:

import subprocess
proc = subprocess.Popen('shell command', stdin=subprocess.PIPE)
proc.stdin.write("my data")

http://docs.python.org/2/library/subprocess.html#subprocess.PIPE

于 2013-08-04T19:28:01.483 回答