[Tutor] Saving Data

don arnold darnold02 at sprynet.com
Tue Dec 30 13:51:09 EST 2003


----- Original Message -----
From: "moore william" <red_necks25 at yahoo.com>
To: "Python" <Tutor at python.org>
Sent: Tuesday, December 30, 2003 12:08 PM
Subject: [Tutor] Saving Data


> I am lookign for a toutorial on how to save data
> created in a program so that it can be used when the
> parogram is started again.
>

While not a tutorial, here's a simple example that uses the shelve module.
This module lets you use a dictionary-like object to manage persistent data.

import shelve

s = shelve.open('c:/temp2/mydata')

if s.has_key('score'):
    s['score'] += 10
else:
    s['score'] = 0

print 'score is %d' % s['score']

s.close()


Here's a few sample runs:

C:\Python22\mystuff>\python22\python shelf.py
score is 0

C:\Python22\mystuff>\python22\python shelf.py
score is 10

C:\Python22\mystuff>\python22\python shelf.py
score is 20


HTH,
Don




More information about the Tutor mailing list