Capturing output through a subprocess without using a connection

I am calling an external program in a Python script using a subprocess. An external program produces a lot of output. I need to record the results of this program. The current code looks 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 get with this code is

The process tried to write to a non-existent channel.

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 program output will not be recorded.

How can I change my code so that I can write the output of a third-party program during its launch?

+3
source share
1 answer

Popen is crowded.

Try:

output = subprocess.check_output('gams "indus89.gms"\r\n', shell=True)

We hope that this will work in your environment.

+3
source

All Articles