相关疑难解决方法(0)

'yes'与子进程通信报告错误()

我使用以下函数在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
Run Code Online (Sandbox Code Playgroud)

它一直工作正常,但现在我正在尝试使用该yes程序将输出管道输出到stdin.我正在尝试运行的命令如下:

yes '' | apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" dist-upgrade
Run Code Online (Sandbox Code Playgroud)

但我相信它可以用一般的例子代替,例如:

yes | head -3 | cat
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果我尝试运行其中的任何命令yes |,上面的subprocess.Popen将包含错误消息:

yes: standard output: Broken pipe
yes: write error
Run Code Online (Sandbox Code Playgroud)

对我来说,似乎管道仍然可以工作,从yes | head -3 | cat答案中可以看出:y y y.

我有以下问题:

  1. 是的管道是否仍然可用,即使是报告错误?
  2. 我该如何解决?

python linux shell command-line subprocess

4
推荐指数
1
解决办法
2015
查看次数

标签 统计

command-line ×1

linux ×1

python ×1

shell ×1

subprocess ×1