Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#############
# Zip ufos! #
#############
### Modules
import zipfile
import os
import shutil
from robofab.interface.all.dialogs import GetFolder
### Functions
def make_zipfile(output_filename, source_dir): # code from http://stackoverflow.com/questions/1855095/how-to-create-a-zip-archive-of-a-directory
relroot = os.path.abspath(os.path.join(source_dir, os.pardir))
with zipfile.ZipFile(output_filename, "w", zipfile.ZIP_DEFLATED) as zip:
for root, dirs, files in os.walk(source_dir):
zip.write(root, os.path.relpath(root, relroot))
for file in files:
filename = os.path.join(root, file)
if os.path.isfile(filename):
arcname = os.path.join(os.path.relpath(root, relroot), file)
zip.write(filename, arcname)
### Variables
input_path = GetFolder('Where do I have to look for ufos?')
### Instructions
for root, dirnames, filenames in os.walk(input_path):
for dirname in dirnames:
if dirname.endswith('.ufo'):
print root + os.sep + dirname
make_zipfile(root + os.sep + dirname[:-4]+'.zip', root + os.sep + dirname)
shutil.rmtree(root + os.sep + dirname)
print 'done!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment