Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function to split path #117

Closed
ghost opened this issue Jan 22, 2017 · 2 comments
Closed

Function to split path #117

ghost opened this issue Jan 22, 2017 · 2 comments

Comments

@ghost
Copy link

ghost commented Jan 22, 2017

Would something like this be acceptable?

    def split_all(path):
        all_parts = []
        while True:
            parts = os.path.split(path)
            if parts[0] == path:  # sentinel for absolute paths
                all_parts.insert(0, parts[0])
                break
            elif parts[1] == path:  # sentinel for relative paths
                all_parts.insert(0, parts[1])
                break
            else:
                path = parts[0]
                all_parts.insert(0, parts[1])
        return all_parts
@harvimt
Copy link

harvimt commented Jan 22, 2017

I recommend you use pathlib it's part of the standard library in Python 3.4+ and it's been backported to earlier python.

You just do pathlib.Path(path).parts to get this functionality.

@ghost
Copy link
Author

ghost commented Jan 22, 2017

Thank you.

@ghost ghost closed this as completed Jan 22, 2017
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant