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?

1
  • Have you tried anything? Stack Overflow helps you with asking questions, so that the community is glad to help you Commented Jun 11, 2017 at 9:23

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

1 Comment

thank you hiro, but want this to be done in python 2.7.13 .
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.

8 Comments

@arul Cheers. Consider marking this answer if it helped, and close the question. :)
Take a look at my solution @arul
This answer shouldn't be accepted . @Coldspeed what if the path has a different file seperator? Can be \ or /
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.
@s_vishnu Also I should mention hiro protagonist's answer is the best one here.
|
-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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.