I'd like to save multiple objects, e.g. figures, created in a loop directly to a zip file, without saving them in directory.
At the moment I'm saving the figures in a folder and then zipping them.
import matplotlib.pyplot as plt
from zipfile import ZipFile
for i in range(10):
plt.plot([i, i])
plt.savefig('fig_' + str(i) + '.png')
plt.close()
image_list = []
for file in os.listdir(save_path):
if file.endswith(".png"):
image_list.append(os.path.join(save_path, file))
with ZipFile(os.path.join(save_path, 'export.zip'), 'w') as zip:
for file in image_list:
zip.write(file)
In case of positive answer, is there a way to do the same for any kind of object or does it depend on object type?