pickled object, read and write..

Prateek surekap at gmail.com
Mon Apr 23 04:26:46 CEST 2007


On Apr 22, 11:40 pm, "israph... at googlemail.com"
<israph... at googlemail.com> wrote:
> Hi all.
>
> I have to put together some code that reads high scores from a saved
> file, then gives the user the opportunity to add their name and score
> to the high scores list, which is then saved.
>
> Trouble is, I can't tell the program to read a file that doesn't
> exist, that generates an error.
>
> So I must have a file created, problem HERE is everytime the program
> is run, it will overwrite the current list of saved high scores.
>
> Advice would be much appreciated.

Try the following idiom:

try:
	try:
		fp = open("filename", 'r+')
	except IOError:
		fp = open("filename", 'w+')

	fp.write(high_score)
finally:
	fp.close()


-Prateek




More information about the Python-list mailing list