Maria Kayed
Maria Kayed

Reputation: 11

Create multiple ZIP files using python

I am trying to organize raster files. I have different images for different dates (30+ image for each date) and I want to compress them into one ZIP file for each date without having to rewrite the code with different names every time.

for example, I want it to compress all files that start with "L5_070704" into a zip file named "L5_070704", files starting with "L5_070501" into another zip file named "L5_070501", etc.

I looked online, every page I read gives only how to compress, if the name of the file starts with or ends with certain characters

this is the python code I am using, it worked for single images.

import os
import zipfile
L5_070704 = zipfile.ZipFile('F:\AOS_input\L5_070704.zip', 'w')
for folder, subfolders, files in os.walk('F:\AOS_input'):
    for file in files:
        if file.startswith('L5_070704'):
            L5_070704.write(os.path.join(folder, file), file, compress_type = zipfile.ZIP_DEFLATED)
L5_070704.close()

I am new to python.

Upvotes: 0

Views: 1443

Answers (2)

Rakesh Mosalpuri
Rakesh Mosalpuri

Reputation: 25

I have written a solution to your problem and it is as follows:

  1. List all the files to compressed.
import os
import shutil

print("----PROCESS STARTED----")

filelist = [] #list of files to be compressed
filegroup = set() #set for grouping files
basedir = "C:/Users/XYZ/" #base directory
extension = "jpg" #extension of the file to be compressed
extensionlen = 3 #extension length of the file to be compressed
folderstart = 0 #starting index of the 
folderend = 9

#list of files to be compressed
for f in os.listdir(basedir):
    if f[-extensionlen:] == extension :
        filelist.append(f)
  1. Find the list of group in which these files can be grouped.
#list of groups        
for file in filelist:
    filegroup.add(file[folderstart:folderend])
    print(file)
  1. Create the folder for these groups.
#create folder for the group    
for group in filegroup:
    print(group)
    if not os.path.isdir(basedir+group) :
        os.mkdir(basedir+group)
  1. Move the files to corresponding folders.
#move files to the folders
for file in filelist:
    os.rename(basedir+file,basedir+file[folderstart:folderend]+"/"+file)
  1. Compress the folders.
#compress the folders    
for group in filegroup:
    shutil.make_archive(basedir+group, 'zip', basedir+group)
    shutil.rmtree(basedir+group)

print("----PROCESS COMPLETED----")

Complete solution.

import os
import shutil

print("----PROCESS STARTED----")

filelist = [] #list of files to be compressed
filegroup = set() #set for grouping files
basedir = "C:/Users/XYZ/" #base directory
extension = "jpg" #extension of the file to be compressed
extensionlen = 3 #extension length of the file to be compressed
folderstart = 0 
folderend = 9

#list of files to be compressed
for f in os.listdir(basedir):
    if f[-extensionlen:] == extension :
        filelist.append(f)

#list of groups        
for file in filelist:
    filegroup.add(file[folderstart:folderend])
    print(file)

#create folder for the group    
for group in filegroup:
    print(group)
    if not os.path.isdir(basedir+group) :
        os.mkdir(basedir+group)

#move files to the folders
for file in filelist:
    os.rename(basedir+file,basedir+file[folderstart:folderend]+"/"+file)

#compress the folders    
for group in filegroup:
    shutil.make_archive(basedir+group, 'zip', basedir+group)
    shutil.rmtree(basedir+group) 

print("----PROCESS COMPLETED----")

I have tested this solution and it works. I have added some more features to this code for my use. This solution can be used to compress images, text files, etc.

Upvotes: 2

turnip
turnip

Reputation: 2346

Untested, sub-optimal solution, but should be enough to get you going.

import os
import zipfile

# gather names of files to create, using existing filenames
zipfile_names = []
for folder, subfolders, files in os.walk('F:\AOS_input'):
    for f in files:
        zipfile_names.append(f)

zipfile_names = set(zipfile_names)  # make unique

for filename in zipfile_names:
    for folder, subfolders, files in os.walk('F:\AOS_input'):
        for f in files:
            if f.startswith(filename):        
                with zipfile.ZipFile(filename, 'w') as z:
                    z.write(os.path.join(folder, file), file, compress_type = zipfile.ZIP_DEFLATED)

Upvotes: 0

Related Questions