2

I've been working on a Python script to interact with ffmpeg; however, I've noticed that even though everything runs fine, stdout is empty and stderr returns what I should expect from stdout. How do I fix it so that the output will be returned by stdout?

Here's the simplest example that reproduces the phenomenon:

from subprocess import Popen, PIPE

p = Popen(['python', '-V'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
out, err = p.communicate()
if out:
    print("Output. ", out.decode())
else:
    print("Error. ", err.decode())

Here's the output:

Error.  Python 3.6.1 :: Anaconda 4.4.0 (64-bit)

I should note that I'm using Windows 10.

1
  • What is your question here ? Commented Aug 1, 2017 at 9:14

1 Answer 1

2

You can redirect the stderr of your process to its stdoutby doing so:

from subprocess import PIPE, STDOUT
p = subprocess.Popen(["python", "-V"], stdout=PIPE, stderr=STDOUT)

Then you can retrieve the output produced by the process like so:

out = p.stdout.read()

This will return the content of the stdout after your process has terminated.

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

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.