I am trying to wrap gnugo in a python script.
I've gone over the other questions in SO about wrapping CLI applications here and here and though they've helped somewhat, I haven't been able to get my script to work completely. It appears the process is killed after the first input from stdin.
The script looks like this:
import subprocess
proc = subprocess.Popen(['gnugo', '--mode', 'gtp'], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
c = raw_input('first command: ')
while True:
r = proc.communicate(c)
print 'gnugo says: ' + str(r)
c = raw_input('next command: ')
The error I'm getting is this:
$ python testgnu.py
first command: play b c4
gnugo says: ('=
', None)
next command: genmove w
Traceback (most recent call last):
File "testgnu.py", line 8, in <module>
r = proc.communicate(c)
File "/usr/local/Cellar/python/2.7.3/lib/python2.7/subprocess.py", line 754, in communicate
return self._communicate(input)
File "/usr/local/Cellar/python/2.7.3/lib/python2.7/subprocess.py", line 1307, in _communicate
self.stdin.flush()
ValueError: I/O operation on closed file
I am running python 2.7.3 (and it probably doesn't matter but the gnugo version is 3.8).
Some answers in the other questions suggested pexpect but I'd like to use as few external libraries as possible.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…