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?
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!
from zipfile import ZipFile
. When using it, you no longer need to use zipfile.ZipFile
, and can use ZipFile(zip_file_name)
. –
Pinup from tempfile import mkdtemp; directory_to_extract_to = mkdtemp()
–
Lorianne z = ZipFile(path_to_zip_file)
z.extractall(directory_to_extract_to)
–
Perjure 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.
zipfile
+ pathlib
= win. mind if i slightly update your answer? –
Aoudad 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)
? –
Stabler system
calls are OS dependent. For example, tar
would not be available on Windows. –
Ainslee zipfile
extracted only the old files. shutil.unpack_archive
does not have this problem. –
Welton 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.
.omt
for OmegaT project packages). It gives raise ReadError("Unknown archive format '{0}'".format(filename))
. –
Zygo shutil.unpack_archive(filename, extract_dir, format)
–
Schifra os.system(f'tar -xvzf {path2zip} -C {path2unzip}/')
? –
Stabler 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. –
Schifra Use the extractall
method, if you're using Python 2.6+
zip = ZipFile('file.zip')
zip.extractall()
zip.close()
at the end if you don't use a with
statement like the other answers suggest. –
Caesaria os.system(f'tar -xvzf {path2zip} -C {path2unzip}/')
? –
Stabler 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.
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. –
Mariettamariette from zipfile import ZipFile
. Hope this helps. –
Contessacontest 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.
tar -xvzf path_file
in your case? –
Stabler 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
) –
Assamese 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
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
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.
© 2022 - 2024 — McMap. All rights reserved.
shutil.unpack_archive()
. – Genesa