Search code examples
pythonpython-zipfile

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

  • 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()