I have a piece of code which generates quite large lists in each iteration. To save memory I want to write each list to a binary file in each iteration after the list has been generated.
I have tried this with text files(even setting the parameter to “wb” in linux). “wb” seems not to have any effect for the file to be written in binary or text format. Moreover, the written file is huge and I don’t want this. I am sure that If i can write these lists in binary format this file will be much smaller.
thanks
I have a piece of code which generates quite large lists in each iteration.
Share
Since you mentioned the need for compressibility, I’d suggest using
picklewith thegzipmodule to compress your output. You can write and read back your lists one at a time, here’s an example of how:And then use a generator to yield the lists back one at a time: