How can I handle a process stderr?
proc = subprocess.Popen('ll'.split(), stdout=subprocess.PIPE)
for i in proc.stdout:
print(i)
Right now I am streaming the output but I am not sure how to properly deal with a potential error that might occur.
I wanted to use out, err = proc.communicate()
but my out
could be a very very very long string
If you know what error messages to expect, then one answer is to pass subprocess.STDOUT
to the stderr
argument of Popen
, so that your stderr messages are in the stdout stream:
proc = subprocess.Popen('ll'.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for i in proc.stdout:
print(i)
# check for error message strings and do something with them
Or if you don't care about the stdout messages then just iterate over stderr instead:
dnull = open(os.devnull, 'w')
proc = subprocess.Popen('ll'.split(), stdout=dnull, stderr=subprocess.PIPE)
for i in proc.stderr:
print(i)
# check for error message strings and do something with them
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.