这是 Python 2.7 代码和两个 Python 程序在同一目录中。
call_script.py
import os
from subprocess import call
if "OTHER_PROGRAM" in os.environ.keys():
program_name = os.environ["OTHER_PROGRAM"]
print "will execute", program_name
call([program_name ,"some_file.pro"])
else:
print "OTHER_PROGRAM env variable not defined"
other.py(必须标记为可执行 - 即:chmod +x other.py)
#!/usr/bin/python
import sys
print "executing in other.py"
if len(sys.argv) > 1:
print "was passed value of ", sys.argv[1]
else:
print "no arguments were passed"
输出:
executing in other.py
was passed value of some_file.pro