8

I'm trying to capture the output here. If, at the python prompt, I run

p = subprocess.Popen(["/path/to/search_by_hash.par", hash_str], 
                      stdout=subprocess.PIPE)

The return value (a list) prints to stdout, but isn't captured

[4460475, 4406612, 4379510]

I've tried following it up with

value = p.communicate()[0]
value

..but value is an empty string, not the list of ints I was expecting, and which is being printed to stdout

I experimented with the solutions from Store output of subprocess.Popen call in a string but haven't been able to capture the output.

UPDATE:

stderr doesn't seem to yield anything either...and the list I'm looking for is being printed out...just not having any luck in capturing it. See below:

>>> p = subprocess.Popen(["/home/jfry/tools/search_by_hash.par", hash_str], 
                          stdout=subprocess.PIPE)
>>> 
[4460475, 4406612, 4379510]
    value, err  = p.communicate()
>>> value
''
>>> err

Thanks!

Community
  • 1
  • 1
Jeff Fry
  • 410
  • 1
  • 4
  • 9
  • 4
    Try `value = p.communicate()[0]` – Jakob Bowyer Jun 05 '11 at 21:45
  • Are you trying to capture the data being sent to the subprocess's stdout, or are you trying to capture the subprocess's exit status? – Adam Rosenfield Jun 05 '11 at 21:51
  • Jakob - thanks, that was a typo in my question, but not at the prompt. I've changed the question. – Jeff Fry Jun 05 '11 at 21:53
  • Adam - I want the return *value*, in this case a list of ints, e.g. [4460475, 4406612, 4379510] – Jeff Fry Jun 05 '11 at 21:53
  • add `stderr=subprocess.PIPE`, just like you already have for stdout – snapshoe Jun 05 '11 at 22:12
  • The answers described here should work. What does the subprocess do? Could it be closing and reopening stdout? Can you capture its output by running it from a shell and redirecting the output, i.e. `/home/jfry/tools/search_by_hash.par hash >out 2>err`? – SimonJ Jun 05 '11 at 22:16
  • mita -- you added the missing piece, thanks! Now I can capture stderr, and see what's up. – Jeff Fry Jun 05 '11 at 22:29
  • the title should be fixed (`s/return value/output/`) to prevent confusion.. – mykhal Aug 08 '13 at 07:53

3 Answers3

5

Because the accepted answer does not solve the issue (at least for me):

To capture the stderr-output, the following should be done:

import subprocess
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)        
output, err = p.communicate()

The crucial part is the parameter stderr=subprocess.PIPE, without this parameter the stderr-output will not be captured and err will be None.

If you are interested only in the stderr-output you actually may use:

p = subprocess.Popen(command, stderr=subprocess.PIPE)        
err = p.communicate()[1]

In this case p.communicate()[0] is None.

ead
  • 32,758
  • 6
  • 90
  • 153
5

Try checking stderr with p.communicate()[1].

Zach Kelling
  • 52,505
  • 13
  • 109
  • 108
  • Zeekay, yes, I want the output of the command -- what's being printed to the screen after I type the command. Unfortunately, neither p.stdout.read() nor p.communicate()[0] is successfully capturing the output. Instead, both give me an empty string. – Jeff Fry Jun 05 '11 at 21:57
  • @Jeff Try checking `stderr`, `p.communicate()[1] for some clue as to what's going on. – Zach Kelling Jun 05 '11 at 21:59
  • Good suggestion, but no dice. I updated the question with the exact results from the python prompt. Thanks. – Jeff Fry Jun 05 '11 at 22:09
  • 1
    OK, with mita's suggestion above to include "stderr=subprocess.PIPE" in my Popen, I'm now able to see stderr with p.communicate()[1]. Thanks! – Jeff Fry Jun 05 '11 at 22:27
1

communicate is a method. So you should call it!

out = p.communicate()