#! /usr/bin python
""" Two simple python function for reading an writing a dictionary
of list of length 4, for cs150 assignment
"""
import sys
def read():
# reading in the diction using argv[1] as the name of the file
if len(sys.argv)<2:
print """ Sorry you must give the name of the file on the command line"""
sys.exit(2)
dict_file_name = sys.argv[1]
try:
dict_file = open(dict_file_name,"r")
except IOError:
print "No data file", dict_file_name, "will be created at the end of the program"
return {}
addressdict={}
line = dict_file.readline()
while line:
nickname = line[:-1]
datastring = dict_file.readline()
if not datastring:
print "Badly formated data file, extra lines"
else:
data = eval(datastring)
if type(data) != type([]) or len(data)!= 4:
print "Badly formated data line:", datastring
else:
addressdict[nickname]=data
line = dict_file.readline()
dict_file.close()
return addressdict
def write(addressdict):
dict_file_name = sys.argv[1]
dict_file = open(dict_file_name,"w")
for nickname in addressdict.keys():
dict_file.write(str(nickname)+"\n")
dict_file.write(repr(addressdict[nickname])+"\n")
dict_file.close()