LinearAndLoom Icons

Zip Files by Type: One ZIP per Extension (Free Script)

Free Python script that sorts a folder into one ZIP per file type: svg.zip, png.zip, eps.zip. Handy for preparing digital download packs.

Download zip-files-by-extension.pyPython 3 · 59 lines · 2 KB · free

Digital products are usually delivered by format: one ZIP with the SVG files, one with the PNG files, one with the EPS files. This script builds those ZIPs from a mixed folder in one step.

What it does

How to use it

  1. Run the script (see below). A folder window opens.
  2. Choose the folder that holds your files.
  3. The script lists each ZIP it creates and how many files went in.
  4. Type Y and press Enter to do another folder, or N to finish.

How to run a Python script

  1. Install Python 3 from python.org if you do not have it. It is free. On Windows, tick Add Python to PATH in the installer.
  2. Download the script with the button on this page and save it somewhere easy, such as your Desktop.
  3. Open a terminal. On Windows, press the Windows key, type cmd and press Enter. On Mac, open Terminal from Applications > Utilities.
  4. Type python followed by a space (on Mac, python3 and a space), drag the script file into the terminal window and press Enter.

On Windows you can also just double-click the script after installing Python.

The folder window is drawn with Tkinter, which comes with Python on Windows and Mac. On Linux, install it with sudo apt install python3-tk.

What it is useful for

Good to know

Running the script again on the same folder rebuilds each ZIP from the files that are there now, replacing the earlier svg.zip, png.zip and so on.

Questions

Can I change the ZIP names?

The ZIPs are named after the extension. Rename them afterwards, or use the find and replace script to add your product name.

Does it include files in subfolders?

No, only the files directly inside the chosen folder.

Is there a size limit?

No limit from the script. Check the upload limit of the shop or service you are sending the ZIP to.

The code

This is the complete script. Nothing else is downloaded or installed.

"""Zip files by extension

Sorts the files in a folder into one ZIP per file type:
all SVG files go into "svg.zip", all PNG files into "png.zip", and so on.

Free script from LinearAndLoom Icons: https://linearandloomicons.netlify.app/scripts/
Provided as is. Renaming and zipping cannot be undone, so try it on a copy of your folder first.
"""
import os
import zipfile
from collections import defaultdict
from tkinter import Tk, filedialog

def zip_by_extension(folder):
    groups = defaultdict(list)

    for filename in os.listdir(folder):
        filepath = os.path.join(folder, filename)

        if os.path.isfile(filepath):
            ext = os.path.splitext(filename)[1].lower().lstrip(".")
            # ZIP files are left out, so archives from an earlier run are never zipped into themselves
            if ext and ext != "zip":
                groups[ext].append(filepath)

    if not groups:
        print("No files found.")
        return

    for ext, files in groups.items():
        zip_path = os.path.join(folder, f"{ext}.zip")

        with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
            for file in files:
                zf.write(file, arcname=os.path.basename(file))

        print(f"Created: {ext}.zip ({len(files)} files)")

# Hide Tk window
root = Tk()
root.withdraw()

while True:
    folder = filedialog.askdirectory(title="Select Folder to Process")

    if not folder:
        print("No folder selected.")
    else:
        print(f"\nProcessing: {folder}")
        zip_by_extension(folder)
        print("Finished.\n")

    answer = input("Do you have another folder to process? (Y/N): ").strip().lower()

    if answer not in ("y", "yes"):
        break

print("Done.")
input("Press Enter to exit...")

Download zip-files-by-extension.pyPython 3 · 59 lines · 2 KB · free

These scripts are free. If one saved you time, you can say thanks by buying a pack from the LinearAndLoom Etsy shop.

More scripts