I need a little help with this problem. I am trying to import a class -- Student with object makestudent but I keep getting this error:
This program sorts student grade information Enter the name of the data file: p2sortin.py Traceback (most recent call last): File "C:\Python23\p2sort.py", line 37, in ? main() File "C:\Python23\p2sort.py", line 31, in main data = readStudents(filename) File "C:\Python23\p2sort.py", line 11, in readStudents for line in infile: ValueError: I/O operation on closed file >>>
Can anyone help me with what i could possibly be doing wrong?
This is my program that I am testing from the book:
This is only the beginning of my problem and i can't even get it to work!!
from gpa import Student, makeStudent def readStudents(filename): infile = open(filename, 'r') students =  for line in infile: students.append(makeStudent(line)) infile.close() return students def writeStudents(students, filename): outfile = open(filename, 'w') for s in students: outfile.write("%s\t%f\t%f\n" % (s.getName(), s.getHours(), s.getQPoints())) outfile.close() def cmpGPA(s1, s2): return cmp(s1.gpa(), s2.gpa()) def main(): print "This program sorts student grade information" filename = raw_input("Enter the name of the data file: ") data = readStudents(filename) data.sort(cmpGPA) filename = raw_input("Enter a name for the output file: ") writeStudents(data, filename) print "The data has been written to", filename if __name__ == '__main__': main()
This is the file that I am trying to import:
import string import math class Student: def __init__(self, name, hours, qpoints): self.name = name self.hours = float(hours) self.qpoints = float(qpoints) def getName(self): return self.name def getHours(self): return self.hours def getQPoints(self): return self.qpoints def gpa(self): return self.qpoints/self.hours def makeStudent(infoStr): name, hours, qpoints = string.split(infoStr,"\t") return Student(name, hours, qpoints) def main(): filename = raw_input("Enter name the grade file: ") infile = open(filename, 'r') best = makeStudent(infile.readline()) for line in infile: s = makeStudent(line) if s.gpa() > best.gpa(): best = s infile.close() print "The best student is:", best.getName() print "hours:", best.getHours() print "GPA:", best.gpa() if __name__ == '__main__': main()
And this is my input file name and data: p2sortin.py
Adams, Henry 127 228 Computewell, Susan 100 400 DibbleBit, Denny 18 41.5 Jones, Jim 48.5 155 Smith, Frank 37 125.33
It seems to me that it is telling me the file is closed but i can' figure it out!