Question

I'm using python to run an external program as follows

 call("/usr/sbin/snif")

and I get

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/subprocess.py", line 480, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.6/subprocess.py", line 633, in __init__
errread, errwrite)
File "/usr/lib/python2.6/subprocess.py", line 1139, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

How do I get the last line, that says :

"No such file or directory"

or better yet, how do I know if the execution succeeded or not?

Thanks

Was it helpful?

Solution

You can catch the error:

try:
    call('/usr/sbin/snif')
except OSError:
    print "It didn't execute"

If you want to see if the command executed correctly, use check_output or check_call instead and catch another error:

import subprocess:

try:
    subprocess.check_output('/usr/sbin/snif')
except OSError:
    print 'That file does not exist'
except subprocess.CalledProcessError:
    print 'Bad exit code'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top