-
Notifications
You must be signed in to change notification settings - Fork 0
/
14_build_a_zip_archive.py
34 lines (28 loc) · 1.09 KB
/
14_build_a_zip_archive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import os
import pathlib
from zipfile import ZipFile
def zip_all(search_dir, extensions, output_path):
"""Zips files from a given searching directory.
This function considers only files with a specified
extension. The folder structure is preserved.
Args:
search_dir (str): searching directory,
extensions (list): extensions; files from
the searching directory will be filtered by them,
output_path (str): path to save the zipped folder.
"""
with ZipFile(output_path, 'w') as zip_object:
for folder, _, filenames in os.walk(search_dir):
rel_path = os.path.relpath(folder, search_dir)
for filename in filenames:
if pathlib.Path(filename).suffix in extensions:
zip_object.write(
os.path.join(folder, filename),
os.path.join(rel_path, filename)
)
if __name__ == "__main__":
zip_all(
search_dir='files',
extensions=['.txt'],
output_path='files/files.zip'
)