I am having a strange problem when using subprocess.Popen.communicate (). For background, I want to execute an application from my python script. When I run the program from the command line, I do it like this (UNIX):
$ echo "input text" | / path / to / myapp
From my script, I also want to pass the input to the application. So, I tried the following. But I get a “broken pipe” error when I try to send an input with a message ():
>>> cmd = ['/ path / to / myapp']
>>> p = subprocess.Popen (cmd, stdout = subprocess.PIPE, stderr = subprocess.STDOUT, stdin = subprocess.PIPE)
>>> out, err = p.communicate ('input text')
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python2.5/subprocess.py", line 670, in communicate
return self._communicate (input)
File "/usr/lib/python2.5/subprocess.py", line 1223, in _communicate
bytes_written = self._write_no_intr (self.stdin.fileno (), buffer (input, input_offset, 512))
File "/usr/lib/python2.5/subprocess.py", line 1003, in _write_no_intr
return os.write (fd, s)
OSError: [Errno 32] Broken pipe
To make the situation stranger, if I don't leave the input, I don't get any errors. However, this is not a good way, because the application requires input to work.
>>> p = subprocess.Popen (cmd, stdout = subprocess.PIPE, stderr = subprocess.STDOUT, stdin = subprocess.PIPE)
>>> out, err = p.communicate ()
>>> print out
[error from myapp regarding lack of input]
, ?