Alex Rivera | Logout

How to call an external program in python and retrieve the output and return code?

Asked 2009-04-01T19:25:10.453
62

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

Edit
Report

1 Answer

89

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

from subprocess import Popen, PIPE

process = Popen(["ls", "-la", "."], stdout=PIPE)
(output, err) = process.communicate()
exit_code = process.wait()
answered 2009-04-01T19:28:30.133

Your Answer