os.system question
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Wed Aug 6 21:23:19 EDT 2008
On Wed, 06 Aug 2008 21:07:40 -0400, Kevin Walzer 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?
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('whoami', 'r').read()
Also look at the popen2 module.
--
Steven
More information about the Python-list
mailing list