How to run a python script from another python script and get the returned status code?

I am relatively new to both Python and bash. However, I find Python much more comprehensible and understandable than bash. I have several bash scripts that I managed to build, but I would like to replace them with Python scripts - for ease of maintenance, etc.

Bash scripts basically run python scripts, check the returned status code and act accordingly (for example, register a message, start an email, etc.) - this is the functionality that I can mostly play, play back the Python script.

The only thing I don’t know how to do is to run a python script from another python script and get the returned status code.

Can someone post a snippet here that shows how to run a small python script 'test.py' from the main python script 'master.py' and correctly get the return code after running test.py from master.py?

+6
source share
2 answers

Using subprocess module

master.py

import subprocess retcode = subprocess.call(["/usr/bin/python", "/path/to/test.py"]) print "Return code of test.py is ", retcode 
+6
source

I would suggest you look at the subprocess module in python. You can start another process using it, manage your threads and get a return code.

0
source

Source: https://habr.com/ru/post/905066/


All Articles