How do you delete 3 shelf files in python 3?

I wrote some unittests with a shelf in http://code.google.com/p/filecache/ , and python 2 saves exactly the file name that I specify in shelve.open (), but in python 3 I get 3 different file "bak", "dat" and "dir". Therefore, before starting the tests, I want to delete these files, but I do not know if I have a guarantee regarding their file name or extension.

How can I erase a shelf if I know this name?

+4
source share
2 answers

Which extensions you get depends on which database backend is used. It is possible that the default value is different from Python 2 and Python 3, but it may also be the difference between the available database interfaces in your environment.

No, you do not have a guarantee for extensions if you do not use a specific implementation, that is, either BsdDbShelf or DbfilenameShelf. You can probably specify the file in a temporary directory created by tempfile, and then delete the while directory.

+6
source

I use shelve because tempFile and dict [] objects cannot be stored in different modules. As you have discovered, a call to .clear () does not clear the contents of a permanent object on disk, leaving the r + w file on disk after exiting. (Similar to a vulnerability without using software) You can remove a shelf when done using:

import os import shelve shelve_name = 'shelve_name' shelve_contents = shelve.open(shelve_name, flag='c', protocol=None, writeback=False) shelve_file = (os.getcwd()+'/'+shelve_name) os.remove(shelve_file) 
+1
source

Source: https://habr.com/ru/post/1339323/


All Articles