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?
You could use Python's zipfile library as follows:
import zipfile
import glob
import string
for letter in string.lowercase:
file_list = glob.glob(f'{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.
This goes through each letter of the alphabet and creates a list of files for each. These are then added to a ZIP file which is called a.zip b.zip etc...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With