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

Steven D'Aprano steve at pearwood.info
Wed Sep 19 18:01:24 CEST 2012


Further comments below:


On 20/09/12 01:10, Gregory Lund wrote:

>> Have you considered the simpler code I gave in
>>
>> http://mail.python.org/pipermail/tutor/2012-August/090743.html
>>
>> before prodding on?
>
> Yes, I did consider it, but I didn't understand it enough to 'run with it'.
> If it worked perfectly I still wouldn't of understood it, and it I
> needed to tweak it, there would have been no way for me to figure out
> what to do to make it fit my scenario.
> While it may be 'simpler' for the experienced python coder, Not being
> familiar with Python, it wasn't simpler for me, I could hardly read
> any of it.
> I even printed it out and saved, but I couldn't understand it and
> didn't want to bother you with it any more (felt like an idiot, to be
> honest) (you would of had to explain everything, as I didn't
> understand hardly any of it)

That's what we're here for! Don't be shy about asking questions.

However, I have to say Peter was a bit optimistic in his assumptions about
your general level of expertise. He mixed Python code and (probably) Linux
shell commands and output, which probably didn't help.

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)


and then run it from the shell like this:

python unzip_twice.py NAME-OF-ZIP-FILE NAME-OF-FOLDER-TO-EXTRACT-TO

(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.

You'll also need to make sure that you have write permission to the
folder, and read permission to the zip file. If you get Permission
Denied errors, check the permissions.

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/

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?"

:-)


-- 
Steven



More information about the Tutor mailing list