title: Use StringIO as stdin with Popen
body: <p>I have the following shell script that I would like to write in Python (of course <code>grep .</code> is actually a much more complex command):</p>
  
  <pre><code>#!/bin/bash
  
  (cat somefile 2&gt;/dev/null || (echo 'somefile not found'; cat logfile)) \
  | grep .
  </code></pre>
  
  <p>I tried this (which lacks an equivalent to <code>cat logfile</code> anyway):</p>
  
  <pre><code>#!/usr/bin/env python
  
  import StringIO
  import subprocess
  
  try:
      myfile = open('somefile')
  except:
      myfile = StringIO.StringIO('somefile not found')
  
  subprocess.call(['grep', '.'], stdin = myfile)
  </code></pre>
  
  <p>But I get the error <code>AttributeError: StringIO instance has no attribute 'fileno'</code>.</p>
  
  <p>I know I should use <code>subprocess.communicate()</code> instead of StringIO to send strings to the <code>grep</code> process, but I don't know how to mix both strings and files.</p>