'Specify Name of extracted Zipfile

for filename in FILENAMES:
    csv_file = filename.replace('.zip','.csv')
    if not os.path.exists(os.path.join('.',csv_file)):
        print('Extracting {}'.format(filename))
        with zipfile.ZipFile(os.path.join('.',filename), 'r') as zip_ref:
            zip_ref.extractall('.')

The above code extracts .zip files to .csv files in the current directory. However it sometimes renames the fields, adding in whitespace and so on. some_file_2021.zip could become some_file 2021.csv or something like this. Is there a way in this loop to force each extract file to become the csv_file I've referenced - if not, at least get the name of the extracted files?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source