code: def _to_filehandle(fname, flag="r", return_opened=False):
      """
      Returns the filehandle corresponding to a string or a file.
      If the string ends in '.gz', the file is automatically unzipped.
  
      Parameters
      ----------
      fname : string, filehandle
          Name of the file whose filehandle must be returned.
      flag : string, optional
          Flag indicating the status of the file ('r' for read, 'w' for write).
      return_opened : boolean, optional
          Whether to return the opening status of the file.
      """
      if _is_string_like(fname):
          if fname.endswith(".gz"):
              import gzip
  
              fhd = gzip.open(fname, flag)
          elif fname.endswith(".bz2"):
              import bz2
  
              fhd = bz2.BZ2File(fname)
          else:
              fhd = file(fname, flag)
          opened = True
      elif hasattr(fname, "seek"):
          fhd = fname
          opened = False
      else:
          raise ValueError("fname must be a string or file handle")
      if return_opened:
          return fhd, opened
      return fhd
  
imports: from __future__ import division, absolute_import, print_function
  import sys
  import numpy
  import numpy.core.numeric
  from numpy.compat import asbytes, asunicode, bytes, basestring
label_desc: Use image node_id 4 for calling a global function with example usage: _to_filehandle(fname, flag, return_opened) and returns: fhd, fhd,  opened