1

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.

BGreen
  • 370
  • 3
  • 17
  • 1
    Step by step debug and exception catch would have told you what was the issue. It is a worthy ritual to adopt when developing, before going to SO. – LoneWanderer Dec 14 '20 at 07:27
  • Thank you! I hadn't thought of that, as I've never really used one before. Following your comment, I just learned how to use the basic functions of `pdb`. However, there is something I do not understand - how would an exception catch have helped here? The code ran to completion without throwing any errors. – BGreen Dec 18 '20 at 01:50
  • 1
    See 2nd answer here https://stackoverflow.com/questions/713794/catching-an-exception-while-using-a-python-with-statement – LoneWanderer Dec 18 '20 at 10:03
  • Thank you - so if I understand, there is an error in the `with` statement that we can catch, but Python doesn't stop executing the code (just the `with` block) for it. Good to know. – BGreen Dec 18 '20 at 18:20

1 Answers1

1

The colon character (":") is not allowed in Windows filepaths, for example this post.

Your strftime format includes a colon, which makes it impossible for that path to be written to, and is also presumably the reason why the characters after the colon (suck as the .pkl extension) are missing.

Running your code on a Unix platform worked as expected. Simply change your strftime format to make it compatible with Windows.

dschurman
  • 236
  • 1
  • 4
  • Thank you! That was exactly it. Yes, the second call with the formatted string was from a code that had previously been run on Unix, and I had missed the colon when rereading the code. – BGreen Dec 14 '20 at 00:36