简体   繁体   中英

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM