title: IOError Input/Output Error When Printing
body: <p>I have inherited some code which is periodically (randomly) failing due to an Input/Output error being raised during a call to print.  I am trying to determine the cause of the exception being raised (or at least, better understand it) and how to handle it correctly.</p>
  
  <p>When executing the following line of Python (in a 2.6.6 interpreter, running on CentOS 5.5):</p>
  
  <pre><code>print &gt;&gt; sys.stderr, 'Unable to do something: %s' % command
  </code></pre>
  
  <p>The exception is raised (traceback omitted):</p>
  
  <pre><code>IOError: [Errno 5] Input/output error
  </code></pre>
  
  <p>For context, this is generally what the larger function is trying to do at the time:</p>
  
  <pre><code>from subprocess import Popen, PIPE
  import sys
  def run_commands(commands):
      for command in commands:
          try:
              out, err = Popen(command, shell=True, stdout=PIPE, stderr=PIPE).communicate()
              print &gt;&gt; sys.stdout, out
              if err:
                  raise Exception('ERROR -- an error occurred when executing this command: %s --- err: %s' % (command, err))
          except:
              print &gt;&gt; sys.stderr, 'Unable to do something: %s' % command
  run_commands(["ls", "echo foo"])
  </code></pre>
  
  <p>The <code>&gt;&gt;</code> syntax is not particularly familiar to me, it's not something I use often, and I understand that it is perhaps the <a href="https://stackoverflow.com/questions/5574702/how-to-print-to-stderr-in-python">least preferred way</a> of writing to stderr.  However I don't believe the alternatives would fix the underlying problem.</p>
  
  <p>From the documentation I have read, IOError 5 is often misused, and somewhat loosely defined, with different operating systems using it to cover different problems.  The best I can see in my case is that the python process is no longer attached to the terminal/pty.</p>
  
  <p>As best I can tell nothing is disconnecting the process from the stdout/stderr streams - the terminal is still open for example, and everything 'appears' to be fine.  Could it be caused by the child process terminating in an unclean fashion?  What else might be a cause of this problem - or what other steps could I introduce to debug it further?</p>
  
  <p>In terms of handling the exception, I can obviously catch it, but I'm assuming this means I wont be able to print to stdout/stderr for the remainder of execution?  Can I reattach to these streams somehow - perhaps by resetting <code>sys.stdout</code> to <code>sys.__stdout__</code> etc?  In this case not being able to write to stdout/stderr is not considered fatal but if it is an indication of something starting to go wrong I'd rather bail early.</p>
  
  <p>I guess ultimately I'm at a bit of a loss as to where to start debugging this one...</p>