I need to zip a folder that containts an .xml file and a .fgdb file by using python. Could anyone help me? I tried a few scripts I found on internet but there is always some technical issue (such as creating an empty zip file, or create zip file I cannot open 'no permission' etc..)
Thanks in advance.
The key to making it work is the os.walk()
function. Here is a script I assembled 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()