Skip to main content
 首页 » 编程设计

python之Scala:带参数的python脚本的系统命令

2024年10月17日8pander-it

所以我在 Scala 中发送这个系统命令:

val command = "python other/evaluateAnswers.py 'chemistry earth' 'the chemistry of the world in champaign' 'the chemistry of the computer science world'" 

命令.!!

这是我的 python 代码的简化版本:

def main(argv): 
    # example run: 
    print(argv) 
    # do stuff here ...  
 
if __name__ == '__main__': 
    main(sys.argv[1:]) 

如果我直接在终端内运行:

daniel$ python other/evaluateAnswers.py 'chemistry earth' 'the chemistry of the world in champaign' 'the chemistry of the computer science world' 

这是我的 python 代码中 print(argv) 的结果:

['chemistry earth', 'the chemistry of the world in champaign', 'the chemistry of the computer science world'] 

这是正确的。

如果我通过 command.!! 从 scala 运行它,我会在 print(argv) 的输出中得到以下内容:

["'chemistry", "earth'", "'the", 'chemistry', 'of', 'the', 'world', 'in', "champaign'", "'the", 'chemistry', 'of', 'the', 'computer', 'science', "world'"] 

这是不正确的拆分。

有什么地方出错了吗?

请您参考如下方法:

手动拆分我的 scala 命令就成功了:

val command = Seq("python", "other/evaluateAnswers.py", "'chemistry earth'", "'the chemistry of the world in champaign'", "'the chemistry of the computer science world'") 
command.!!