3

I am very new to Python as well a programming, and I would like to slice the folder path. For example, if my original path is:

C:/Users/arul/Desktop/jobs/project_folder/shots/shot_folder/elements/MexicoCity-Part1/

I would like to get the path like this:

C:/Users/arul/Desktop/jobs/project_folder/shots/shot_folder/

What are the methods of doing this in Python?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
arul
  • 33
  • 1
  • 3
  • Have you tried anything? [Stack Overflow](https://stackoverflow.com/help/how-to-ask) helps you with asking questions, so that the community is glad to help you – tschale Jun 11 '17 at 09:23

3 Answers3

6

You could use the pathlib module:

from pathlib import Path

pth = Path('C:/Users/arul/Desktop/jobs/project_folder/shots/shot_folder/')

print(pth)
print(pth.parent)
print(pth.parent.parent)  # C:/Users/arul/Desktop/jobs/project_folder

The module has a lot more of very convenient methods for handling paths: your problem could also be solved using parts like this:

print('/'.join(pth.parts[:-2]))

In Python 2.7 you could build your own parts function using os.path:

from os import path

pth = 'C:/Users/arul/Desktop/jobs/project_folder/shots/shot_folder/'

def parts(pth):
    ret = []
    head, tail = path.split(pth)
    if tail != '':
        ret.append(tail)
    while head != '':
        head, tail = path.split(head)
        ret.append(tail)
    return ret[::-1]

ret = path.join(*parts(pth)[:-2])
print(ret)  # C:/Users/arul/Desktop/jobs/project_folder
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
0

If you just want to split on elements, then use this.

>>> path = 'C:/Users/arul/Desktop/jobs/project_folder/shots/shot_folder/elements/MexicoCity-Part1/'
>>> path.split('elements')[0]
'C:/Users/arul/Desktop/jobs/project_folder/shots/shot_folder/'

One drawback of this approach is that it'll fail if you encounter the word elements in your path multiple times. In that case, you can do something like:

>>> '/'.join(path.split('/')[:-3]) + '/'
'C:/Users/arul/Desktop/jobs/project_folder/shots/shot_folder/'

Assuming you know the depth of the path you need.

cs95
  • 379,657
  • 97
  • 704
  • 746
  • @arul Cheers. Consider marking this answer if it helped, and close the question. :) – cs95 Jun 11 '17 at 09:39
  • Take a look at my solution @arul – void Jun 11 '17 at 09:55
  • This answer shouldn't be accepted . @Coldspeed what if the path has a different file seperator? Can be \ or / – void Jun 11 '17 at 10:23
  • Okay, you have a valid point. But can you please refrain from browbeating here? This is not a competition to see who can get the most upvotes. In the last 10 minutes you have made over 3 comments explaining why my answer is trash. Alright, you are contributing to a collective knowledge base, not racing at the derby. Please behave accordingly. Thank you. – cs95 Jun 11 '17 at 10:24
  • @s_vishnu Also I should mention hiro protagonist's answer is the best one here. – cs95 Jun 11 '17 at 10:26
  • Truth spoken. But sometimes it's good even great to be competitive. And i see in many posts related to python and that gave me a push. In a way it is making me a little positive. hope you take it in that way too :) – void Jun 11 '17 at 10:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146354/discussion-between-s-vishnu-and-coldspeed). – void Jun 11 '17 at 10:28
  • @s_vishnu That's all right, I've said all I need to. :x – cs95 Jun 11 '17 at 10:29
-1

You can do something like this:

folder = 'C:/Users/arul/Desktop/jobs/project_folder/shots/shot_folder/elements/MexicoCity-Part1/'

folder.rsplit('/', 3)[0]

str.rsplit() basically returns a list of the words in the string, separated by the delimiter string (starting from right).

Please have a look at documentation for more details on this method.

Chiheb Nexus
  • 9,104
  • 4
  • 30
  • 43
Sid
  • 76
  • 2
  • 8