Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate subprocess.check_output()?

Tags:

python

I am executing commands through subprocess.check_output() because i want the o/p of it to be stored in a buffer.

Now, while doing this if command gets failed or if there is any error then it is causing problem for my whole application.

What i want is, even if command fails it should just print and go on for next instruction.

Can anybody help me to solve it?

Below is the sample code.

from subprocess import check_output
buff=check_output(["command","argument"])
if buff=="condition":
    print "Do Some Task "
like image 237
Chirag Avatar asked Dec 10 '25 19:12

Chirag


1 Answers

One way to do this would be to use the Popen.communicate method on a Process instance.

from subprocess import Popen, PIPE
proc = Popen(["command", "argument"], stdout=PIPE, stderr=PIPE)
out, err = proc.communicate() # Blocks until finished
if proc.returncode != 0: # failed in some way
    pass # handle however you want
# continue here