title: Python 3 writing to a pipe
body: <p>I'm trying to write some code to put data into a pipe, and I'd like the solution to be python 2.6+ and 3.x compatible.
  Example:</p>
  
  <pre><code>from __future__ import print_function
  
  import subprocess
  import sys
  
  if(sys.version_info &gt; (3,0)):
      print ("using python3")
      def raw_input(*prmpt):
          """in python3, input behaves like raw_input in python2"""
          return input(*prmpt)
  
  class pipe(object):
      def __init__(self,openstr):
          self.gnuProcess=subprocess.Popen(openstr.split(),
                                           stdin=subprocess.PIPE)
  
      def putInPipe(self,mystr):
          print(mystr, file=self.gnuProcess.stdin)
  
  if(__name__=="__main__"):
      print("This simple program just echoes what you say (control-d to exit)")
      p=pipe("cat -")
      while(True):
          try:
              inpt=raw_input()
          except EOFError:
              break
          print('putting in pipe:%s'%inpt)
          p.putInPipe(inpt)
  </code></pre>
  
  <p>The above code works on python 2.6 but fails in python 3.2 (Note that the above code was mostly generated with 2to3 -- I just messed with it a little to make it python 2.6 compatible.)</p>
  
  <pre><code>Traceback (most recent call last):
    File "test.py", line 30, in &lt;module&gt;
     p.putInPipe(inpt)
    File "test.py", line 18, in putInPipe
     print(mystr, file=self.gnuProcess.stdin)
  TypeError: 'str' does not support the buffer interface
  </code></pre>
  
  <p>I've tried the bytes function (e.g. print(bytes(mystr,'ascii')) suggested here,
  <a href="https://stackoverflow.com/questions/5471158/typeerror-str-does-not-support-the-buffer-interface">TypeError: &#39;str&#39; does not support the buffer interface</a>
  But that doesn't seem to work.
  Any suggestions?</p>