code: def file_iterator(root_dir, extension=None, recurse=True):
      """Generator that iterates over all files in the given directory.
  
      Will recurse into sub-directories if `recurse` is True.
  
      Args:
        root_dir: Path to root directory to search for files in.
        extension: If given, only files with the given extension are opened.
        recurse: If True, subdirectories will be traversed. Otherwise, only files
            in `root_dir` are opened.
  
      Yields:
        Raw bytes (as a string) of each file opened.
  
      Raises:
        ValueError: When extension is an empty string. Leave as None to omit.
      """
      if extension is not None:
          if not extension:
              raise ValueError(
                  "File extension cannot be an empty string."
              )
          extension = extension.lower()
          if extension[0] != ".":
              extension = "." + extension
      dirs = [
          os.path.join(root_dir, child)
          for child in tf.gfile.ListDirectory(root_dir)
      ]
      while dirs:
          sub = dirs.pop()
          if tf.gfile.IsDirectory(sub):
              if recurse:
                  dirs.extend(
                      [
                          os.path.join(sub, child)
                          for child in tf.gfile.ListDirectory(sub)
                      ]
                  )
          else:
              if extension is None or sub.lower().endswith(extension):
                  with open(sub, "rb") as f:
                      yield f.read()
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import abc
  import inspect
  import os.path
  from magenta.pipelines import statistics
  import six
  import tensorflow.compat.v1
label_desc: Use image node_id 3 for calling a global function with example usage: file_iterator(root_dir, extension, recurse) without return types