python zip folder and subfolders

Solutions on MaxInterview for python zip folder and subfolders by the best coders in the world

showing results for - "python zip folder and subfolders"
David
25 Jul 2016
1import zipfile
2import os
3import sys
4
5def zipfolder(foldername, target_dir):            
6    zipobj = zipfile.ZipFile(foldername + '.zip', 'w', zipfile.ZIP_DEFLATED)
7    rootlen = len(target_dir) + 1
8    for base, dirs, files in os.walk(target_dir):
9        for file in files:
10            fn = os.path.join(base, file)
11            zipobj.write(fn, fn[rootlen:])
12
13zipfolder('thenameofthezipfile', 'thedirectorytobezipped') #insert your variables here
14sys.exit()
15