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.
Creating a zip folder allows files to be organized and compressed to a smaller file size for distribution or saving space. Zip folder can have subfolders within this main folder.
Creating a Directory with Subdirectories Actually, makedirs() is implemented in such a way that it calls mkdir() to create one directory after the next. As a parameter makedirs() accepts the entire path to be created. This method is similar to the UNIX/Linux command mkdir -p .
Python's zipfile is a standard library module intended to manipulate ZIP files. This file format is a widely adopted industry standard when it comes to archiving and compressing digital data. You can use it to package together several related files.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With