[Tutor] executing a program and retrieving it's output
Daniel Yoo
dyoo@hkn.EECS.Berkeley.EDU
Fri, 28 Jul 2000 09:30:31 -0700 (PDT)
On Fri, 28 Jul 2000, Ben Beuchler wrote:
> I'm familiar with using os.popen('command') to execute a command and
> retrieve it's output. However, I rather dreadfully miss being able to do
> this in Perl:
>
> $myvariable = `/path/to/my/program`
>
> Is there an easy equivalent in Python?
The most direct equivalent I can think to do this would be:
###
myvariable = os.popen('/path/to/my/program').read()
###
However, remember, you can write a definition to make this easy on
yourself.
###
def suckExec(program_name):
return os.popen(program_name).read()
###
Afterwards, you should be able to just say:
###
myvariable = suckExec('/path/to/my/program')
###
For example:
>>> suckExec('ls')
'mail\012nsmail\012' # (Just did a reinstall.)
True, it is a little longer to write. But it reads so beautifully...
*grin* Hope this helps!