Create 2 zipped files with files from a folder

Asked

Viewed 51 times

3

I’m trying to create a code that reads a directory with pdf files and creates 2 zipped files containing these pdf, in my code, I only managed to play all pdf in a single zipped file, my question is how to create the two.

Code:

class zipar_pdf:
    
    def __init__(self, diretorio, nome):
        self.diretorio = diretorio
        self.nome = nome        

    def zipar(self):        
        #Criando arquivo zip com os pdf          
        with zipfile.ZipFile(self.nome, "w") as oZip:            
            print('Buscando arquivos...\n')            
            for caminho, _ , arquivos in os.walk(self.diretorio):
                print(f'Compactando arquivos em {self.nome}...\n')
                for arquivo in arquivos:
                    caminhoCompleto = os.path.join(caminho, arquivo)                    
                    if arquivo.startswith("test"):                        
                        oZip.write(caminhoCompleto, basename(caminhoCompleto))
        
PDF = zipar_pdf(r'exemplo_diretorio', 'arquivos.zip')
PDF.zipar()

In the directory has 6 pdf, with the name Test1.pdf, up to test6.pdf, it is possible to put up the test3 in a zipped file and then the rest in another?

1 answer

2


An alternative is to use pathlib.Path.glob passing a Pattern corresponding to the names of the files you want, and add only these files in the zip. Only then you will need a different name for each zip file.

Another point is that I don’t know if you really need to have a class for this, I think "exaggerate" too much. Would look like this:

from pathlib import Path
import zipfile

diretorio = 'exemplo_diretorio'
zip_config = { # mapeia o nome do arquivo zip com os arquivos que ele terá
    'arquivos1.zip': 'teste[1-3].pdf', # de 1 a 3
    'arquivos2.zip': 'teste[4-6].pdf' # de 4 a 6
}

p = Path(diretorio)
for zip_name, file_pattern in zip_config.items():
    with zipfile.ZipFile(zip_name, "w") as oZip:
        for f in p.glob(file_pattern):
            oZip.write(f, f.name)

At most, I would make a function that gets the directory and zip file settings:

def zipar(diretorio, zip_config):
    p = Path(diretorio)
    for zip_name, file_pattern in zip_config.items():
        with zipfile.ZipFile(zip_name, "w") as oZip:
            for f in p.glob(file_pattern):
                oZip.write(f, f.name)

zipar('exemplo_diretorio', { 'arquivos1.zip': 'teste[1-3].pdf', 'arquivos2.zip': 'teste[4-6].pdf' })

So you can reuse the logic, just changing the parameters informed (for example, if you want a single zip with all the files, just pass { 'tudo.zip': '*.pdf' }).


But if you really want to use a class (which as I said, I find "exaggeration"/unnecessary for this case), it would look something like this:

class ZiparPdf:
    def __init__(self, diretorio, zip_config):
        self.diretorio = diretorio
        self.zip_config = zip_config

    def zipar(self):
        p = Path(self.diretorio)
        for zip_name, file_pattern in self.zip_config.items():
            with zipfile.ZipFile(zip_name, "w") as oZip:
                for f in p.glob(file_pattern):
                    oZip.write(f, f.name)

z = ZiparPdf('exemplo_diretorio', { 'arquivos1.zip': 'teste[1-3].pdf', 'arquivos2.zip': 'teste[4-6].pdf' })
z.zipar()
  • Thanks for the help! About using classes, the reason is that I’m learning about this subject and I thought about applying, I’m new in the programming area, I made another version only with a function that was giving the same result

  • @Vitorxavier If the answer solved your problem, you can accept it, see here how and why to do it. It is not mandatory, but it is a good practice of the site, to indicate to future visitors that it solved the problem. Don’t forget that you can also vote in response, if it has found it useful.

  • One question, in this function, is taking the test.pdf files by the number that comes along in front of it, has some function that would put the files inside the . zip by quantity and not by name?

  • @Vitorxavier I don’t know, probably you will have to count manually (maybe use a counter in the loop, for example). Or you take the list of all the files and then iterate by parts. Ex: arquivos = list(Path(diretorio).glob('*.pdf')) to have a list of all the PDF’s, and then take arquivos[0:10] for the first 10 (from zero to 9), then arquivos[10:20] to catch from the tenth to the nineteenth, etc

Browser other questions tagged

You are not signed in. Login or sign up in order to post.