import csv
from pathlib import Path

import shutil

if __name__ == '__main__':

    for old_out_file in (Path() / 'output').rglob('*_test_out.csv'):
        old_out_file.unlink()

    for new_out_file in (Path() / 'output').rglob('*_test_out_repair.csv'):
        shutil.move(str(new_out_file), str(new_out_file).replace('_test_out_repair', '_test_out'))
    exit()
    '''
    with old_out_file.open('r') as old_f:
        predictions = []
        file_names = []

        idx = 1
        zeros = '00000'

        _ = old_f.readline()

        for row in old_f:
            split_row = row.split(',')
            file_names.append(f'test_{zeros[:-len(str(idx))]}{idx}.wav')
            predictions.append(split_row[-1].strip()
                               .replace('"', '').replace('(', '').replace(')', '').replace("'", '')
                               )
            idx += 1
    try:
        (old_out_file.parent / f'{old_out_file.name}_repair').unlink()
    except FileNotFoundError:
        pass
    with (old_out_file.parent / f'{old_out_file.name[:-4]}_repair.csv').open('w') as new_f:
        headers = ['file_name', 'prediction']

        writer = csv.DictWriter(new_f, delimiter=',', lineterminator='\n', fieldnames=headers)
        writer.writeheader()  # write a header

        writer.writerows([dict(file_name=file_name, prediction=prediction)
                          for file_name, prediction in zip(file_names, predictions)]
                         )
    '''
    pass
