2

I want to know if the subprocess.call() has terminated correctly without any error in the called function. For example, in the below code, if the path provided is not appropriate, the ls command gives an error as:

ERROR:No such file or directory.

I want same output to be stored as string.

import subprocess
path = raw_input("Enter the path")
subprocess.call(["ls","-l",path])
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Prasanna Kumar
  • 69
  • 4
  • 11
  • 1
    a similar question has already been asked. http://stackoverflow.com/questions/1996518/retrieving-the-output-of-subprocess-call – George Simms Dec 13 '14 at 10:57
  • @GeorgeSimms: In general the output and the exit status are different things. It might be enough to know the return code without retrieving subprocess' stdout/stderr, to check whether the subprocess terminated properly e.g., nonzero exit status often indicates an error (it depends on the subprocess) regardless of stdout/stderr value. – jfs Dec 16 '14 at 16:10
  • @J.F.Sebastian the author asks - for whether the subprocess succeeded (which can be determined from the falsiness of the exit code) - the output of the subprocess to stdout/stderr as a string (which can be obtained by calling `communicate` on the result of Popen) `subprocess.call` is insufficient because the output to stdout/stderr cannot be retrieved. Otherwise we could do `something_went_wrong() if subprocess.call(stuff) else we_are_ok()` – George Simms Dec 16 '14 at 21:09

2 Answers2

1

You cannot do that with call, because what it does is only:

Run the command described by args. Wait for command to complete, then return the returncode attribute.

So you can only determine the return code of a program, which usually means zero if no error occurred, and non-zero otherwise.

Use the check_output method from the same module:

try:
    result = subprocess.check_output(["ls", "-l", path],
                                     stderr = subprocess.STDOUT)
    print result
except subprocess.CalledProcessError, e:
    print "Error:", e.output

Here is a working demo.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
1
from subprocess import Popen, PIPE
p = Popen(["ls", "-l", path], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err = p.communicate()
status = p.returncode
if  status:
  # something went wrong
  pass
else:
  # we are ok
  pass

Although consider using os.listdir

George Simms
  • 3,930
  • 4
  • 21
  • 35
  • You don't need to read the output, to check the success status. to get the exit status; it is enough to store `subprocess.call()` return value (an integer). Nonzero status means an error in many cases. The output could be redirected to DEVNULL, to avoid noise e.g., `stderr=DEVNULL`. – jfs Dec 16 '14 at 16:08
  • @J.F.Sebastian OP explicitly stated that wants to read the output. – BartoszKP Dec 16 '14 at 17:55
  • @BartoszKP: I know. The comment is for people who landed on the page due to its title. – jfs Dec 16 '14 at 18:02
  • @J.F.Sebastian Consider fixing the title instead :) – BartoszKP Dec 16 '14 at 20:19