Created
December 27, 2012 12:37
-
-
Save friskfly/4388065 to your computer and use it in GitHub Desktop.
python getFloders 根据filePath 获得所有的父级目录 向上查找
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
nestingLimit=30 | |
def getFolders(file_path): | |
if file_path is None: | |
return [] | |
folders = [file_path] | |
limit = nestingLimit | |
while True: | |
split = os.path.split(file_path) | |
# nothing found | |
if len(split) == 0: | |
break | |
# get filepath | |
file_path = split[0] | |
limit -= 1 | |
# nothing else remains | |
if len(split[1]) == 0 or limit < 0: | |
break | |
folders.append(split[0]) | |
return folders |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment