I have several functions that read the file directly from a cache path
, but eventually it will cost a lot of space on filesystem
, is there any way to generate this file in memory and save it already zipped to disk? >
I have several functions that read the file directly from a cache path
, but eventually it will cost a lot of space on filesystem
, is there any way to generate this file in memory and save it already zipped to disk? >
Well if I understood 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 a new zip file, because if it does not exist we do not want to delete the previous zip ( with ZipFile(zip_name, 'w')
), os.remove(file_to_zip)
here delete the file ancient