- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find the resolved Unix style path in Python

Suppose we have a Unix path, in a list of strings, we have to find its resolved version. As we know in Unix, ".." denotes the previous directory and "." denotes stay on the current directory. Here resolving indicates evaluation of the two symbols so that we get the final directory we're currently in.

So, if the input is like path = ["usr", "..", "usr", ".", "local", "etc", "foo"], then the output will be ['usr', 'local', 'etc', 'foo'], as the part represents "/usr/../usr/./local/etc" which resolves to "/usr/local/etc/foo"

To solve this, we will follow these steps −

- s := a new list
- for each element i in path, do
- if i is same as '..', then
- if s is not empty, then
- delete last element from s

- if s is not empty, then
- otherwise when i is not same as '.', then
- insert i at the end of s

- if i is same as '..', then
- return s

Let us see the following implementation to get better understanding −

## Example

class Solution: def solve(self, path): s = [] for i in path: if i == '..': if len(s) >0: s.pop() elif i !='.': s.append(i) return s ob = Solution() print(ob.solve(["usr", "..", "usr", ".", "local", "etc", "foo"]))

## Input

["usr", "..", "usr", ".", "local", "etc", "foo"]

## Output

['usr', 'local', 'etc', 'foo']

- Related Questions & Answers
- Unix style pathname pattern expansion in Python (glob)
- Program to find path with minimum effort in Python
- Program to find out the shortest path to reach the goal in Python
- Program to find path with maximum probability using Python
- Program to find out the minimum path to deliver all letters in Python
- Program to find length of longest matrix path length in Python
- Program to find length of longest path with even sum in Python
- Program to find length of the longest path in an n-ary tree in Python
- Program to find a path a continuous path in a rectangular area without engaging a bomb in Python
- Program to find sum each of the diagonal path elements in a binary tree in Python
- Program to find length of the longest path in a DAG without repeated nodes in Python
- Program to find longest even value path of a binary tree in Python
- Program to find longest path between two nodes of a tree in Python
- Program to find the largest sum of the path between two nodes in a binary tree in Python
- Program to find largest sum of any path of a binary tree in Python

Advertisements