from student import * def readStudents(fname): """ fname is the file name, tries to read the information from the file and generate a list of students records, returns the list of student records if successful, retuns False otherwise """ try: source = open(fname) except: # in case if file cannot be opened, terminate the function, return False return False studentsRecords = [] student = source.readline() # read the first line from the file print("read:",student) # create a student record and append it to the list of students' records studentsRecords.append(makeStudent(student)) for line in source: # read the rest of the lines from the file print("read:",line) studentsRecords.append(makeStudent(line)) # append to the list of records source.close() print("{0} student records are read".format(len(studentsRecords))) return studentsRecords def writeStudents(fname,records): """ fname is the file name, tries to record all the students records into the file with name fname, if not possible, returns False""" try: out = open(fname,"w") except: # if not possible to open the file for writing, return False return False for record in records: print("{0} {1} {2}".format(record.getName(),record.getHours(),record.getQPoints()),file=out) out.close() def main(): # get the file name fname = input("Enter the name of input file:") records = readStudents(fname) print(records) if records != False: records.sort(key = Student.getGPA) # sorting records by getGPA method from Student class fname = input("Enter the name for output file:") writeStudents(fname,records) print("finished") else: print("something happened. The records were not read") main()