import csv
import re
from pathlib import Path


def convert_dir(path):
    source_path = Path(path)
    target_path = source_path.parent.joinpath('staging')

    for source_file in source_path.glob('*.csv'):
        print(source_file.name)
        target_file = target_path / source_file.name
        convert_file(source_file, target_file)


def convert_file(source, target):
    with open(source, 'r', encoding='utf-8', newline='') as frh:
        with open(target, 'w', encoding='latin-1', newline='') as fwh:
            csv_reader = csv.reader(frh, delimiter=';')
            csv_writer = csv.writer(fwh, delimiter='\t')
            for row in csv_reader:
                converted = [convert_field(col) for col in row]
                csv_writer.writerow(converted)


def convert_field(col):
    if re.match(r'\d:\d\d:\d\d', col):
        col = '0' + col
    return col


def main():
    convert_dir('E:\\GEBOS\\data')


if __name__ == '__main__':
    main()