I am baffled by the behavior of the code below. There are two problems; I am not sure whether they are related in some strange way.
The first problem is the more important one. The code should save two files containing the same data. However, test.pkl
is about 79 kB whereas the other is empty. Why is nothing being saved there after the file is created?
The second issue I noticed is that the second file is saved as something like e-v-w1-n2-2020-12-13-19
without any file extension. What happened to the .pkl
?
I am using Python 3.8 with pickle 4.0 on Windows 10.
import pickle
import numpy as np
import datetime
n = 100
z = np.zeros((n,n))
with open("test.pkl" , 'wb') as myfile:
pickle.dump(z, myfile)
filename = "e-v-w%s-n%s-%s.pkl" % (1, 2, datetime.datetime.now().strftime("%Y-%m-%d-%H:%M"))
print(filename)
with open(filename, "wb") as outfile:
pickle.dump(z, outfile, pickle.HIGHEST_PROTOCOL)
When I print filename
, the .pkl
appears as it should, by the way.