'ZipFile script that zips all desired file contents
So I'm dealing with a script that needs to zip all files into a single folder that share the same name. So, for example, the folder structure looks like this...
001.flt
001.hdr
001.prj
002.flt
002.hdr
002.prj
. . .
700.flt
                                                                          700.hdr
700.prj
In order to get a file to zip, I have a script that can handle a single file but does not recognize ["*.flt", "*.hdr", "*.prj"]
Is there a workaround for getting the script to recognize the file names based on their names and group them by name as well? I would like each individual zip file to contain file contents but zip it as 001.zip, 002.zip....
meaning the zip file contains the different file extensions
001.zip( 001.hdr, 001.prj, 001.flt )
'''
import zipfile, sys, os, glob 
inDir = r"\test\DEM"
outDir = r"\test\DEM_out"  
filetype = "*.flt"
def zipfiletypeInDir(inDir, outDir):  
    # Check that input directory exists  
    if not os.path.exists(inDir):  
        print ("Input directory %s does not exist!" % inDir) 
        return False  
    print ("Zipping filetype(s) in folder %s to output folder %s" % (inDir, outDir))
    # Loop through "filetype" in input directory, glob will match pathnames from 
    for inShp in glob.glob(os.path.join(inDir, filetype)):  
        # Build the filename of the output zip file  
        outZip = os.path.join(outDir, os.path.splitext(os.path.basename(inShp))[0] + ".zip")  
        # Zip the "filetype"  
        zipfiletype(inShp, outZip)  
    return True  
def zipfiletype(infiletype, newZipFN):  
    print ('Starting to Zip '+(infiletype)+' to '+(newZipFN))
    # Delete output zipfile if it already exists  
    if (os.path.exists(newZipFN)):  
        print ('Deleting'+ newZipFN)
        os.remove(newZipFN)  
    # Output zipfile still exists, exit  
    if (os.path.exists(newZipFN)):  
        print ('Unable to Delete'+newZipFN)
        return False  
    # Open zip file object
    zipobj = zipfile.ZipFile(newZipFN,'w')  
    # Loop through "filetype" components  
    for infile in glob.glob( infiletype.lower().replace(filetype,"*.flt")):  
        # Skip .zip file extension  
        if os.path.splitext(infile)[1].lower() != ".zip":  
            print ("Zipping %s" % (infile)) 
            # Zip the "filetype" component  
            zipobj.write(infile,os.path.basename(infile),zipfile.ZIP_DEFLATED)  
    zipobj.close()  
    return True  
if __name__=="__main__":  
    zipfiletypeInDir(inDir, outDir)  
    print ("done!")
Solution 1:[1]
I Found what I was looking for, This script identifies the names of the files and groups them based on that with the Iterator.
#group files into separate zipfolders from single directory based from
#individual file names 
import fnmatch, os, glob, zipfile
#edit data folders for in and out variables
path = r"D:\Users\in_path"
out_path = D"C:\Users\out_path"   
#create variables used in iterator
obj = os.listdir(path)
my_iterator = obj.__iter__()
##
#iterate each file name as '%s.*' 
for obj in my_iterator:
    #define name of file for rest of iterator to preform
    name = os.path.splitext(obj)[0]
    print (name)
    #create a zip folder to store data that is being compressed
    zip_path = os.path.join(out_path, name + '.zip')
    #create variable 'zip' that directs the data into the compressed folder
    zip = zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED)
    os.chdir(path)
    #files are written to the folder with glob.glob
    for files in glob.glob('%s.*' %name):
        zip.write(os.path.join(path,files), files)
    #print each iteration of files being written
    print ('All files written to %s' %zip_path)
    zip.close()
Solution 2:[2]
If the possible duplicate I provided doesn't answer your question....
One way would be to iterate over all the file names and make a dictionary grouping all the files with the same name.
In [54]: import collections, os, zipfile
In [55]: zips = collections.defaultdict(list)
In [55]: 
In [56]: for f in os.listdir():
    ...:     name, ext = os.path.splitext(f)
    ...:     zips[name].append(f)
Then iterate over the dictionary; creating a new zip file for each key and adding each key's files to it.
In [57]: outdir = r'zips'
In [58]: for k,v in zips.items():
    ...:     zname = k+'.zip'
    ...:     fpath = os.path.join(outdir,zname)
    ...:     #print(fpath)
    ...:     with zipfile.ZipFile(fpath, 'w') as z:
    ...:         for name in v:
    ...:             z.write(name)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|---|
| Solution 1 | Matthew Peck | 
| Solution 2 | wwii | 
