python执行另一个脚本并获取其return的值_如何从另一个python脚本运行一个python脚本,并获得返回的状态code?...

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.

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.

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.

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?

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值