Path shortener for GUI application
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 pathlib

def cut_path(path):
    parts = list(pathlib.PurePath(path).parts)
    if len(parts) &gt;= 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)) &gt;= max_size:
            path /= '...'
            break
    if len(parts) &gt; 1:
        path /= parts[-1]
    return path




If however you're using Python 2 or Python &lt;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) &gt;= 4:
        parts [2:-1] = ['...']
    return os.path.join(*parts)