def reuse_filename(filename):
    """If the file exists, renames it adding a number."""
    if filename.exists():
        n = 0
        new = filename.parent.joinpath(
            f'{filename.stem}{n:04}{filename.suffix}')
        while new.exists():
            n += 1
            new = filename.parent.joinpath(
                f'{filename.stem}{n:04}{filename.suffix}')
        filename.rename(new)
        return new
    return filename
