我无法找到适合下列情况的解决方案 . 所以,基本上我想管道两个shell命令,并将一些输入数据(这里是 mypassword
)传递给第二个 . 这里有一些代码证明了这个问题:
import subprocess
import shlex
args1 = shlex.split("find /home/vasya/rmps/ -name *.rpm")
ps = subprocess.Popen(args1, stdout=subprocess.PIPE)
args2 = shlex.split("xargs rpmsign --addsign")
p2 = subprocess.Popen(args2, stdin=ps.stdout, stdout=subprocess.PIPE)
ps.stdout.close()
output = p2.communicate('mypassword\n')[0]
print(output)
当它运行时在控制台中询问: Enter pass phrase:
并停止,尽管我已经传递了密码短语(我的 mypassword
输入) .
我的错在哪里,我该如何解决?
1 回答
您的标准输入
xargs
是find
的输出,您也不能输入其他内容 .一种选择是将
find
的结果写入文件并使用xargs的-a
(--arg-file
)选项 . 这将允许您输入文件名,同时保持stdin
免费从您的python程序管道输入(通过communicate
)