Created
November 21, 2020 02:07
-
-
Save gitcrtn/8b2c48c0fd3e18427a0766081e000d68 to your computer and use it in GitHub Desktop.
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
""" | |
Compress directory to zip | |
Usage: | |
python dir2zip.py <directory-path> | |
""" | |
import sys | |
import os | |
import shutil | |
from datetime import datetime | |
def error(msg): | |
print('Error: %s' % msg) | |
sys.exit(1) | |
def timestamp(): | |
return datetime.now().strftime("%Y%m%d-%H%M%S") | |
def main(): | |
if len(sys.argv) != 2: | |
error('set directory path as argument') | |
return | |
dir_path = sys.argv[1] | |
if not os.path.isdir(dir_path): | |
error('specified path is not directory') | |
return | |
parent_dir = os.path.dirname(dir_path) | |
if parent_dir == dir_path: | |
error('parent directory not found') | |
return | |
dir_name = os.path.basename(dir_path) | |
zip_path = os.path.join(parent_dir, '{}_{}'.format(dir_name, timestamp())) | |
shutil.make_archive(zip_path, 'zip', dir_path) | |
if __name__ == '__main__': | |
main() | |
# EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment