Skip to content

Review: normalising path splitter respecting absolute paths #15

@thclark

Description

@thclark

From amy.utils.path we have a path splitter which retains the initial '/' if present in a path.

Review and refactor into the SDK if helpful.

import os.path
import sys


def split(path, max_depth=1):
   """
   http://nicks-liquid-soapbox.blogspot.co.uk/2011/03/splitting-path-to-list-in-python.html
   :param path:        str     Path to split
   :param max_depth:   int     Recursion limit (max number of directories in the path to be split out), default 1.
                               Setting max_depth=1 gives normal (head, tail) result consistent with
                               os.path.split(path), whilst setting max_depth=None allows recursion to the system limit
   :return:
   """

   if max_depth is None:
       # Add one so that the system will raise a recursion exception when the limit is reached, instead of quietly returning the wrong thing.
       max_depth = sys.getrecursionlimit() + 1

   def splitpath_recurse(path, depth=1):
       # TODO replace the last \ or / with os.path.sep to allow processing of paths not generated on this system
       (head, tail) = os.path.split(path)
       return splitpath_recurse(head, depth - 1) + [tail] if depth and head and head != path else [head or tail]

   return splitpath_recurse(path, max_depth)

Metadata

Metadata

Assignees

Labels

backendRelated to the back endfeatureA new feature of the app

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions