Skip to content

Instantly share code, notes, and snippets.

@friskfly
Created January 1, 2013 08:36
Show Gist options
  • Save friskfly/4425936 to your computer and use it in GitHub Desktop.
Save friskfly/4425936 to your computer and use it in GitHub Desktop.
python getFloders 根据给定的文件路径,向上查找 返回所有的文件路径。
# Get all folders paths from given path upwards
#
# @type file_path: string
# @param file_path: absolute file path to return the paths from
#
# @return list<string> of file paths
#
# @global nestingLimit
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