Tuesday, September 17, 2024

Zipping files

import os

from datetime import datetime

from zipfile import ZipFile


today = datetime.now() # set file name and time of creation

file_name = 'zipper_' + today.strftime('%Y.%m.%dh%H%M') + '.zip'

dir_name = 'To Zip'  # update path

def zipdir(path, zip):

  for root, dirs, files in os.walk(path):

    for file in files:

      zip.write(os.path.join(root, file))


if __name__ == '__main__':

  zipfile = ZipFile(file_name, 'w')

  zipdir(dir_name, zipfile)

  zipfile.close()

No comments:

Post a Comment