(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.
Generator that iterates over all files in the given directory.
Generator that iterates over all files in the given directory .
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()
def file_iterator ( root_dir , extension = None , recurse = True ) : 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 ( )