python - how to properly evaluate a value from a system command -


i'm learning python (using 2.7.8) , i'm trying figure out best way evaluate output of system command. i've read use subprocess. example, need run if statment , evaluate > 0, process it.

example of unix command: cat dump_cars_tbl.out | grep -i hummer | wc -l' (in case should return value of 1)

in python, i've tested this, results wrong. should return yes.

>>> import subprocess >>> mycat=subprocess.call('cat dump_cars_tbl.out | grep -i hummer | wc -l', shell=true) 1 >>> if mycat > 0: ...     print('yes') ... else: ...     print('no') ... no 

what doing wrong? also, should use subprocess.call this? need make sure command mycat doesn't return error too.

if have better example how execute , evaulate command this, please show me. thank you.

you need use subprocess.check_output:

mycat = int(subprocess.check_output(cmd, shell=true)) 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -