Скрипт ZipFile, который архивирует все желаемое содержимое файла - PullRequest
0 голосов
/ 13 января 2020

Итак, я имею дело со скриптом, который должен заархивировать все файлы в одну папку с одинаковыми именами. Так, например, структура папок выглядит следующим образом ...

001.flt
001.hdr
001.prj
002.flt
002.hdr
002.prj

. , .

700.flt
700.hdr
700.prj

Чтобы получить файл в zip, у меня есть скрипт, который может обрабатывать один файл, но не распознавать ["*.flt", "*.hdr", "*.prj"]

Есть ли способ заставить скрипт распознавать имена файлов по их именам и группировать их по именам? Я хотел бы, чтобы каждый отдельный zip-файл содержал содержимое файла, но заархивировал его как 001.zip, 002.zip ....

, что означает, что zip-файл содержит различные расширения файла

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!")

Ответы [ 2 ]

0 голосов
/ 23 января 2020

Я нашел то, что искал. Этот скрипт идентифицирует имена файлов и группирует их на основе итератора.

#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()
0 голосов
/ 14 января 2020

Если возможный дубликат, который я предоставил, не отвечает на ваш вопрос ....

Один из способов - перебрать все имена файлов и создать словарь, объединяющий все файлы с одинаковыми именами.

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)

Затем переберите словарь; создание нового zip-файла для каждого ключа и добавление в него файлов каждого ключа.

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)
...