python3代码实现linux路径简化
目标:通过python3代码,实现linux路径的简化,如:
/a/b/c/d -> /a/b/c/d
/a/b/c/./d -> /a/b/c/d
/a/b/c/../d -> /a/b/d
/a/b/c/../../d -> /a/d
import sys def zip_path(): #用户输入路径 bpath = input('please input path: ') #路径要求是绝对路径,以"/"开始 if bpath[0] != "/": print("path should start with '/', such as '/a/b/c' ") sys.exit() #把路径各级子目录拆解到列表中 pathlist = bpath.split("/") #重组列表 newlist = [] i=1 while i < len(pathlist) : if pathlist[i] == ".." : newlist.pop() elif pathlist[i] != "." : newlist.append(pathlist[i]) i += 1 #组装重组的列表并打印 for ss in newlist: print("/" + ss, end = '') print("") zip_path()

浙公网安备 33010602011771号