要传递的Python子流程是/否

要传递的Python子流程是/否,python,python-2.7,Python,Python 2.7,我的命令要求输入“是”。如何自动传递此答案 我使用了下面的代码,但它不起作用 from subprocess import Popen, PIPE foo_proc = Popen([cmd], stdin=PIPE, stdout=PIPE) yes_proc = Popen(['YES'], stdout=foo_proc.stdin) foo_output = foo_proc.communicate()[0] yes_proc.wait() 我得到的错误: echo: write er

我的命令要求输入“是”。如何自动传递此答案

我使用了下面的代码,但它不起作用

from subprocess import Popen, PIPE
foo_proc = Popen([cmd], stdin=PIPE, stdout=PIPE)
yes_proc = Popen(['YES'], stdout=foo_proc.stdin)
foo_output = foo_proc.communicate()[0]
yes_proc.wait()
我得到的错误:

echo: write error: Broken pipe

PS:我正在使用python2.7,我建议直接在Popen语句中使用简单的管道命令。您可以使用以下命令:

foo_proc = Popen(['echo' , 'yes', '|', cmd])
您需要使用
shell=True
,例如

foo_proc = subprocess.Popen(['echo yes | conda install pyqtgraph'], shell=True)

有关更多详细信息,请参阅

有关CLI交互,我将使用Python模块来控制类似伪终端的交互程序

它将允许您执行类似和更复杂的任务:

# This connects to the openbsd ftp site and
# downloads the recursive directory listing.
import pexpect
child = pexpect.spawn('ftp ftp.openbsd.org')
child.expect('Name .*: ')
child.sendline('anonymous')
child.expect('Password:')
child.sendline('noah@example.com')
child.expect('ftp> ')
child.sendline('lcd /tmp')
child.expect('ftp> ')
child.sendline('cd pub/OpenBSD')
child.expect('ftp> ')
child.sendline('get README')
child.expect('ftp> ')
child.sendline('bye')

你可以找到它的文档。

我得到了错误:foo_proc=Popen(['echo YES |',cmd],stdin=PIPE,stdout=PIPE)文件“/usr/lib64/python2.7/subprocess.py”,第390行,在init errread,errwrite)文件“/usr/lib64/python2.7/subprocess.py”,第1024行,在“执行”中,在“孩子抚养孩子”[Errno 2]没有这样的文件或目录,简单地“conda install-y pyqtgraph”不是更容易吗?啊,对不起,我刚刚明白这是一个例子。