Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the return value of subprocess.call()?

Tags:

python

linux

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 :-)

like image 780
anonymous Avatar asked Nov 08 '09 16:11

anonymous


People also ask

How does subprocess run get return value?

subprocess. check_output() is the one that runs the command and returns the return value. If you want the output write your value to STDOUT and use check_output() to get the value.

How do I get the output of a subprocess call?

communicate() #Another way to get output #output = subprocess. Popen(args,stdout = subprocess. PIPE). stdout ber = raw_input("search complete, display results?") print output #... and on to the selection process ...

What is a subprocess call?

Subprocess call():Subprocess has a method call() which can be used to start a program. The parameter is a list of which the first argument must be the program name. The full definition is: subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False) # Run the command described by args.

What is the return type of subprocess Check_output?

CalledProcessError Exception raised when a process run by check_call() or check_output() returns a non-zero exit status. returncode Exit status of the child process.


2 Answers

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

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

like image 76
Jochen Ritzel Avatar answered Oct 06 '22 07:10

Jochen Ritzel


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.

like image 22
Ned Batchelder Avatar answered Oct 06 '22 07:10

Ned Batchelder