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

Oscar Benjamin oscar.j.benjamin at gmail.com
Tue Sep 25 02:20:50 CEST 2012


On 25 September 2012 00:33, Gregory Lund <gnj091405 at gmail.com> wrote:
>
> 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()
>
> 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.
>

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'
you need to give
 'D:\D_Drive_Documents\Student_Work_Sample_usecopy1\Lab_2\aforker\aforker_Lab2.zip'

You can create the full path with:

import os.path   # Put this at the top of your file

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

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

Oscar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120925/233486ad/attachment.html>


More information about the Tutor mailing list