0

I have a path as '/img/dataset/A/B/3/5/img1.png' How can I extract the '/A/B/3/5/img1.png' part out of this?

I tried
path = '/img/dataset/A/B/3/5/img1.png'

extracted = path.partition('/')

This gave me the whole path as the output.

1 Answer 1

1

This should work :

path[len('/img/dataset'):]

Strings share some properties with lists, indexing and len beeing some.

Sign up to request clarification or add additional context in comments.

1 Comment

For more complicated path manipulations it's better to use PathLib: docs.python.org/3/library/pathlib.html

Your Answer

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