I need to display a path within a GUI application.Displaying the whole path would be an overkill. so I made a custom path shortner.def cut_path(path):    ''' This function takes a path and outputs the path cut with the ... after the      the first folder after the first character and completes the name with the filename or the last folder    Example:     C:/Users/Mauro/Desktop/DenoiseAvg/DenoiseAverage/test_cut_path.py    C:/Users/.../test_cut_path.py     '''          endidx = 5    char = path[endidx]        while char != '/' and endidx < len(path):        endidx += 1        char = path[endidx]    inpath = path[:endidx + 1 ]    rpath = path[::-1]        idx = 1       char = rpath[idx]        while char != / and idx < len(rpath):        idx += 1        char = rpath[idx]    endpath = path[len(path) - idx - 1 : ]    outpath = inpath + ... + endpath    return outpathPaste bin for synthax highlighthttps://pastebin.com/aefj8b6G Is there a shorter, more readable, pythonic way to do it? Is there a way to limit how many characters the path would be long? how would you do it?
Path shortener for GUI application
python;strings;file system
Python is 'batteries included', this means offloading code to the core library is a good thing, and often possible. In this case, if you're running Python 3.4+ you could make use of pathlib. All you need is to know the pathlib.PurePath constructor and PurePath.parts.And so you can change your code to:import pathlibdef cut_path(path):    parts = list(pathlib.PurePath(path).parts)    if len(parts) >= 4:        parts [2:-1] = ['...']    return pathlib.PurePath(*parts)However, as you're doing it on how long the path is, you could dynamically build the new path. And so could use something like:def cut_path(path, max_size=5):    if not path:        return path    parts = list(pathlib.PurePath(path).parts)    path = pathlib.PurePath(parts[0])    for part in parts[1:-1]:        path /= part        if len(str(path)) >= max_size:            path /= '...'            break    if len(parts) > 1:        path /= parts[-1]    return pathIf however you're using Python 2 or Python <3.4 then you can't use the nice new interface, and would have to use the old interface, os.path. Which doesn't support splitting and joining the paths very well. First you may want to use os.path.normcase to normalize the path. Which may destroy the path if you have a Posix path on Windows. After this you'd want to take the drive with os.path.splitdrive, and then split the rest of the parts using os.sep. Then you want to do the same as above, and replace the middle section of the path, and finally os.path.join them all together.Which could leave you with something like:def cut_path(path):    path = os.path.normcase(path)    drive, path = os.path.splitdrive(path)    parts = path.split(os.sep)    parts[0] = drive + parts[0]    # Join plays funny, so you have to add a separator in these cases.    if parts[0][-1] == ':':        parts[0] += os.sep    if len(parts) >= 4:        parts [2:-1] = ['...']    return os.path.join(*parts)