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. 3 1123
On Apr 22, 2:40 pm, "israph...@googlemail.com"
<israph...@googlemail.comwrote:
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? is*******@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.
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()
On Apr 22, 11:40 pm, "israph...@googlemail.com"
<israph...@googlemail.comwrote:
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: simon place |
last post by:
is the code below meant to produce rubbish?, i had expected an exception.
f=file('readme.txt','w')
f.write(' ')
f.read()
( PythonWin 2.3 (#46, Jul 29 2003, 18:54:32) on
win32. )
I got...
|
by: Maurizio Berlusconi |
last post by:
Hi all
I have (my mistake) overwritten the beginning of a file where a long
dictionary was pickled with another (pickled and much smaller) dictionary.
Since only 10% or so of the original and...
|
by: BBM |
last post by:
I have an object that has a fairly complex construction sequence, so I have
written a dedicated "factory" class that invokes the constructor of my object
class (which does nothing but instantiate...
|
by: DazedAndConfused |
last post by:
Can you encrpt a serialized object?
Or am I trying to do something that just doesn't work that way?
I am trying to encrypt a serialized object. I can read and write the object
to a file...
|
by: Bram Stolk |
last post by:
Hi there,
I'm transfering small pickled object over a socket.
The performance I see is lower than expected.
How fast should I be able to ping/pong small objects in python?
I use a threaded...
|
by: David Hirschfield |
last post by:
I have a pair of programs which trade python data back and forth by
pickling up lists of objects on one side (using
pickle.HIGHEST_PROTOCOL), and sending that data over a TCP socket
connection to...
|
by: krishnakant Mane |
last post by:
hello all,
I am trying a very complex kind of a task in a project.
I have a knowledge management system where I need to store a lot of
objects (pickled). I have to store mostly lists and...
|
by: krishnakant Mane |
last post by:
hello,
finally the errors for my sql query have changed so I have even
changed the thread subject because I feel now that this is not doable
in mysql and this seams to be a bug, ither in python or...
|
by: milan_sanremo |
last post by:
I've read the library entry for pickle a couple of times, and I'm
still not
sure what data is maintained when an item is pickled and sent over a
socket.
Two importable modules:
class...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| | |