[Python] How to solve BadZipFile: File is not a zip file error in Jupyter Notebook?

Discussão em 'Python' iniciado por Stack, Março 17, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to read .xlsx files from folders in a specific directory and to write/export them into 4 new .xlsx files, which every new .xlsx will be containing data catalog per year. The script works well when i tried it some months ago but it's not working anymore. It keeps on resulting BadZipFile: File is not a zip file error.

    Do I miss something? I've tried upgrading and downgrading the anaconda, python, openpyxl, and pandas version but it doesn't help.

    from openpyxl import load_workbook
    import pandas as pd
    import os
    import re

    path_folder = r'C:\\Users\\lala\\Downloads\\New folder\\Data Klimatologi\\'
    folder_tahun = os.listdir(path_folder)

    year_folder

    for x in year_folder:
    year_folder = os.listdir(path_folder + x)
    frames = []
    for y in station_folder:
    path_file = path_folder + '{}\\{}'.format(x,y)
    files = os.listdir(path_file)
    for z in files:
    pattern = path_folder + '{}\\{}\\{}'.format(x,y,z)
    wb = load_workbook(filename = pattern)
    sheet = wb.active#has 1 sheet
    max_row_for_Tn = max((b.row for b in sheet['B'] if b.value is not None))
    cell = 'A9:K%d' % (max_row_for_Tn)
    data = sheet[cell]
    row_list = []
    for row in data:
    cols = []
    for col in row:
    cols.append(col.value)
    row_list.append(cols)
    df = pd.DataFrame(data = row_list[1:], index=None, columns=row_list[0])
    cell_id = sheet.cell(row = 1, column = 3)
    pk = cell_id.value
    pk = re.sub('[\s]+', '', pk)
    pk = int(re.sub(r'[^.,a-zA-Z0-9 \n\.]','', pk))
    df['Id WMO'] = pk
    frames.append(df)
    result = pd.concat(frames)
    result.to_excel(r'C:\Users\lala\OneDrive\Documents\Dataset\Dataset Stasiun BMKG Tahun {}.xlsx'.format(x), index = False)


    The script works well until year_folder giving the output ('2000','2001','2002','2003','2004').

    Here's the traceback.

    ---------------------------------------------------------------------------
    BadZipFile Traceback (most recent call last)
    <ipython-input-4-e8e2d94d1368> in <module>
    7 for z in files:
    8 pattern = path_folder + '{}\\{}\\{}'.format(x,y,z)
    ----> 9 wb = load_workbook(filename = pattern)
    10 sheet = wb.active#has 1 sheet
    11 max_row_for_Tn = max((b.row for b in sheet['B'] if b.value is not None))

    ~\anaconda3\envs\Pandas\lib\site-packages\openpyxl\reader\excel.py in load_workbook(filename, read_only, keep_vba, data_only, keep_links)
    312 """
    313 reader = ExcelReader(filename, read_only, keep_vba,
    --> 314 data_only, keep_links)
    315 reader.read()
    316 return reader.wb

    ~\anaconda3\envs\Pandas\lib\site-packages\openpyxl\reader\excel.py in __init__(self, fn, read_only, keep_vba, data_only, keep_links)
    122 def __init__(self, fn, read_only=False, keep_vba=KEEP_VBA,
    123 data_only=False, keep_links=True):
    --> 124 self.archive = _validate_archive(fn)
    125 self.valid_files = self.archive.namelist()
    126 self.read_only = read_only

    ~\anaconda3\envs\Pandas\lib\site-packages\openpyxl\reader\excel.py in _validate_archive(filename)
    94 raise InvalidFileException(msg)
    95
    ---> 96 archive = ZipFile(filename, 'r')
    97 return archive
    98

    ~\anaconda3\envs\Pandas\lib\zipfile.py in __init__(self, file, mode, compression, allowZip64)
    1129 try:
    1130 if mode == 'r':
    -> 1131 self._RealGetContents()
    1132 elif mode in ('w', 'x'):
    1133 # set the modified flag so central directory gets written

    ~\anaconda3\envs\Pandas\lib\zipfile.py in _RealGetContents(self)
    1196 raise BadZipFile("File is not a zip file")
    1197 if not endrec:
    -> 1198 raise BadZipFile("File is not a zip file")
    1199 if self.debug > 1:
    1200 print(endrec)

    BadZipFile: File is not a zip file

    Continue reading...

Compartilhe esta Página