2017-12-18 6 views
-1

subprocess.call (cmd, shell = True)을 사용할 때이 print 문의 끝 부분에 매달려있는 0을 어떻게 중지합니까?Subprocess.call이 반환 코드를 인쇄하지 못하게하는 방법은 무엇입니까?

print("The top five memory consumers on the system are:") 
print(subprocess.call('ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head -n 6', shell=True)) 

출력 :

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 

^문제 아이는

+3

음 - 인쇄물을 꺼내십시오. 당신은 그것을하는 사람입니다. 그러지 마. 'print (subprocess.call (...))'을'subprocess.call (...)'로 대체하십시오. –

답변

1

당신은 subprocess.check_output() 대신 subprocess.call() 사용할 수 있습니다

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) 

는 또한 check_output()의 결과가 바이트 같은이므로주의 개체이므로 .decode()에 전화해야합니다. 그걸로 문자열로 작업하기를 원한다면