Re: commands.getstatusoutput result is not command line exit value!!!

Discussion in 'Python' started by Fredrik Lundh, Oct 2, 2006.

  1. Hari Sekhon wrote:

    > I'm sorry, this may seem dense to you but I have to ask. What on earth
    > are you talking about?
    >
    > Why is it shifted 8 bits to the left? Why is there bitshifting at all?
    > Why doesn't commands give the same exit value as os.system() and the
    > unix cli?


    because that's how Unix's wait() operation returns the status code (as
    mentioned in the "commands" section of the library reference).

    you can use the os.WIFEXITED(status) and os.WEXITSTATUS(code) helpers to
    convert between wait return codes and signal numbers/exit codes. see:

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

    or you can forget about the obsolete "commands" module, and use the new
    subprocess module instead; e.g.

    def getstatusoutput(command):
    from subprocess import Popen, PIPE, STDOUT
    p = Popen(command, stdout=PIPE, stderr=STDOUT, shell=True)
    s = p.stdout.read()
    return p.wait(), s

    print getstatusoutput("ls -l /bin/ls")
    (0, '-rwxr-xr-x 1 root root 68660 Aug 12 2003 /bin/ls\n')

    the subprocess module is highly flexible; see the library reference for
    details.

    </F>
    Fredrik Lundh, Oct 2, 2006
    #1
    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. David Carroll
    Replies:
    2
    Views:
    624
    Peter Otten
    Sep 13, 2003
  2. n.o.
    Replies:
    0
    Views:
    335
  3. Replies:
    2
    Views:
    304
  4. Hari Sekhon
    Replies:
    0
    Views:
    321
    Hari Sekhon
    Oct 2, 2006
  5. Hari Sekhon
    Replies:
    1
    Views:
    829
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Oct 2, 2006
Loading...

Share This Page