Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python + how to verify if linux command success in python

Tags:

python

linux

bash

How to capture the standard output of command in python script

for example , I want to check if tar command success or not and results will be return in stndStatus value

import commands

def runCommandAndReturnValue():

      status,output = commands.getstatusoutput("  tar xvf Test.tar ")
      return stndStatus

other example - its like $? in shell scripts so stndStatus will be the value of $?


1 Answers

I need to redirect the output to DEVNULL:

import subprocess
import os

FNULL = open(os.devnull, 'w')
retcode = subprocess.call(['tar', 'xvf', 'test.tar'],
                          stdout=FNULL,
                          stderr=subprocess.STDOUT)
print retcode

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!