I saved a numpy array in .npy format on disk I load it using np.load() but I don't know how to save on the disk the changes I made .
Asked
Active
Viewed 1,395 times
4
-
Use the save [function](https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.save.html) – overfull hbox Jan 12 '19 at 19:16
-
1You just have to save the array again; there's no overwrite of the existing saved file. – hpaulj Jan 12 '19 at 19:17
1 Answers
2
There are two options you could explore. The first is if you know the position of the change in the file, you can:
file = open("path/to/file", "rb+")
file.seek(position)
file.seek(file.tell()). # There seems to be a bug in python which requires you to do this
file.write("new information") # Overwriting contents
Also see here why file.seek(file.tell())
The second is to save the modified array itself
myarray = np.load("/path/to/my.npy")
myarray[10] = 50.0 # Any new value
np.save("/path/to/my.npy", myarray)

newkid
- 1,368
- 1
- 11
- 27
-
1A `npy` file is a binary file, not text. The file contains a byte image of the data buffer, but we don't normally know where a particular element occurs. – hpaulj Jan 12 '19 at 19:31
-
@hpaulj, edited answer now to reflect that `npy` is a binary file. Indeed it is difficult to know where a particular element occurs but the answer also includes the situation when it is known. – newkid Jan 12 '19 at 19:38
-
Thank you, newkid. I used np.save() to create the .npy file, but I didn't think to use it also to save on the disk changes I made. It is so obvious now. Thanks again, it is very appreciated – Cristian Danciu Jan 12 '19 at 22:38