1

I need to zip groups of files in a single directory similar to the example below -

a.jpg
a.png
a.gif
b.jpg
b.png
b.gif
c.jpg
c.png
c.gif

I need all of the a's to be zipped into one file, all of the b's zipped into one file and all of the c's zipped into one file.

What's the best way to go about doing this? Most of what I've found online is for zipping an entire directory. I'm considering shutil.make_archive?

franchyze923
  • 1,060
  • 2
  • 12
  • 38
  • ---------- The answer you should find here: https://stackoverflow.com/questions/1855095/how-to-create-a-zip-archive-of-a-directory – artona Jun 02 '17 at 17:40

1 Answers1

0

You could use Python's zipfile library as follows:

import zipfile
import glob
import string


for letter in string.lowercase:
    file_list = glob.glob('{}*.*'.format(letter))

    if len(file_list):
        with zipfile.ZipFile(r'c:\output zips\{}.zip'.format(letter), 'w') as zip:
            for file_name in file_list:
                zip.write(file_name)

First adjust the output zip folder location.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97