code: def maybe_open(infile, mode='r'):
      
      # ENH: Exception safety?
      if isinstance(infile, basestring):
          handle = open(infile, mode)
          do_close = True
      else:
          handle = infile
          do_close = False
      yield handle
      if do_close:
          handle.close()
docs: Take a file name or a handle, and return a handle.
  
      Simplifies creating functions that automagically accept either a file name
      or an already opened file handle.