Getting the exit code of a subprocess

Jason electron at emypeople.net
Wed Dec 15 23:58:23 EST 2021


On Wed, Dec 15, 2021 at 08:19:16PM -0800, Kushal Kumaran wrote:
> 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.

Thank you, that is the info I needed!

-- 
Jason Rissler



More information about the Python-list mailing list