How can I extract a .zip
or .rar
file using Python?
Asked
Active
Viewed 1.3e+01k times
5 Answers
42
Late, but I wasn't satisfied with any of the answers.
pip install patool
import patoolib
patoolib.extract_archive("foo_bar.rar", outdir="path here")
Works on Windows and linux without any other libraries needed.
-
Good library, but very few flexibility. For example, it cannot overwrite GZ files. See this question for more details: http://stackoverflow.com/questions/29631793/overwriting-previously-extracted-files-instead-of-creating-new-ones – multigoodverse Apr 27 '15 at 11:00
-
60I tried the above and got `patoolib.util.PatoolError: could not find an executable program to extract format rar; candidates are (rar,unrar,7z),` If I am understanding it correctly, I need to have one of the utilities, which defeats the purpose. – some user Dec 11 '16 at 07:34
22
Try the pyunpack
package:
from pyunpack import Archive
Archive('a.zip').extractall('/path/to')

fenceop
- 1,439
- 3
- 18
- 29

Irakli Darbuashvili
- 311
- 1
- 13
-
2
-
1yes without it only zip files can be extracted. look in the pyunpack documentation the link I provided there is everything – Irakli Darbuashvili Feb 07 '14 at 10:30
12
A good package for it is rarfile
:
Here is an example:
import rarfile
rf = rarfile.RarFile("myarchive.rar")
for f in rf.infolist():
print(f.filename, f.file_size)
if f.filename == "README":
print(rf.read(f))
Infos and docs here :

Davit Tovmasyan
- 3,238
- 2
- 20
- 36

Romibuzi
- 184
- 7
-
1If you decided to use `rarfile` you might have a problem when trying to extract a file. This is because the extraction is using the UnRaR.exe tool from winrar website (http://www.rarlab.com/rar_add.htm). Direct link for windows: http://www.rarlab.com/rar/unrarw32.exe. Make sure to have this file. I put it in `C:\Python27\UnRar.exe`. Edit the file: `C:\Python27\Lib\site-packages\rarfile.py` like that: `UNRAR_TOOL = r"c:\python27\unrar.exe"` It helped me. – E235 Apr 04 '17 at 13:25
-
-
@MaksymGanenko: On Linux it needs the `unrar` executable, which can be installed using the package manager. On Debian/Ubuntu it's `sudo apt install unrar` – MestreLion Aug 07 '20 at 07:27
-
for listing archive member names it doesn't require `unrar`, however, to extract and update it is required. You can have the open source program `unar` instead of `unrar` as a backed as well. – giuliano-oliveira Oct 26 '20 at 20:48
-
For windows it worked perfect for me, but the other packages introduced in the other answer didn't. – Maryam Bahrami Apr 28 '21 at 09:23
12
After some deep diving, here are my findings:
- RAR is not an free open format and is owned by RARLabs. You must install their DLL or exe first to work with RAR. Some programs like 7zip might already include this with them.
patool
is application that provides uniform command line as wrapper to other external compression applications. Natively, it can only deal with TAR, ZIP, BZIP2 and GZIP without needing external support.pyunpack
is Python library that can only deal with zip natively but provides interface topatool
.
With this in mind, following things worked for me:
- Make sure 7zip is installed
pip install patool pyunpack
Then to use it,
import pyunpack
pyunpack.Archive(archive_file).extractall(extract_dir)

Shital Shah
- 63,284
- 17
- 238
- 185
4
This method only requires having 7zip installed.
Works flawlessly on every system 7zip does.
No python packages needed at all.
import subprocess
subprocess.run('7z x -oOutdir archive.rar')
The subprocess module is comes with python.

Alexander
- 16,091
- 5
- 13
- 29