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
1import os
2import optparse
3import zipfile
4
5def get_arguments():
6 parser = optparse.OptionParser()
7 parser.add_option("--src","--source",dest="paths", help="Removing file with extension .SRT and .VTT")
8 (options,arguments)=parser.parse_args()
9 if not options.paths:
10 parser.error("[-] Please specify source, use --help for more info.")
11 return options
12
13
14def Source_folder(folder_path):
15 print('[+] Extracting zip file')
16 for path, dir_list, file_list in os.walk(folder_path):
17 try:
18 for file_name in file_list:
19 if file_name.endswith(".zip"):
20 abs_file_path = os.path.join(path, file_name)
21 parent_path = os.path.split(abs_file_path)[0]
22 output_folder_name = os.path.splitext(abs_file_path)[0]
23 output_path = os.path.join(parent_path, output_folder_name)
24
25 zip_obj = zipfile.ZipFile(abs_file_path, 'r')
26 zip_obj.extractall(output_path)
27 zip_obj.close()
28
29 except FileNotFoundError:
30 print('Error', file_name)
31
32
33options = get_arguments()
34Source_folder(options.paths)
1>>> x = [1, 2, 3]
2>>> y = [4, 5, 6]
3>>> zipped = zip(x, y)
4>>> list(zipped)
5[(1, 4), (2, 5), (3, 6)]
6>>> x2, y2 = zip(*zip(x, y))
7>>> x == list(x2) and y == list(y2)
8True