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
Post a Comment