使用 python 中的子进程模块到 运行 带有命令行输入的 lua 脚本

Using the subprocess module in python to run a lua script with command line inputs

我有一个带有命令行输入的 Lua 脚本,我想在 Python (2.7) 中 运行 并读取输出。例如,我将在终端 (Ubuntu 14.xx) 中 运行 的代码如下所示:

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)

正确的做法是什么?

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

试试这个:

import subprocess

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

如果您不确定,您通常可以传递在 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'])