python/ruby question..

Matt Nordhoff mnordhoff at mattnordhoff.com
Thu Jun 19 06:14:11 EDT 2008


Mensanator wrote:
> On Jun 18, 10:33�pm, "bruce" <bedoug... at earthlink.net> wrote:
>> hi...
>>
>> 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
>>
>> test.py
>> �a = os.exec(testruby.rb)
>>
>> testruby.py
>> �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!!)
>>
>> thanks
> 
> 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 =
> '50818429800343305993022114330311033271249313957919046352679206262204589342623811236647989889145173098650749'
> 
> # 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,

<snip output>

You're supposed to use the subprocess module.

In this case, something like:

import subprocess
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 [1], which includes guides on replacing
os.popen* and other functions with it.

[1] <http://docs.python.org/lib/module-subprocess.html>
-- 



More information about the Python-list mailing list