53

I am not sure what the return value of subprocess.call() means.

  • Can I safely assume a zero value will always mean that the command executed successfully?

  • Is the return value equivalent to the exit staus of a shell command?

For example, will the following piece of code work for virtually any command on Linux?

 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!!"

Please enlighten me :-)

David Cain
  • 16,484
  • 14
  • 65
  • 75
anonymous
  • 539
  • 1
  • 4
  • 3

3 Answers3

39

Yes, Subprocess.call returns "actual process return code".

You can check official documentation of Subprocess.call and Subprocess.Popen.returncode

Håken Lid
  • 22,318
  • 9
  • 52
  • 67
Jochen Ritzel
  • 104,512
  • 31
  • 200
  • 194
14

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
  • 364,293
  • 75
  • 561
  • 662
6

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
  • 842,883
  • 184
  • 1,785
  • 1,677