'MemoryError zipfile in memory

I'm getting MemoryError (out of memory) when zipping files in memory with Python. I'm using cStringIO.StringIO as an in memory buffer stream. Follow the code below:

    zip_file = cStringIO.StringIO()
    with zipfile.ZipFile(file=zip_file, mode='w', compression=zipfile.ZIP_DEFLATED) as zf:
        for f in files:
            zf.writestr(f.file_name, f.get_data())
        for f in images:
            try:
                zf.writestr(str(f.name), f.read())
            except:
                pass

    files_email.append({
        'clinic_name': clinic.name,
        'url': send_to_s3(zip_file, file_name, 60 * 60 * 48),
    })
    zip_file.close()

Any idea on how can I fix it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source