:type path: str :rtype: str
(self, path)
| 35 | |
| 36 | class Solution(object): |
| 37 | def simplifyPath(self, path): |
| 38 | """ |
| 39 | :type path: str |
| 40 | :rtype: str |
| 41 | """ |
| 42 | path = re.sub(r'/+', '/', path) |
| 43 | if path[-1] == '/': |
| 44 | path = path[:-1] |
| 45 | |
| 46 | path = path.split('/') |
| 47 | |
| 48 | new_path = [] |
| 49 | for i in path: |
| 50 | if i == '..': |
| 51 | try: |
| 52 | new_path.pop() |
| 53 | continue |
| 54 | except: |
| 55 | continue |
| 56 | if i =='.': |
| 57 | continue |
| 58 | |
| 59 | new_path.append(i) |
| 60 | |
| 61 | x = '/'.join(new_path) |
| 62 | if x and x[0] == '/': |
| 63 | return x |
| 64 | return '/' + x |