Im building a database to hold files i am using anydbm and what i would like is something that does this:

import Tkinter
import anydbm
import tkFileDialog

file = tkFileDialog.askopenfile(parent=None,mode='rb',title='Choose a file')
print file
print str(file)
db = anydbm.open("database", "c")
#    db["1"] = str(file)

 

for file in files
db[len(database) + 1] = [str(file)]
db.close()

what i mean is as another file is selected a new key is created that is one higher than the highest number in the key. and the new file is added to that key so that key #1 = file #1 and key 2 = file 2
i do not want to have a set number of keys so i can run the script again and have a expanging database without re-writing the script. thanks so much in advance

This will produce a unique number for each file name.

for ctr, fname in enumerate(files):
    print ctr, fname

with better check with unique number. I think its not a good idea using enumerate function. Since enumearte repeates the number once new directory of files/ data in called.
I use anydbm alot and i think the best way is to use a counter with a dict. Then you check with the last dict call incrementing by 1 and use that for next key.

import anydbm


ddf = anydbm.open("ns.cdx",'r')

unique =  len(ddf.keys())


db =    anydbm.open("ns.cdx",'c')
for x in range(300):
    for y in reversed(range(1,4)):
       db[str(unique)]    =   str(y)
       unique = int(unique)
       unique  +=   1

print("Key  | Values \n ")
print "="*50
for x in ddf.keys():
    print x ,":", ddf[x]
db.close()
ddf.close()

from my htc phone. hope you get my idea ;)

This question has already been answered. Start a new discussion instead.