Cristian Danciu
Cristian Danciu

Reputation: 43

Is it possible to change (update) the entries of a numpy array already saved in a .npy file? How?

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 .

Upvotes: 4

Views: 1817

Answers (1)

newkid
newkid

Reputation: 1458

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)

Upvotes: 2

Related Questions