execute commands and return output

Discussion in 'Python' started by billiejoex, Sep 10, 2005.

  1. billiejoex

    billiejoex Guest

    Hi all. I'm searching for a portable (working on *nix and win32) function
    that executes a system command and encapsulate its output into a string.
    Searching for the web I found this:

    os.popen('command').read()

    It is perfect but when che command return an error the funciotn returns an
    empy string.
    Does it is possible to return stdout and stderr too?

    Best regards
     
    billiejoex, Sep 10, 2005
    #1
    1. Advertising

  2. billiejoex

    tiissa Guest

    billiejoex wrote:
    > Hi all. I'm searching for a portable (working on *nix and win32) function
    > that executes a system command and encapsulate its output into a string.
    > Searching for the web I found this:
    >
    > os.popen('command').read()
    >
    > It is perfect but when che command return an error the funciotn returns an
    > empy string.
    > Does it is possible to return stdout and stderr too?


    You may want to look at the subprocess [1] module and its Popen class [2].

    [1] http://python.org/doc/2.4.1/lib/module-subprocess.html
    [2] http://python.org/doc/2.4.1/lib/node234.html
     
    tiissa, Sep 10, 2005
    #2
    1. Advertising

  3. billiejoex wrote:
    > Hi all. I'm searching for a portable (working on *nix and win32) function
    > that executes a system command and encapsulate its output into a string.
    > Searching for the web I found this:
    >
    > os.popen('command').read()
    >
    > It is perfect but when che command return an error the funciotn returns an
    > empy string.
    > Does it is possible to return stdout and stderr too?


    Use subprocess:

    from subprocess import Popen, PIPE
    proc = Popen(['command', 'arg', 'arg'], stdout=PIPE, stderr=PIPE)
    return_code = proc.wait()
    if return_code == 0:
    print "Success:\n%s" % proc.stdout.read()
    else:
    print "Failure %s:\n%s" % (return_code, proc.stderr.read())
     
    Leif K-Brooks, Sep 10, 2005
    #3
  4. "billiejoex" wrote:

    > Hi all. I'm searching for a portable (working on *nix and win32) function that executes a system command and encapsulate its
    > output into a string.
    > Searching for the web I found this:
    >
    > os.popen('command').read()
    >
    > It is perfect but when che command return an error the funciotn returns an empy string.
    > Does it is possible to return stdout and stderr too?


    see the variations popen2, popen3 and popen4:

    http://docs.python.org/lib/os-newstreams.html

    or use the subprocess module:

    http://docs.python.org/lib/module-subprocess.html

    </F>
     
    Fredrik Lundh, Sep 10, 2005
    #4
  5. billiejoex

    billiejoex Guest

    Thank you for your help but I'm searching a different way.
    Moreover it doesn't work always (for exaple: try a 'dir' command).
    Because of I'm implementing a remote shell the
    [[os.popen('command').read()]] rapresents the best for me because it can
    also accepts arguments direclty (for example:
    os.popen('netstat -a -n -o').read() and this is a great advantage.
    I was looking at sys.stdout and sys.stderr. Can they be helpful?

    Cheers
    > Use subprocess:
    >
    > from subprocess import Popen, PIPE
    > proc = Popen(['command', 'arg', 'arg'], stdout=PIPE, stderr=PIPE)
    > return_code = proc.wait()
    > if return_code == 0:
    > print "Success:\n%s" % proc.stdout.read()
    > else:
    > print "Failure %s:\n%s" % (return_code, proc.stderr.read())
     
    billiejoex, Sep 10, 2005
    #5
  6. Hi !

    Look (the doc for) Popen2, Popen3, Popen4 & Subprocess

    @-salutations

    Michel Claveau
     
    Do Re Mi chel La Si Do, Sep 10, 2005
    #6
  7. "billiejoex" wrote:

    > Moreover it doesn't work always (for exaple: try a 'dir' command).


    why use os.system("dir") when Python already offers things
    like os.listdir, os.walk, and glob.glob ?

    </F>
     
    Fredrik Lundh, Sep 10, 2005
    #7
  8. billiejoex wrote:
    > Thank you for your help but I'm searching a different way.
    > Moreover it doesn't work always (for exaple: try a 'dir' command).
    > Because of I'm implementing a remote shell the
    > [[os.popen('command').read()]] rapresents the best for me because it can
    > also accepts arguments direclty (for example:
    > os.popen('netstat -a -n -o').read() and this is a great advantage.


    If you really need shell evaluation, try subprocess.Popen('foo',
    shell=True) instead.
     
    Leif K-Brooks, Sep 10, 2005
    #8
  9. billiejoex

    billiejoex Guest

    Thanks for suggestions.
    I'll try one of these solutions soon.
     
    billiejoex, Sep 10, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DB
    Replies:
    2
    Views:
    1,424
    Eliyahu Goldin
    Jun 22, 2005
  2. jlc488
    Replies:
    1
    Views:
    528
    Andrew Thompson
    Jan 21, 2005
  3. Ben Pfaff

    Re: man pages for C commands (GCC commands)

    Ben Pfaff, Jun 24, 2003, in forum: C Programming
    Replies:
    4
    Views:
    3,977
    Thomas Stegen
    Jun 28, 2003
  4. Tim Stanka
    Replies:
    1
    Views:
    807
    Jeff Epler
    Aug 2, 2004
  5. Replies:
    8
    Views:
    270
    Woody Peterson
    Sep 12, 2008
Loading...

Share This Page