Python Forum
Errors when extracting files from ZIPs
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Errors when extracting files from ZIPs
#1
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
Reply
#2
(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
Reply
#3
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)
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Extracting information from .xlsx files hobbyist 0 1,593 Jan-06-2021, 07:20 PM
Last Post: hobbyist
  Python script that recursively zips folders WITHOUT nesting the folder inside the zip umkc1 1 2,822 Feb-11-2020, 09:12 PM
Last Post: michael1789
  Errors to get information of multiple files into a single file csv Clnprof 3 2,598 Aug-30-2019, 04:59 PM
Last Post: ThomasL
  Extracting data from multiple txt files Emmanouil 7 5,131 Aug-25-2019, 10:24 PM
Last Post: Emmanouil
  Extracting to files. Stealthychu 4 3,248 Oct-30-2018, 10:05 AM
Last Post: Stealthychu
  Help extracting comment data from multiple zip files SoulsKeeper 10 6,124 Sep-10-2018, 10:33 AM
Last Post: SoulsKeeper

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020