Does anybody have a good method of doing this?
I've tried using the zipfile python library but am utterly confused on how to do it for a file geodatabase since it's not exactly a directory/file structure. My folder structure looks like so, I want to zip up the folder/geodatabase outlined in red:
I've tried this (all the indentation is correct in my script, don't know why GeoNet is changing it upon publishing the question):
import zipfile #Creates the empty zip file and opens it for writing myzipfile = zipfile.ZipFile("D:\GIS_Testing\HabitatDbase\MyZip.zip", 'w', zipfile.ZIP_DEFLATED) for root, dirs, files in os.walk("D:\GIS_Testing\HabitatDbase"): if root == "D:\GIS_Testing\HabitatDbase\HabitatData.gdb": for f in files: myzipfile.write(os.path.join(root, file))
I've also tried using shutil.make_archive and for some reason instead of zipping up my file geodatabase it zips up my python script file which is completely bewildering as I have no idea how it would even get the path to the script file...
I get a TypeError on the last line that says "object of type 'type' has no len()"
Solved! Go to Solution.
Hahaha, seriously, of course it's just a typo. Thank you!! I'll leave the thread up because I couldn't find any good examples on how to zip up a file geodatabase in python on GeoNet. Maybe it'll help someone later...
This is one of the only examples I can find of zipping up a .gdb with python, however when I try to use this method,(accounting for the typo in the last line) my new zip file contains the entire folder structure leading up to the .gdb. Anyone have an idea of how to just zip up the .gdb file?
As usual, seconds after posting a question -- I get it to work. However I used shutil.make_archive instead of the above ZipFile method.
shutil.make_archive(zipfilename, 'zip', fgdb)
EDIT: Actually, while the above code technically works, it won't be a good solution if you're trying to zip up the fgdb for uploading to ArcGIS Online. Instead, have fgdb point to the folder that contains the MyFGDB.gdb file, rather than the .gdb file itself. Otherwise you'll get an error when uploading to AGO. You'll also want separate the the new zipfile and the fgdb into their own folders, or add code to grab the fgdb specifically (similar to what the original post is doing). If not, you'll get an empty zip within your zip since it creates the new zip file first before adding in your .gdb, looks like. I'm sure someone else has a more elegant solution to this bit.
The '.gdb' of a file geodatabase is a directory, not a file. The file geodatabase is the directory and all the files below it (except the lock files -- you can keep those out).