def _path_insensitive(path):
    """
    Recursive part of path_insensitive to do the work.
    """
    path = str(path)
    if path == '' or os.path.exists(path):
        return path

    base = os.path.basename(path)  # may be a directory or a file
    dirname = os.path.dirname(path)

    suffix = ''
    if not base:  # dir ends with a slash?
        if len(dirname) < len(path):
            suffix = path[:len(path) - len(dirname)]

        base = os.path.basename(dirname)
        dirname = os.path.dirname(dirname)

    if not os.path.exists(dirname):
        dirname = _path_insensitive(dirname)
        if not dirname:
            return

    # at this point, the directory exists but not the file

    try:  # we are expecting dirname to be a directory, but it could be a file
        files = os.listdir(dirname)
    except OSError:
        return

    baselow = base.lower()
    try:
        basefinal = next(fl for fl in files if fl.lower() == baselow)
    except StopIteration:
        return

    if basefinal:
        return os.path.join(dirname, basefinal) + suffix
    else:
        return
def _path_insensitive ( path ) : path = str ( path ) if path == '' or os . path . exists ( path ) : return path base = os . path . basename ( path ) dirname = os . path . dirname ( path ) suffix = '' if not base : if len ( dirname ) < len ( path ) : suffix = path [ : len ( path ) - len ( dirname ) ] base = os . path . basename ( dirname ) dirname = os . path . dirname ( dirname ) if not os . path . exists ( dirname ) : dirname = _path_insensitive ( dirname ) if not dirname : return try : files = os . listdir ( dirname ) except OSError : return baselow = base . lower ( ) try : basefinal = next ( fl for fl in files if fl . lower ( ) == baselow ) except StopIteration : return if basefinal : return os . path . join ( dirname , basefinal ) + suffix else : return
Recursive part of path_insensitive to do the work.