os.system question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kevin Walzer

    os.system question

    >>import os
    >>foo = os.system('whoa mi')
    kevin
    >>print foo
    0
    >>>
    The standard output of the system command 'whoami' is my login name. Yet
    the value of the 'foo' object is '0,' not 'kevin.' How can I get the
    value of 'kevin' associated with foo?

    --
    Kevin Walzer
    Code by Kevin

  • Steven D'Aprano

    #2
    Re: os.system question

    On Wed, 06 Aug 2008 21:07:40 -0400, Kevin Walzer wrote:
    >>>import os
    >>foo = os.system('whoa mi')
    kevin
    >>print foo
    0
    >>>
    >>>
    The standard output of the system command 'whoami' is my login name. Yet
    the value of the 'foo' object is '0,' not 'kevin.' How can I get the
    value of 'kevin' associated with foo?

    That's because os.system captures the return code of the system call,
    which is 0 in this case because whoami succeeded. Meanwhile whoami
    printed its result to standard output, as normal.

    What you want is os.popen('whoam i', 'r').read()

    Also look at the popen2 module.



    --
    Steven

    Comment

    • Larry Wang

      #3
      Re: os.system question

      os.system() simply executes the command in a subshell, and returns the
      command's exit status which in your case is '0'. If you need to capture the
      stdout, stderr, etc. stuff, subprocess module is preferred which offers more
      powerful functionalities over os.system().

      Nessus

      "Kevin Walzer" <kw@codebykevin .comwrote in message
      news:1c5bf$489a 4add$4275d90a$1 5022@FUSE.NET.. .
      >import os
      >foo = os.system('whoa mi')
      kevin
      >print foo
      0
      >>
      >
      The standard output of the system command 'whoami' is my login name. Yet
      the value of the 'foo' object is '0,' not 'kevin.' How can I get the value
      of 'kevin' associated with foo?
      >
      --
      Kevin Walzer
      Code by Kevin
      http://www.codebykevin.com

      Comment

      • SamG

        #4
        Re: os.system question

        On Aug 7, 6:07 am, Kevin Walzer <k...@codebykev in.comwrote:
         >>import os
         >>foo = os.system('whoa mi')
        kevin
         >>print foo
        0
         >>>
        >
        The standard output of the system command 'whoami' is my login name. Yet
        the value of the 'foo' object is '0,' not 'kevin.' How can I get the
        value of 'kevin' associated with foo?
        >
        --
        Kevin Walzer
        Code by Kevinhttp://www.codebykevin .com
        Why dont you try commands module instead....

        In [28]: import commands

        In [29]: dir(commands)
        Out[29]:
        ['__all__',
        '__builtins__',
        '__doc__',
        '__file__',
        '__name__',
        'getoutput',
        'getstatus',
        'getstatusoutpu t',
        'mk2arg',
        'mkarg']

        In [30]: (a,b) = commands.getsta tusoutput('whoa mi')

        In [31]: print a
        0

        In [32]: print b
        luma35

        Comment

        • Kevin Walzer

          #5
          Re: os.system question

          SamG wrote:
          >
          Why dont you try commands module instead....
          >
          Thank you! That was just what I needed.



          --
          Kevin Walzer
          Code by Kevin

          Comment

          • Eric Wertman

            #6
            Re: os.system question

            In your case you could also use the os.environ dictionary:

            import os
            print os.environ['USER']

            Comment

            • Mike Driscoll

              #7
              Re: os.system question

              On Aug 6, 8:07 pm, Kevin Walzer <k...@codebykev in.comwrote:
               >>import os
               >>foo = os.system('whoa mi')
              kevin
               >>print foo
              0
               >>>
              >
              The standard output of the system command 'whoami' is my login name. Yet
              the value of the 'foo' object is '0,' not 'kevin.' How can I get the
              value of 'kevin' associated with foo?
              >
              --
              Kevin Walzer
              Code by Kevinhttp://www.codebykevin .com
              Just an FYI, the os.system, commands, and os.popen* stuff is
              deprecated in Python 2.4+. The subprocess module is supposed to
              replace them all.

              See the docs for more info: http://docs.python.org/lib/module-subprocess.html

              I think the equivalent is subprocess.Pope n with the communicate()
              method:



              HTH

              Mike

              Comment

              • Brad

                #8
                Re: os.system question

                Kevin Walzer wrote:
                >>import os
                >>foo = os.system('whoa mi')
                kevin
                >>print foo
                0
                >>>
                >
                The standard output of the system command 'whoami' is my login name. Yet
                the value of the 'foo' object is '0,' not 'kevin.' How can I get the
                value of 'kevin' associated with foo?
                >
                Hi Kevin, check out popen. It should let you do what you want.

                import os
                name=os.popen(' whoami').read()

                Comment

                • Asun Friere

                  #9
                  Re: os.system question

                  On Aug 8, 6:07 am, Mike Driscoll <kyoso...@gmail .comwrote:
                  On Aug 6, 8:07 pm, Kevin Walzer <k...@codebykev in.comwrote:
                  >
                  >>import os
                  >>foo = os.system('whoa mi')
                  kevin
                  >>print foo
                  0
                  >>>
                  >
                  The standard output of the system command 'whoami' is my login name. Yet
                  the value of the 'foo' object is '0,' not 'kevin.' How can I get the
                  value of 'kevin' associated with foo?
                  >
                  --
                  Kevin Walzer
                  Code by Kevinhttp://www.codebykevin .com
                  >
                  Just an FYI, the os.system, commands, and os.popen* stuff is
                  deprecated in Python 2.4+. The subprocess module is supposed to
                  replace them all.
                  >
                  See the docs for more info:http://docs.python.org/lib/module-subprocess.html
                  >
                  I think the equivalent is subprocess.Pope n with the communicate()
                  method:
                  >

                  >
                  HTH
                  >
                  Mike
                  Something like this should work,
                  >>from subprocess import Popen, PIPE
                  >>me = Popen('whoami', stdout=PIPE, shell=True).std out.read()
                  but if I was in a hurry to find out who I was I would be tempted still
                  to use the deprecated "os.popen('whoa mi').read()".

                  Comment

                  • norseman

                    #10
                    Re: os.system question

                    Asun Friere wrote:
                    On Aug 8, 6:07 am, Mike Driscoll <kyoso...@gmail .comwrote:
                    >On Aug 6, 8:07 pm, Kevin Walzer <k...@codebykev in.comwrote:
                    >>
                    >> >>import os
                    >> >>foo = os.system('whoa mi')
                    >>kevin
                    >> >>print foo
                    >>0
                    >> >>>
                    >>The standard output of the system command 'whoami' is my login name. Yet
                    >>the value of the 'foo' object is '0,' not 'kevin.' How can I get the
                    >>value of 'kevin' associated with foo?
                    >>--
                    >>Kevin Walzer
                    >>Code by Kevinhttp://www.codebykevin .com
                    >Just an FYI, the os.system, commands, and os.popen* stuff is
                    >deprecated in Python 2.4+. The subprocess module is supposed to
                    >replace them all.
                    >>
                    >See the docs for more info:http://docs.python.org/lib/module-subprocess.html
                    >>
                    >I think the equivalent is subprocess.Pope n with the communicate()
                    >method:
                    >>
                    >http://docs.python.org/lib/node532.html
                    >>
                    >HTH
                    >>
                    >Mike
                    >
                    Something like this should work,
                    >
                    >>>from subprocess import Popen, PIPE
                    >>>me = Popen('whoami', stdout=PIPE, shell=True).std out.read()
                    >
                    but if I was in a hurry to find out who I was I would be tempted still
                    to use the deprecated "os.popen('whoa mi').read()".
                    I assume Asun added the above line. I agree with whoever wrote it. SLT
                    =============== ===========

                    Can we take a moment and look at what is happening here?

                    Python developers are creating an unwanted item!

                    Let's take a good look:

                    1) import os 9
                    2) name=os.popen(' whoami').read() 30

                    3) from subprocess import Popen, PIPE 34
                    4) me = Popen('whoami', stdout=PIPE, shell=True).std out.read() 59

                    While lines 1 and 3 are typically done once per module, line 3 would
                    seem to imply the need for making specific requests for sub-process and
                    associated and (required) other pieces. Lots of combinations to
                    remember, fumble, and have to re-research to do the simple.

                    Stats point up something too:
                    30/9 = 3.333...
                    59/30 = 1.966...

                    without any other factors such as normally additional lines of code for
                    the more verbose 'better' package it still will take at least twice as
                    long to code the same thing. Add Murphy's Law and the typos go through
                    the roof. Expect (after all is said and done) for the same process to
                    take 3 times as much clock time to complete the same project using the
                    padded up new and improved vs the concise.

                    This is not intuitive and it is not necessary and it most certainly is
                    not wanted. Not by the programmer, not by the client and not by the one
                    paying the paycheck. If 1&2 have a security hole - address it directly.
                    Do not add more confusion to help cover greater holes. Or to pad your
                    pockets because you are bored.

                    Somebody had to say it. Besides, as a trouble shooter I used to get
                    paid to locate and fire people that padded it up to their employers. The
                    things of which individuals can justify to themselves is truly amazing.

                    I still prefer assembly. It's all right there, right up front. No
                    compiler anomalies (accidental or intentional) to deal with.


                    Man the shelters Honey; major incoming expected momentarily. :)



                    Steve
                    norseman@hughes .net

                    Comment

                    • Steven D'Aprano

                      #11
                      Re: os.system question

                      On Tue, 12 Aug 2008 14:42:28 -0700, norseman wrote:
                      Can we take a moment and look at what is happening here?
                      >
                      Python developers are creating an unwanted item!
                      >
                      Let's take a good look:
                      >
                      1) import os 9
                      2) name=os.popen(' whoami').read() 30
                      >
                      3) from subprocess import Popen, PIPE 34
                      4) me = Popen('whoami', stdout=PIPE, shell=True).std out.read() 59
                      ....
                      I still prefer assembly. It's all right there, right up front. No
                      compiler anomalies (accidental or intentional) to deal with.
                      Please tell me more, I am very interested in this assembly of which you
                      speak. How would you write a program in assembly that does the same as
                      the above code, and will run on any major platform? I would love to see
                      the code. I expect it will be much shorter and more intuitive than the
                      Python code you dislike, leading to fewer errors and less padding of the
                      programmer's pockets at the expense of their employers.



                      --
                      Steven

                      Comment

                      • Steven D'Aprano

                        #12
                        Re: os.system question

                        On Mon, 11 Aug 2008 19:28:13 -0700, Asun Friere wrote:
                        Something like this should work,
                        >
                        >>>from subprocess import Popen, PIPE
                        >>>me = Popen('whoami', stdout=PIPE, shell=True).std out.read()
                        >
                        but if I was in a hurry to find out who I was I would be tempted still
                        to use the deprecated "os.popen('whoa mi').read()".
                        Is it really deprecated? Since when? I'm using Python 2.5 and it doesn't
                        raise any warnings or mention anything in the doc string.

                        The current documentation does say:

                        "The subprocess module provides more powerful facilities for spawning new
                        processes and retrieving their results; using that module is preferable
                        to using this function."




                        but that's not the same as deprecating os.popen.



                        --
                        Steven

                        Comment

                        • Wojtek Walczak

                          #13
                          Re: os.system question

                          Dnia 12 Aug 2008 22:58:22 GMT, Steven D'Aprano napisa³(a):
                          >but if I was in a hurry to find out who I was I would be tempted still
                          >to use the deprecated "os.popen('whoa mi').read()".
                          >
                          Is it really deprecated? Since when? I'm using Python 2.5 and it doesn't
                          raise any warnings or mention anything in the doc string.
                          I think that the deprecation warnings were added in Python 2.6.

                          --
                          Regards,
                          Wojtek Walczak,

                          Comment

                          • Asun Friere

                            #14
                            Re: os.system question

                            On Aug 13, 8:58 am, Steven D'Aprano <st...@REMOVE-THIS-
                            cybersource.com .auwrote:
                            On Mon, 11 Aug 2008 19:28:13 -0700, Asun Friere wrote:
                            >
                            but if I was in a hurry to find out who I was I would be tempted still
                            to use the deprecated "os.popen('whoa mi').read()".
                            >
                            Is it really deprecated? Since when? I'm using Python 2.5 and it doesn't
                            raise any warnings or mention anything in the doc string.
                            >
                            I should perhaps have put 'deprecated' in quotation marks? Note the
                            post I was responding to and my own stated preference. Though I
                            admit, I have been trying out Popen just recently.
                            The current documentation does say:
                            >
                            "The subprocess module provides more powerful facilities for spawning new
                            processes and retrieving their results; using that module is preferable
                            to using this function."
                            >

                            >
                            but that's not the same as deprecating os.popen.
                            >
                            Current documentation also states:

                            "[The subprocess] module intends to replace several other, older
                            modules and functions, such as: ... [inter alia] ... os.system,
                            os.popen*, commands.*"

                            Source code: Lib/subprocess.py 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 seve...


                            Which is also not exactly the same thing as deprecating os.popen, but
                            it does sound somehwat more ominous. One hopes the subprocess module
                            is not successful in realising its intentions.

                            I note 3.0 runs os.popen without complaint (and had thought to mention
                            that in my previous). Right now I'm wondering whether I should
                            install the beta 2.6 to see whether Wotjek is pulling our leg or
                            not. :)

                            Comment

                            • Wojtek Walczak

                              #15
                              Re: os.system question

                              Dnia Tue, 12 Aug 2008 23:38:56 -0700 (PDT), Asun Friere napisa³(a):
                              I note 3.0 runs os.popen without complaint (and had thought to mention
                              that in my previous). Right now I'm wondering whether I should
                              install the beta 2.6 to see whether Wotjek is pulling our leg or
                              not. :)
                              :) Checked it to make sure.

                              Python 2.6b2 (r26b2:65082, Aug 13 2008, 01:12:28)
                              [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
                              Type "help", "copyright" , "credits" or "license" for more information.
                              >>import popen2
                              __main__:1: DeprecationWarn ing: The popen2 module is deprecated. Use the subprocess module.
                              >>import os
                              >>print os.popen('whoam i').read()
                              gminick
                              >>print os.popen2('whoa mi')[1].read()
                              gminick
                              >>print os.popen2('whoa mi')[0].read()
                              __main__:1: DeprecationWarn ing: os.popen2 is deprecated. Use the subprocess module.
                              Traceback (most recent call last):
                              File "<stdin>", line 1, in <module>
                              IOError: [Errno 9] Bad file descriptor
                              >>>
                              The strange thing is that os.popen2 prints the deprecation warning
                              only when it's called inappropriately . Got to investigate it further.

                              os.popen won't return any deprecation warning in Py2.6beta2 probably
                              because it is defined in Lib/platform.py and not in Lib/os.py and
                              I guess that somebody has forgotten to add the warnings in there.

                              --
                              Regards,
                              Wojtek Walczak,

                              Comment

                              Working...