Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you erase the 3 shelve files in python 3?

I wrote a few unittests with shelve at http://code.google.com/p/filecache/ and python 2 saves exactly the filename I specifiy in shelve.open() but in python 3 I get 3 different files "bak", "dat" and "dir". So before the tests start I want to erase these files but I don't know if I have any guarantee as to their filename or extension.

How can I erase a shelve if I know it's name?

like image 411
ubershmekel Avatar asked Feb 11 '11 08:02

ubershmekel


2 Answers

What extensions you get depends on which database backend is used. It's possible that the default differs between Python 2 and Python 3, but it can also be a difference between what database interfaces are available in your environment.

So no, you don't have a guarantee to the extensions, unless you use a specific implementation, ie either BsdDbShelf or DbfilenameShelf. You could probably specify a file in a temporary directory created by tempfile, and then delete the while directory.

like image 88
Lennart Regebro Avatar answered Nov 12 '22 07:11

Lennart Regebro


I use shelve because tempFile and dict[] objects cannot persist across modules. As you have discovered, calling .clear() does not clear content from the persistent object on disk, leaving a populated r+w file on disk after exit. (Similar to a use-after-free vulnerability) You can delete the shelve when finished using:

import os
import shelve

shelve_name = 'shelve_name'
shelve_contents = shelve.open(shelve_name, flag='c', protocol=None, writeback=False)

shelve_file = (os.path.join(os.getcwd(), shelve_name))
os.remove(shelve_file)
like image 26
TheMagician Avatar answered Nov 12 '22 07:11

TheMagician