Skip to content

Instantly share code, notes, and snippets.

@friskfly
Created December 27, 2012 12:37
Show Gist options
  • Save friskfly/4388065 to your computer and use it in GitHub Desktop.
Save friskfly/4388065 to your computer and use it in GitHub Desktop.
python getFloders 根据filePath 获得所有的父级目录 向上查找
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