<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def write_config(config_data: Dict[str, Path], path: Path = None):
 """ Save the config file. :param config_data: The index to save :param base_dir: The place to save the file. If ``None``, :py:meth:`infer_config_base_dir()` will be used Only keys that are in the config elements will be saved. """

    path = Path(path) if path else infer_config_base_dir()
    valid_names = [ce.name for ce in CONFIG_ELEMENTS]
    try:
        os.makedirs(path, exist_ok=True)
        with (path/_CONFIG_FILENAME).open('w') as base_f:
            json.dump({k: str(v) for k, v in config_data.items()
                       if k in valid_names},
                      base_f, indent=2)
    except OSError as e:
        sys.stderr.write("Config index write to {} failed: {}\n"
                         .format(path/_CONFIG_FILENAME, e))