-1

I'm trying to print the below code to a file. Looking through stack overflow I found the file=open code but it does not seem to be writing to a file.

PS. I haven't created the file yet

print("Average Grade " + str(average), "At least 70% " + 
      str(Grade1), "60% - 69% " + str(Grade2), "50% - 59% " +
      str(Grade3), "40% - 49% " + str(Grade4), "Less than 40% " + 
      str(Grade5), "Highest Grade " +
      str(max_grade), "Student Name:" + str(student_name), sep="\n", 
      file=open("results.txt", 'a'))
khelwood
  • 55,782
  • 14
  • 81
  • 108

4 Answers4

1

In python, we don't normally do this using print statements. Consider the following alternative. Using a "context manager" allows you to write more than one line, and also helps avoid certain problems if the program throws an error while writing to the file.

with open('results.txt', 'a') as f:
    line = "Average Grade " + str(average), "At least 70% " + str(Grade1), "60% - 69% " + str(Grade2), "50% - 59% " + str(Grade3), "40% - 49% " + str(Grade4), "Less than 40% " + str(Grade5), "Highest Grade " + str(max_grade), "Student Name:" + str(student_name)
    f.write(line)

Incidentally, if you are doing this interactively, you may not see data written to the file instantly- this can be a real point of confusion when using a terminal instead of running a script! What happens is that python writes data to the file in large blocks, rather than asking the hard drive to do work every time f.write is called. flushing the output buffer tells python to write the data immediately- eg f.flush().

The print statement actually does have a flush argument, which may be highly relevant for that reason. Still, I would recommend using a with statement instead.

abought
  • 2,652
  • 1
  • 18
  • 13
0
with open("results.txt", 'a') as f:
    f.write("Average Grade " + str(average), "At least 70% " + 
    str(Grade1), "60% - 69% " + str(Grade2), "50% - 59% " +
    str(Grade3), "40% - 49% " + str(Grade4), "Less than 40% " + 
    str(Grade5), "Highest Grade " +
    str(max_grade), "Student Name:" + str(student_name))

note that the 'a' option in open is for appending. You can also use 'w' for write.

Florian H
  • 3,052
  • 2
  • 14
  • 25
0

I got it to work. I opened file then used file= at end of print.

newFile = open("results.txt", 'w')

print("Average Grade " + str(average), "At least 70% " + str(Grade1), "60% - 69% " + str(Grade2), "50% - 59% " +
  str(Grade3), "40% - 49% " + str(Grade4), "Less than 40% " + str(Grade5), "Highest Grade " +
  str(max_grade), "Student Name:" + str(student_name), sep="\n", file=newFile)

newFile.close()
0

try this:

file = open("Here you put your path to a file, but replace '\' with '\\'", 'w+')
# That opened your file in 'write' mode, and '+' means that if it does not 
# exist, one will be created
file.write("Average Grade " + str(average), "At least 70% " + 
str(Grade1), "60% - 69% " + str(Grade2), "50% - 59% " +
str(Grade3), "40% - 49% " + str(Grade4), "Less than 40% " + 
str(Grade5), "Highest Grade " +
str(max_grade), "Student Name:" + str(student_name))
file.close()
Andrii Chumakov
  • 279
  • 3
  • 12