【问题标题】:Using the subprocess module in python to run a lua script with command line inputs使用 python 中的 subprocess 模块运行带有命令行输入的 lua 脚本
【发布时间】:2016-11-08 01:05:45
【问题描述】:

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

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

如何使用 subprocess 模块在 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

【问题讨论】:

    标签: python python-2.7 command-line lua subprocess


    【解决方案1】:

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

    【讨论】:

    • 这对我有用,看起来是最完整的答案。谢谢!
    【解决方案2】:

    试试这个:

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

    【讨论】:

    • 感谢您的回复。我试过这个,但效果不太好。不过,这可能只是我的代码的一个功能。也许它会帮助其他来这里的人
    猜你喜欢
    • 2014-09-30
    • 1970-01-01
    • 2014-09-18
    • 2018-04-01
    • 2016-09-05
    • 2015-06-02
    • 2019-07-26
    • 2018-09-06
    • 2018-02-12
    相关资源
    最近更新 更多