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

2 Answers 2

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'])
Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me and looks like the most complete answer. Thanks!
1

try this:

import subprocess

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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.