Returns a recursive or non-recursive directory walker
175899
def _walk(recursion):
    """"""
    try:
        from scandir import walk as walk_function
    except ImportError:
        from os import walk as walk_function

    if recursion:
        walk = partial(walk_function)
    else:
        def walk(path):  # pylint: disable=C0111
            try:
                yield next(walk_function(path))
            except NameError:
                yield walk_function(path)
    return walk
230626
def walk(
    root_path: Union[str, pathlib.Path], top_down: bool = True
) -> Generator[
    Tuple[pathlib.Path, Sequence[pathlib.Path], Sequence[pathlib.Path]], None, None
]:
    
    root_path = pathlib.Path(root_path)
    directory_paths, file_paths = [], []
    for path in sorted(root_path.iterdir()):
        if path.is_dir():
            directory_paths.append(path)
        else:
            file_paths.append(path)
    if top_down:
        yield root_path, directory_paths, file_paths
    for directory_path in directory_paths:
        yield from walk(directory_path, top_down=top_down)
    if not top_down:
        yield root_path, directory_paths, file_paths
1264335
def sorted_walk(dir):
    
    for base, dirs, files in os.walk(dir):
        dirs.sort()
        files.sort()
        yield base, dirs, files
427485
function treewalk(path, prefix, depth, cb) {

      function sort(entry) { return self.sortEntry(entry.name + (entry.type === 'dir' ? '/' : '')); }

      self.readdir(path, function(err, list) { if (err) return cb(err);
        var ab = asyncbuilder(cb);

        // first normalize names to fix decomposed unicode e.g. from OSX-HFS
        u.each(list, function(entry) { entry.name = normalize(entry.name); });

        u.each(u.sortBy(list, sort), function(entry) {
          var pathname = u.join(path, entry.name);
          var pname = u.join(prefix, entry.name);
          if (self.exclude(entry)) return;
          if (entry.type === 'dir') {
            if (depth >= self.depth) return;
            return treewalk(pathname, pname, depth + 1, ab.asyncAppend()); // recurse
          }
          if (self.mm && !self.mm.match(pname.slice(1))) return; // failed minimatch glob test
          if (entry.type === 'file') {
            var listEntry = { filepath:pname };
            if (entry.sha) { listEntry.sha = entry.sha; }
            if (entry.hash) { listEntry.hash = entry.hash; }
            return ab.append(listEntry);
          }
        });

        ab.complete();
      });
    }
96999
function crawlDirectory(parent, sync) {
    if (sync) {
        return crawlDirectory(parent, sync);
    }
    /* Use `find` if available, because it's fast! */
    return exec('find ' + parent + ' -not -type d').spread(function (stdin) {
        return stdin.split('\n').filter(function (file) {
            return !!file;
        });
    }).fail(function () {
        return crawlDirectoryWithFS(parent, sync);
    });
}
591660
function rmdirSyncRecursive(root, chunks) {

  var chunk = chunks.pop();
  if (!chunk) {
    return;
  }
  var pathname = path.join(root, '..'); // backtrack

  if (fs.existsSync(root) === false) { // already done
    return rmdirSyncRecursive(root, chunks);
  }
  var err = fs.rmdirSync(root);
  return err ? err : rmdirSyncRecursive(pathname, chunks); // let's magic
}
151780
function mkdirsInner(dirnames, currentPath, callback) {
    // Check for completion and call callback
    if (dirnames.length === 0) {
      return _callback(callback);
    }

    // Make next directory
    var dirname = dirnames.shift();
    currentPath = path.join(currentPath, dirname);
    fs.mkdir(currentPath, function(err) {
      return mkdirsInner(dirnames, currentPath, callback);
    });
  }
1096168
def walk(value, walker, path=None, seen=None):
  
  seen = seen or set()
  path = path or []

  # Recursion
  if id(value) in seen:
    walker.visitRecursion(path)
    return

  # Error
  if isinstance(value, Exception):
    walker.visitError(path, value)
    return

  # List
  if isinstance(value, list):
    # Not actually a tuple, but okay
    recurse = walker.enterList(value, path)
    if not recurse: return
    next_walker = walker if recurse is True else recurse

    with TempSetAdd(seen, id(value)):
      for i, x in enumerate(value):
        walk(x, next_walker, path=path + ['[%d]' % i], seen=seen)

      walker.leaveList(value, path)
    return

  # Scalar
  if not isinstance(value, framework.TupleLike):
    walker.visitScalar(path, value)
    return

  # Tuple
  recurse = walker.enterTuple(value, path)
  if not recurse: return
  next_walker = walker if recurse is True else recurse

  with TempSetAdd(seen, id(value)):
    keys = sorted(value.keys())
    for key in keys:
      key_path = path + [key]
      elm = get_or_error(value, key)
      walk(elm, next_walker, path=key_path, seen=seen)

    walker.leaveTuple(value, path)
