pickled object, read and write..


H

hg

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.


You can run this small script a few times to see that the file does not get
deleted

hg

import shelve


class Grades:
def __init__(self):
self.GRADES = 'GRADES'
self.m_gdb = shelve.open('MYGRADES.dat')

try:
self.m_grades = self.m_gdb[self.GRADES]
except:
self.m_grades = []
self.m_gdb[self.GRADES] = self.m_grades

self.m_gdb.close()

def Add(self, p_grade):
self.m_gdb = shelve.open('MYGRADES.dat')
self.m_grades.append(p_grade)
self.m_gdb[self.GRADES] = self.m_grades
self.m_gdb.close()

def Grades(self):
return self.m_grades

l_o = Grades()
print l_o.Grades()
l_o.Add(10)
print l_o.Grades()
 
Ad

Advertisements

I

israphelr

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.
 
P

phreaki

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.

What did adding the standard try statement do?
 
Ad

Advertisements

P

Prateek

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top