Skip to content

Instantly share code, notes, and snippets.

@roberto-arista
Created February 12, 2015 10:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roberto-arista/a2607a892254a71f159f to your computer and use it in GitHub Desktop.
Save roberto-arista/a2607a892254a71f159f to your computer and use it in GitHub Desktop.
#!/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