[英]Python append specific times to list
I am trying to append 5 times to a list and calculate the average of the values. 我正在尝试将5次附加到列表中并计算值的平均值。 Since the
top
command is looping forever, I am not able to do so. 由于
top
命令将永远循环,因此我无法这样做。
#/usr/bin/python
import subprocess
values= []
cpu=subprocess.call(["top -b | awk '/sshd/ {print $9}'"], shell=True)
values.append(cpu)
print values
Output: 输出:
3.0
0.3
2.3
2.2
First, You need to use subprocess.check_output
if you want cpu
to have output of the command. 首先,如果您希望
cpu
具有命令的输出,则需要使用subprocess.check_output
。 .call
does not return output of the command. .call
不返回命令的输出。
Second you can specify -n 5
in you top
command. 其次,您可以在
top
命令中指定-n 5
。
#/usr/bin/python
import subprocess
cpu = subprocess.check_output(["top -n 5 -b | awk '/sshd/ {print $9}'"], shell=True)
values = cpu.split()
print values
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.