[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
Fri Sep 21 21:10:01 CEST 2012

> You need to add the folder where python.exe is to your PATH variable.

I just realized the path we're heading down here isn't going to work.
I need on .py file to be up an running so that I can add it to my tool
in ArcGIS.

> Oscar

I appreciate the help Oscar, and I believe it would work that way, but...

I need on .py file to be up an running so that I can add it to my tool
in ArcGIS.

I would like to revise my question and add some more specifics.

I must use Python 2.6!
This is a task that I really want to automate, the situation is static
and once I create this, I'll be golden (good).
I must create one, stand alone script (in Idle, I hope) that will:

1. Unzip a single original zipfile (in_Zip) to the contents of the
folder that the zipfile currently resides.
2. Go to the unique (NON ZIPPED) folders (actually student usernames
'aforker', 'allisw99', 'btaylor7', etc) that result from step 1.
(there may be anywhere from 1 to 40 of these unique student folders)
3. Within each unique folder ('aforker', 'allisw99', 'btaylor7', etc)
extract any and all (could be none, could be 3 or 4) .zip files
within, to their relative aforementioned unique folders ('aforker',
'allisw99', 'btaylor7', etc), while 'navigating' i.e. not getting hung
up on possible .pdf or docx files that may or may not reside in the
unique folders ('aforker', 'allisw99', 'btaylor7', etc)

This is what I've got so far: (and it 'works') (I'll modify later so
that I do not need to hard-code the original zipfile (in_Zip))

import os, os.path, zipfile inZip =

outDir = r"D:\D_Drive_Documents\Student_Work_Sample_usecopy1"

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


zipContents = z.namelist()

print zipContents


It works, I get the following in the Python Shell:

'>>> ================================ RESTART ================================'


['Lab_2/aforker/', 'Lab_2/aforker/aforker_Lab2.zip',
'Lab_2/allisw99/', 'Lab_2/allisw99/allisw99_Lab2.zip',
'Lab_2/allisw99/allisw99_Lab2_Bonus.pdf', 'Lab_2/allisw992/',
'Lab_2/allisw992/allisw99_Lab2_Bonus.pdf', 'Lab_2/btaylor7/',
'Lab_2/btaylor7/btaylor7_Lab2.zip', 'Lab_2/']

'>>> '

But, what I can't figure out is how to get 'into' each unique folder:
aforker, allisw99, etc. and then extract any and all zips within
'aforker', 'allisw99', etc.

I have no doubt that the suggestions posted here will work, but they
all seem to get away from a single .py file and....
 a) I can't get them to work, and
b) it doesn't really help me because I need ONE stand alone .py file
to make this all work in ArcGIS.

 I will be using this to create an ArcGIS 'tool' that requires one
script (at least for me to comprehend it) :-)

Thank you in advance for any and all suggestions, tips etc.

For the record, I did try the following  (to at least try
something....) at  the bottom of the code above:

for item in zipContents:
    itemLoc = os.path.join(outDir,item)
    y = zipfile.ZipFile(itemLoc,'a')

but I get the following error:

Traceback (most recent call last): File
line 50, in y = zipfile.ZipFile(itemLoc,'a') File
"C:\Python26\ArcGIS10.0\lib\zipfile.py", line 687, in init self.fp =
open(file, modeDict[mode]) IOError: [Errno 13] Permission denied:

I guess my first issue is to resolve the 'Permission denied' problem.
And, I know I need an 'if' statement, somehow...

thanks in advance for any and all input!


PS, neophyte/rookie, trying to keep it simple. This is not my normal
daily task (good thing, right?)

More information about the Tutor mailing list