I'm working on a library-type checkout/checkin system. When the user clicks exit
, the program calls a close_window
function which dumps the current dictionary objects into pickle files before the window is destroyed.
def close_window(self):
if messagebox.askokcancel("Quit", "You want to close the program now?"):
patrons.dump_data()
self.master.destroy()
When the program is started again, it calls a load_data
function which loads the pickled files. Somehow I ran into MemoryError
when exiting the system and one of the pickled files was overwritten with an empty file. From the documentation, I gather that MemoryError
occurs when the program creates too many objects and runs out of memory. I am not sure why this happened in my case since I am not dealing with large data. The pickled file that got overwritten was only 1 KB.
How can I ensure my pickled file is not overwritten with an empty file when MemoryError
occurs? This can lead to serious data loss. I am new to programming and am using this project to learn. It is possible I've done something seriously wrong to lead to the memory error or maybe I just need more computer memory. In any case, it doesn't make sense to overwrite a saved file with an empty file whether memory error or not.
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1487, in __call__
return self.func(*args)
File "C:/Python34/Lib/site-packages/toolkit/main.py", line 435, in close_window
patrons.dump_data()
File "C:\Python34\Lib\site-packages\toolkit\patrons.py", line 20, in dump_data
pickle.dump(patronslist, f)
MemoryError
This error is partially discussed in MemoryError while pickling data in python. Here though, I got an empty file, not even a partial file. And I want to know if there is a workaround for this problem. Perhaps saving the pickled data to a temporary file. If no memory error occurred during the save, then the temp file can be used to overwrite the permanent file (but this may yet trigger another MemoryError
right?).
I run Win7 x86, 3 GB RAM, Python 3.4.1