0

I have an executable script, let's call it script.py. I am trying to run a series of commands within the terminal starting with

python3 ./differentscript.py console

which will allow me to type in additional commands.

However, I was wondering if this can be automated i.e. have my script somehow output and execute the commands?

Edit: I took a look at the links commented below; can someone provide an example of what this would look like?

noobling
  • 21
  • 5
  • 2
    `subprocess`: https://stackoverflow.com/questions/450285/executing-command-line-programs-from-within-python `os.system`: https://raspberrypi.stackexchange.com/questions/17017/how-insert-a-command-line-command-in-python-script/17029 – Matt Takao Aug 22 '17 at 16:45
  • Possible duplicate of [Calling an external command in Python](https://stackoverflow.com/questions/89228/calling-an-external-command-in-python) – Aurora0001 Aug 22 '17 at 16:49
  • Possible duplicate of [Executing command line programs from within python](https://stackoverflow.com/questions/450285/executing-command-line-programs-from-within-python) – Iván Rodríguez Torres Aug 22 '17 at 16:50
  • Thanks for the links guys. I am able to output and execute that command line. However, how do I get it to call multiple commands in succession? – noobling Aug 22 '17 at 17:10

1 Answers1

0

@Chen Xie

If you want to call a few commands at once, you can call it like below:

subprocess.Popen('echo First&echo Second&echo Third', stdout=subprocess.PIPE, shell=True).stdout.read()

It's not a Pythonic solution, but it works.

Surrerstry
  • 21
  • 3