#! /usr/bin/python3

# creates Student objects using a data file 
# and stores them in a dictionary

from student import Student
import sys
import pickle

pickle_filename = 'students.dat'
try:
    pickle_file = open(pickle_filename, 'rb')
except:
    students = {}
else:
    students = pickle.load(pickle_file)
    
data_filename = input('Data filename: ')    
try:
    data_file = open(data_filename, 'r')
except:
    print("Cannot open", data_file)
    sys.exit()

for line in data_file:
    id, last_name, first_name = line.split(',')
    if id not in students:
        student = Student(id, last_name, first_name)
        students[id] = student

print()
print("Contents of students dictionary")     
for id in students:
    print(students[id])
     
pickle_file = open(pickle_filename, 'wb')
pickle.dump(students, pickle_file)

    

