如何从另一个python脚本运行一个python脚本,并获得返回的状态code?

问题描述:

我是比较新的Python和庆典。然而,我发现Python的更直观,比bash的更容易。我有我设法凑齐数的bash脚本,但我想用Python脚本,以取代他们 - 易于维护等

I'm relatively new to both Python and bash. However, I am finding Python much more intuitive and easier than bash. I have a few bash scripts I have managed to cobble together, but I would like to replace them with Python scripts - for ease of maintenance etc.

本的bash脚本基本上运行Python脚本,检查返回的状态code和采取适当行动(例如日志消息,火了一封电子邮件等等) - 这是功能,我的事情,我可以在大多数情况下,再现Python脚本。

The bash scripts essentially run python scripts, check the returned status code and act appropriately (e.g. log a message, fire off an email etc) - this is functionality that I thing I can for the most part, reproduce in a Python script.

有一件事我不知道怎么办,就是如何从另一个python脚本运行一个python脚本,并获得返回的状态code。

The one thing I am not sure of how to do, is how to run a python script from another python script and get the returned status code.

任何人都可以发布一个片段在这里,将展示如何从主python脚本master.py运行一个小的python脚本test.py,并正确地从主机上运行test.py检索后返回code的.py?

Can anyone post a snippet here that will show how to run a small python script 'test.py' from a main python script 'master.py' and correctly retrieve the return code after running test.py from master.py?

使用模块

master.py

master.py

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