Skip to content

Instantly share code, notes, and snippets.

@akhdaniel
Created December 30, 2020 20:46
Show Gist options
  • Save akhdaniel/4c2e4e7b7c72fc2d8b2ed343d4d167e4 to your computer and use it in GitHub Desktop.
Save akhdaniel/4c2e4e7b7c72fc2d8b2ed343d4d167e4 to your computer and use it in GitHub Desktop.
Get Error Message of subprocess.call()
import subprocess
import sys
# create two files to hold the output and errors, respectively
with open('out.txt','w+') as fout:
with open('err.txt','w+') as ferr:
out=subprocess.call(["ls",'-lha'],stdout=fout,stderr=ferr)
# reset file to read from it
fout.seek(0)
# save output (if any) in variable
output=fout.read())
# reset file to read from it
ferr.seek(0)
# save errors (if any) in variable
errors = ferr.read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment