I read through the zipfile
documentation, but couldn't understand how to unzip a file, only how to zip a file. How do I unzip all the contents of a zip file into the same directory?

- 30,962
- 25
- 85
- 135

- 61,037
- 23
- 50
- 66
-
3Related: unzipping files recursively https://stackoverflow.com/questions/28339000/unzip-zip-files-in-folders-and-subfolders-with-python & https://stackoverflow.com/questions/36285502/how-to-extract-zip-file-recursively-in-python – Tung Feb 26 '20 at 18:33
-
6For a one line extraction, see [`shutil.unpack_archive()`](https://docs.python.org/3/library/shutil.html#shutil.unpack_archive). – phoenix Dec 23 '20 at 13:05
-
1@fonini answer is -- as of 2021 -- the right/best one: https://stackoverflow.com/a/64110098/687896 – Brandt Jun 07 '21 at 06:06
9 Answers
import zipfile
with zipfile.ZipFile(path_to_zip_file, 'r') as zip_ref:
zip_ref.extractall(directory_to_extract_to)
That's pretty much it!

- 19,744
- 1
- 25
- 29
-
11what if the contents of the .zip archive are same, in all .zip archives? how to rename the content before extracting? example: 1.zip 2.zip.. all contain content.txt : extract all like 1content.txt 2content.txt? – Irtaza Apr 07 '16 at 16:59
-
13@iratzhash I typically create a new temporary directory for the contents using tempfile: https://docs.python.org/3/library/tempfile.html I unzip to the temporary directory and the move / organize the files from there. – Dave Forgac Jul 12 '17 at 17:28
-
19@3kstc I would `from zipfile import ZipFile`. When using it, you no longer need to use `zipfile.ZipFile`, and can use `ZipFile(zip_file_name)`. – Debug255 Feb 13 '18 at 02:00
-
@iratzhash I realize you commented 1.5 years ago. But just so others know, usually contents within a zip file are read-only. A good answer is [here](https://stackoverflow.com/questions/7428318/rename-files-in-zip-folder-using-zipmodule?lq=1) by "bouke" – Debug255 Feb 13 '18 at 02:03
-
3thnx, note: There is no zipfile library, no need to pip install, zipfile is already there... – ntg Apr 09 '19 at 09:06
-
3I'd add: `from tempfile import mkdtemp; directory_to_extract_to = mkdtemp()` – Martin Thoma Jul 31 '19 at 15:23
-
1for passwords use: with ZipFile(zip_file_path, 'r') as zipObj: zipObj.extractall(output_folder, pwd=b'myPassword') – Ryan Loggerythm Aug 29 '21 at 00:42
-
This minimal code will still do the job without problem: `z = ZipFile(path_to_zip_file)` `z.extractall(directory_to_extract_to)` – Selfcontrol7 Feb 03 '22 at 20:05
If you are using Python 3.2 or later:
import zipfile
with zipfile.ZipFile("file.zip","r") as zip_ref:
zip_ref.extractall("targetdir")
You dont need to use the close or try/catch with this as it uses the context manager construction.

- 31,877
- 16
- 137
- 115

- 4,949
- 2
- 19
- 28
-
24ZipFile also works as a context manager in 2.7 or later: https://docs.python.org/2/library/zipfile.html#zipfile.ZipFile – FelixEnescu Jan 22 '17 at 15:01
-
How to deal with https://docs.python.org/3.6/library/zipfile.html#zipfile.BadZipFile exception? Generally, what is the best practice to use try/except with context manager (with-statement)? – SnowOnion Mar 06 '19 at 08:24
-
2
-
2is there a reason to avoid `os.system(f'tar -xvzf {filename}')` and instead use zipfile (e.g. `zip = ZipFile('file.zip'); zip.extractall() )` or `shutil.unpack_archive(filename, extract_dir)`? – Charlie Parker Jan 21 '21 at 20:09
-
3@CharlieParker The main reason is portability. `system` calls are OS dependent. For example, `tar` would not be available on Windows. – Fareanor May 18 '21 at 16:24
-
I do not recommend zipfile. I had problems to extract files added in existing zip file. This can be done with total commander, which addz new file entry and file database at the end of the file. `zipfile` extracted only the old files. `shutil.unpack_archive` does not have this problem. – tista3 Oct 25 '21 at 09:53
zipfile
is a somewhat low-level library. Unless you need the specifics that it provides, you can get away with shutil
's higher-level functions make_archive
and unpack_archive
.
make_archive
is already described in this answer. As for unpack_archive
:
import shutil
shutil.unpack_archive(filename, extract_dir)
unpack_archive
detects the compression format automatically from the "extension" of filename
(.zip
, .tar.gz
, etc), and so does make_archive
. Also, filename
and extract_dir
can be any path-like objects (e.g. pathlib.Path instances) since Python 3.7.

- 2,989
- 3
- 21
- 39
-
2This method doesn't work when the zip file has a custom extension, e.g. (`.omt` for OmegaT project packages). It gives `raise ReadError("Unknown archive format '{0}'".format(filename))`. – msoutopico Mar 03 '21 at 21:00
-
1@msoutopico you can specify thje format explicitly: `shutil.unpack_archive(filename, extract_dir, format)` – fonini Mar 04 '21 at 19:42
-
1what is wrong with `os.system(f'tar -xvzf {path2zip} -C {path2unzip}/')`? – Charlie Parker Aug 10 '21 at 18:49
-
10@CharlieParker you have already asked the same thing in a comment to another answer, and that comment was answered: https://stackoverflow.com/questions/3451111/unzipping-files-in-python/64110098?noredirect=1#comment119468939_36662770 `os.system` is not portable, opens up security issues, is harder to use correctly (e.g. your proposal fails when the paths have special characters), and is less readable. – fonini Aug 10 '21 at 18:58
-
This solution doesn’t seem to maintain executable bits on the stuff inside the archive as it’s unzipped? – Cody Martin Jan 26 '23 at 00:51
Use the extractall
method, if you're using Python 2.6+
zip = ZipFile('file.zip')
zip.extractall()

- 12,626
- 4
- 38
- 49
-
Don't you have to specify a destination (zip.extractall(destination))? – asonnenschein Oct 24 '13 at 18:19
-
4Not if you're just extracting into the same directory as the zipfile – Dan Gayle Dec 04 '13 at 20:01
-
17@DanGayle this appears to be extracting the zip file into the current working directory, NOT the location of the zip file – Brian Leishman Jun 09 '17 at 14:41
-
5for me, ZipFile() didn't work but zipfile.ZipFile() did - after import zipfile – Agile Bean Sep 29 '18 at 07:34
-
4You need to `zip.close()` at the end if you don't use a `with` statement like the other answers suggest. – Boris Verkhovskiy Jun 22 '20 at 17:52
-
what is wrong with `os.system(f'tar -xvzf {path2zip} -C {path2unzip}/')`? – Charlie Parker Aug 10 '21 at 18:49
-
You can also import only ZipFile
:
from zipfile import ZipFile
zf = ZipFile('path_to_file/file.zip', 'r')
zf.extractall('path_to_extract_folder')
zf.close()
Works in Python 2 and Python 3.

- 31,877
- 16
- 137
- 115
-
1Thank you for your attention @MylesHollowed However, this is not a copy from the accepted answer. I agree that they are similar to each other, but they are different. This is also indicated by your comment, because the accepted one is definitely better for you than mine. If it was a copy, it would be the same... For someone my answer may be valuable because it is perhaps more readable and as you noticed import less code... It is because of these differences that I decided to put my answer to give an alternative. Is not that why we can put other answers after accepting one? All the best – simhumileco Oct 30 '18 at 08:22
-
What's wrong with this answer? Why did someone give her a negative point? After all, it is the answer to the question and is distinguished by its simplicity compared to other answers, which may be important for some people who are looking for an answer. Isn't it? – simhumileco Jan 28 '20 at 14:09
-
1@MylesHollowed `import zipfile.ZipFile` generates `ModuleNotFoundError: No module named 'zipfile.ZipFile'; 'zipfile' is not a package` in 3.6.5. I am open to it being operator error on my part, but I don't know what it is. – MikeF Feb 26 '20 at 16:38
-
1@MikeF I had the same problem with Python 3.8.5 but the workaround was to use `from zipfile import ZipFile`. Hope this helps. – Ben Dalling Oct 15 '20 at 09:53
try this :
import zipfile
def un_zipFiles(path):
files=os.listdir(path)
for file in files:
if file.endswith('.zip'):
filePath=path+'/'+file
zip_file = zipfile.ZipFile(filePath)
for names in zip_file.namelist():
zip_file.extract(names,path)
zip_file.close()
path : unzip file's path

- 157
- 1
- 4
from zipfile import ZipFile
ZipFile("YOURZIP.zip").extractall("YOUR_DESTINATION_DIRECTORY")
The directory where you will extract your files doesn't need to exist before, you name it at this moment
YOURZIP.zip is the name of the zip if your project is in the same directory. If not, use the PATH i.e : C://....//YOURZIP.zip
Think to escape the /
by an other /
in the PATH
If you have a permission denied
try to launch your ide (i.e: Anaconda) as administrator
YOUR_DESTINATION_DIRECTORY will be created in the same directory than your project

- 2,871
- 17
- 22
If you want to do it in shell, instead of writing code.
python3 -m zipfile -e myfiles.zip myfiles/
myfiles.zip
is the zip archive and myfiles
is the path to extract the files.

- 1,332
- 17
- 27
-
2
-
3ZIP files are not tar files. Unless you have a special version of `tar` which handles ZIP archives, your command wont work at all. `tar` with the `-z` option processes gzipped tar archives (generally files with extensions `.tgz` or `.tar.gz`) – Perry Jan 22 '21 at 19:00
import os
zip_file_path = "C:\AA\BB"
file_list = os.listdir(path)
abs_path = []
for a in file_list:
x = zip_file_path+'\\'+a
print x
abs_path.append(x)
for f in abs_path:
zip=zipfile.ZipFile(f)
zip.extractall(zip_file_path)
This does not contain validation for the file if its not zip. If the folder contains non .zip file it will fail.

- 39
- 3