import zipfile
archive = zipfile.ZipFile('archive.zip')
for file in archive.namelist():
if file.startswith('foo/'):
archive.extract(file, 'destination_path')
import os
import shutil
import zipfile
my_dir = r"D:Download"
my_zip = r"D:Downloadmy_file.zip"
with zipfile.ZipFile(my_zip) as zip_file:
for member in zip_file.namelist():
filename = os.path.basename(member)
# skip directories
if not filename:
continue
# copy file (taken from zipfile's extract)
source = zip_file.open(member)
target = open(os.path.join(my_dir, filename), "wb")
with source, target:
shutil.copyfileobj(source, target)
from zipfile import ZipFile
import zipfile
myzip = ZipFile('test.zip')
myzip.extract(member='Roughwork/pretify.html')
import zipfile, os
working_directory = 'my_directory'
os.chdir(working_directory)
for file in os.listdir(working_directory): # get the list of files
if zipfile.is_zipfile(file): # if it is a zipfile, extract it
with zipfile.ZipFile(file) as item: # treat the file as a zip
item.extractall() # extract it in the 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 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)