def sanitize_path(path):
 trailing_slash = ('/' if path.endswith('/') else '')
 path = posixpath.normpath(path)
 new_path = '/'
 for part in path.split('/'):
  if ((not part) or (part in ('.', '..'))):
   continue
  new_path = posixpath.join(new_path, part)
 trailing_slash = ('' if new_path.endswith('/') else trailing_slash)
 return (new_path + trailing_slash)

def sanitize_path ( path ) : trailing_slash = ( '/' if path . endswith ( '/' ) else '' ) path = posixpath . normpath ( path ) new_path = '/' for part in path . split ( '/' ) : if ( ( not part ) or ( part in ( '.' , '..' ) ) ) : continue new_path = posixpath . join ( new_path , part ) trailing_slash = ( '' if new_path . endswith ( '/' ) else trailing_slash ) return ( new_path + trailing_slash )
make path absolute with leading slash to prevent access to other data .