from pathlib import Path


def auto_id(directory):
    """Returns the next version number available for a sub-directory.

    previous version numbers are extracted from the number after
    the last underscore i.e. directory/version_1, directory/version_2
    In this case 3 would be the returned value.
    """

    max_id = 0
    directory = Path(directory)
    if directory.is_dir():
        for path in directory.iterdir():
            if path.is_dir() and not path.name.startswith("."):
                path_id = int(path.name.split("_")[-1])
                if path_id > max_id:
                    max_id = path_id
    return max_id + 1