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')
1import zipfile
2filePaths = [] # Make an array string with all paths to files
3for root, directories, files in os.walk("MyDirectoryPath"): # Scans for all subfolders and files in MyDirectoryPath
4 for filename in files: # loops for every file
5 filePath = os.path.join(root, filename) # Joins both the directory and the file name
6 filePaths.append(filePath) # appends to the array
7z = zipfile.ZipFile("MyDirectoryPathWithZipExt.zip", 'w')
8with z:
9 for file in filePaths: # Loops for all files in array
10 z.write(file) # Writes file to MyDirectoryPathWithZipExt.zip