filename used by shelve

N

Nemesis

In the python docs about shelve module I read:

-----------------------------------------------------------------------------
open( filename[,flag='c'[,protocol=None[,writeback=False[,binary=None]]]])
Open a persistent dictionary. The filename specified is the base filename
for the underlying database. As a side-effect, an extension may be
added to the filename and more than one file may be created.
-----------------------------------------------------------------------------

So the real filename may be different from the argument passed to
"open". I have this problem, I want to delete (in some circustances) the
file created by shelve.open, how can I know which is the name of this
file (or files) ?
 
F

Fredrik Lundh

Nemesis said:
So the real filename may be different from the argument passed to
"open". I have this problem, I want to delete (in some circustances) the
file created by shelve.open, how can I know which is the name of this
file (or files) ?

if you put the shelve in a subdirectory, and nuke the entire directory when
done, you don't have to know the names.

dbfile = "mydatabase"
if not os.path.isdir(dbfile):
os.makedirs(dbfile)
db = shelve.open(os.path.join(dbfile, "data"), ...)

...

db.close()
del db

...

shutil.rmtree(dbfile)

</F>
 
N

Nemesis

Mentre io pensavo ad una intro simpatica "Fredrik Lundh" scriveva:
if you put the shelve in a subdirectory, and nuke the entire directory when
done, you don't have to know the names.

dbfile = "mydatabase"
if not os.path.isdir(dbfile):
os.makedirs(dbfile)
db = shelve.open(os.path.join(dbfile, "data"), ...)
shutil.rmtree(dbfile)

Hmm, this is a good idea. Thanks.

Anyway I think that shelve should provide a method which returns the
files used by the backend database, and the argument "filename" can
confuse users, it should be named "dbname".
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top