import zipfile
with zipfile.ZipFile("file.zip","r") as zip_ref:
zip_ref.extractall("targetdir")
import zipfile
with zipfile.ZipFile(path_to_zip_file, 'r') as zip_ref:
zip_ref.extractall(directory_to_extract_to)
# Extract all contents from zip file
import zipfile
with zipfile.ZipFile('filename', 'r') as myzip: #'r' reads file, 'w' writes file
myzip.extractall()
# The zipfile will be extracted and content will be available in your working
# directory.
# app.py
from zipfile import ZipFile
with ZipFile('Mail3.zip', 'r') as zipObj:
# Extract all the contents of zip file in different directory
zipObj.extractall('temp')
print('File is unzipped in temp folder')
from zipfile import ZipFile
import zipfile
with ZipFile('test.zip') as myzip:
with myzip.open('Roughwork/textfile.text') as myfile:
print(myfile.readlines())Copy Code
from zipfile import ZipFile
import zipfile
myzip = ZipFile('test.zip')
myzip.extract(member='Roughwork/pretify.html')
ZipFile.extractall(path=None, members=None, pwd=None)
import shutil
import zipfile
# base_name is the name of the zip file you want to create
# format is zip for zip file
# root_dir is the direct path of the folder or file you want to zip
shutil.make_archive(base_name='zip_file_name', format='zip', root_dir='data')
# read zip file from current path
with zipfile.ZipFile(file='zip_file_name.zip', mode='r') as zip_ref:
# create folder name extract_data in current directory with the extracted data
zip_ref.extractall(path='extract_data')
# Extract a single file from a zip file
with zipfile.ZipFile(file='zip_file_name.zip', mode='r') as zip_ref:
# Extract a file name called secrets.dat
zip_ref.extract(member='secrets.dat')
# extract a list of filename within a zip file
with zipfile.ZipFile(file='zip_file_name.zip', mode='r') as zip_obj:
# Get list of files names in zip
filenames = zip_obj.namelist()
# Iterate over the list of file names in given list & print them
for filename in filenames:
print(filename)
with ZipFile('spam.zip') as myzip:
with myzip.open('eggs.txt') as myfile:
print(myfile.read())
ZipFile.extract(member, path=None, pwd=None)