54

This is the code I am currently using to extract a zip file that lives in the same current working directory as the script. How can I specify a different directory to extract to?

The code I tried is not extracting it where I want.

import zipfile

fh = open('test.zip', 'rb')
z = zipfile.ZipFile(fh)
for name in z.namelist():
    outfile = open(name, 'wb')
    outfile.write('C:\\'+z.read(name))
    outfile.close()
fh.close()
lodkkx
  • 1,163
  • 2
  • 17
  • 29

6 Answers6

113

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

import zipfile

fh = 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 zipfile

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

Use pip install zipfile36 for recent versions of Python

import zipfile36
Van Peer
  • 2,127
  • 2
  • 25
  • 35
secretmike
  • 9,814
  • 3
  • 33
  • 38
  • ZipFile has no attribute `__exit__` so I don't know how your with statement would work as is. – adam Aug 31 '15 at 18:03
  • If you create a `ZipFile` instance it does have an `__exit__` method. I just gave the example another try (python 2.7) and it works as written. – secretmike Sep 02 '15 at 13:57
  • Python 2.6 has different implementation of ZipFile for some reason. – adam Sep 02 '15 at 16:51
  • Yeah, looking at the docs (https://docs.python.org/2/library/zipfile.html) it says ZipFile only supports use as a context manager as of python 2.7 - I didn't try with 2.6 since in this case the question specified python 2.7 – secretmike Sep 02 '15 at 22:58
12

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.

fiatjaf
  • 11,479
  • 5
  • 56
  • 72
  • 2
    I suggest calling `zfile.close()`, too, as it says in [ZipFile.close](https://docs.python.org/2/library/zipfile.html#zipfile.ZipFile.close) that **You must call close() before exiting your program or essential records will not be written.**. – erwaman Apr 11 '16 at 16:47
5

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

import zipfile
import contextlib


with contextlib.closing(zipfile.ZipFile('test.zip', "r")) as z:
   z.extractall("C:\\")
Slakker
  • 171
  • 2
  • 7
4

If you just want to extract a zip file from the command line using Python (say because you don't have the unzip command available), then you can call the zipfile module directly

python -m zipfile -e monty.zip target-dir/

Take a look at the docs. It also supports compression and listing the contents.

Peter Gibson
  • 19,086
  • 7
  • 60
  • 64
2

Peter de Rivaz has a point in the comment above. You are going to want to have the directory in the call to open(). You are going to want to do something like this:

import zipfile
import os

os.mkdir('outdir')
fh = open('test.zip','rb')
z = zipfile.ZipFile(fh)
for name in z.namelist():
    outfile = open('outdir'+'/'+name, 'wb')
    outfile.write()
    outfile.close()
fh.close()
ratmatz
  • 569
  • 3
  • 8
0

I've modified the code to ask the user input the file name and its path where it need to be extracted and so the user will've more control on where to put the extracted folder and what name should be assigned to the extracted folder.

import zipfile

#picking zip file from the directory
ZipFileName = raw_input("Enter full path to zip file:")  
fh = open( ZipFileName , 'rb')
z = zipfile.ZipFile(fh)

#assigning a name to the extracted zip folder
DestZipFolderName = raw_input("Assign destination folder a name: ")
DestPathName = raw_input("Enter destination directory: ")
DestPath = DestPathName + "\\" + DestZipFolderName

for name in z.namelist():   
    outpath = DestPath
    z.extract(name, outpath)
fh.close()
MD SARFARAZ
  • 117
  • 3
  • 17