1

I have a Lua script with command line inputs that I would like to run in Python (2.7) and read the output. For example the code I would run in terminal (Ubuntu 14.xx) looks like:

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

How do I run a Lua script with command line inputs in Python using the subprocess module? I think it would be something like this:

import subprocess

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

What is the right way to do this?

This is very similar to the link below but different in that I am trying to use command line inputs as well. The below question just calls a Lua function defined in the (Lua) script and feeds the inputs directly to that function. Any help would be very much appreciated.

Run Lua script from Python

Community
  • 1
  • 1
sfortney
  • 2,075
  • 6
  • 23
  • 43

2 Answers2

2

If you aren't sure, you can generally pass the verbatim string that works in the shell and split it with shlex.split:

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

However, you usually don't need to do this and can just split the arguments by hand if you know what they are ahead of time:

subprocess.check_output(['lua', 'sample.lua', '-arg1', 'helloworld', '-arg2', 'helloworld'])
mgilson
  • 300,191
  • 65
  • 633
  • 696
1

try this:

import subprocess

print subprocess.check_output('lua sample.lua -arg1 helloworld -arg2 "helloworld"', shell=True)
Fujiao Liu
  • 2,195
  • 2
  • 24
  • 28
  • Thanks for the reply. I tried this one and it didn't quite work. That might just be a function of my code though. Maybe it will help someone else who comes here – sfortney Jul 06 '16 at 04:47