python子进程和传入shell参数

python子进程和传入shell参数,python,shell,Python,Shell,我试图利用python的子进程运行一个下载文件的命令,但它需要一个参数才能继续。如果我单独运行该命令,它将提示您,如下所示: ./goro-new export --branch=testing --file=corp/goro.sites/test/meta.json Finding pages ......... The following pages will be exported from Goro to your local filesystem: /goro.sites/t

我试图利用python的子进程运行一个下载文件的命令,但它需要一个参数才能继续。如果我单独运行该命令,它将提示您,如下所示:

./goro-new export --branch=testing --file=corp/goro.sites/test/meta.json

Finding pages .........
The following pages will be exported from Goro to your local filesystem:

  /goro.sites/test/meta.json -> /usr/local/home/$user/schools/goro.sites/test/meta.json

Export pages? [y/N]: y
Exporting 1 pages ..............................................................................................................   0%  0:00:03

Exported 1 pages in 3.66281s.
我的问题是,如何回答导出页面部分中的“是/否”?我怀疑我需要将一个参数传递给我的子流程,但我对python相对来说是一个新手,所以我希望得到一些帮助。下面是我在python环境中测试的打印输出:

>>> import subprocess
>>> cmd = ['goro-new export --branch=test --file=corp/goro.sites/test/meta.json']
>>> p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE, stdin=subprocess.PIPE)
>>> out, err = p.communicate()
>>> print out
Finding pages .... 
The following pages will be exported from Goro to your local filesystem:
/goro.sites/test/meta.json -> /var/www/html/goro.sites/test/meta.json
Export pages? [y/N]: 

我如何传入“是/否”以便它可以继续进行?

您可以使用您已经在使用的函数,-函数并传递您想要的任何内容作为它的
输入
参数。我无法证实这一点,但它应该能给你一个想法:

>>> import subprocess
>>> cmd = ['goro-new export --branch=test --file=corp/goro.sites/test/meta.json']
>>> p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE, stdin=subprocess.PIPE)
>>> out, err = p.communicate(input="y")
>>> print out

如果您总是想回答“是”(我假设您会这样做),那么最简单的方法就是使用bash:
yes | python myscript.py
。要直接在python中执行此操作,可以使用
stdout=subprocess.PIPE
创建一个新的
subprocess.Popen
(例如,称为
yes
),并将
p
stdin
设置为等于
yes.stdout
。参考资料:

@user2690151请随意接受提供解决方案的答案。