我正在使用python编写功能脚本脚本,我无法处理此命令行的结果:
os.system("ps aux -u %s | grep %s | grep -v 'grep' | awk '{print $2}'" % (username, process_name)
Run Code Online (Sandbox Code Playgroud)
它显示了我的pids但我不能用它作为List.
如果我测试:
pids = os.system("ps aux -u %s | grep %s | grep -v 'grep' | awk '{print $2}'" % (username, process_name)
print type(pids)
#Results
29719
30205
31037
31612
<type 'int'>
Run Code Online (Sandbox Code Playgroud)
为什么pids一个int?我该如何处理这个结果List?
陌生人:
print type(os.system("ps aux -u %s | grep %s | grep -v 'grep' | awk '{print $2}'" % (username, process_name))
Run Code Online (Sandbox Code Playgroud)
空无一物.我的控制台上没有写任何类型..
os.system不捕获它运行的命令的输出.为此,您需要使用subprocess.
from subprocess import check_output
out = check_output("your command goes here", shell=true)
Run Code Online (Sandbox Code Playgroud)
以上内容适用于Python 2.7.对于较老的蟒蛇,请使用:
import subprocess
p = subprocess.Popen("your command goes here", stdout=subprocess.PIPE, shell=True)
out, err = p.communicate()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3320 次 |
| 最近记录: |