I keep getting this error and i don't understand why.
Traceback (most recent call last):
File "E:\University\Workspace eclipse\dictionares\src\dcts\dicts.py", line 105, in <module>
fr_runt()
File "E:\University\Workspace eclipse\dictionares\src\dcts\dicts.py", line 95, in fr_runt
run.run()
File "E:\University\Workspace eclipse\dictionares\src\dcts\dicts.py", line 58, in run
self.data.write(file, key, value)
File "E:\University\Workspace eclipse\dictionares\src\dcts\dicts.py", line 23, in write
fh = open(file, 'a')
TypeError: coercing to Unicode: need string or buffer, type found
I'm trying to make a so called 'database', working with dictionaires, and it won't let me write down the changes in the file.
I'll put the code bellow:
'''
Created on Feb 20, 2012
@author: sin
'''
import os
import sys
import time
import random
class data:
def __init__(self, file):
self.file = file
self.dic = {}
def print_dic(self, file):
fh = open(file, 'r')
line = open(file).read().splitlines()
for i in range(0, len(line)):
print i, "-"*2, line[i], '\n'
fh.close()
def write(self, file, key, value):
fh = open(file, 'a')
self.dic[key] = value
fh.write(str(self.dic))
fh.close()
def delet(self, file, single):
fh = open(file, 'r')
data_lines = fh.readlines()
fh.close()
del data_lines[:single]
fh = open(file, 'w')
for i in range(0, len(data_lines)):
fh.write(str(data_lines[i]))
fh.close()
def clear(self, file):
fh = open(file, 'w')
fh.write('')
fh.close()
class main:
def __init__(self, data):
self.data = data
def run(self):
while True:
print """
1. Add to database
2. Remove from database
3. Print database
"""
cmd = raw_input("\\\\:")
if cmd == '1' or cmd == '1.':
key = raw_input("Key: ")
value = raw_input("Value: ")
self.data.write(file, key, value)
print "Your key: " + str(key) + " was added to our database, with it's value of", value + '.\n', '-' * 60
elif cmd == '2' or cmd == '2.':
self.data.print_dic(file)
single = int(raw_input("Position: "))
self.data.delet(file, single)
print '-' * 60
elif cmd == '3' or cmd == '3.':
self.data.print_dic(file)
elif cmd == 'clear':
self.data.clear(file)
print "You have succesfully deleted all data from the database.\n", '-' * 60
elif cmd == 'exit':
exit()
else: print "Invalid command.\n", '-' * 60
def fr_runt():
print """Do you wish to load an existing database, or to create a new one?
1. Load
2. Create
"""
while True:
cmd = raw_input("\\\\: ")
if cmd == '1' or cmd == '1.':
pathname = os.path.dirname(sys.argv[0])
print "Your current path: \n", pathname
print 'sys.argv[0]=' + str(sys.argv[0])
file = raw_input("Insert database name: ") + '.txt'
dt = data(file)
run = main(dt)
if os.path.isfile(str(pathname) + '\\' + file):
print "Your database was loaded."
time.sleep(2)
x = random.randint(0, 10)
print "Please, wait", x, 'seconds.'
time.sleep(x)
run.run()
else: print "Invalid file."
elif cmd == '2' or cmd == '2.':
file = raw_input("Insert filename: ") + '.txt'
dt = data(file)
run = main(dt)
run.run()
elif cmd == 'exit':
sys.exit()
else: print "Invalid command."
fr_runt()