Search code examples
pythonsplitpathroot

splitting the root from path in a platform-agnostic way in python


i have a path that describe a folder:

s = foo/bar/baz

and i need to split the root from everything else. I know that i could use:

s.split('/',1)

but this is not platform agnostic. Also it seems that the os.path module doesn't implement this feature, so i'm a bit lost. Writing the function from scratch looks like ridiculous amount of work for the task.


Solution

  • You can use the os.path.sep variable whose value is defined according to the platform in which the module is installed.

    import os
    s.split(os.path.sep, 1)