Write and Read a NumPy Array
The documentation for writing/reading a NumPy array to/from a file without pickling is a tad hard to follow. One may need to do this when dealing with large NumPy arrays. Below is some simple code for writing and reading a NumPy array to a temporary file:
Here, we leverage the tempfile
module for dynamically creating our intermediate files and then deleting them only after the file is no longer needed. This is rather useful if you are using the multiprocessing
module and your files are created within a subprocess. The key part to all of this is the necessity to append the .npy
extension to the file name that is created by tempfile
and also to turn off pickling (this will allow us to create files that are larger than 4GiB) in size.