When I call an external .exe
program in Python, how can I get printf
output from the .exe
application and print it to my Python IDE?
To call an external program from Python, use the subprocess module.
The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.
An example from the doc (output
is a file object that provides output from the child process.):
output = subprocess.Popen(["mycmd", "myarg"], stdout=subprocess.PIPE).communicate()[0]
A concrete example, using cmd
, the Windows command line interpreter with 2 arguments:
>>> p1 = subprocess.Popen(["cmd", "/C", "date"],stdout=subprocess.PIPE)
>>> p1.communicate()[0]
'The current date is: Tue 04/14/2009 \r\nEnter the new date: (mm-dd-yy) '
>>>