Read stderr from python subprocess.Popen

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 subp…


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Read stderr from python subprocess.Popen." Waylon Walker | Sciencx - Thursday February 10, 2022, https://www.scien.cx/2022/02/10/read-stderr-from-python-subprocess-popen/
HARVARD
Waylon Walker | Sciencx Thursday February 10, 2022 » Read stderr from python subprocess.Popen., viewed ,<https://www.scien.cx/2022/02/10/read-stderr-from-python-subprocess-popen/>
VANCOUVER
Waylon Walker | Sciencx - » Read stderr from python subprocess.Popen. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/10/read-stderr-from-python-subprocess-popen/
CHICAGO
" » Read stderr from python subprocess.Popen." Waylon Walker | Sciencx - Accessed . https://www.scien.cx/2022/02/10/read-stderr-from-python-subprocess-popen/
IEEE
" » Read stderr from python subprocess.Popen." Waylon Walker | Sciencx [Online]. Available: https://www.scien.cx/2022/02/10/read-stderr-from-python-subprocess-popen/. [Accessed: ]
rf:citation
» Read stderr from python subprocess.Popen | Waylon Walker | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.