代码之家  ›  专栏  ›  技术社区  ›  Rick

Linux命令行调用不返回它应该从操作系统?

  •  57
  • Rick  · 技术社区  · 15 年前

    我需要对linux进行一些命令行调用,并从中得到返回,但是按照下面的方式操作只是返回 0 当它应该返回一个时间值时,比如 00:08:19 ,我在常规命令行中测试完全相同的调用,它返回时间值 所以我很困惑我做错了什么,因为我认为这是如何在python中实现的。

    import os
    retvalue = os.system("ps -p 2993 -o time --no-headers")
    print retvalue
    
    10 回复  |  直到 8 年前
        1
  •  100
  •   Community Mohan Dere    9 年前

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

        2
  •  25
  •   JoshD    15 年前

    check_output 功能:

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

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

        3
  •  10
  •   JackDev    12 年前

    最简单的方法如下:

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

    这将作为列表返回

        4
  •  8
  •   vikkyhacks    12 年前

    您的代码返回 0 如果传递的命令执行成功,如果失败则为非零。以下程序适用于python2.7、pytho3和以上版本。试试这个代码。

    >>> import commands
    >>> ret = commands.getoutput("ps -p 2993 -o time --no-headers")
    >>> print ret
    
        5
  •  2
  •   Yauhen Yakimovich    12 年前

    是的,这是违反直觉的,看起来不太像python,但实际上它只是模仿unixapi设计,其中这些calld是C POSIX函数。检查 man 3 popen && man 3 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).
    
        6
  •  2
  •   Joe    10 年前

    我不能对这个问题发表评论 爱奥尼克汉堡 我将添加一个新条目。我很抱歉。 欧斯波本() 对于多个/复杂的命令(我的观点)是最好的,除了获得标准输出外,还可以获得返回值,如以下更复杂的多个命令:

    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就不会像 '*' 不正确。这在Python2.7中起作用。以下是示例输出:

          stdout:  ['fileFilter.py', 'fileProcess.py', 'file_access..py', 'myfile.py']
     returnValue:  0
    
        7
  •  1
  •   Coriolis Force    12 年前

    这是一个旧线程,但纯粹是使用 os.system ,以下是访问 ps 打电话。注意:它确实使用管道将数据写入磁盘上的文件。 OP并没有特别要求使用 .

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

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

        8
  •  1
  •   Asif Hasnain    11 年前

    根据您的需求,子流程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
    
        9
  •  0
  •   George    14 年前

    好吧,我相信最快的办法

    import os
    print(os.popen('command').readline())
    x = _
    print(x)
    
        10
  •  -1
  •   BusyProgrammer    9 年前
    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