João Vieira
João Vieira

Reputation: 29

Zipping files from a set using zipfile

I need to zip specific files on a path all together. I need to find certain files (for instance: AAA.txt, BBB.inf and etc) with exception to the ones that contain SFF in the name, and then zip them all together.

Every time I run this, I end up with my DB.zip with only CCC.txt inside. It means that every time the script finds a file from the set, it creates a new DB.zip with that new file inside, overwriting the previous one. How can I fix this?

import os
import zipfile

pr_directory = r'C:\Users\vijo9001\Desktop\Infact'
pr_path = os.listdir(pr_directory)
dst = r'C:\Users\vijo9001\Desktop\Infact'
os.chdir(r'C:\Users\vijo9001\Desktop\Infact')

my_set = {
"AAA",
"BBB",
"CCC"
}

for file in pr_path:
    zip_file = pr_directory + '/' + 'DB.zip'
    for x in my_set:
        if 'SFF' in file:
            continue
        if (str(x)) in file:
           zip = zipfile.ZipFile(zip_file, "w",)
           zip.write(file, compress_type=zipfile.ZIP_DEFLATED)
           zip.close()

Upvotes: 1

Views: 32

Answers (1)

sekky
sekky

Reputation: 834

There are a couple of things about your code I spotted which you might want to optimize/correct:

  1. zip is instantiated within each iteration through the for loops, overriding each other. Meaning you will generate a DB.zip file for a file "AAA1.txt", then overwrite it with a DB.zip containing only a file "AAA2.txt", and so on. By initiating zip before the for loops you will add each file in turn, generating a single DB.zip containing all files.
  2. Instead of having a for loop for every element in my_set, you can use a simple if-statement with the following condition: if any(x in file_split for x in my_set)
  3. zip_file would only need to be moved before the for loops to correctly declar zip
  4. zip shadows the zip library, for better clarity and to avoid mistakes it would be better to rename the variable (I used zip_dir in the code below)

I played around with your code a bit and came up with the following snippet:

my_set = {"AAA", "BBB", "CCC"}

zip_file = pr_directory + '/' + 'DB.zip'
zip_dir = zipfile.ZipFile(zip_file, "w",)

for file in pr_path:
    if 'SFF' in file:
        continue
    if any(x in file for x in my_set):
        zip_dir.write(file, compress_type=zipfile.ZIP_DEFLATED)

zip_dir.close()

This seems to work fine for me. Hope I was of help.

Upvotes: 1

Related Questions