I have Python script that calls several batch files through os.system. However, sometimes the batch file will throw errors and exit via exit /b 1.
However, even though the batch script fails, the Python script continues running, but I’d like the entire python script to quit as well if any batch script throws an error. How do I catch this exception in Python?
>Solution :
os.system‘s return value is (usually. There are some caveats so you should check out the docs).
It can be used to catch the exit 1 in the batch script:
import os
exit_code = os.system('./some_batch_script')
if exit_code != 0:
raise Exception(f'batch script exited with {exit_code}')