Danny Garcia
Danny Garcia

Reputation: 227

Opening a file for append error

I'm trying to open a file for appending, but I keep getting the "except" portion of my try/except block, meaning there is some sort of error with the code but I can't seem to find what exactly is wrong with it. It only happens when I try to open a new file like so:

    results = open("results.txt", "a")
    results.append(score3)

Here's my full code:

import statistics

# input
filename = input("Enter a class to grade: ")

try:
    # open file name
    open(filename+".txt", "r")
    print("Succesfully opened", filename,".txt", sep='')
    print("**** ANALYZING ****")
    with open(filename+".txt", 'r') as f:
        counter1 = 0
        counter2 = 0
        right = 0
        answerkey = "B,A,D,D,C,B,D,A,C,C,D,B,A,B,A,C,B,D,A,C,A,A,B,D,D"
        a = []
        # validating files
        for line in f:
            if len(line.split(',')) !=26:
                print("Invalid line of data: does not contain exactly 26 values:")
                print(line)
                counter2 += 1
                counter1 -= 1
            if line.split(",")[0][1:9].isdigit() != True:
                print("Invalid line of data: wrong N#:")
                print(line)
                counter2 += 1
                counter1 -= 1
            if len(line.split(",")[0]) != 9:
                print("Invalid line of data: wrong N#:")
                print(line)
                counter2 += 1
                counter1 -= 1
            counter1 += 1
        #grading students
            score = len(([x for x in zip(answerkey.split(","), line.split(",")[1:]) if x[0] != x[1]]))
            score1 = 26 - score
            score2 = score1 / 26
            score3 = score2 * 100
            a.append(score3)
            # results file 
            results = open("results.txt", "a")
            results.write(score3)
        # in case of no errors
        if counter2 == 0:
            print("No errors found!")
        # calculating 
        number = len(a)
        sum1 = sum(a)
        max1 = max(a)
        min1 = min(a)
        range1 = max1 - min1
        av = sum1/number

        # turn to int
        av1 = int(av)
        max2 = int(max1)
        min2 = int(min1)
        range2 = int(range1)

        # median
        sort1 = sorted(a)
        number2 = number / 2
        number2i = int(number2)
        median = a[number2i]
        median1 = int(median)

        # mode
        from statistics import mode
        mode = mode(sort1)
        imode = int(mode)



    # printing
    print ("**** REPORT ****")
    print ("Total valid lines of data:", counter1)
    print ("Total invalid lines of data:", counter2)

    print ("Mean (average) score:", av1)
    print ("Highest score:", max2)
    print("Lowest score:", min2)
    print("Range of scores:", range2)
    print("Median Score:", median1)
    print("Mode score(s):", imode)


    results.close()


except:
    print("File cannot be found.")

Upvotes: 0

Views: 1341

Answers (1)

Strik3r
Strik3r

Reputation: 1057

I don't think there is a method called append for writing into file. You can use the write or writelines method only to write. As you already opened the file with append permissions. It wont change the old data and will append the text to the file.

f=open('ccc.txt','a')
f.write('Hellloooo')
f.close()

Hope it helps.

Upvotes: 3

Related Questions