633322
def mkdir(*args):
    
    path = ''
    for chunk in args:
        path = os.path.join(path, chunk)
        if not os.path.isdir(path):
            os.mkdir(path)
    return path
749302
def iter_directory(directory):
    """"""
    for path, dir, files in os.walk(directory):
        for f in files:
            filepath = os.path.join(path, f)
            key = os.path.relpath(filepath, directory)
            yield (filepath, key)
859956
def tree_walk(cls, directory, tree):
        """"""
        results = []
        dirs = [d for d in tree if d != FILE_MARKER]
        files = tree[FILE_MARKER]
        results.append((directory, dirs, files))
        for d in dirs:
            subdir = os.path.join(directory, d)
            subtree = tree[d]
            results.extend(cls.tree_walk(subdir, subtree))
        return results
1236170
def walk_dirs(path, include=None, include_ext=None, exclude=None,
        exclude_ext=None, recursion=True, file_only=False,
        use_default_pattern=True, patterns=None):
    
    default_exclude = ['.svn', '_svn', '.git']
    default_exclude_ext = ['.pyc', '.pyo', '.bak', '.tmp']
    exclude = exclude or []
    exclude_ext = exclude_ext or []
    include_ext = include_ext or []
    include = include or []

    if not os.path.exists(path):
        raise StopIteration

    for r in os.listdir(path):
        if match(r, exclude) or (use_default_pattern and r in default_exclude):
            continue
        if include and r not in include:
            continue
        fpath = os.path.join(path, r)
        if os.path.isdir(fpath):
            if not file_only:
                if patterns and match(r, patterns):
                    yield os.path.normpath(fpath).replace('\\', '/')
            if recursion:
                for f in walk_dirs(fpath, include, include_ext, exclude,
                    exclude_ext, recursion, file_only, use_default_pattern, patterns):
                    yield os.path.normpath(f).replace('\\', '/')
        else:
            ext = os.path.splitext(fpath)[1]
            if ext in exclude_ext or (use_default_pattern and ext in default_exclude_ext):
                continue
            if include_ext and ext not in include_ext:
                continue
            if patterns:
                if not match(r, patterns):
                    continue
            yield os.path.normpath(fpath).replace('\\', '/')
1076173
def walkfiles(startdir, regex=None, recurse=True):
    """"""
    for r,_,fs in os.walk(startdir):
        if not recurse and startdir != r:
            return
        for f in fs:
            path = op.abspath(op.join(r,f))
            if regex and not _is_match(regex, path):
                    continue
            if op.isfile(path):
                yield path
987054
function getDirectoryTree(dir) {
    const items = [];
    return new Promise(function(resolve) {
        fs.walk(dir)
        .on('data', function (item) {
            items.push(item.path);
        })
        .on('end', function () {
            resolve(items);
        });
    });
}
1331124
def tail(fpath, n=2, trailing=True):
    
    return path_ndir_split(fpath, n=n, trailing=trailing)
1328089
def next(self):
        
        # get depth of starting root directory
        base_depth = self.__root.count(os.path.sep)

        # walk files and folders
        for root, subFolders, files in os.walk(self.__root):

            # apply folder filter
            if not self.filter_folder(root):
                continue

            # make sure we don't pass depth limit
            if self.__depth_limit is not None:
                curr_depth = root.count(os.path.sep)
                if curr_depth - base_depth > self.__depth_limit:
                    continue

            # if need to return folders return it
            if self.__ret_folders:
                yield root

            # return files
            if self.__ret_files:
                for f in files:
                    yield os.path.join(root, f)

        # end iterator
        raise StopIteration
999830
def finddirs(pattern, path='.', exclude=None, recursive=True):
    """"""
    import fnmatch
    import os
    if recursive:
        for root, dirnames, filenames in os.walk(path):
            for pat in _to_list(pattern):
                for dirname in fnmatch.filter(dirnames, pat):
                    dirpath = join(abspath(root), dirname)
                    for excl in _to_list(exclude):
                        if excl and fnmatch.fnmatch(dirpath, excl):
                            break
                    else:
                        yield dirpath
    else:
        for pat in _to_list(pattern):
            for dirname in fnmatch.filter(listdirs(path), pat):
                dirpath = join(abspath(path), dirname)
                for excl in _to_list(exclude):
                    if excl and fnmatch.fnmatch(dirpath, excl):
                        break
                else:
                    yield dirpath