Skip to content

Instantly share code, notes, and snippets.

@tnedev
Created April 24, 2014 12:54
Show Gist options
  • Save tnedev/11253581 to your computer and use it in GitHub Desktop.
Save tnedev/11253581 to your computer and use it in GitHub Desktop.
Reduce file path
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