Python subprocess hide stdout and wait for it to finish

I have this code:

def method_a(self):
    command_line = 'somtoolbox GrowingSOM ' + som_prop_path
    subprocess.Popen(shlex.split(command_line))
    ......

def method_b(self): .....
....

and, as you can see, method_a has a subprocess that calls the somtoolbox program. But this program has a long stdout, and I want to hide it. I tried:

subprocess.Popen(shlex.split(command_line), stdout=subprocess.PIPE)

But it returned this sentence:

cat: record error: Broked Pipe   

(This is a translation of the Portuguese sentence: "cat: erro de gravação: Pipe quebrado") (I'm from Brazil)

In addition, I have other methods (e.g. method_b) that are called after the method_a method, and tis methods are executed before the subprocess completes the process.

How can I hide stdout at all (and don't want it anywhere) and make other code wait for the subprocess to complete?

Obs: somtoolbox is a java program that provides long output to the terminal. I tried:

outputTuple = subprocess.Popen(shlex.split(command_line), stdout = subprocess.PIPE).communicate()

but continuous output to shell. Help!

+5
2

- /dev/null. :

devnull = open('/dev/null', 'w')
subprocess.Popen(shlex.split(command_line), stdout=devnull)

, , , .wait() Popen, :

devnull = open('/dev/null', 'w')
process = subprocess.Popen(shlex.split(command_line), stdout=devnull)
retcode = process.wait()

retcode .

: , stderr. stderr, :

devnull = open('/dev/null', 'w')
process = subprocess.Popen(shlex.split(command_line), stdout=devnull, stderr=devnull)
retcode = process.wait()
+18

Popen.communicate . :

from subprocess import PIPE, Popen
outputTuple = Popen(["gcc", "--version"], stdout = PIPE).communicate()

, stdout stderr.

+5

All Articles