I have created a function to split paths into lists of directories in python like so:
splitAllPaths = lambda path: flatten([[splitAllPaths(start), end] if start else end for (start, end) in [os.path.split(path)]])
with this helper function:
#these only work one directory deep
def flatten(list_of_lists):
return list(itertools.chain.from_iterable(list_of_lists))
The output from this function looks like so:
> splitAllPaths('./dirname/dirname2/foo.bar')
[[[['.'], 'dirname'], 'dirname2'], 'foo.bar']
now I want this as a flat list. my attempts are as follows (with the output):
> flatten(splitAllPaths('./diname/dirname2/foo.bar'))
['.', 'd', 'i', 'r', 'n', 'a', 'm', 'e', 'd', 'i', 'r', 'n', 'a', 'm', 'e', '2', 'f', 'o', 'o', '.', 'b', 'a', 'r']
and
> reduce(list.__add__, (list(mi) for mi in splitAllPaths('./dirname/dirname2/foo.bar')))
me2/foo.bar')))
[[['.'], 'dirname'], 'dirname2', 'f', 'o', 'o', '.', 'b', 'a', 'r']
How do I unfold this list correctly (I would also welcome any suggestions for how to improve my splitAllPaths
function)?