def limit_path(path, ellipsis=True): """Reduces the filename length of all filenames in the given path to the common maximum length for current platform. While the limits are depended on the file system and more restrictions may apply, this covers the common case. """ assert isinstance(path, fsnative) main, ext = os.path.splitext(path) parts = main.split(sep) for i, p in enumerate(parts): # Limit each path section to 255 (bytes on linux, chars on win). # http://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits limit = 255 if i == len(parts) - 1: limit -= len(ext) if len(p) > limit: if ellipsis: p = p[:limit - 2] + fsnative(u"..") else: p = p[:limit] parts[i] = p return sep.join(parts) + ext