tags:

views:

211

answers:

3

I am not sure what is return value of subprocess.call() (with shell=True). Can I safely assume a zero value will always mean command executed successfully. Is return vale == exit staus of a shell command ??

Will following piece of code work for virtually any command on linux?

e.g.

 cmd = "foo.txt > bar.txt"
 ret = subprocess.call(cmd, shell=True)
 if ret != 0:
     if ret < 0:
         print "Killed by signal", -ret
     else:
         print "Command failed with return code", ret
 else:
     print "SUCCESS!!"

Pls enlighten me :-)

+1  A: 

It is the return code, but keep in mind it's up to the author of the subprocess what the return code means. There is a strong culture of 0 meaning success, but there's nothing enforcing it.

Ned Batchelder
A: 

You are at the mercy of the commands that you call. Consider this:

test.py

#!/usr/bin/env python
success=False
if not success:
    exit()

Then running your code (with cmd='test.py') will result in SUCCESS!!

merely because test.py does not conform to the convention of returning a non-zero value when it is not successful.

unutbu