Extracting Zip Files

Alex Martelli aleaxit at yahoo.com
Wed Sep 29 15:50:46 CEST 2004


Greg Lindstrom <greg.lindstrom at novasyshealth.com> wrote:

> I've been zipping up files for months with zipfile, but today I was asked to
> extract the contents of a zip file and was sure there would be an "extract"
> method...but there's not one that I can find!  Am I just missing something??
> I found a module, unzip.py, on Parnassus, but it doesn't work.  Does anyone
> have a way for me to extract files?  Thanks!

First, instantiate z=zipfile.ZipFile('whatever.zip').  Now, z.namelist()
gives you a list of all the names in 'whatever.zip', and for each of
these names z.read(name) gives you all the bytes.  Net of subdirectory
issues, therefore, something like (note: untested!):

for n in z.namelist():
    open(n, 'w').write(z.read(n))

should extract all files to the current directory.


Alex



More information about the Python-list mailing list