[Tutor] Why doesn't it save the data before exiting? CORRECTION

Alan Gauld alan.gauld at freenet.co.uk
Tue Jun 27 06:26:27 CEST 2006


Hi Nathan,

> How do I know? Simple. Next time I load it and ask it to print the 
> list of accounts and how much in each, I only get:

OK, but that could be down to:
1) The save file not working,
2) the load file not working
3) the print accounts not working

Have you checked whether the store file exists and is non
zero in size?

Have you checked that the load file is populating your
accountlist?

The code looks OK on first inspection, so we need to do a
bit more detailed digging.

I would definitely tidy it up accoding to the previous suggestions,
in particular the spurious open(store('w') in the loadfile should be
removed, its doing no good and could be causing harm...

And using a format string defined at the top of the code and
then reused in each function would improve consistency,
maintainability and performance.

Good luck

Alan G.

>>> Consider % formatting for the outputs as in:
>>>         print "%s\t $%2f\n" % (account, accountlist[account]) # 
>>> instead

fmt = "%s\t $.2f\n"
....
print fmt % (account,accountList[account])

>>>>  def load_file(ac):
>>>>     import os
>>>>     import pickle
>>>>     filename = 'accounts.txt'
>>>>     if os.path.exists(filename):
>>>>         store = open(filename, 'r')
>>>>         ac = pickle.load(store)
>>>>     else:
>>>>         store = open(filename, 'w')

The else bit does nothing useful.





More information about the Tutor mailing list