Search code examples
pythonpopen

Correct use of Popen in Python, or something alike


I'm completely new to Python, and now I've to change a script so it calls another program and get it's output. I've researched, and all the solutions point to something like

p = sub.Popen('echo '+s+' | ${morphg_res:-./morphg.ix86_linux -t}', shell=True,stdout=sub.PIPE,stderr=sub.PIPE )
out,err=p.communicate()
print out

but I couldn't get it to work. "print out" prints nothing.

I had it working with

 os.system('echo '+s+' | ${morphg_res:-./morphg.ix86_linux -t}')

but this way I can't get the output of morphg.

Also, I don't exactly understand what the second part means, but I know it calls the program ccorrectly. Just that the output is to stdin, and I need it to be in a variable.

Any help?

P.S.: subprocess.check_output is not an option as I have Python 2.6.


Solution

  • try this:

    import os
    p = os.popen('echo '+s+' | ${morphg_res:-./morphg.ix86_linux -t}')
    out = p.read()
    print out