[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 04:24:30 CEST 2012

>> Near as I can tell, I got rid of the permissions error, the ZipFile
>> error with the missing capital 'F'
>> Now I need to 'get into' the non zipped folder of each student and
>> unzip any and all zips that are inside of it.
> The error message says 'No such file or directory'. That means that when
> Python tries to open a file with the name you gave it can't find a file that
> has that name. In this case I suspect the problem is that you need to give
> the full path to each file i.e. instead of
>   'Lab_2/aforker/aforker_Lab2.zip'

full code is now:

import os, os.path, zipfile, arcpy

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

outDir = r"D:\D_Drive_Documents\Student_Work_Sample_usecopy1"

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


zipContents = z.namelist()
print zipContents

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,'a')

NO errors! yea!
But, it's (I'm) still just extracting the first zip, not the zipfiles
within the Lab_2\aforker, Lab_2\allisw99 folders.

however, I used my brain (no comments!) and figured out that while
it's not extracting the info to the folders I want (the Lab_2\aforker,
Lab_2\allisw99 etc. folder, it's extracting them to the location in
which my script is stored.
I kept looking at it thinking 'OK, it didn't come up with an error,
but it's 'not' extracting, but it should be.... then I realized that I
didn't specify the folder to which I want it extracted.
Ta-da, that's why it's going back to where the script is stored.

I tried to use:
x.extractall(fullpath) but that of course gave me errors because
'fullpath' is the path of the file.
I need to figure out how to just list the respective Lab_2\aforker,
Lab_2\allisw99 folders.

Thus far, I have not had any luck.
Does python have a way to go back to the relative folder where the zip
is located?

> I just googled to find you a page explaining absolute paths and, by chance,
> came up with this from the ArcGIS documentation:
> http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Pathnames%20explained%3A%20Absolute%2C%20relative%2C%20UNC%2C%20and%20URL

Good find, It's like pulling teeth to explain paths/directories to my students.
The relative vs. absolute paths is one of their first obstacles to
learning, yet really it's a simple check box in ArcGIS that is NOT a
default, and resets each night in the computer lab.

I am going to use that link you provided, tomorrow, the first day of GIS311!


More information about the Tutor mailing list