os.system question

Mike Driscoll kyosohma at gmail.com
Thu Aug 7 22:07:13 CEST 2008


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



More information about the Python-list mailing list