[Tutor] Unzip a zipfile, then unzip the zipfiles within the original zip
__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.
> 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
> ## Actual Code:
> import zipfile, arcpy, os.path, os
> #1 Defining a function to Unzip the initial (.zip) folder. (this worked
> def unzip_Orig(oZ):
> z = zipfile.ZipFile(oZ)
> q = "unzip_Unzip_Data_Test.zip"
> #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.
Forget about messing with the current working directory. Instead specify the
location of the outer zipfile and the destination directory explicitly, e.
$ cat unzip_twice.py
source_file = sys.argv
dest_folder = sys.argv
inner_zips_pattern = os.path.join(dest_folder, "*.zip")
for filename in glob.glob(inner_zips_pattern):
inner_folder = filename[:-4]
0 directories, 2 files
$ python unzip_twice.py 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
4 directories, 14 files
More information about the Tutor