how to get the stdout of the script that os.system executes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wxy212
    New Member
    • Oct 2007
    • 5

    how to get the stdout of the script that os.system executes

    i know that in python, os.system(cmd) can be used to execute a script or command, and the return value is 0 if successful, rather than the standard output of the script that it executes.

    e.g.

    >>> a=os.system("ec ho hello")
    >>> a
    0 (rather than "hello")

    my question is how to direct the standard output into a variable in my python script.
  • KaezarRex
    New Member
    • Sep 2007
    • 52

    #2
    The only way I've been able to get output from os.system is to use ">" in the command to make the subshell write its output to a file. Then all I have to do is read the file.
    [CODE=python]>>> os.system("echo hello > output.txt")
    0
    >>> reader = open("output.tx t")
    >>> print reader.read()
    hello [/CODE]
    If you want to just append to the file instead of truncating then appending, use ">>" instead of ">".

    It would be nice if there was a more direct method, so hopefully someone else knows one. I'd be very interested to know what it is.

    Comment

    • Smygis
      New Member
      • Jun 2007
      • 126

      #3
      Originally posted by KaezarRex
      It would be nice if there was a more direct method, so hopefully someone else knows one. I'd be very interested to know what it is.
      Tere is the commands module.

      [code=python]
      >>> import commands
      >>> print commands.getout put("echo helllo")
      helllo
      >>> import commands
      >>> print commands.getout put("echo hello")
      hello
      >>> txt = commands.getout put("uname -a")
      >>> print txt
      Linux Bob 2.6.20-15-server #2 SMP Sun Apr 15 07:41:34 UTC 2007 i686 GNU/Linux
      [code]

      I also think os.popen works. but i have no experience with that. commands is quick and easy. i think os.popen is more advanced

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by wxy212
        i know that in python, os.system(cmd) can be used to execute a script or command, and the return value is 0 if successful, rather than the standard output of the script that it executes.

        e.g.

        >>> a=os.system("ec ho hello")
        >>> a
        0 (rather than "hello")

        my question is how to direct the standard output into a variable in my python script.
        Since you have not stated your OS, our freind Smygis has given a great solution (if you are running under *nix). I'd recommend, however, that you use the newer subprocess module.
        Originally posted by 2.4 Docs
        6.8 subprocess -- Subprocess management

        New in version 2.4.

        The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several other, older modules and functions, such as:


        os.system
        os.spawn*
        os.popen*
        popen2.*
        commands.*
        Last edited by bartonc; Oct 31 '07, 03:01 PM.

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by bartonc
          Since you have not stated your OS, our freind Smygis has given a great solution (if you are running under *nix). I'd recommend, however, that you use the newer subprocess module.
          Try:
          [CODE=python]import subprocess as sub
          retcode = sub.call(["ls", "-l"])[/CODE]And let us know how that works out for you.

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by bartonc
            Try:
            [CODE=python]import subprocess as sub
            retcode = sub.call(["ls", "-l"])[/CODE]And let us know how that works out for you.
            On Windows, this:[CODE=python]
            >>> import subprocess as sub
            >>> sub.call(['dir'], shell=True)[/CODE]worked in a Python command-line shell, but not in my IDE (Boa Constructor).

            Comment

            • Smygis
              New Member
              • Jun 2007
              • 126

              #7
              Originally posted by bartonc
              Try:
              [CODE=python]import subprocess as sub
              retcode = sub.call(["ls", "-l"])[/CODE]And let us know how that works out for you.
              Still, that prints the result. And does not return it.

              So no, Ill stick with commands.

              Whenever i need something like this. And thats only happend like two times.

              Comment

              • wxy212
                New Member
                • Oct 2007
                • 5

                #8
                Thanks for all of your replies.

                "var = commands.getout put(cmd)" seems to be good. However, it doesn't check if the command has been executed successfully.

                What i want is if it's executed successfully, then write the stdout to a variable, otherwise die with an error message..

                If i use "if os.system(cmd) != 0:" to check it before running "var=commands.g etoutput(cmd)", then the stdout will not only be written to variable var, but also pop on the prompt screen, which is not what I like..

                Does anyone have some idea?

                Comment

                • Smygis
                  New Member
                  • Jun 2007
                  • 126

                  #9
                  Originally posted by wxy212
                  Thanks for all of your replies.

                  "var = commands.getout put(cmd)" seems to be good. However, it doesn't check if the command has been executed successfully.

                  What i want is if it's executed successfully, then write the stdout to a variable, otherwise die with an error message..

                  If i use "if os.system(cmd) != 0:" to check it before running "var=commands.g etoutput(cmd)", then the stdout will not only be written to variable var, but also pop on the prompt screen, which is not what I like..

                  Does anyone have some idea?
                  commands.getsta tusoutput

                  [code=python]
                  >>> fail = commands.getsta tusoutput("ps -q")
                  >>> notfail = commands.getsta tusoutput("ps -A")
                  >>> fail[0]
                  256
                  >>> len(fail[1]) # the output
                  1517
                  >>> notfail[0]
                  0
                  >>> len(notfail[1]) # the output
                  3408

                  [/code]

                  Comment

                  • wxy212
                    New Member
                    • Oct 2007
                    • 5

                    #10
                    Originally posted by Smygis
                    commands.getsta tusoutput

                    [code=python]
                    >>> fail = commands.getsta tusoutput("ps -q")
                    >>> notfail = commands.getsta tusoutput("ps -A")
                    >>> fail[0]
                    256
                    >>> len(fail[1]) # the output
                    1517
                    >>> notfail[0]
                    0
                    >>> len(notfail[1]) # the output
                    3408

                    [/code]


                    brilliant!! thanks a lot..

                    Comment

                    • ghostdog74
                      Recognized Expert Contributor
                      • Apr 2006
                      • 511

                      #11
                      note also that commands modules is Unix specific.( AFAIK , correct me if i am wrong)
                      i use this sometimes
                      Code:
                      import os
                      fin, fout = os.popen4(cmd)
                      print fout.read() #standard out

                      Comment

                      • Smygis
                        New Member
                        • Jun 2007
                        • 126

                        #12
                        Originally posted by ghostdog74
                        note also that commands modules is Unix specific.( AFAIK , correct me if i am wrong)
                        i use this sometimes
                        Code:
                        import os
                        fin, fout = os.popen4(cmd)
                        print fout.read() #standard out

                        commands is a simple unix specific wrapper for os.popen:
                        The whole content of commands.py:

                        [code=python]
                        smygis@Bob:~$ cat /usr/lib/python2.5/commands.py
                        """Execute shell commands via os.popen() and return status, output.

                        Interface summary:

                        import commands

                        outtext = commands.getout put(cmd)
                        (exitstatus, outtext) = commands.getsta tusoutput(cmd)
                        outtext = commands.getsta tus(file) # returns output of "ls -ld file"

                        A trailing newline is removed from the output string.

                        Encapsulates the basic operation:

                        pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
                        text = pipe.read()
                        sts = pipe.close()

                        [Note: it would be nice to add functions to interpret the exit status.]
                        """

                        __all__ = ["getstatusoutpu t","getoutput", "getstatus"]

                        # Module 'commands'
                        #
                        # Various tools for executing commands and looking at their output and status.
                        #
                        # NB This only works (and is only relevant) for UNIX.


                        # Get 'ls -l' status for an object into a string
                        #
                        def getstatus(file) :
                        """Return output of "ls -ld <file>" in a string."""
                        return getoutput('ls -ld' + mkarg(file))


                        # Get the output from a shell command into a string.
                        # The exit status is ignored; a trailing newline is stripped.
                        # Assume the command will work with '{ ... ; } 2>&1' around it..
                        #
                        def getoutput(cmd):
                        """Return output (stdout or stderr) of executing cmd in a shell."""
                        return getstatusoutput (cmd)[1]


                        # Ditto but preserving the exit status.
                        # Returns a pair (sts, output)
                        #
                        def getstatusoutput (cmd):
                        """Return (status, output) of executing cmd in a shell."""
                        import os
                        pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
                        text = pipe.read()
                        sts = pipe.close()
                        if sts is None: sts = 0
                        if text[-1:] == '\n': text = text[:-1]
                        return sts, text


                        # Make command argument from directory and pathname (prefix space, add quotes).
                        #
                        def mk2arg(head, x):
                        import os
                        return mkarg(os.path.j oin(head, x))


                        # Make a shell command argument from a string.
                        # Return a string beginning with a space followed by a shell-quoted
                        # version of the argument.
                        # Two strategies: enclose in single quotes if it contains none;
                        # otherwise, enclose in double quotes and prefix quotable characters
                        # with backslash.
                        #
                        def mkarg(x):
                        if '\'' not in x:
                        return ' \'' + x + '\''
                        s = ' "'
                        for c in x:
                        if c in '\\$"`':
                        s = s + '\\'
                        s = s + c
                        s = s + '"'
                        return s
                        [/code]

                        Comment

                        Working...