code: def _walk(path, follow_links=False, maximum_depth=None):
      
      root_level = path.rstrip(os.path.sep).count(os.path.sep)
      for root, dirs, files in os.walk(path, followlinks=follow_links):
          yield root, dirs, files
          if maximum_depth is None:
              continue
  
          if root_level + maximum_depth <= root.count(os.path.sep):
              del dirs[:]
docs: A modified os.walk with support for maximum traversal depth.