Extracting zip file contents to specific directory in Python 2.7 Extracting zip file contents to specific directory in Python 2.7 python python

Extracting zip file contents to specific directory in Python 2.7


I think you've just got a mixup here. Should probably be something like the following:

import zipfilefh = open('test.zip', 'rb')z = zipfile.ZipFile(fh)for name in z.namelist():    outpath = "C:\\"    z.extract(name, outpath)fh.close()

and if you just want to extract all the files:

import zipfilewith zipfile.ZipFile('test.zip', "r") as z:    z.extractall("C:\\")

Use pip install zipfile36 for recent versions of Python

import zipfile36


I tried the other answers in this thread, but the final solution for me was simply:

zfile = zipfile.ZipFile('filename.zip')zfile.extractall(optional_target_folder)

Look at extractall, but use it only with trustworthy zip files.


Adding to secretmike's answer above with support for python 2.6 for extracting all files.

import zipfileimport contextlibwith contextlib.closing(zipfile.ZipFile('test.zip', "r")) as z:   z.extractall("C:\\")