python os linux 命令,Python,os.system命令行调用(linux)不返回什么,它应该?

返回的是执行该命令的返回值。 在直接执行时看到的是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

如果您只对stream程的输出感兴趣,那么使用子stream程的check_output函数是最简单的:

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

然后输出将程序输出保存到stdout。 检查上面的链接了解更多信息。

如果传递的命令执行成功,你的代码将返回0如果失败则返回非零值。 下面的程序在python2.7上工作,没有检查过3和上面的版本。 试试这个代码。

>>> import commands >>> ret = commands.getoutput("ps -p 2993 -o time --no-headers") >>> print ret

是的,这是违反直觉的,看起来不是pythonic,但它实际上只是模仿了unix APIdevise,其中这些calld是C POSIX函数。 检查man 3 popen && man 3 system

用一些更方便的代码来replace我使用的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).

最简单的方法是这样的:

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

这将作为一个列表返回

我不能给IonicBurger添加评论,因为我没有“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]

这将列出所有名称中包含“file”字样的 python文件。 […]是从每个条目中删除(剥离)换行符的列表理解。 echo $? 是一个shell命令,用于显示最后执行的命令的返回状态,这将是本例中的grep命令和列表的最后一项。 2> / dev / null表示将grep命令的stderr打印到/ dev / null,以便它不会显示在输出中。 'ls'命令之前的'r'是使用原始string,因此shell不会错误地解释元字符'*' 。 这在Python 2.7中工作。 这里是示例输出:

stdout: ['fileFilter.py', 'fileProcess.py', 'file_access..py', 'myfile.py'] returnValue: 0

这是一个旧的线程,但纯粹使用os.system ,以下是访问由ps调用返回的数据的有效方法。 注意:它使用pipe道将数据写入磁盘上的文件。 而 OP没有特别要求使用os.system的解决scheme。

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

不知道为什么他们都返回零。

为了您的要求,subprocesspython模块的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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值