Python 子进程堆叠

Python subprocess stacking

嗨,我在使用 python 编程时遇到了问题。 我想要做的是 运行 Linux 命令 Python.

这里我有命令“usbrh”这个 return 5 行如下。

# usbrh -t

  27.49
  27.49
    
  Temperature

我只想获取并显示第二行的值“27.49”。

所以我试了一下

import subprocess
runcmd = subprocess.call(["usbrh", "-t"])
print (runcmd[0:5])

然后得到如下错误。

# python sample.py
27.47
27.47

Temperature
Traceback (most recent call last):
  File "sample.py", line 3, in <module>
    print (runcmd[0:5])
TypeError: 'int' object has no attribute '__getitem__'

我正在使用 python 2.7 版本。 有人帮我吗?

According to documentation, subprocess.call returns 命令的退出状态,是一个int.

您必须使用 subprocess.check_output if you want to handle the command output.