python - pickle module doesn't work for this simple code -


When I run this code in Python 3.4.2 (win7-64) it does not work! This creates the file but nothing in it. (0 bytes) I do not know what the problem is? Help - thank you voyo

  import pickle = open ("g: \\ database.txt", "wb") pickle.dump (12345, f)  

You have to close the file object that you have opened. Just add

  f.close ()  

to the end and it will work.

As an alternative, you can also use the statement with to open the file, then it will automatically close the file for you when it Will be:

  Open with import ("G: \\ database.txt", "wb") as f: pickle.dump (12345, f)  < / Pre> 

Comments