I have a dictionary containing { Name : Email Address }
I have a seperate .py to pickle this:
emailDict = {'Kilizo': 'info%40kilizo.com' , 'about': 'about%40google.com' }
# write python dict to a file
output = open('orig.pkl', 'wb')
pickle.dump(emailDict, output)
output.close()
which works, in that it pickles the original dictionary to orig.pkl
Then in my main website, i have:
# Pickling # Deleting Old Temp & Creating New One
tmp = os.path.isfile("tmp.pkl")
if tmp == True:
os.remove("tmp.pkl")
shutil.copyfile("orig.pkl", "tmp.pkl")
# Pickling # Loading File
pkl_file = open('tmp.pkl', 'rb')
emailDict = pickle.load(pkl_file)
pkl_file.close()
I then have two form inputs on a website that take the email address and corresponding name
#Processing input
emailAdded = fs.getvalue('emailAdd')
nameAdded = fs.getvalue('nameAdd')
if emailAdded != None or nameAdded != None:
print emailAdded
print nameAdded
emailDict[nameAdded] = emailAdded
else:
print "Please enter a name & email address"
output = open('tmp.pkl', 'wb')
pickle.dump(emailDict, output)
output.close()
print emailDict
However no new data gets stored to either tmp.pkl or orig.pkl
Any ideas to get me started?
Thanks
Using pickle as a dynamically updated data store for a website isn’t great. In order to avoid concurrency issues you’ll have to implement a lockfile mechanism and hope that everything else that accesses the file will respect it.
I strongly suggest that you use a data store that supports concurrent access. E.g. a database.
Have a read of: http://en.wikipedia.org/wiki/Concurrency_control
You could start easy with sqlite. See:
http://docs.python.org/library/sqlite3.html