CCF Python題解(100分)201604-3 路徑解析
阿新 • • 發佈:2018-11-13
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)