This content originally appeared on DEV Community and was authored by Waylon Walker
I often run shell commands from python with Popen, but not often enough do I set up error handline for these subprocesses. It's not too hard, but it can be a bit awkward if you don't do it enough.
Using Popen
import subprocess from subprocess import Popen
# this will run the shell command `cat me` and capture stdout and stderr
proc = Popen(["cat", "me"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# this will wait for the process to finish.
proc.wait()
reading from stderr
To get the stderr we must get it from the proc, read it, and decode the bystring. Note that we can only get the stderr object once, so if you want to do more than just read it you will need to store a copy of it.
proc.stderr.read().decode()
Better Exception
Now that we can read the stderr
we can make better error tracking for the user so they can see what to do to resolve the issue rather than blindly failing.
err_message = proc.stderr.read().decode() if proc.returncode != 0:
# the process was not successful
if "No such file" in err_message:
raise FileNotFoundError('No such file "me"')
This content originally appeared on DEV Community and was authored by Waylon Walker
Waylon Walker | Sciencx (2022-02-10T21:46:10+00:00) Read stderr from python subprocess.Popen. Retrieved from https://www.scien.cx/2022/02/10/read-stderr-from-python-subprocess-popen/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.