How can I extract a .zip
or .rar
file using Python?
5 Answers
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: stackoverflow.com/questions/29631793/… Commented Apr 27, 2015 at 11:00
-
67I 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. Commented Dec 11, 2016 at 7:34
Try the pyunpack
package:
from pyunpack import Archive
Archive('a.zip').extractall('/path/to')
-
2
-
1yes without it only zip files can be extracted. look in the pyunpack documentation the link I provided there is everything Commented Feb 7, 2014 at 10:30
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)
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 :
-
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 (rarlab.com/rar_add.htm). Direct link for windows: rarlab.com/rar/unrarw32.exe. Make sure to have this file. I put it inC:\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.– E235Commented Apr 4, 2017 at 13:25 -
1No, it needs
unrar.exe
which is not available on Linux Commented May 8, 2020 at 17:40 -
1@MaksymGanenko: On Linux it needs the
unrar
executable, which can be installed using the package manager. On Debian/Ubuntu it'ssudo apt install unrar
Commented Aug 7, 2020 at 7: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 programunar
instead ofunrar
as a backed as well. Commented Oct 26, 2020 at 20:48 -
For windows it worked perfect for me, but the other packages introduced in the other answer didn't. Commented Apr 28, 2021 at 9:23
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.