【问题标题】:run terminal command but don't show output via python运行终端命令但不通过 python 显示输出
【发布时间】:2019-06-05 19:13:26
【问题描述】:

我试过了

a = subprocess.run(['apt download timeshhhh'],stdout = subprocess.PIPE, shell = True)

但这表明:

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

E: Unable to locate package timeshhhh

我只想要:

E: Unable to locate package timeshhhh 

怎么办?

【问题讨论】:

  • 在换行符处拆分并捕获最后一项。
  • 您的问题标题与正文不符。 “不通过python显示输出”与只想看到最后一行不同。

标签: python linux python-3.x command-line terminal


【解决方案1】:

这是 stderr 输出,您没有重定向。不想看的可以重定向到DEVNULL

a = subprocess.run(['apt download timeshhhh'],stdout = subprocess.PIPE, stderr = subprocess.DEVNULL, shell = True)

【讨论】:

  • WARNING 的输出不会 100% 出现在 stderr 上。
  • 如果不是来自管道,可能是。
猜你喜欢
  • 2018-01-11
  • 1970-01-01
  • 1970-01-01
  • 2015-06-23
  • 1970-01-01
  • 2017-09-04
  • 1970-01-01
  • 2020-09-07
  • 2017-01-25
相关资源
最近更新 更多