import gzip
from pathlib import Path


def unzip_gzip(file: Path, target: str):
    with gzip.open(file, 'rb') as r:
        with open(target, 'wb') as w:
            return w.write(r.read())


def unzip_gzfiles(dirpath: Path) -> None:
    """Unzip the .gz files from a dir."""
    if not dirpath.exists() or not dirpath.is_dir():
        print(f'"{dirpath}" not found')
        return None

    gzfiles = list(dirpath.glob('*.gz'))

    if not gzfiles:
        print(f'No .gz files found in "{dirpath}"')
        return None

    # Create a directory to output the files
    Path(f'{dirpath}/out').mkdir(exist_ok=True)

    for file in gzfiles:
        filename = file.name.removesuffix('.gz')

        if not file.is_file():
            continue

        if unzip_gzip(file, f'{dirpath}/out/{filename}'):
            print(f'Unzipped "{file.name}"')


if __name__ == '__main__':
    try:
        dirpath = input('Type the directory path\n')
        unzip_gzfiles(Path(dirpath))
    except (KeyboardInterrupt, Exception):
        print('See ya')
