Python Forum

Full Version: Errors when extracting files from ZIPs
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Greetings!
I’m having errors when extracting files from ZIP archives, see the errors below:
line 1698, in _extract_member
    with self.open(member, pwd=pwd) as source, \

ine 698, in _check_compression
    raise NotImplementedError("That compression method is not supported")
I’m using “try-except ” to handle the errors, but the code stops anyway for some reason.
Here is a part of the code that produces errors:
if os.path.isfile(file_path):
                print("FILE path name:", file_path) 
                try :        
                    with ZipFile (file_path, 'r') as zip_file :
                        file_name_list = zip_file.namelist ()
                        for file_name in file_name_list :
                            if 'Cell_' in file_name :             
                                print ("IN ZIP -->>  ",file_path)
                                print ("FILE NAME-->", file_name)
                                try :
                                    zip_file.extract(file_name, zip_out)                       
                                except BadZipfile :
                                    continue
                except BadZipfile :
                    print ("Bad Zip File", zip_file)
                    continue    
I really would like just to skip the file if failing to unzip, to process the next file if any.

Thank you.
Vlade
(Mar-10-2021, 09:24 PM)tester_V Wrote: [ -> ]I really would like just to skip the file if failing to unzip, to process the next file if any.
Change to.
except BadZipfile:
    print("Bad Zip File", zip_file)
    pass
I cannot duplicate your error. This code works as expected:

from zipfile import *
import os

zip_out = '/home/BashBedlam/zip_out/'
file_path = '/home/BashBedlam/zip_tester.zip'

if os.path.isfile(file_path):
	print("FILE path name:", file_path) 
	try :        
		with ZipFile (file_path, 'r') as zip_file :
			file_name_list = zip_file.namelist ()
			for file_name in file_name_list :
				if 'Cell_' in file_name :             
					print ("IN ZIP -->>  ",file_path)
					print ("FILE NAME-->", file_name)
					try :
						zip_file.extract(file_name, zip_out)                       
					except BadZipfile :
						continue
	except BadZipfile :
		print ("Bad Zip File", zip_file)
I really appreciate your Help!
thank you!

I added a new exception:
                                except RuntimeError:
                                    continue
And it seems it did the trick.
Not sure this is the best way t handle this kind of situation but I just wanted to share it with you.
Thank you again, I love this forum.
I think it is the best place for Python coding help.
God bless you all.