Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the output of a python subprocess

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!

like image 928
Jeff Fry Avatar asked Jun 05 '11 21:06

Jeff Fry


People also ask

How do I get output from Popen?

popen. To run a process and read all of its output, set the stdout value to PIPE and call communicate(). The above script will wait for the process to complete and then it will display the output.

What does Python subprocess return?

The Python subprocess call() function returns the executed code of the program. If there is no program output, the function will return the code that it executed successfully. It may also raise a CalledProcessError exception.


1 Answers

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

like image 79
zeekay Avatar answered Oct 24 '22 11:10

zeekay