Is it possible to get shelve database filename using file object attribute? (python)

920 views Asked by At

I have a python3 script that creates multiple database files using the shelve module. FYI, the shelve module wraps the dbm module, as can be seen in the shelve source code. The shelf.open() method is used to create the database file on disc and takes the desired output filepath as an argument. However, the name of the file that is created is dependent upon the available dbm modules on the user's system. This means that the behaviour is not platform-agnostic, as has been described elsewhere on SO: here.

I have been unable to find an easy method to glean the filenames. I had hoped that I could access the file name(s) as attribute(s) of the shelf object such as can be done with the FileIO class's name attribute. How can I get the filename(s) of a shelf object via an object attribute or method?

Failing that, I could resort to passing a self-destructing tempfile object into the shelve.open() call instead. However I am not clear on how to accomplish that. Note that the temporary shelf question has been asked on SO here...but the answer provided will not work if the user is on a system that has a dbm module that will attach a fname suffix.

Thank you for your time.

1

There are 1 answers

0
Lionel Brooks On

I just found this answer, which suggests creating the shelve files in a temporary subdirectory, then deleting the whole directory. I think that this is the best solution available at this time.