4

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 .

feedMe
  • 3,431
  • 2
  • 36
  • 61

1 Answers1

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
  • 1
    A `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