Hi Everyone!

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 ?
  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:
    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()))

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)
    filename = raw_input("Enter a name for the output file: ")
    writeStudents(data, filename)
    print "The data has been written to", filename
if __name__ == '__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
    print "The best student is:", best.getName()
    print "hours:", best.getHours()
    print "GPA:", best.gpa()
if __name__ == '__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!


The infile and the outfile were indented into the loop. I emailed my instructor and he figured out what was going wrong. Thanks anyway.

Problem solved so far.

Always check you statement blocks! After you program with Python for a short while, these kind of things stick out like a sore thumb.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.