我需要对 linux 进行一些命令行调用并从中获取返回值,但是如下执行只是0
在它应该返回时间值时返回,例如00:08:19
,我正在常规命令行中测试完全相同的调用并返回时间价值00:08:19
,所以我对自己做错了什么感到困惑,因为我认为这是如何在 python 中做到这一点。
import os
retvalue = os.system("ps -p 2993 -o time --no-headers")
print retvalue
我需要对 linux 进行一些命令行调用并从中获取返回值,但是如下执行只是0
在它应该返回时间值时返回,例如00:08:19
,我正在常规命令行中测试完全相同的调用并返回时间价值00:08:19
,所以我对自己做错了什么感到困惑,因为我认为这是如何在 python 中做到这一点。
import os
retvalue = os.system("ps -p 2993 -o time --no-headers")
print retvalue
返回的是执行该命令的返回值。您在直接执行时看到的是标准输出中命令的输出。返回 0 表示执行没有错误。
使用 popen 等来捕获输出。
沿着这条线的一些事情:
import subprocess as sub
p = sub.Popen(['your command', 'arg1', 'arg2', ...],stdout=sub.PIPE,stderr=sub.PIPE)
output, errors = p.communicate()
print output
或者
import os
p = os.popen('command',"r")
while 1:
line = p.readline()
if not line: break
print line
ON SO : Popen 和 python
如果您只对流程的输出感兴趣,最简单的方法是使用子流程的check_output函数:
output = subprocess.check_output(["command", "arg1", "arg2"]);
然后输出将程序输出保存到标准输出。检查上面的链接以获取更多信息。
最简单的方法是这样的:
import os
retvalue = os.popen("ps -p 2993 -o time --no-headers").readlines()
print retvalue
这将作为列表返回
Your code returns 0
if the execution of the commands passed is successful and non zero if it fails. The following program works on python2.7, haven checked 3 and versions above. Try this code.
>>> import commands
>>> ret = commands.getoutput("ps -p 2993 -o time --no-headers")
>>> print ret
是的,它违反直觉并且看起来不是很pythonic,但它实际上只是模仿了 unix API 设计,其中这些被称为 C POSIX 函数。检查man 3 popen
&&man 3 system
替换我使用的os.system更方便的代码段:
from subprocess import (PIPE, Popen)
def invoke(command):
'''
Invoke command as a new system process and return its output.
'''
return Popen(command, stdout=PIPE, shell=True).stdout.read()
result = invoke('echo Hi, bash!')
# Result contains standard output (as you expected it in the first place).
我无法向IonicBurger添加评论,因为我没有“50 声望”,所以我将添加一个新条目。我很抱歉。 os.popen()最适用于多个/复杂的命令(我的观点),并且除了像以下更复杂的多个命令一样获取 stdout 之外,还用于获取返回值:
import os
out = [ i.strip() for i in os.popen(r"ls *.py | grep -i '.*file' 2>/dev/null; echo $? ").readlines()]
print " stdout: ", out[:-1]
print "returnValue: ", out[-1]
这将列出所有名称中包含“文件”一词的所有 python 文件。[ ...]是一个列表理解,用于从每个条目中删除(去除)换行符。回声 $ ? 是一个 shell 命令,用于显示最后执行的命令的返回状态,该命令将是 grep 命令和本示例中列表的最后一项。2>/dev/null表示将grep命令的 stderr 打印到/ dev / null,因此它不会显示在输出中。'ls'命令之前的' r'是使用原始字符串,因此 shell 不会解释像'*'这样的元字符不正确。这适用于 python 2.7。这是示例输出:
stdout: ['fileFilter.py', 'fileProcess.py', 'file_access..py', 'myfile.py']
returnValue: 0
这是一个旧线程,但纯粹使用os.system
,以下是访问ps
调用返回的数据的有效方式。注意:它确实使用管道将数据写入磁盘上的文件。并且OP 并没有特别要求使用os.system
.
>>> os.system("ps > ~/Documents/ps.txt")
0 #system call is processed.
>>> os.system("cat ~/Documents/ps.txt")
PID TTY TIME CMD
9927 pts/0 00:00:00 bash
10063 pts/0 00:00:00 python
12654 pts/0 00:00:00 sh
12655 pts/0 00:00:00 ps
0
因此,
>>> os.system("ps -p 10063 -o time --no-headers > ~/Documents/ps.txt")
0
>>> os.system("cat ~/Documents/ps.txt")
00:00:00
0
不知道为什么它们都返回零。
根据您的要求,子进程 python 模块的 Popen 函数就是答案。例如,
import subprocess
..
process = subprocess.Popen("ps -p 2993 -o time --no-headers", stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
print stdout
好的,我相信最快的方式
import os
print(os.popen('command').readline())
x = _
print(x)
using commands module
import commands
"""
Get high load process details
"""
result = commands.getoutput("ps aux | sort -nrk 3,3 | head -n 1")
print result -- python 2x
print (result) -- python 3x