Python subprocess wait keypress

Viewed 50

I want to run a program using the subprocess module. While running the program, there are cases where it waits for a button press to continue. It is not waiting for this input to the stdin input. The input sent to stdin is ignored by the program. Only when I press a button on the console will the program continue running.

I tried it like this:

proc = Popen("festival.exe level.sok", stdin=PIPE, text=True)
proc.stdin.write('.')
proc.stdin.flush()

It this case nothing happened.

and like this:

proc = Popen...
proc.communicate('.')

In this case, the python code will not continue running until I press a button on the console. If I set a timeout then the python code continues to run but festival.exe does not continue to run.

What should I do to make festival.exe continue running?

P.S.: festival.exe is a solver for sokoban. This case occurs when the given level cannot be solved. E.g.:

########  
#.. $ $#
#  @   #
########
1 Answers

assuming you have the festival program like this:


from random import getrandbits


solved = getrandbits(1)
if solved:
    print("executed correctly")
    
else:
    print('a error ocurred')
    input('press a button to continue') 
    exit(1)

you can solve with:

from subprocess import Popen, PIPE


p = Popen(['python3','festival.py'],stdin=PIPE)
p.communicate(input=b'any\n')
  
Related