Baz
Baz

Reputation: 13135

Failing to capture stdout from application

I have the following script:

import subprocess

arguments = ["d:\\simulator","2332.txt","2332.log", "-c"]

output=subprocess.Popen(arguments, stdout=subprocess.PIPE).communicate()[0]
print(output)

which gives me b'' as output.

I also tried this script:

import subprocess

arguments = ["d:\\simulator","2332.txt","atp2332.log", "-c"]
process = subprocess.Popen(arguments,stdout=subprocess.PIPE)
process.wait()

print(process.stdout.read())
print("ERROR:" + str(process.stderr))

which gives me the output: b'', ERROR:None

However when I run this at the cmd prompt I get a 5 lines of text.

d:\simulator atp2332.txt atp2332.log -c

I have added to simulator a message box which pops up when it launches. This is presented for all three cases. So I know that I sucessfully launch the simulator. However the python scripts are not caturing the stdout.

What am I doing wrong?

Barry.

Upvotes: 1

Views: 272

Answers (2)

David Sykes
David Sykes

Reputation: 49832

The following code gives me text output on stdout. Perhaps you could try it, and then substitute your command for help

import subprocess

arguments = ["help","2332.txt","atp2332.log", "-c"]
process = subprocess.Popen(arguments,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.wait()

print 'Return code', process.returncode
print('stdout:', process.stdout.read())
print("stderr:" + process.stderr.read())

Upvotes: 1

ba__friend
ba__friend

Reputation: 5903

If possible (not endless stream of data) you should use communicate() as noted on the page.

Try this:

import subprocess

arguments = ["d:\\simulator","2332.txt","atp2332.log", "-c"]
process = subprocess.Popen(arguments, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
sout, serr = process.communicate()

print(sout)
print(serr)

Upvotes: 2

Related Questions