1import os
2import zipfile
3
4def zip_directory(folder_path, zip_path):
5 with zipfile.ZipFile(zip_path, mode='w') as zipf:
6 len_dir_path = len(folder_path)
7 for root, _, files in os.walk(folder_path):
8 for file in files:
9 file_path = os.path.join(root, file)
10 zipf.write(file_path, file_path[len_dir_path:])
11
12zip_directory('C:/FolderToZip', 'C:/Folder.zip')
1#!/usr/bin/env python
2import os
3import zipfile
4
5def zipdir(path, ziph):
6 # ziph is zipfile handle
7 for root, dirs, files in os.walk(path):
8 for file in files:
9 ziph.write(os.path.join(root, file))
10
11if __name__ == '__main__':
12 zipf = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED)
13 zipdir('tmp/', zipf)
14 zipf.close()