Split a pathname into drive, root and tail. The tail contains anything after the root.
(p, /)
| 170 | from nt import _path_splitroot_ex as splitroot |
| 171 | except ImportError: |
| 172 | def splitroot(p, /): |
| 173 | """Split a pathname into drive, root and tail. |
| 174 | |
| 175 | The tail contains anything after the root.""" |
| 176 | p = os.fspath(p) |
| 177 | if isinstance(p, bytes): |
| 178 | sep = b'\\' |
| 179 | altsep = b'/' |
| 180 | colon = b':' |
| 181 | unc_prefix = b'\\\\?\\UNC\\' |
| 182 | empty = b'' |
| 183 | else: |
| 184 | sep = '\\' |
| 185 | altsep = '/' |
| 186 | colon = ':' |
| 187 | unc_prefix = '\\\\?\\UNC\\' |
| 188 | empty = '' |
| 189 | normp = p.replace(altsep, sep) |
| 190 | if normp[:1] == sep: |
| 191 | if normp[1:2] == sep: |
| 192 | # UNC drives, e.g. \\server\share or \\?\UNC\server\share |
| 193 | # Device drives, e.g. \\.\device or \\?\device |
| 194 | start = 8 if normp[:8].upper() == unc_prefix else 2 |
| 195 | index = normp.find(sep, start) |
| 196 | if index == -1: |
| 197 | return p, empty, empty |
| 198 | index2 = normp.find(sep, index + 1) |
| 199 | if index2 == -1: |
| 200 | return p, empty, empty |
| 201 | return p[:index2], p[index2:index2 + 1], p[index2 + 1:] |
| 202 | else: |
| 203 | # Relative path with root, e.g. \Windows |
| 204 | return empty, p[:1], p[1:] |
| 205 | elif normp[1:2] == colon: |
| 206 | if normp[2:3] == sep: |
| 207 | # Absolute drive-letter path, e.g. X:\Windows |
| 208 | return p[:2], p[2:3], p[3:] |
| 209 | else: |
| 210 | # Relative path with drive, e.g. X:Windows |
| 211 | return p[:2], empty, p[2:] |
| 212 | else: |
| 213 | # Relative path, e.g. Windows |
| 214 | return empty, empty, p |
| 215 | |
| 216 | |
| 217 | # Split a path in head (everything up to the last '/') and tail (the |
searching dependent graphs…