Created
January 12, 2018 23:12
-
-
Save derekedelaney/044881a1cbd9ec492ff38c5d3a19b55e to your computer and use it in GitHub Desktop.
zip a folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# zips a folder | |
import os | |
import sys | |
import argparse | |
import zipfile | |
def zip(src, dst): | |
fname = "%s.zip" % (dst) | |
if os.path.exists(fname): | |
os.remove(fname) | |
print("deleted file " + fname) | |
zf = zipfile.ZipFile("%s.zip" % (dst), "w", zipfile.ZIP_DEFLATED) | |
abs_src = os.path.abspath(src) | |
for dirname, subdirs, files in os.walk(src): | |
for filename in files: | |
absname = os.path.abspath(os.path.join(dirname, filename)) | |
arcname = absname[len(abs_src) + 1:] | |
print("zipping " + os.path.join(dirname, filename) + " as " + arcname) | |
zf.write(absname, arcname) | |
zf.close() | |
return True | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("src", help="Name of the root file") | |
parser.add_argument("dst", help="Name of the zip file") | |
args = parser.parse_args() | |
if not zip(args.src, args.dst): | |
sys.exit(1) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment