Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
7 lines proc (commented) solution in Clear category for Simplify Unix Path by CDG.Axel
def simplify_path(path):
stack = []
for part in path.rstrip('/').split('/'): # interesting, buf rstrip is for first '/' maintain
if part == '..' and stack and stack[-1] != '..':
if stack != ['']: stack.pop()
elif part != '.' and (part or not stack): # when '/' first we add '' to stack
stack.append(part)
return ('/'.join(stack), '/')[stack == ['']] or '.' # '/' if only '' in stack, '.' if empty
Feb. 13, 2022