Created
December 30, 2020 20:46
-
-
Save akhdaniel/4c2e4e7b7c72fc2d8b2ed343d4d167e4 to your computer and use it in GitHub Desktop.
Get Error Message of subprocess.call()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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