# Extract all contents from zip fileimport 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.
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 pathwith 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 filewith 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 filewith 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 themfor filename in filenames:print(filename)
"""
Joining any number of iterables by combining elements in order
- Iterables include: str, list, tuples, dict etc...
- No error will be incurred if you zip lists of differing lengths,...
...it will simply zip up to the length of the shortest list
"""
lst1 =[1,2,3,4,5,7]
lst2 ="mangos"
lst3 =(3.1,5.4,0.2,23.2,8.88,898)
lst4 ={"Car":"Mercedes Benz","Location":"Eiffel Tower","Organism":"Tardigrade"}# lst5, lst6, ...
result =list(zip(lst1, lst2, lst3, lst4.keys()))# Check out dictionary methodsprint(result)## [(1, 'm', 3.1, 'Car'), (2, 'a', 5.4, 'Location'), (3, 'n', 0.2, 'Organism')]