Python subfolder zip folder

I need to zip up the folder containing the .xml file and the .fgdb file using python. Can anyone help me? I tried several scripts that I found on the Internet, but there is always some technical problem (for example, creating an empty zip file or creating a zip file, I cannot open "without permission", etc.)

Thanks in advance.

+5
source share
1 answer

The key to its operation is the os.walk () function. Here is a script that I put together in the past that should work. Let me know if you get any exceptions.

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()
+6
source

All Articles