Advertisement
Guest User

[Python] Path shortener

a guest
Jul 14th, 2017
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. def cut_path(path):
  2.     ''' This function takes a path and outputs the path cut with the ... after the
  3.     the first folder after the first character and completes the name with the filename or the last folder
  4.    Example:
  5.    C:/Users/Mauro/Desktop/DenoiseAvg/DenoiseAverage/test_cut_path.py
  6.    C:/Users/.../test_cut_path.py
  7.     '''      
  8.     endidx = 5
  9.     char = path[endidx]    
  10.     while char != '/' and endidx < len(path):
  11.         endidx += 1
  12.         char = path[endidx]
  13.        
  14.     inpath = path[:endidx + 1 ]
  15.    
  16.     rpath = path[::-1]    
  17.     idx = 1  
  18.     char = rpath[idx]    
  19.     while char != "/" and idx < len(rpath):
  20.         idx += 1
  21.         char = rpath[idx]
  22.  
  23.     endpath = path[len(path) - idx - 1 : ]
  24.    
  25.     outpath = inpath + "..." + endpath
  26.     return outpath
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement