Getting the exit code of a subprocess

Kushal Kumaran kushal at locationd.net
Wed Dec 15 23:19:16 EST 2021


On Wed, Dec 15 2021 at 09:38:48 PM, Jason <electron at emypeople.net> wrote:
> Hello,
>
> How can I find out the exit code of a process when using the
> subprocess module? I am passing an email message to a shell script and
> I need to know whether the shell script threw an error.
>
> Here is my code:
> p = Popen(cmd, stdout=None, stdin=PIPE, stderr=None)
> p.communicate(input=msg)
>
> I tried something like this:
> e_stat = p.communicate(input=msg)
>
> but the value of e_stat is always '(None, None)'
>

You want to look at p.returncode after communicate returns:
https://docs.python.org/3/library/subprocess.html#subprocess.Popen.returncode

https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate
returns a tuple with the stdout and stderr contents.

-- 
regards,
kushal


More information about the Python-list mailing list