Question

Based on the answer provided here, I wanted to save the error as a string:

p = subprocess.Popen(['ding', 'dong'], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
output, errors = p.communicate()

However, it seems that redirecting stderr is not working:

>>> p = subprocess.Popen(['ding', 'dong'], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib64/python2.4/subprocess.py", line 550, in __init__
    errread, errwrite)
  File "/usr/lib64/python2.4/subprocess.py", line 993, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

What is the correct way to capture the error as a string?

Was it helpful?

Solution

The error message in the question does not come from the subprocess. It was generated before the subprocess execution. You cannot capture that error using stderr option.

Make sure there's ding program in the path.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top