my_zip_file_1.py 719 B

123456789101112131415161718
  1. def archiver(path):
  2. '''Create a Zip archive, truncated to the basename of its path'''
  3. archive_name = os.path.abspath(os.path.basename(path) + '.zip')
  4. zf = zipfile.ZipFile(archive_name, 'w')
  5. inventory = glob.glob(os.path.join(path, '*'))
  6. for item in inventory:
  7. if os.path.isfile(item):
  8. base = os.path.basename(os.path.dirname(item))
  9. source = os.path.basename(item)
  10. join = os.path.join(base, source)
  11. try:
  12. zf.write(join)
  13. except:
  14. if not os.path.exists(base):
  15. os.mkdir(base)
  16. shutil.copy(item, base)
  17. zf.write(join)
  18. shutil.rmtree(base)