views:

1737

answers:

2

Hi,

How can I call an external program with a python script and retrieve the output and return code?

+11  A: 

Look at the subprocess module: a simple example follows...

from subprocess import Popen

process = Popen(["ls", "-la", "."], stdout=PIPE)
exit_code = os.waitpid(process.pid, 0)
output = process.communicate()[0]

HTH

jkp
+2  A: 

Check out the subprocess module here: http://docs.python.org/library/subprocess.html#module-subprocess. It should get what you need done.

David Ackerman