Monday, June 27, 2011

Run external program and check output and errors

import subprocess

command = ['ls', '-a']
print ' '.join(command)

    print subprocess.check_output(command, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
    print 'There was an error when running the command:'
    print e.output

This runs an external program an gets its output and errors.
subprocess.check_output(*popenargs, **kwargs)

No comments:

Post a Comment