I have a simple game I'm working on in py 2.7 that uses pickle to read and write save files.
I'm self-taught, and I just started learning like a week ago. I usually can find solutions to my problems online, but this one is taking way too long for what it would accomplish, so I'm asking for a little help.
The problem is that I have this try/except
f = file("VTSave2.pkl", "rb")
try:
game = load(f)
print "Game has been loaded from save 2."
except IOError:
pass
in a save-reading thing. VTSave2.pkl does not currently exist- I'm using this fact to test the try/except.
While I do get the appropriate error, [Errno 2], the IOError passer doesn't do squat. Instead of "load cancelled" I get a quit program and a traceback.
Am I making any dumb mistakes? Anything I should know? Any more information I should give? Thanks a bunch for your time!
Edit: Thank you! It works now. Since I've had someone try to "correct" the formatting of the code, and it ended up breaking my prog when I adopted it, I've taken out all code no longer relevant to the question to try to avoid baiting erroneous cleanup. Thank you!
proper working code:
try:
f = open("VTSave2.pkl", "rb")
game = load(f)
print "Game has been loaded from save 2."
except IOError:
pass
This is raising the exception
f = file("VTSave2.pkl", "rb")
you need to move it after the try:
If you read the traceback carefully, you should see that the exception is raised at that line number
Aside: file()
is deprecated. You should use f = open("VTSave2.pkl", "rb")
.