2024-05-29 11:49:33 发布
网友
我有一个主进程,在其中运行一个子进程,stdin是我想要的管道。我知道我可以用文件来做:
import subprocess subprocess.call('shell command', stdin=open('somefile','mode'))
是否有任何选项可以在没有实际硬盘文件的情况下使用自定义stdin管道?例如,是否有使用字符串列表的选项(每个列表元素都是一个换行符)?
我知道python子进程调用管道对象上的.readline()。
.readline()
首先,使用subprocess.Popen-.call只是它的一个快捷方式,您需要访问Popen实例以便可以写入管道。然后将subprocess.PIPE标志作为stdinkwarg传递。类似于:
subprocess.Popen
.call
Popen
subprocess.PIPE
stdin
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
首先,使用
subprocess.Popen
-.call
只是它的一个快捷方式,您需要访问Popen
实例以便可以写入管道。然后将subprocess.PIPE
标志作为stdin
kwarg传递。类似于:http://docs.python.org/2/library/subprocess.html#subprocess.PIPE
相关问题 更多 >
编程相关推荐