[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
Wed Sep 19 18:29:37 CEST 2012


> Using Peter's code, if you create a plain text file called "unzip_twice.py"
> containing:
>
>
> 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)
>
Consider it done, i have a new .py file saved as 'unzip_twice.py'
pasted below:

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)

>
> and then run it from the shell like this:
>
> python unzip_twice.py NAME-OF-ZIP-FILE NAME-OF-FOLDER-TO-EXTRACT-TO
>
In the IDLE 2.6.5 shell I typed (exactly what is in quotes, without
quotes of course):  "python unzip_twice.py 2012-09-18 Lab_2.zip
Student_Work_Sample_usecopy1"
where  '2012-09-18 Lab_2.zip' was/is the original zipfile and
'Student_Work_Sample_usecopy1' is the folder in which I want it all
housed/extracted

I got what is in quotes below:
"IDLE 2.6.5
>>> python unzip_twice.py 2012-09-18 Lab_2.zip Student_Work_Sample_usecopy1
SyntaxError: invalid syntax"
(unzip_twice) was the highlighted invalid syntax

I tried again:
In the IDLE 2.6.5 shell I typed (exactly what is in quotes, without
quotes of course):  "unzip_twice.py 2012-09-18 Lab_2.zip
Student_Work_Sample_usecopy1"
where  '2012-09-18 Lab_2.zip' was/is the original zipfile and
'Student_Work_Sample_usecopy1' is the folder in which it exists and I
want it all housed/extracted

I got what is in quotes below:
"IDLE 2.6.5
>>> unzip_twice.py 2012-09-18 Lab_2.zip Student_Work_Sample_usecopy1
SyntaxError: invalid syntax"
(2012) was the highlighted 'invalid syntax'

Maybe its the version of Python?
Maybe I didn't read what you wrote to type into the shell properly?
Maybe it's the spaces and dashes in the zipfile name? (the LMS does
that (learning Management system)


> (the folder must already exist), it may do what you want. Make sure
> you test it on a sample set of data, not the real thing.
I created copies of my original test data and folders:
'Student_Work_Sample_usecopy1' was the folder and w/in it: the actual
zipfile.

>
> I see that you're using Windows. I don't have Windows myself, but I
> think you'll probably have fewer problems with pathnames if you use
> forward slashes instead of backslashes. So:
>
> D:/D_Drive_Documents/Student_Work_Sample_use/Lab_2/aforker/
>
yes, Windows 7, but I didn't type out the slashes, they were coming in
via what the code was running.
I couldn't figure out where to ensure that they were forward (or
double backslashes).
> Good luck and don't worry about asking dumb questions, the only dumb
> question is "Was it you or your brother that was killed in the war?"

Thanks, as you can tell, I need it!
Greg


More information about the Tutor mailing list