As part of some automation work, I needed to execute a program which requires each command to be explictely validated by the user by expliciltely selecting “y” or “n” , on the command line. Since I was processing a large number of files, I decided to use python script.
I used the subprocess module of python to invoke the external program. It was easy to capture the output sent on stdout. In order to send ‘y‘ on stdin for each iteration, I tried sending ‘y‘ on stdin, but that would not work. The script would hang. After discussing this, with more experienced python programmer, it was suggested that one possible reason why the script hangs is may be because the stdin buffer wasn’t flushed. Both of us were not sure how to do that. Then it was discussed that when we run the external program from command line, we not only type ‘y‘ as response, but we also hit Enter there after, which results in flushing the stdin buffer. So may be that is what I ought to try.
To my surprise, it worked. So the solution was to pass ‘y/n‘ instead of single ‘y‘
Here is how my code looked like :
import subprocess
cmd = "...." # The command you wish to execute
proc = subprocess.Popen(cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
print proc.communicate('Y\n')[0]