0

I have two python scripts call a.py and b.py.

From inside a.py, I am giving command

os.system('python b.py')

If some exception is found in b.py, I want the complete execution of both a.py and b.py to be halted. Is there a way to do that?

I am not importing b.py in a.py. I am simply calling it using os.system().

How can the same be achieved even if I import the other script?

0

2 Answers 2

1

If you import a module b that might raise an exception which you won't handle, either let the exception halt the execution by not handling it, or, handle it and exit yourself.

If you opt for calling os.system check the error code returned; if Python doesn't exit gracefully (i.e an exception was raised) it is going to be a non 0 value. You can test on and act accordingly:

if os.system('python b.py'):
    # exit from script a.by
Sign up to request clarification or add additional context in comments.

Comments

1

os.system() returns the (encoded) process exit value. 0 means success.

I would rather reccomend the subprocess Module since it is more powerful. Take a look at subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False).

Good luck.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.