2016-07-06 96 views
1

我有一个带有命令行输入的Lua脚本,我想用Python(2.7)运行并读取输出。例如,我会在终端(Ubuntu的14.xx)运行代码的样子:在Python中使用子进程模块运行带命令行输入的lua脚本

lua sample.lua -arg1 helloworld -arg2 "helloworld" 

如何运行使用的子模块在Python命令行中输入一个Lua脚本?我认为它会是这样的:

import subprocess 

result = subprocess.check_output(['lua', '-l', 'sample'], 
    inputs= "-arg1 helloworld -arg2 "helloworld"") 
print(result) 

什么是正确的方法来做到这一点?

这与以下链接非常相​​似,但不同之处在于我尝试使用命令行输入。下面的问题只是调用(Lua)脚本中定义的Lua函数,并将输入直接提供给该函数。任何帮助将非常感激。

Run Lua script from Python

回答

2

如果你不知道,你通常可以通过在shell工作逐字字符串,并将shlex.split把它分解:

import shlex 
subprocess.check_output(shlex.split('lua sample.lua -arg1 helloworld -arg2 "helloworld"')) 

但是,你通常不需要这样做,只要你知道,可以用手分割参数他们提前什么:

subprocess.check_output(['lua', 'sample.lua', '-arg1', 'helloworld', '-arg2', 'helloworld']) 
+0

这对我来说工作,看起来像最完整的答案。谢谢! – sfortney

1

试试这个:

import subprocess 

print subprocess.check_output('lua sample.lua -arg1 helloworld -arg2 "helloworld"', shell=True) 
+0

感谢您的回复。我尝试过这一个,它并没有工作。这可能只是我的代码的一个功能。也许它会帮助其他人来到这里 – sfortney

相关问题