我正在使用一个python脚本(让我们称之为MyScript.py),它包含一个函数,其中包含许多不同类型的参数,我不允许修改:
def MyFunction(str1, int1, str2, str3, int2, bool1):
#Do something with input
if __name__ == "__main__":
eval(sys.argv[1])
我想从一个带有参数列表的批处理脚本中调用这个函数。我尝试了下面的方法,但是失败了:
python -c "import MyScript;MyScript.MyFunction('str1', 'int1', 'str2', 'str3', 'int2', 'bool1')"
python -c "import MyScript;MyScript.MyFunction([str1], [int1], [str2], [str3], [int2], [bool1])"
我该如何调用这个函数呢?什么是格式化python脚本输入参数的正确方法?谢谢你!
Frompython --help
:
-c cmd : program passed in as string (terminates option list)
application. Typical usage is python3 -X importtime -c 'import asyncio'
所以,当你使用-c
标志时,你已经在python中运行,所以不需要使用eval(sys.argv[1])
,你只需要用参数(它们的值)调用函数:
python -c "import MyScript;MyScript.MyFunction('str1', 1, 'str2', 'str3', 2, True)"