[Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

Gregory Lund gnj091405 at gmail.com
Tue Sep 25 16:52:04 CEST 2012


> Why did you change file mode to "a"?
I was trying different things and forgot to change it back before I cut/pasted.
>


>
> dest_path = os.path.dirname(fullpath)
> x.extractall(dest_path)
>
Ding ding ding, winner winner chicken dinner!
It's working!
Final .py stand alone code is:

import os, os.path, zipfile, arcpy

in_Zip = r'D:\D_Drive_Documents\Student_Work_Sample_usecopy1\2012-09-18
Lab_2.zip'

outDir = r"D:\D_Drive_Documents\Student_Work_Sample_usecopy1"

z = zipfile.ZipFile(in_Zip,'r')

z.extractall(outDir)

zipContents = z.namelist()
z.close()

for item in zipContents:
    if item.endswith('.zip'):
        # Combine the base folder name with the subpath to the zip file
        fullpath = os.path.join(outDir, item)
        x = zipfile.ZipFile(fullpath,'r')
        dest_path = os.path.dirname(fullpath)
        x.extractall(dest_path)
        x.close()

and... the final code that I'll use in my ArcGIS script/tool is:
(I kept the old code with absolute paths to help anyone who wanted to
use this on their zip of zips (with it commented out and my ArcGIS
requirements immediately below.)


import os, os.path, zipfile, arcpy

#in_Zip = r'D:\D_Drive_Documents\Student_Work_Sample_usecopy1\2012-09-18
Lab_2.zip'
in_Zip = arcpy.GetParameterAsText(0)

#outDir = r"D:\D_Drive_Documents\Student_Work_Sample_usecopy1"
outDir = os.getcwd()

z = zipfile.ZipFile(in_Zip,'r')

z.extractall(outDir)

zipContents = z.namelist()
z.close()

for item in zipContents:
    if item.endswith('.zip'):
        # Combine the base folder name with the subpath to the zip file
        fullpath = os.path.join(outDir, item)
        x = zipfile.ZipFile(fullpath,'r')
        dest_path = os.path.dirname(fullpath)
        x.extractall(dest_path)
        x.close()

--------------------------------
Words/pixels can not express how grateful I am to everyone that
pitched in and helped guide me through this seemingly simple task.
The code works & the Esri ArcGIS tool works!

It's not really 'my' code or tool:
the credit goes to Peter, Oscar, Dave, Stephen and others for
their/your comments, debugging, code suggestions, and patience.

I also appreciate the fact that someone didn't just give me the full
code, struggling through googling etc. helped me learn.
I'm still a rookie/neophyte, but a very happy one this morning!
Aug. 7 to now, thanks for all the help!

Thanks again!

Regards,
Greg Lund

PS, but aren't you all going to miss my daily stupid questions? (ok,
maybe not!).


More information about the Tutor mailing list