I am calling an external program within Python script using subprocess. The external program produces a lot of output. I need to capture the output of this program. The current code is something like this:
process = subprocess.Popen('cmd.exe', shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=None)
process.stdin.write('gams "indus89.gms"\r\n')
while process.poll() != None:
line = process.stdout.readline()
print line
The error I am getting with this code is
The process tried to write to a nonexistent pipe.
If I use the following code:
process = subprocess.Popen('cmd.exe', shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=None)
process.stdin.write('gams "indus89.gms"\r\n')
o, e = process.communicate()
print o
then the output of the program is not captured.
How should I alter my code so that I can capture the output of the third party program while it runs?
Popen is overkill.
Try:
Hopefully that will work in your environment.