0

I have this code:

pprocess = subprocess.Popen('PROGRAM', stdout=subprocess.PIPE)

while True:
    output = pprocess.stdout.readline()
    print 'output = ', output

But the output is not being printed using the python code, but instead it is being printed on the console and it appears to be printed directly from the process.

Does anyone came across this issue?

4
  • what is PROGRAM returning when you call it IN TERMINAL? Commented Jan 10, 2018 at 9:17
  • It prints the output correctly on the terminal Commented Jan 10, 2018 at 9:19
  • You need to move the popen as well into the while loop. Commented Jan 10, 2018 at 9:25
  • Why? It will cause the popen to be opened every time Commented Jan 10, 2018 at 11:51

2 Answers 2

2

But the output is not being printed using the python code, but instead it is being printed on the console

It seems the output is being printed on the STDERR stream, rather than the STDOUT stream; you're only capturing the STDOUT.

So, consume the stderr stream (too):

pprocess = subprocess.Popen('PROGRAM', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Checking just the STDERR here
for line in pprocess.stderr:
    print(line)

As a side note, rather that using readline() in a while loop, just iterate over the file-like object provided by Popen.stdout/Popen.stderr, which are iterators.

Sign up to request clarification or add additional context in comments.

3 Comments

The " for line in pprocess.stderr: " line appears to be blocking, the flow stops there
@JayR.W From terminal, run program 2>/dev/null . Do you get any output? (This is to confirm that the output actually is being written to STDERR)
The output that is being written is different from the output I am receiving from this line of code: " print = self.sip_process.stderr.readline() "
-1

Once you read from the stdout stream by doing stdout.readline it becomes empty. You either have to move your Popen call into the while loop or change your logic:

while True:
    pprocess = subprocess.Popen('PROGRAM', stdout=subprocess.PIPE)
    output = pprocess.stdout.readline()
    print 'output = ', output

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.