Skip to content

Instantly share code, notes, and snippets.

@wasabi0522
Created April 8, 2011 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wasabi0522/909736 to your computer and use it in GitHub Desktop.
Save wasabi0522/909736 to your computer and use it in GitHub Desktop.
#!/bin/env python
# require python 2.x
# usage: zipdirs.py <targetdirname>
# $ cd ; zipdirs.py ~/hoge
# $ cd hoge ; ls
# fuga/
# fuga.zip
# piyo/
# piyo.zip
import sys
import os
import zipfile
def zipalldir(tareget_dir):
pwd = os.getcwd()
os.chdir(target_dir)
pathbase = os.path.abspath(os.getcwd())
for entry in os.listdir("./"):
target = pathbase + "/" + entry
if os.path.isdir(target):
zipdir(entry)
os.chdir(pwd)
# dirname: archive target
def zipdir(dirname):
arcname = dirname + ".zip"
arcfile = zipfile.ZipFile(arcname, "w", zipfile.ZIP_DEFLATED)
for root, dirs, files in os.walk(dirname):
for dir in dirs:
arcfile.write(root + "/" + dir)
for file in files:
arcfile.write(root + "/" + file)
arcfile.close()
if __name__ == '__main__':
target_dir = sys.argv[0]
zipalldir(target_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment