【发布时间】:2014-03-31 10:26:41
【问题描述】:
我正在使用以下函数在 Python 中运行命令:
def run_proc(cmd):
child = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = child.communicate()
returncode = child.returncode
return stdout, stderr, returncode
它一直运行良好,但是现在我尝试使用yes 程序将输出通过管道传输到标准输入。我尝试运行的命令如下:
yes '' | apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" dist-upgrade
但我相信它可以用一个通用示例代替,例如:
yes | head -3 | cat
我的问题是,如果我尝试运行任何包含yes | 的命令,上面的 subprocess.Popen 将包含错误消息:
yes: standard output: Broken pipe
yes: write error
对我来说,管道似乎仍然有效,从yes | head -3 | cat 的回答中可以看出:y y y。
我有以下问题:
- 即使 yes 报告错误,yes 管道是否仍然正常工作?
- 我该如何解决?
【问题讨论】:
标签: python linux shell command-line subprocess