Skip to content Skip to sidebar Skip to footer

Badzipfile: File Is Not A Zip File

This is my code. I get the error when I try to execute this script Error raise BadZipFile('File is not a zip file') BadZipFile: File is not a zip file This is my s

Solution 1:

in both zip archive open statements:

with zipfile.ZipFile(os.path.join(data_dir,folder),mode='r')

and

with zipfile.ZipFile(os.path.join(temp_dir,sub_folder),mode='r')

nothing (at least nothing that we can check) guarantees that the file names you're passing are actually .zip files. It could be a directory, an already extracted file, some file that was already there...

I suggest that you check the file extension prior to extracting, for instance:

import fnmatch
zfn = os.path.join(temp_dir,sub_folder)
if fnmatch.fnmatch(zfn,"*.zip"):
    with zipfile.ZipFile(zfn,mode='r') as whatever:

Some .zip files could be corrupt, but that's less likely. Also, if you wanted to extract .jar and other zip-structured files with a different extension, replace the fnmatch by

if zfn.lower().endswith(('.zip','.jar','.docx')):

Post a Comment for "Badzipfile: File Is Not A Zip File"