How to run executable from python and pass it arguments asked for?
command, python, subprocess
Solution
You can use `subprocess` and the `Popen.communicate` method:
import subprocess
def create_grid(*commands):
process = subprocess.Popen(
['grid.exe'],
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE)
process.communicate('\n'.join(commands) + '\n')
if __name__ == '__main__':
create_grid('grid.grd', 'yes', 'not really')
The "communicate" method essentially passes in input, as if you were typing it in. Make sure to end each line of input with the newline character.
If you want the output from `grid.exe` to show up on the console, modify `create_grid` to look like the following:
def create_grid(*commands):
process = subprocess.Popen(
['grid.exe'],
stdin=subprocess.PIPE)
process.communicate('\n'.join(commands) + '\n')
Caveat: I haven't fully tested my solutions, so can't confirm they work in every case.
Problem
I can't figure out how to run executable from python and after that pass it commands which is asking for one by one. All examples I found here are made through passing arguments directly when calling executable. But the executable I have needs "user input". It asks for values one by one. Example: ``` subprocess.call(grid.exe) >What grid you want create?: grid.grd >Is it nice grid?: yes >Is it really nice grid?: not really >Grid created ```