[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 01:33:36 CEST 2012


> No but Python did when it ran your code and it tried to tell you. The trick
> is to read the error message, see what line of code it occurs at and then
> look very closely at that line of code and the surrounding lines of code.
> The first thing to check for is a typo.

To be honest, I did check for typos, a misplaced period, etc., but
didn't notice it, I even compared it with the similar line above, but
didn't notice the 'F'.
(Truth Hurts)
>
> The error message that Python gives may seem cryptic but it's actually very
> informative if you know how to read it. For this reason it's also more
> helpful to show the *verbatim* error message when posting to this (or other)
> lists.
>
I will show the verbatim error message from now on, thank you for suggesting it!
, speaking of which:

This is my code, as modified based on all the great help thus far.

(again, I need a stand alone .py file for my purposes and need to use
Python 2.6 because it's the version that works with my GIS application
(Esri's ArcGIS).


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()
print zipContents
z.close()

for item in zipContents:
    if item.endswith('.zip'):
        x = zipfile.ZipFile(item,'r')
        x.extractall()
        x.close()

for the record, I print  'zipContents' to help me figure out how to
'get into' the first folder of the extracted zip which is never a zip
folder, but a non-zipped folder, that holds (usually) 1 or more .zip
files (folders that are zipped)

This is the way I receive the data from the Learning Management System
LMS). The initial zip (2012-09-18 Lab_2.zip) is created by the LMS,
and actually the internal folders (aforker, allisw99, etc.) are also
created by the LMS (its' how the LMS sorts the individual student
folders/upload using their user name.

my results and error of the above code is listed below.

IDLE 2.6.5
>>> ================================ 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/2nd_btaylor7_Lab2.zip',
'Lab_2/btaylor7/btaylor7_Lab2.zip', 'Lab_2/']

Traceback (most recent call last):
  File "D:/D_Drive_Documents/Scripts/Unzip_a_zip_of_zips/Scripts/unzip_a_zip_of_zips_rewrite_shortest_of_the_shorts2.py",
line 18, in <module>
    x = zipfile.ZipFile(item,'r')
  File "C:\Python26\ArcGIS10.0\lib\zipfile.py", line 683, in __init__
    self.fp = open(file, modeDict[mode])
IOError: [Errno 2] No such file or directory: 'Lab_2/aforker/aforker_Lab2.zip'
>>>

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.

Thoughts?
Thanks again Oscar and Dave for sticking with me and not throwing in
the towel on my rookie errors!

Greg


More information about the Tutor mailing list