Badiou30120
Badiou30120

Reputation: 73

Python : Zip extraction exception

I made an automatic extractor for my music catalog. I wrote my code as sort to work with zips and rar files. The code is working fine but if there are other kind of files in the working directories, then I receive an error message :

raise BadZipfile, "File is not a zip file"
BadZipfile: File is not a zip file

That means a mp3 file or anything else is blocking or interrupting the extraction process. Here's my code :

def extraction():
    funcs = {'.rar':rarfile.RarFile, '.zip':zipfile.ZipFile}
    for ArchivesFiles in chemin_zipfiles :    
        truncated_file, ext = os.path.splitext(os.path.basename(ArchivesFiles)) 
        if not os.path.exists(truncated_file):
            new_folder = os.makedirs(truncated_file)
            arch_ref = funcs[ext](ArchivesFiles,'r')
            new_folder = os.path.realpath(truncated_file)
            arch_ref.extractall(new_folder)

How could I avoid this?

edit :

I made some changes:

def extraction():
    funcs = {'.rar':rarfile.RarFile, '.zip':zipfile.ZipFile}
    for ArchivesFiles in chemin_zipfiles :    
        truncated_file, ext = os.path.splitext(os.path.basename(ArchivesFiles)) 
        if not os.path.exists(truncated_file):
            new_folder = os.makedirs(truncated_file)
            arch_ref = funcs[ext](ArchivesFiles,'r')
            new_folder = os.path.realpath(truncated_file)
            try:
                arch_ref.extractall(new_folder)
            except BadZipfile:
                continue
            except NotRarFile:
                continue

but still receiving an error:

raise NotRarFile("Not a Rar archive: "+self.rarfile)
NotRarFile: Not a Rar archive: /Volumes/me/albums/reggae/reggae_dub/._Dubalizer_SubExisteÌncia_freshpoulp.rar

Many thanks.

Upvotes: 0

Views: 6460

Answers (3)

Cameron Cantwell
Cameron Cantwell

Reputation: 1

Old post but,

I believe the issue you are having is with the accent you have over the ê.

Because you are trying to access a certain file it will come back as a file does not exist error, but if you were to use that character elsewhere in the code, say trying to print it you would get a UnicodeDecode error.

More information on this here: https://pythonhosted.org/kitchen/unicode-frustrations.html

Upvotes: 0

Tarun Gaba
Tarun Gaba

Reputation: 1113

You are specifying exceptions wrongly. Instead of:

except BadZipfile: 
    continue
except NotRarfile:
    continue

you should be doing either:

except zipfile.BadZipfile:
    continue
except rarfile.NotRarFile:
    continue

or, in a compact manner:

except (zipfile.BadZipfile, rarfile.NotRarFile):
    continue

Upvotes: 0

tea2code
tea2code

Reputation: 1007

I think there are two ways. First you could catch the exception:

try:
    arch_ref.extractall(new_folder)
except BadZipfile:
    continue

or you could try to prevent loading non-zip files by filtering the file extension and/or checking the file header.

Upvotes: 2

Related Questions