7

I am trying to split a path given as a string into sub-parts using the "/" as a delimiter recursively and passed into a tuple. For ex: "E:/John/2012/practice/question11" should be ('E:', 'John', '2012', 'practice', 'question11').

So I've passed every character excluding the "/" into a tuple but it is not how I wanted the sub-parts joint as displayed in the example. This is a practice question in homework and would appreciate help as I am trying to learn recursion.

Thank You so much

user1757703
  • 2,925
  • 6
  • 41
  • 62

2 Answers2

12

Something like this

>>> import os
>>> s = "E:/John/2012/practice/question11"
>>> os.path.split(s)
('E:/John/2012/practice', 'question11')

Notice os.path.split() doesn't split up the whole path as str.split() would

>>> def rec_split(s):
...     rest, tail = os.path.split(s)
...     if rest == '':
...         return tail,
...     return rec_split(rest) + (tail,)
...
>>> rec_split(s)
('E:', 'John', '2012', 'practice', 'question11')

Edit: Although the question was about Windows paths. It's quite easy to modify it for unix/linux paths including those starting with "/"

>>> def rec_split(s):
...     rest, tail = os.path.split(s)
...     if rest in ('', os.path.sep):
...         return tail,
...     return rec_split(rest) + (tail,)
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • 2
    This solution is better because it is OS independent, and makes no assumptions about whether forward slashes or backslashes are used. – DaveP Nov 22 '12 at 05:45
  • This does not work in Linux, as "rest" eventually becomes "/". Unfortunately I do not have enough reputation yet to vote this down, thus this comment. :( I'm using this now: ` def rec_split(path): elements = [] while ((path != '/') and (path != '')): path, tail = os.path.split(path) elements.insert(0,tail) return elements ` – Axel Heider May 04 '15 at 11:19
  • 1
    @AxelHeider, sure if you like, but it's not recursive so you might want to change the name :) – John La Rooy May 04 '15 at 13:26
  • @John: you have a point there. At some point I change this implementation, as recursion seem not necessary there - but never bothered to change the name.... – Axel Heider May 05 '15 at 13:40
1

Your error is not in recursion, but rather what you're doing to concatenate the recursive results. Say you have reached ('E:', 'John', '2012', 'prac'), and the next character is 't'; you don't want to append 't' to the recursive result, you want to append it to the last word of the recursive result. Similarly, when you reach a separator, you want to initialise the new word as empty.

When you're doing recursion, you will (pretty much) always have two cases: a recursive one, and a terminal one. The terminal one is usually easy, and you did it correctly (if there's no string, there's no words). But I find it helps immensely if you try to have a specific example of the recursive case, somewhere mid-computation as above, to work out exactly what needs to happen.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Could you give me an example please? I don't understand how you go about adding it to the last word when doing it recursively. – user1757703 Nov 22 '12 at 04:29
  • I don't want to solve your homework for you, you should still work for it. But you can append a letter to the last element of a tuple with `path = last_path[:-1] + (last_path[-1] + s[0],)`. – Amadan Nov 22 '12 at 04:36