I have a program.exe that runs on cmd window.
How the program works:
It requires three inputs, one at a time. ie. on cmd window Program.exe program starts, waits for 1st input Input 1 program continues, waits for 2nd input Input 2 program runs and reads information into a csv file Input 3: Exit Input Terminates the program and allow the csv file to be accessed
After which, I have to repeat these steps all over, albeit having to rename the files to be read (which I have already figured out).
Hence, I would like to automate it and let it run automatically since it would take up to hours to fully read the files and I don't want to have to physically exit the program and start it up again.
-- A Beginner in Python
My code currently:
import subprocess
subprocess.Popen('Start cmd /k program.exe && Input 1 && Input 2 && Input 3, shell=True)
However, it only opens a cmd window and starts up the program but doesn't enter the inputs into the program.
Returncodes returns a "None", so it would seem that the Inputs are not being entered as the command: Program.exe does not terminate and hence, the subsequent portions do not start.
I've tried:
import subprocess
subprocess.Popen('Start cmd /k program.exe Input 1 Input 2 Input 3', shell=True)
It will instead run "program.exe Input 1 Input 2 Input 3" as a single string into cmd, causing an error.
and also
code = subprocess.Popen('Start cmd /k program.exe', shell=True)
code.communicate(input=Input 1)
But again, it only opens a cmd window and starts up the program My guess is that because the program does not terminate, nothing after that command gets input. However, I am not really sure how .communicate works and would like some insights into the issue.
Sorry if this might sound foolish. I am a complete novice in Python and only have some experience in Matlab.
I am wondering if there is any way to make the program run automatically and exit when it senses that it have finished running (based on how the program is used).
CodePudding user response:
you could use subprocess.run with inputs separated by new lines
#inputs stored in a list
inputs=['input1','input2','input3']
#calling the subprocess with the inputs
process = subprocess.run(['program.exe'],
input='\n'.join(inputs),
capture_output=True,
text=True)
result = process.stdout.split("\n")
There is detailed description on how to use this command with further scopes in the official python website subprocess.run
hope this helps!
CodePudding user response:
Here are an example where I use the process object to pass stdin to subprocess. This example expect that program.exe
at some point returns after the three inputs is process - and it then collect any output program.exe
generate.
p = subprocess.Popen('./program.exe', stdin = subprocess.PIPE, stdout = subprocess.PIPE)
p.stdin.write(b'input1\n')
p.stdin.write(b'input2\n')
p.stdin.write(b'input3\n')
print(p.communicate())
CodePudding user response:
First Thoughts
import subprocess subprocess.Popen('Start cmd /k program.exe && Input 1 && Input 2 && Input 3, shell=True)
I think you're missing a quote there. I changed it to correct it and it opens the program.exe itself, throws an error as I don't have the "program.exe" you have specified.
import subprocess
subprocess.Popen('Start cmd /k program.exe Input 1 Input 2 Input 3', shell=True)
the corrected code would be :
Also If it is possible , can you explain the nature of program.exe
Contemplation
I think what you are trying to do is add a wait to the program.exe so that it waits for the for the first output to be processed.
You can add a wait with python time.sleep()
function in python