mnordhoff at mattnordhoff.com
Thu Jun 19 12:14:11 CEST 2008
> On Jun 18, 10:33�pm, "bruce" <bedoug... at earthlink.net> wrote:
>> can someone point me to where/how i would go about calling a ruby app from a
>> python app, and having the python app being able to get a returned value
>> from the ruby script.
>> something like
>> �a = os.exec(testruby.rb)
>> �foo = 9
>> �return foo
>> i know this doesn't work... but i've been searching for hours on this with
>> no luck.... (and yeah, i'm relatively new to both ruby/python!!)
> Well, I don't know anything about Ruby, but here's
> how I do it for C programs (compiled to .exe that
> write to stdout).
> import os
> factor_program = 'factor! -d200 ' # factor!.exe from MIRACL
> n =
> # call external program and capture stdout
> the_output = os.popen(factor_program+n).readlines()
> print 'n: %s' % n
> for i in the_output:
> print i,
You're supposed to use the subprocess module.
In this case, something like:
factor_program = ['factor!', '-d200']
p = subprocess.Popen(factor_program + [n], stdout=subprocess.PIPE)
p.wait() # wait for it to finish; not sure how necessary it is
the_output = p.stdout.readlines()
See subprocess's documentation , which includes guides on replacing
os.popen* and other functions with it.
More information about the Python-list