Nick Sims
Nick Sims

Reputation: 31

Can't get python to write to a text file, gives me an empty file

I am trying to get my program to write a list to a text file. I've changed it to a string and it creates the text file but it is empty. Also I got it to print the variable I'm trying to write to the text file and it comes out exactly as it is supposed to.

import string
import re
wholeLine = ""
file = open("bowlingscores.txt", "r")
for line in file:
    line = line.strip()
    wholeLine += line
    scores = re.findall('\d+', wholeLine)
    names = re.findall('\D+', wholeLine)
file.close()
scores = list(map(int,scores))
validScores = [x for x in scores if 300 >= x >= 0]
average = sum(validScores) / len(validScores)
numScores = len(scores)
output = []
for i in range(numScores):
    if scores[i] == 300:
        output.append(names[i])
        output.append("\nperfect")
    if scores[i] == average:
        output.append(names[i])
        output.append("\naverage")
    if scores[i] < average:
        output.append(names[i])
        output.append("\nbelow average")
    if scores[i] > average:
        if scores[i] <300:
            output.append(names[i])
            output.append("\nabove average")
    if scores[i] > 300:
        output.append(names[i])
        output.append("\ninvalid score")
outputFile = open('bowlingaverages.txt', 'w')
outputFile.write(str(output))
outputFile.close
print(output)

Upvotes: 0

Views: 82

Answers (3)

Nick Sims
Nick Sims

Reputation: 31

Update: I edited the code as follows and had a friend run it on his computer and it did as it was supposed to, however still does not work on my computer. I am now more confused than I was in the first place. Is it possibly a problem with a different program?

import string
import re
wholeLine = ""
file = open("bowlingscores.txt", "r")
for line in file:
    line = line.strip()
    wholeLine += line
    scores = re.findall('\d+', wholeLine)
    names = re.findall('\D+', wholeLine)
file.close()
scores = list(map(int,scores))
validScores = [x for x in scores if 300 >= x >= 0]
average = sum(validScores) / len(validScores)
numScores = len(scores)
output = []
for i in range(numScores):
    if scores[i] == 300:
        output.append(names[i])
        output.append("\tperfect\n")
    if scores[i] == average:
        output.append(names[i])
        output.append("\taverage\n")
    if scores[i] < average:
        output.append(names[i])
        output.append("\tbelow average\n")
    if scores[i] > average:
        if scores[i] <300:
            output.append(names[i])
            output.append("\tabove average\n")
    if scores[i] > 300:
        output.append(names[i])
        output.append("\tinvalid score\n")
outputFile = open('bowlingaverages.txt', 'w')
for item in output:
    outputFile.write(item)
outputFile.close
print(output)

Upvotes: 0

Cyzanfar
Cyzanfar

Reputation: 7146

Here is how you'd write to the file with output (which is a list):

with open('bowlingaverages.txt', 'w') as f:
    f.write(''.join(output))
print(output)

Upvotes: 0

pushpendra chauhan
pushpendra chauhan

Reputation: 2375

you should convert your output which is a list to string properly, try using

outputFile = open('bowlingaverages.txt', 'w')
outputFile.write(''.join(output))
outputFile.close()

Upvotes: 1

Related Questions