"badzipfile: File Is Not A Zip File" - Error Popped Up All Of A Sudden
Solution 1:
Excel XLSX
files are zipped, XLS
files are not.
I believe this bug is related to a combination of
XLS
is not zipped, and- Since python-3.9, the
openpyxl
module must be used withXLSX
files.
This problem is easy to solve by checking which type of Excel file is uploaded and using the appropriate engine to read into Pandas
.
By file extension
from pathlib import Path
import pandas as pd
file_path = Path(filename)
file_extension = file_path.suffix.lower()[1:]
if file_extension == 'xlsx':
df = pd.read_excel(file.read(), engine='openpyxl')
elif file_extension == 'xls':
df = pd.read_excel(file.read())
elif file_extension == 'csv':
df = pd.read_csv(file.read())
else:
raise Exception("File not supported")
By file mimetype
If you happen to have access to the file mimetype, you can perform the following test:
import pandas as pd
if file.content_type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
df = pd.read_excel(file.read(), engine='openpyxl') # XLSXelif file.content_type == 'application/vnd.ms-excel':
df = pd.read_excel(file.read()) # XLSelif file.content_type == 'text/csv':
df = pd.read_csv(file.read()) # CSVelse:
raise Exception("File not supported")
Solution 2:
It is a very common issue and many people are trying to solve.It is related to excel file and openpyxl. Like @Barmar said in his comments xlsx, xlsm, etc are indeed zip. It was working fine until python 2.7 .
Try reading and writing to a csv instead, it won't be a problem.
Solution 3:
As others have already pointed out, a corrupted file is the culprit.
Perform these quick sanity checks:
- Open the excel file. Is the data appearing correctly?
- Are you able to see the file size in the file's details in Windows Explorer?
In my case, I manually checked the excel file content and it turns out it was empty because I was not storing the file correctly. Once I fixed this, the "File is not a zip file" error got resolved.
Post a Comment for ""badzipfile: File Is Not A Zip File" - Error Popped Up All Of A Sudden"