os.system question
Asun Friere
afriere at yahoo.co.uk
Mon Aug 11 22:28:13 EDT 2008
On Aug 8, 6:07 am, Mike Driscoll <kyoso... at gmail.com> wrote:
> On Aug 6, 8:07 pm, Kevin Walzer <k... at codebykevin.com> wrote:
>
> > >>> import os
> > >>> foo = os.system('whoami')
> > 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.Popen 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).stdout.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('whoami').read()".
More information about the Python-list
mailing list