A WARNING. This only works on UNIX systems.
I find that subprocess is redundant when all you need is output for capture. I recommend using commands.getoutput() :
>>> import commands >>> foo = commands.getoutput('bar')
Technically, it just makes popen() on your behalf, but it is much simpler for this basic purpose.
BTW, os.system() does not return the result of the command, it returns the exit status, so it does not work for you.
Alternatively, if you require both exit status and command exit, use commands.getstatusoutput() , which returns a 2-tuple (status, output):
>>> foo = commands.getstatusoutput('bar') >>> foo (32512, 'sh: bar: command not found')
source share