python – Send an array of files and return the first one that exists

I have a little project, chuy, which is basically a copy of Make.

By request of users I have added support for toml configuration, since originally it only accepted json.

The files that chuy have to check are: ("chuy.json","chuy.toml","pyproject.toml").

The order of priority is the same order in which they are placed. (If it is true that chuy.json exists, it no longer matters whether chuy.toml or pyproject.toml exist.)

So I wrote:

def get_config_file() -> str:
    try:
        with open("chuy.json", mode="r", encoding="utf-8"):
            return "json"
    except FileNotFoundError:
        try:
            with open("pyproject.toml", mode="r", encoding="utf-8"):
                return "pyproject"
        except FileNotFoundError:
            try:
                with open("chuy.toml", mode="r", encoding="utf-8"):
                    return "toml"
            except FileNotFoundError:
                raise BaseException("I can't find your configuration file :(")

Which is horrible, some refactor later:

def get_config_file(posible_config_files = ("chuy.json", "chuy.toml", "pyproject.toml"))-> str:
    
    for file in posible_config_files:
        try:
            with open(file, mode="r", encoding="utf-8"):
                return file
        except FileNotFoundError:
            continue

Which is a bit better, but I still don’t think it’s a good solution.

Any ideas?