os.system question

SamG mad.vijay at gmail.com
Thu Aug 7 02:58:08 EDT 2008


On Aug 7, 6:07 am, 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

Why dont you try commands module instead....

In [28]: import commands

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

In [30]: (a,b) = commands.getstatusoutput('whoami')

In [31]: print a
0

In [32]: print b
luma35




More information about the Python-list mailing list