不使用文件的子进程管道stdin

2024-05-29 11:49:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个主进程,在其中运行一个子进程,stdin是我想要的管道。我知道我可以用文件来做:

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

是否有任何选项可以在没有实际硬盘文件的情况下使用自定义stdin管道?例如,是否有使用字符串列表的选项(每个列表元素都是一个换行符)?

我知道python子进程调用管道对象上的.readline()


Tags: 文件import列表管道进程mode选项stdin
1条回答
网友
1楼 · 发布于 2024-05-29 11:49:33

首先,使用subprocess.Popen-.call只是它的一个快捷方式,您需要访问Popen实例以便可以写入管道。然后将subprocess.PIPE标志作为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

相关问题 更多 >

    热门问题