Python 如何防止subprocess.call打印返回码?

Python 如何防止subprocess.call打印返回码?,python,subprocess,Python,Subprocess,在使用subprocess.call(cmd,shell=True)时,如何防止该零挂在这个print语句的末尾 输出: The top five memory consumers on the system are: PID PPID CMD %MEM %CPU 807 1 /usr/bin/python -Es /usr/sb 3.1 0.0 615 555 /sbin/dhclient -d -q -sf /u

在使用subprocess.call(cmd,shell=True)时,如何防止该零挂在这个print语句的末尾

输出:

The top five memory consumers on the system are:
  PID  PPID CMD                         %MEM %CPU
  807     1 /usr/bin/python -Es /usr/sb  3.1  0.0
  615   555 /sbin/dhclient -d -q -sf /u  3.0  0.0
 1500   917 python                       1.7  0.0
 9921   917 python ./dkap_sysinfo.py     1.7  0.0
  556     1 /usr/sbin/rsyslogd -n        1.3  0.0
0

^问题子进程

您可以使用
子进程。请检查\u output()
而不是
子进程调用()


还要注意的是,
check\u output()
的结果是一个类似字节的对象,因此如果要将其作为字符串使用,则必须对其调用
.decode()

Um-删除打印。是你干的。所以,不要。将打印(子流程调用(…)替换为
子流程调用(…)
The top five memory consumers on the system are:
  PID  PPID CMD                         %MEM %CPU
  807     1 /usr/bin/python -Es /usr/sb  3.1  0.0
  615   555 /sbin/dhclient -d -q -sf /u  3.0  0.0
 1500   917 python                       1.7  0.0
 9921   917 python ./dkap_sysinfo.py     1.7  0.0
  556     1 /usr/sbin/rsyslogd -n        1.3  0.0
0
import subprocess

print("The top five memory consumers on the system are:")

cmd = "ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head -n 6"
result = subprocess.check_output(cmd, shell=True).decode()

lines = result.split("\n")
for line in lines:
    print(line)