How to get error reason or build output from command line to a script?

I have a python script that we use to make our builds. Currently it is calling:
os.system( '../gradlew -p ../ assembleRelease')

I would like a way to get the error output so that I can save it to a variable within the script when this build fails.

Or the success output so I can do the same when it succeeds. I’ve looked everywhere and can’t seem to find any way to do this.

This python script runs a bunch of builds one after the next and when it is done I would like to be able to compile a summary of the results of all the builds it ran, which ones had an error and which succeeded and the reasons why.

You’ll want to pass --console=plain on gradle command line to disable Gradle’s “rich” console and enable plain text mode. You might also want to pass --stacktrace

https://docs.gradle.org/current/userguide/command_line_interface.html

A better option could involve invoking gradle via the tooling api as you’ll have a much better way of dealing with success/fail cases rather than parsing command line output