How to empty DBM file in Python efficiently? -


there command in dbm module delete value stored @ key.

del d[key]      # delete data stored @ key (raises keyerror   # if no such key) 

but cannot iterate command, because runtime error occurs.(runtimeerror: dictionary changed size during iteration.)

import dbm db=dbm.open("file.db","c") key in db:     del db[key] print(len(db)) db.close() 

is there efficient way empty dmb file @ once? using python 3.3

for key in list(db):     del db[key] 

should work.

edit: if goal empty database completely, can close database , re-open dbm.open('filename', 'n'). 'n' flag means "always create new, empty database, open reading , writing"; seems override previously-existing file.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -