运行python脚本并将参数捕获到另一个python脚本中

时间:2018-10-10 06:03:32

标签: python

我无法正常工作。 我有这样的东西

python executor.py arg1 arg2 arg3

然后我有另一个python脚本,它不是由excecutor.py直接调用,而是由另一个在executor.py中调用的脚本文件调用。让它调用

script.py

有一个变量名argument,我想在其中捕获arg1。 怎么做?

2 个答案:

答案 0 :(得分:0)

我假设您正在import script脚本中执行executor.py。如果是这种情况,则必须添加到executor.py

import script
import sys
argument_1 = sys.argv[1] # since [0] is the script name
...
script.yourfunction(argument_1)

答案 1 :(得分:0)

假设script.py和executor.py位于同一文件夹中:

script.py:

def function1(somearg, otherarg):
    pass
def function2(moreargs):
    pass

和executor.py

import sys
import script

# assign input args; check for valid arguments etc..
arg1 = sys.argv[1]
arg2 = sys.argv[2]
etc...

# call system functions
script.function1(arg1, arg2)
script.function2(arg1)