0

I am trying to set up a (iron)python script that shows some processing information to the command line. Something like:

list = ["a", "b", "c"]
for i in list:
    cmd /k echo str(list.index(i)) #just echo index and leave window open so user can see progression

The challenge is to send multiple echo commands to the same instance of the command line.

Note: I can't just use print(i) in this case, because this ironpython script will be running inside a visual programming application, that does not allow printing to the command line. The goal of the script is that users can see progression during heavy operations.

I have come across multiple threads that suggest subprocess for this. I've seen and tested a lot of examples but I can get none of them to work. I could really use some help with that.

For example, when I do this...

import subprocess
proc=subprocess.Popen("echo hello world", shell=True, stdout=subprocess.PIPE)

.... then I can see that 'hello world' is being returned, so the echo was probably successful. But I never see a shell window pop up.

Edit 1

Some more test results: method 1 gives me a good result (command is executed and screen is left open for reading); method 2: adding stdout opens the shell and leaves the screen open but without a visible result - theres nothing to read; method 3: adding stdin flashes the screen (closes it too soon)

method 1:

process = Popen("cmd /k dir")

method2:

process = Popen("cmd /k dir",stdout=subprocess.PIPE)

method 3:

process = Popen('cmd /k dir',stdin=subprocess.PIPE)

Edit 2

Method 4 - creates two separate cmd windows, one showing a, one showing b. Not it.

process = Popen("cmd /k echo a")
process = Popen("cmd /k echo b")

Method 5 - adding process.stdin.write after stdin - just flashes

process = Popen('cmd /k echo a',stdin=subprocess.PIPE)
process.stdin.write("cmd /k echo b")

It seems so simple what I'm looking for but its giving me a headache...

  • Are you wanting to open a new CMD window from a Python script and have the output of the script printed there? – James Oct 01 '19 at 08:20
  • Yes, open a new CMD window from a python script and show the above mentioned list processing info (current index being processed for example) –  Oct 01 '19 at 08:26

1 Answers1

0

What you are trying to do is not as simple as you think. Because the CMD window is a different process from your IronPython script, to do anything interactive (like showing the progress of a script), you need to use some form of inter-process communication.

For example, you could write a server script to run in the CMD window and then send messages to it from your IronPython script. But then you have to worry about how to start the server, how to stop it, and how to synchronise with the IronPython client script. You can see an example of this kind of inter-process communication in this StackOverflow answer.

Before trying something like that I would make sure that there's no way to display progress in your visual programming application, as staying inside the same process is likely to be easier.

Jack Taylor
  • 5,588
  • 19
  • 35
  • Thank you for your answer. It does shed some light. I am thinking now that maybe using subprocess is not the right way to go. All Im really looking for is 'just a simple send echo command' towards the cmd. Maybe there is another way to get this done? other than subprocess? There do exist some other ways to display progress in my visual programming application. For example writing to a log file. Or some not-so-user-friendly progress bar module. But I have exhausted those options and was looking for a more elegant way to get this done. –  Oct 01 '19 at 13:25