Created
April 24, 2014 12:54
-
-
Save tnedev/11253581 to your computer and use it in GitHub Desktop.
Reduce file path
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def kill_char(string, n): | |
begin = string[:n] | |
end = string[n+1:] | |
return begin + end | |
def reduce_file_path(path): | |
counter=1 | |
if path[len(path)-1]=='.': | |
path = kill_char(path,len(path)-1) | |
if len(path)>1: | |
if path[0]=='.' and path[1]!='.': | |
path = kill_char(path,len(path)-1) | |
while counter < (len(path)-1): | |
if len(path)>1 and path[counter]=='.' and path[counter+1]!='.' and path[counter-1]!='.': | |
path = kill_char(path,counter) | |
counter-=1 | |
counter+=1 | |
counter = 0 | |
while counter < (len(path)-1): | |
if path[counter]=='/' and path[counter]== path[counter+1] and len(path)>2: | |
path = kill_char(path,counter+1) | |
counter-=1 | |
counter+=1 | |
if path[len(path)-1]=='/' and len(path)>1: | |
path = kill_char(path,len(path)-1) | |
counter=len(path)-1 | |
counter2=0 | |
if len(path)>=3: | |
while counter > 0 : | |
if path[counter]=='.' and path[counter-1]=='.' and len(path)>3: | |
while counter2<2: | |
if path[counter]=='/': | |
counter2+=1 | |
path = kill_char(path,counter+1) | |
counter-=1 | |
counter2=0 | |
counter=len(path)-1 | |
counter-=1 | |
if len(path)==3 and path[2]=='.' and path[1]=='.': | |
path = kill_char(path,2) | |
path = kill_char(path,1) | |
return path | |
myInput = input("File path: ") | |
print (reduce_file_path(myInput)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment