1import zipfile
2
3archive = zipfile.ZipFile('archive.zip')
4
5for file in archive.namelist():
6 if file.startswith('foo/'):
7 archive.extract(file, 'destination_path')
1import os
2import shutil
3import zipfile
4
5my_dir = r"D:\Download"
6my_zip = r"D:\Download\my_file.zip"
7
8with zipfile.ZipFile(my_zip) as zip_file:
9 for member in zip_file.namelist():
10 filename = os.path.basename(member)
11 # skip directories
12 if not filename:
13 continue
14
15 # copy file (taken from zipfile's extract)
16 source = zip_file.open(member)
17 target = open(os.path.join(my_dir, filename), "wb")
18 with source, target:
19 shutil.copyfileobj(source, target)