-3

It is possible to make a python script which opens a cmd window and enters 5 commands one by one, and after waits for an external trigger to continue entering another 2 commands in the same window.

It is posibble? I hope You understand what I ask. PS: maybe you can share with me a sample code or something.

Thank you in advance. M.

Mihail
  • 1
  • 1
  • 1
    Is it important for you that the terminal actually opens or does it suffice if you only run the commands? – Ohumeronen Jul 14 '16 at 11:39
  • You can take a look at Python's [`subprocess` module](https://docs.python.org/2/library/subprocess.html) – ForceBru Jul 14 '16 at 11:40
  • Its not important It can be either way, but maybe the terminal should be on in order to see the progress of the commands. – Mihail Jul 14 '16 at 11:41
  • 1
    Possible duplicate of [How to execute a command prompt command from python](http://stackoverflow.com/questions/5486725/how-to-execute-a-command-prompt-command-from-python) – SiHa Jul 14 '16 at 11:42
  • i guess you want to see them typed in, as if somebody was typing them, right? – Ma0 Jul 14 '16 at 11:42
  • Yes, something like this if its posible – Mihail Jul 14 '16 at 11:44
  • Possible duplicate of [Calling an external command in Python](http://stackoverflow.com/questions/89228/calling-an-external-command-in-python) – Łukasz Rogalski Jul 14 '16 at 11:44

1 Answers1

3

What i have done in the past is use Python to write a .bat file and run it. And this does produce the result you describe. You can do this like that:

import subprocess

with open(r'my_bat_file.bat','w') as fout:
    fout.write('command no1')
    fout.write('command no2')
    ...
    fout.write('command non')
    fout.write('pause')

subprocess.run(r'my_bat_file.bat', creationflags=subprocess.CREATE_NEW_CONSOLE)

The pause command keeps the cmd open and waiting for a key stroke. When the key even logs, execution of the bat file will continue. If the pause is the last line in your batch file, cmd will close.

Ma0
  • 15,057
  • 4
  • 35
  • 65
  • Yes, I know about bat file, but after 4 commands I want to wait until i command with a trigger to enter another 2 commands in the same window. – Mihail Jul 14 '16 at 11:54
  • @Mihail why not add them from the beginning ? They execute sequentially any way – Ma0 Jul 14 '16 at 11:56
  • Because the last 2 commands are activated from an external system such a button or something. – Mihail Jul 14 '16 at 12:00
  • @Mihail Adding a `pause` command to the shell does not help you either, does it? – Ma0 Jul 14 '16 at 12:02
  • This pause command it like a delay? because with delay will not work, just because I don't have a specific time for the last commands, its entirely depends on the feedback of some device. – Mihail Jul 14 '16 at 12:04
  • it is not. it pauses until you hit a key with the cmd window on focus – Ma0 Jul 14 '16 at 12:05
  • Yes, It will maybe work, thank you for your help, I will try your suggestions in a few hours And I will tell you. – Mihail Jul 14 '16 at 12:09