Skip to content

Instantly share code, notes, and snippets.

@a3linux
Created October 6, 2017 01:45
Show Gist options
  • Select an option

  • Save a3linux/b296ba807fccf2f578c66ecaa35d6c2b to your computer and use it in GitHub Desktop.

Select an option

Save a3linux/b296ba807fccf2f578c66ecaa35d6c2b to your computer and use it in GitHub Desktop.
To get both the process output and the returned code:
from subprocess import Popen, PIPE
p = Popen(["ls", "non existent"], stdout=PIPE)
output = p.communicate()[0]
print(p.returncode)
subprocess.CalledProcessError is a class. To access returncode use the exception instance:
from subprocess import CalledProcessError, check_output
try:
output = check_output(["ls", "non existent"])
returncode = 0
except CalledProcessError as e:
output = e.output
returncode = e.returncode
print(returncode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment