Created
June 8, 2015 15:26
-
-
Save NiklasRosenstein/021833d4c36f4a81b02e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
def splitpath(path): | |
""" | |
splitpath(path) -> (drive, parts, isabs) | |
Splits a path into its drive letter and path elements. If the OS | |
does not use drive letters, \\c drive will always be an empty | |
string. Note that on Windows, if a Unix-style absolute path is | |
passed, an empty \\c drive letter will be returned but \\c isabs | |
will be True. | |
@param[in] path The path to split. | |
@return A tuple of \\c (drive, parts, isabs) | |
""" | |
drive = '' | |
parts = [] | |
isabs = False | |
if os.name == 'nt': | |
if path.startswith('/'): | |
isabs = True | |
path = path.replace('/', os.sep) | |
drive, path = os.path.splitdrive(path) | |
# Remove any leading path separators. | |
leadings = 0 | |
while path.startswith(os.sep): | |
path = path[len(os.sep):] | |
leadings += 1 | |
# Split until no end. | |
while path: | |
path, element = os.path.split(path) | |
parts.insert(0, element) | |
# If we're on windows and it had two or more leading | |
# separators and no drive letters, it's a network drive. | |
if os.name == 'nt': | |
if leadings >= 2 and not drive and parts: | |
drive = '\\\\' + parts.pop(0) | |
if drive: | |
isabs = True | |
return drive, parts, isabs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment