Answers for "python zip folder and subfolders"

0

python zip folder and subfolders

import zipfile
import os
import sys

def zipfolder(foldername, target_dir):            
    zipobj = zipfile.ZipFile(foldername + '.zip', 'w', zipfile.ZIP_DEFLATED)
    rootlen = len(target_dir) + 1
    for base, dirs, files in os.walk(target_dir):
        for file in files:
            fn = os.path.join(base, file)
            zipobj.write(fn, fn[rootlen:])

zipfolder('thenameofthezipfile', 'thedirectorytobezipped') #insert your variables here
sys.exit()
Posted by: Guest on December-09-2020

Code answers related to "python zip folder and subfolders"

Python Answers by Framework

Browse Popular Code Answers by Language