def renumber_filename(filename, i, zfill_len):
    """
    Discard the numbered part of the filename and renumber according to its
    new position in the list of replacement rows to be inserted (passed as `i`).
    E.g. from ".../segmented/output_035_6.wav" to ".../segmented/output_035_012.wav"
    when `i` = 12 and `zfill` = 3.
    """
    filename_prefix = filename.stem.rpartition("_")[0]
    new_number = str(i).zfill(zfill_len)
    new_filename = f"{filename_prefix}_{new_number}{filename.suffix}"
    #print(f"Renaming {filename.name} as {new_filename} ({i}:{zfill_len} -> {new_number})")
    return filename.parent / new_filename