0

I have a Mac program with a command line interface that I am trying to automate from Python. So far I have:

os.system("cd /Applications/program/MyApp.app/Contents/bin/; ./MyApp -prompt;")

Which runs the CLI. Now I want to enter a command into the command line from Python. Is there a way to pass this in as an argument to the first command?

Example:

os.system("cd /Applications/program/MyApp.app/Contents/bin/; ./MyApp -prompt; and then run MySpecialCommand in CLI")

I'm not commited to a specific approach, I just need to be able to enter the command into the CLI from a python script.

2
  • 1
    Possible duplicate of Calling an external command in Python Commented Sep 8, 2017 at 19:32
  • Agreed with duplicate. The first (accepted) answer even explains why to use subprocess.call instead of os.system. Commented Sep 8, 2017 at 19:43

1 Answer 1

1

Try using sys.argv:

import os
import sys

my_string = ' '.join(sys.argv[1:])

template_cmd = "cd /Applications/program/MyApp.app/Contents/bin/; ./MyApp -prompt; {additional_arg}"

os.system(template_cmd.format(additional_arg=my_string))

This would be run as follows:

python my_script.py ls -l

Where ls -l would be your entered command.

Sign up to request clarification or add additional context in comments.

4 Comments

here, argparse is probably overkill (although it's a very useful module). The OP is only expecting one argument, with no need to transform or inspect them.
How would you write this If there were only ever going to be one command and it would always be the same?
Why would you have it in the CLI if it were always the same?
I want python to enter it into the programs command line. I don't want to enter it from the command line myself.

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.