2
I am wanting to compress a file and a folder that has multiple files inside in a single file. zip using Python. The script is next p:
from zipfile import ZipFile,ZIP_DEFLATED
def zipar(lista):
with ZipFile('teste.zip','w',ZIP_DEFLATED) as z:
for i in lista:
z.write(i)
zipar(['registro.py','Tudo'])
When I run the script, it compresses "register.py" and "All" folder, but the files that are inside the original "All" folder are not compressed together, that is, the folder is compressed empty. How do I fix this?
There is a problem with this solution, how do you put several trustees/directories inside the same zip? in this case the
['registro.py','Tudo']
?– Miguel
It compresses everything inside
/path/da/pasta
copying the structure of directories and files withinzipfile_name.zip
. Test here on my computer before posting the answer and it’s all right. Tested there?– fernandosavio
Yeah, but how do you put the file
registro.py
into that zip? As asked in the question you should put all files/directories that are in the entry list in itficheiro.zip
, in this case the fileregistro.py
, and the directoryTudo
(and all its contents)– Miguel
This command copies the directory structure. Just adjust the directory as you want and compress. I don’t understand the mystery
– fernandosavio
But in the case of this problem the file
registo.py
is not inside that directory, but is supposed to put it in the same destination zip– Miguel
I edited the answer. What I meant is that it can copy the files and folder that it wants to a temporary folder, compress and delete the temporary folder. Maybe it doesn’t even need the temporary folder, it depends on how it is working with this structure.
– fernandosavio
Yes, this gives but implies changing the original folder/file structure, however it gives yes
– Miguel
Anyway, I just wanted to supplement your answer, which I think is the right one to ask. However, someone will fall for this question in a google search, and I found it interesting to provide a different answer that can apply better to other situations. Thanks for the feedback, it sure improved the quality of the response. :)
– fernandosavio
You are welcome, yes your answer is appropriate when we want a directory and all its contents, it is also not wrong
– Miguel