CCF Python题解(100分)201604-3 路径解析
import re
P = int(input())
currentdir = input()
for i in range(P):
relpath = input()
if relpath:
if re.match('^(/)+$', relpath):
print('/')
else:
if not re.match('^/', relpath):
relpath = currentdir + '/' + relpath
relpath = relpath.rstrip("/")
relpath = re.sub(r"(/)+", r"/", relpath)
list1 = relpath.split('/')[1:]
for index in range(len(list1)):
if list1[index] == ".":
list1[index] = ""
elif list1[index] == "..":
list1[index] = ""
for j in range(index - 1, -1, -1):
if list1[j] != "":
list1[j] = ""
break
str1 = ""
for i in list1:
if i != "":
str1 += '/' + i
if str1 == "":
str1 = '/'
print(str1)
else:
print(currentdir)