【问题标题】:Difference between subprocess Popen/call/check_output子进程 Popen/call/check_output 之间的区别
【发布时间】:2017-05-28 17:02:28
【问题描述】:

大家好,谁能详细说说两者的区别

subprocess.Popen
subprocess.call
subprocess.check_output

如果可能的话,请解释两者之间的区别 x.readlines()x.communicate()?

即区别

import subprocess
from subprocess import PIPE
ls = subprocess.Popen(['ls','-l'], stdout=subprocess.PIPE)
**out = ls.stdout.readlines()**
print out

import subprocess
from subprocess import PIPE
ls = subprocess.Popen(['ls','-l'], stdout=subprocess.PIPE)
out = ls.communicate()
print out

【问题讨论】:

  • 感谢编辑帖子.. 我无法找到编辑帖子的方法
  • 您的帖子左下角有一个编辑链接。

标签: python subprocess


【解决方案1】:

callcheck_output(以及check_call)只是在底层调用Popen 的实用函数。

  • call返回子进程的退出码
  • 如果退出代码不为零,check_call 会引发 CalledProcessError 错误
  • check_output 与上面相同,但也返回输出。

readlinescommunicate 的区别在于readlines 只是在缓冲区(stdout)上创建的一个函数,而communicate 是一个进程类的方法,因此它可以处理不同的异常,您可以传递输入在其中,它会等待进程完成。

Read more here

【讨论】:

  • 感谢您的解释,最后一件事 check_output 可以与标准输入一起使用吗?
  • 有没有办法逐行获取 check_output 返回?它似乎要等待整个过程需要多长时间才能获得完整的输出,而不是每个 print() 一个一个
猜你喜欢
  • 2018-04-16
  • 1970-01-01
  • 2016-10-31
  • 2011-12-02
  • 2012-11-07
  • 2011-05-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多