簡體   English   中英

使用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)

這樣做的正確方法是什么?

這與下面的鏈接非常相​​似,但不同之處在於我也嘗試使用命令行輸入。 下面的問題只是調用(Lua)腳本中定義的Lua函數,並將輸入直接提供給該函數。 任何幫助將非常感謝。

從Python運行Lua腳本

如果您不確定,通常可以傳遞在shell中工作的逐字字符串,並使用shlex.split將其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'])

嘗試這個:

import subprocess

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM