1

I searched a lot here and googled also, trying to find why stderr from my first command is not seen in the final stderr. I know of other methods like "check_output" (python3) and "commands" (python2), but I want to write my own cross-platform one. Here is the problem:

import subprocess

p1 = subprocess.Popen('dirr', shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
p2 = subprocess.Popen('find "j"', shell=True, stdin=p1.stdout, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
p1.stdout.close()
output,error=p2.communicate()
print(output,'<----->',error)

I also tried redirecting stderr=subprocess.STDOUT, but this didn't change things. Can you please tell how to redirect the stderr from the first command, so I can see it in the stdout or stderr?

Regards,

  • `check_output()` is available in Python 2.7. [It easy to implement it on older versions](http://stackoverflow.com/a/2924457/4279) – jfs Jun 20 '12 at 17:47
  • I know and I have included this in the question. I want to write my own method, based on these commands. I want make sure the command will work well with python2/3 on windows/linux, because it will be part of my library. Sofar this is doing the job, except for the error catching part. – Todor Ivanov Jun 20 '12 at 18:02

2 Answers2

1

To see stderr of the first command in stdout/stderr of the second command:

  • the second command could read stderr1 e.g., from its stdin2
  • the second command could print this content to its stdout2/stderr2

Example

#!/usr/bin/env python
import sys
from subprocess import Popen, PIPE, STDOUT

p1 = Popen([sys.executable, '-c', """import sys; sys.stderr.write("stderr1")"""],
           stderr=STDOUT, stdout=PIPE)
p2 = Popen([sys.executable, '-c', """import sys
print(sys.stdin.read() + " stdout2")
sys.stderr.write("stderr2")"""],
           stdin=p1.stdout, stderr=PIPE, stdout=PIPE)
p1.stdout.close()
output, error = p2.communicate()
p1.wait()
print(output, '<----->', error)

Output

('stderr1 stdout2\n', '<----->', 'stderr2')
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Thank you very much, Sebastian! This is exactly what I needed! Unfortunately I cannot vote on your brilliant explanation, because newbies like me are not allowed. – Todor Ivanov Jun 20 '12 at 19:22
-1

For p2, you shouldn't be using subprocess.PIPE for either of the outputs because you're not piping it to another program.

C0deH4cker
  • 3,959
  • 1
  • 24
  • 35
  • 1
    Not true. `PIPE` is also used when you want to programmatically read the streams in Python (e.g. via `communicate()` which is being used here). – Amber Jun 20 '12 at 17:06