[Tutor] Unzip a zipfile, then unzip the zipfiles within the original zip

Peter Otten __peter__ at web.de
Tue Aug 7 19:55:35 CEST 2012


Gregory Lund wrote:

[For improved readability please avoid # prefixes for the parts of your post 
that are not comments in snippets of python code]

> neophyte .py/pyTutor user.

Welcome.

> I am also a university GIS lecturer. My students submit their lab
> assignments (zipped up into one zipfile) to a depository, which I
> download as a single zip containing all 40 students' zipfiles.
> 
> Goal: unzip a zipfile of student zipfiles and then unzip each
> students' zipfile ( to prevent me from having to unzip each students'
> zipfile by hand. )
> 
> eventually I'll turn this into a tool in ArcGIS, that will grade
> some of their assignment, but for now, just want to get these extract
> processes running in python.
> 
> using (and want to use) 2.6 because I'm working with ArcGIS
> this is what I've got thus far:
> 
> 1 Unzip a single starting zipfile "initial_Zipfile" (that contains a
> series of 40 individual zipfiles) into a single folder named whatever
> it was originally named.
> 
> 2 Unzip all of the zipfiles within the 'initial_Zipfile' into their own 
folder.
> 
> ## Actual  Code:
> 
> import zipfile, arcpy, os.path, os
> 
> #1 Defining a function to Unzip the initial (.zip) folder. (this worked 
fine)
> 
> def unzip_Orig(oZ):
>     z = zipfile.ZipFile(oZ)
>     z.extractall()
>     return
> 
> q = "unzip_Unzip_Data_Test.zip"
> unzip_Orig(q)
> 
> #2 my attempts to unzip each zipfile within the new folder below
> (failed thus far)
> 
> mywd = os.getcwd()
> zipList = os.listdir(mywd)
> print zipList  #this didn't work, it printed the directory of where
> the .py was stored.
> #Zipfile.printdir() # returned "'Zipfile' is not defined.
> 
> thoughts?

Forget about messing with the current working directory. Instead specify the 
location of the outer zipfile and the destination directory explicitly, e. 
g.:

$ cat unzip_twice.py 
import glob
import os
import sys
import zipfile

source_file = sys.argv[1]
dest_folder = sys.argv[2]

zipfile.ZipFile(source_file).extractall(dest_folder)

inner_zips_pattern = os.path.join(dest_folder, "*.zip")
for filename in glob.glob(inner_zips_pattern):
    inner_folder = filename[:-4]
    zipfile.ZipFile(filename).extractall(inner_folder)
$ tree
.
├── all.zip
└── unzip_twice.py

0 directories, 2 files
$ python unzip_twice.py all.zip outer
$ tree
.
├── all.zip
├── outer
│   ├── 123
│   │   ├── 1.txt
│   │   ├── 2.txt
│   │   └── 3.txt
│   ├── 123.zip
│   ├── 456
│   │   ├── 4.txt
│   │   ├── 5.txt
│   │   └── 6.txt
│   ├── 456.zip
│   ├── 789
│   │   ├── 7.txt
│   │   ├── 8.txt
│   │   └── 9.txt
│   └── 789.zip
└── unzip_twice.py

4 directories, 14 files




More information about the Tutor mailing list