3
I have a few functions that read the file directly from a cache path
, but eventually it will cost a lot of space in the filesystem
, there is some way to generate that file in memory and already save it zipped on disk?
3
I have a few functions that read the file directly from a cache path
, but eventually it will cost a lot of space in the filesystem
, there is some way to generate that file in memory and already save it zipped on disk?
4
If I understand correctly you can do the following:
from zipfile import *
import os
zip_name = 'my_zip.zip' # caminho para o zip
file_to_zip = 'file.txt' # caminho para o ficheiro a inserir no zip
if(not os.path.isfile(file_to_zip)):
print('ficheiro {} nao existe, operacao cancelada'.format(file_to_zip))
else:
with ZipFile(zip_name, 'w') as myzip: # criar um zip
try:
myzip.write(file_to_zip) # inserir ficheiro no zip
os.remove(file_to_zip) # apagar ficheiro
except Exception as err:
print(err)
else:
print('Execucao bem sucedida, {} zipado em {}'.format(file_to_zip, zip_name))
I do some checks that I consider important, if(not os.path.isfile(file_to_zip))
will be to ensure that there is new file zipping, because if it does not exist we do not want to delete the connector from the previous zip (with ZipFile(zip_name, 'w')
), os.remove(file_to_zip)
here we delete the old file
Browser other questions tagged python
You are not signed in. Login or sign up in order to post.
What do you want to put the file in a zip? I don’t quite understand what you want to solve "it will cost a lot of space in the filesystem"
– Miguel
Exactly when saving, already save in zip. thought the following, saving the file in memory and then zipping, it is better than saving the file on disk, zipping, and then deleting the file that is not zipped
– Guilherme Lima