I have a 3d party python script which takes input from the command line. The relevant code from this script (input.py) looks like the following:
import sys
def chooseinput():
valid_inputs = ('a', 'b')
inp = raw_input('Enter choice (%s): ' % "/".join(valid_inputs))
if inp not in valid_inputs:
sys.stderr.write("Unsupported input %s\n" % inp)
return
print 'You chose ' + '\'' + inp + '\''
return inp
if __name__ == "__main__":
chooseinput()
# do something with the input...
chooseinput()
# do something with the input...
I'm trying to write another python script (harness.py) to generate the inputs for the above script.
import subprocess
def harness():
p = subprocess.Popen(['python', 'input.py'], stdin=subprocess.PIPE)
p.stdin.write('a')
p.stdin.write('b')
if __name__ == '__main__':
harness()
From the command line, I run:
$ python harness.py
Enter choice (a/b): Enter choice (a/b): Traceback (most recent call last):
File "input.py", line 13, in <module>
chooseinput()
File "input.py", line 5, in chooseinput
inp = raw_input('Enter choice (%s): ' % "/".join(valid_inputs))
EOFError: EOF when reading a line
If I only have one input in the first script, then I can make the second script work by removing the second write call. If the first script requires more than one input, then I get the above error.