python function in pipe

7stud bbxx789_05ss at yahoo.com
Sat Apr 28 13:35:50 EDT 2007


On Apr 28, 7:37 am, Bart <u... at o2.pl> wrote:
> Hi everyone!
>
> Im using module that gives errors to stderr/stdout  (generated by SWIG)
> Problem is that I need to parse this errors/information from module.
>
> os.popen3 looks nice but this executes  command  not  function.
>
> Is there any solution?
>
> Best regards
> Bart.

Maybe something like this:

moduleA.py:
-----------
def someFunc():
    print "hello"
    raise ValueError

someFunc()
----------

import subprocess

print "Main executing"
try:
    p  = subprocess.Popen(["python", "6test.py"],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)

    result = p.wait()
    if result == 0:
        print "output:", p.stdout.read()
    else:
        print "output before error:", p.stdout.read()
        print "error:", p.stderr.read()

except (OSError, TypeError, ValueError), e:
    print "subprocess was never started"
    print e





More information about the Python-list mailing list