[Tutor] Unzip a zipfile, then unzip the zipfiles within the original zip

Peter Otten __peter__ at web.de
Wed Aug 8 10:15:27 CEST 2012


Gregory Lund wrote:

> Not too sure on protocol either, sorry if this email is out of line.

You have to hit [Reply all] in your email client for your answer to go to 
the list instead of just me.

When you answer a post put your reply after the relevant part of the quoted 
post (don't "top-post"). Remove the parts of the original message that are 
not relevant to your follow-up.

> Thanks for your assistance.
> unfortunately I can't make what you sent, work.
> i am not familiar enough to know which are variables and which I must not
> touch.
> I kept getting list index out of range.

That is because you didn't invoke the script from the commandline (DOS box), 
with two arguments. sys.argv is a list containing the script name and the 
arguments provided on the commandline. If you invoke the script from Idle or 
by double-clicking no arguments are passed.

> 
> This is what I attempted:

> source_file = sys.argv[1]

The error occurs in the line above (the traceback should tell you that), so 
the following lines including your modification are newer executed.

> dest_folder = sys.argv[2]
> 
> zipfile.ZipFile("unzip_Unzip_Data_Test.zip").extractall(r"D:
\D_Drive_Documents\test_folder")
> 
> inner_zips_pattern = os.path.join(dest_folder, "*.zip")
> for filename in glob.glob(inner_zips_pattern):
>     inner_folder = filename[:-4]
>     zipfile.ZipFile(filename).extractall(inner_folder)

Let's go back to to my original script and for the moment hard-code the 
filename and folder. It becomes

import glob
import os
import sys
import zipfile

source_file = "unzip_Unzip_Data_Test.zip"
dest_folder = r"D:\D_Drive_Documents\test_folder"

zipfile.ZipFile(source_file).extractall(dest_folder)

inner_zips_pattern = os.path.join(dest_folder, "*.zip")
for filename in glob.glob(inner_zips_pattern):
    inner_folder = filename[:-4]
    zipfile.ZipFile(filename).extractall(inner_folder)


That should work provided the source file unzip_Unzip_Data_Test.zip is in 
the current working directory -- otherwise you must specify the complete 
path like you did for the destination folder.

> I was also thinking I could list the files in the new folder (from the
> outer) and then if (:-4) = ".zip" then extract them, but can't figure
> that out either.

Read up what glob.glob() does in the documentation. If you were to hand code 
what it does the loop could become

for filename in os.listdir(dest_folder):
    if filename.lower().endswith(".zip"):
        filename = os.path.join(dest_folder, filename)
        inner_folder = filename[:-4]
        zipfile.ZipFile(filename).extractall(inner_folder)




More information about the Tutor mailing list