我需要对Linux进行一些命令行调用并从中获得返回 0 何时应返回时间值,例如 00:08:19, ,我正在常规命令行中测试完全相同的调用,并返回时间值 00:08:19 因此,我对自己的做法感到困惑,因为我想这是如何在Python中做的。

import os
retvalue = os.system("ps -p 2993 -o time --no-headers")
print retvalue
有帮助吗?

解决方案

返回的是执行此命令的返回值。直接执行它时看到的是stdout中命令的输出。返回的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

因此: Popen和Python

其他提示

如果您只对流程的输出感兴趣,则最容易使用子过程' check_output 功能:


output = subprocess.check_output(["command", "arg1", "arg2"]);

然后输出将程序输出保存到Stdout。检查上面的链接以获取更多信息。

最简单的方法就是这样:

import os
retvalue = os.popen("ps -p 2993 -o time --no-headers").readlines()
print retvalue

这将作为列表返回

您的代码返回 0 如果命令的执行成功,则如果失败,则非零。以下程序在Python2.7上起作用,Haven在上面检查了3个版本。尝试此代码。

>>> 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).

我不能添加评论 离子堡 因为我没有“ 50个声誉”,所以我会添加一个新的条目。我很抱歉。 os.popen() 是多个/复杂命令(我的意见)以及获得返回值的最佳选择,除了获得以下更复杂的多个命令之外,还获得了返回值:

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文件 '文件' 以他们的名字在任何地方。这 [...] 是从每个条目中删除(剥离)newline字符的列表理解。这 回声$? 是显示最后执行的最后一个命令的返回状态的shell命令,该状态将是GREP命令和列表的最后一项。这 2>/dev/null 说打印 stderr格雷普 命令 /dev/null 因此它不会显示在输出中。这 'r' 之前 'ls' 命令是使用原始字符串,因此外壳不会像 '*' 错误。这在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 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top