def pad_filename_with_zeros(filepath, file_index_delim = '_' , pad_digits = 4):
    """
    filepath: string. Contains path to file whose name is to be padded
    file_index_delim: string. Contains character which immediately precedes file index number.
    pad_digits: int. Specifies the number of digits to be present in the final file number after padding.
    """
    # Find the last period, which should denote the beginning of the file extension
    final_period = filepath.rfind('.')
    # Determine the file extension
    extension = filepath[final_period:]
    # Find the final ocurrence of the file index delimiter
    final_underscore = filepath.rfind(file_index_delim)
    # Determine the file index
    file_number = filepath[final_underscore+1:final_period]
    # Generate the new file index, padded to specified number of digits
    new_file_number = '0'*(pad_digits-len(file_number))+file_number
    # Generate new file name, including padded file index.
    new_filename = filepath[:final_underscore] + '_' + new_file_number + extension
    return new_filename